1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

create and test VehicleRouteBuilder to easily build routes

This commit is contained in:
oblonski 2013-10-25 22:18:36 +02:00
parent 401afad447
commit 8283a416c1
2 changed files with 118 additions and 6 deletions

View file

@ -1,7 +1,17 @@
package basics.route;
import java.util.HashSet;
import java.util.Set;
import basics.Service;
import basics.Shipment;
/**
* Builds a {@link VehicleRoute}.
*
* @author schroeder
*
*/
public class VehicleRouteBuilder {
private Vehicle vehicle;
@ -10,48 +20,92 @@ public class VehicleRouteBuilder {
private Start start;
private End end;
private TourActivities tourActivities = new TourActivities();
private TourActivityFactory serviceActivityFactory = new DefaultTourActivityFactory();
private TourShipmentActivityFactory shipmentActivityFactory = new DefaultShipmentActivityFactory();
private Set<Shipment> openShipments = new HashSet<Shipment>();
public void setServiceActivityFactory(TourActivityFactory serviceActivityFactory) {
this.serviceActivityFactory = serviceActivityFactory;
}
public void setShipmentActivityFactory(
TourShipmentActivityFactory shipmentActivityFactory) {
public void setShipmentActivityFactory(TourShipmentActivityFactory shipmentActivityFactory) {
this.shipmentActivityFactory = shipmentActivityFactory;
}
/**
* Constructs the route-builder.
* @param vehicle
* @param driver
*/
public VehicleRouteBuilder(Vehicle vehicle, Driver driver) {
super();
this.vehicle = vehicle;
this.driver = driver;
start = Start.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
start.setEndTime(vehicle.getEarliestDeparture());
end = End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
End.newInstance(vehicle.getLocationId(), vehicle.getEarliestDeparture(), vehicle.getLatestArrival());
}
/**
* Sets the departure-time of the route.
*
* @param departureTime
* @return
*/
public VehicleRouteBuilder setDepartureTime(double departureTime){
start.setEndTime(departureTime);
return this;
}
public VehicleRouteBuilder addService(Service service){
tourActivities.addActivity(serviceActivityFactory.createActivity(service));
return this;
}
/**
* Adds a the pickup of the specified shipment.
*
* @param shipment
* @throws IllegalStateException if method has already been called with the specified shipment.
* @return
*/
public VehicleRouteBuilder addPickup(Shipment shipment){
if(openShipments.contains(shipment)) throw new IllegalStateException("shipment has already been added. cannot add it twice.");
tourActivities.addActivity(shipmentActivityFactory.createPickup(shipment));
openShipments.add(shipment);
return this;
}
/**
* Adds a the delivery of the specified shipment.
*
* @param shipment
* @throws IllegalStateException if specified shipment has not been picked up yet (i.e. method addPickup(shipment) has not been called yet).
* @return
*/
public VehicleRouteBuilder addDelivery(Shipment shipment){
tourActivities.addActivity(shipmentActivityFactory.createDelivery(shipment));
if(openShipments.contains(shipment)){
tourActivities.addActivity(shipmentActivityFactory.createDelivery(shipment));
openShipments.remove(shipment);
}
else{ throw new IllegalStateException("cannot deliver shipment. shipment " + shipment + " needs to be picked up first."); }
return this;
}
/**
* Builds the route.
*
* @return {@link VehicleRoute}
* @throws IllegalStateException if there are still shipments that have been picked up though but not delivery.
*/
public VehicleRoute build(){
if(!openShipments.isEmpty()){
throw new IllegalStateException("there are still shipments that have not been delivered yet.");
}
VehicleRoute route = VehicleRoute.newInstance(tourActivities, driver, vehicle);
return route;
}