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

simplify VehicleRoute.Builder by adding .addPickup(Pickup) and .addDelivery(Delivery)

This commit is contained in:
oblonski 2014-09-22 05:45:49 +02:00
parent 20a898cd25
commit fe7ad0d316
3 changed files with 76 additions and 76 deletions

View file

@ -222,7 +222,7 @@ public class VrpXMLReader{
if(service==null) throw new IllegalStateException("service to serviceId " + serviceId + " is missing (reference in one of your initial routes). make sure you define the service you refer to here in <services> </services>."); if(service==null) throw new IllegalStateException("service to serviceId " + serviceId + " is missing (reference in one of your initial routes). make sure you define the service you refer to here in <services> </services>.");
//!!!since job is part of initial route, it does not belong to jobs in problem, i.e. variable jobs that can be assigned/scheduled //!!!since job is part of initial route, it does not belong to jobs in problem, i.e. variable jobs that can be assigned/scheduled
freezedJobIds.add(serviceId); freezedJobIds.add(serviceId);
routeBuilder.addService(service, arrTime, endTime); routeBuilder.addService(service);
} }
else{ else{
String shipmentId = actConfig.getString("shipmentId"); String shipmentId = actConfig.getString("shipmentId");
@ -231,10 +231,10 @@ public class VrpXMLReader{
if(shipment == null) throw new IllegalStateException("shipment to shipmentId " + shipmentId + " is missing (reference in one of your initial routes). make sure you define the shipment you refer to here in <shipments> </shipments>."); if(shipment == null) throw new IllegalStateException("shipment to shipmentId " + shipmentId + " is missing (reference in one of your initial routes). make sure you define the shipment you refer to here in <shipments> </shipments>.");
freezedJobIds.add(shipmentId); freezedJobIds.add(shipmentId);
if(type.equals("pickupShipment")){ if(type.equals("pickupShipment")){
routeBuilder.addPickup(shipment, arrTime, endTime); routeBuilder.addPickup(shipment);
} }
else if(type.equals("deliverShipment")){ else if(type.equals("deliverShipment")){
routeBuilder.addDelivery(shipment, arrTime, endTime); routeBuilder.addDelivery(shipment);
} }
else throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here"); else throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here");
} }
@ -283,7 +283,7 @@ public class VrpXMLReader{
String serviceId = actConfig.getString("serviceId"); String serviceId = actConfig.getString("serviceId");
if(serviceId != null) { if(serviceId != null) {
Service service = getService(serviceId); Service service = getService(serviceId);
routeBuilder.addService(service, arrTime, endTime); routeBuilder.addService(service);
} }
else{ else{
String shipmentId = actConfig.getString("shipmentId"); String shipmentId = actConfig.getString("shipmentId");
@ -291,10 +291,10 @@ public class VrpXMLReader{
Shipment shipment = getShipment(shipmentId); Shipment shipment = getShipment(shipmentId);
if(shipment == null) throw new IllegalStateException("shipment with id " + shipmentId + " does not exist."); if(shipment == null) throw new IllegalStateException("shipment with id " + shipmentId + " does not exist.");
if(type.equals("pickupShipment")){ if(type.equals("pickupShipment")){
routeBuilder.addPickup(shipment, arrTime, endTime); routeBuilder.addPickup(shipment);
} }
else if(type.equals("deliverShipment")){ else if(type.equals("deliverShipment")){
routeBuilder.addDelivery(shipment, arrTime, endTime); routeBuilder.addDelivery(shipment);
} }
else throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here"); else throw new IllegalStateException("type " + type + " is not supported. Use 'pickupShipment' or 'deliverShipment' here");
} }

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (C) 2013 Stefan Schroeder * Copyright (C) 2014 Stefan Schroeder
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -20,9 +20,7 @@ import jsprit.core.problem.AbstractActivity;
import jsprit.core.problem.JobActivityFactory; import jsprit.core.problem.JobActivityFactory;
import jsprit.core.problem.driver.Driver; import jsprit.core.problem.driver.Driver;
import jsprit.core.problem.driver.DriverImpl; import jsprit.core.problem.driver.DriverImpl;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.*;
import jsprit.core.problem.job.Service;
import jsprit.core.problem.job.Shipment;
import jsprit.core.problem.solution.route.activity.*; import jsprit.core.problem.solution.route.activity.*;
import jsprit.core.problem.vehicle.Vehicle; import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl; import jsprit.core.problem.vehicle.VehicleImpl;
@ -149,6 +147,7 @@ public class VehicleRoute {
* *
* @param serviceActivityFactory the factory to create serviceActivities * @param serviceActivityFactory the factory to create serviceActivities
*/ */
@Deprecated
public Builder setServiceActivityFactory(TourActivityFactory serviceActivityFactory) { public Builder setServiceActivityFactory(TourActivityFactory serviceActivityFactory) {
this.serviceActivityFactory = serviceActivityFactory; this.serviceActivityFactory = serviceActivityFactory;
return this; return this;
@ -161,6 +160,7 @@ public class VehicleRoute {
* *
* @param shipmentActivityFactory the factory to create shipmentActivities * @param shipmentActivityFactory the factory to create shipmentActivities
*/ */
@Deprecated
public Builder setShipmentActivityFactory(TourShipmentActivityFactory shipmentActivityFactory) { public Builder setShipmentActivityFactory(TourShipmentActivityFactory shipmentActivityFactory) {
this.shipmentActivityFactory = shipmentActivityFactory; this.shipmentActivityFactory = shipmentActivityFactory;
return this; return this;
@ -220,66 +220,53 @@ public class VehicleRoute {
* @return this builder * @return this builder
* @throws IllegalArgumentException if service is null * @throws IllegalArgumentException if service is null
*/ */
@SuppressWarnings("deprecation")
public Builder addService(Service service){ public Builder addService(Service service){
if(service == null) throw new IllegalArgumentException("service must not be null"); if(service == null) throw new IllegalArgumentException("service must not be null");
addService(service,0.0,0.0); List<AbstractActivity> acts = jobActivityFactory.createActivities(service);
return this;
}
/**
* Adds a service with specified activity arrival- and endTime.
*
* <p>This implies that for this service a serviceActivity is created with {@link TourActivityFactory} and added to the sequence of tourActivities.
*
* <p>Basically this activity is then scheduled with an activity arrival and activity endTime.
*
* @param service to be added
* @param arrTime at service activity
* @param endTime of service activity
* @return builder
*/
@Deprecated
public Builder addService(Service service, double arrTime, double endTime){
List<AbstractActivity> acts = jobActivityFactory.createActivities(service);
TourActivity act = acts.get(0); TourActivity act = acts.get(0);
act.setArrTime(arrTime); tourActivities.addActivity(act);
act.setEndTime(endTime);
tourActivities.addActivity(act);
return this; return this;
} }
/** /**
* Adds a pickup to this route.
*
* @param pickup pickup to be added
* @return the builder
*/
public Builder addPickup(Pickup pickup){
if(pickup == null) throw new IllegalArgumentException("pickup must not be null");
addService(pickup);
return this;
}
/**
* Adds a delivery to this route.
*
* @param delivery delivery to be added
* @return the builder
*/
public Builder addDelivery(Delivery delivery){
if(delivery == null) throw new IllegalArgumentException("delivery must not be null");
addService(delivery);
return this;
}
/**
* Adds a the pickup of the specified shipment. * Adds a the pickup of the specified shipment.
* *
* @param shipment to be picked up and added to this route * @param shipment to be picked up and added to this route
* @throws IllegalStateException if method has already been called with the specified shipment. * @throws IllegalStateException if method has already been called with the specified shipment.
* @return the builder * @return the builder
*/ */
@SuppressWarnings("deprecation")
public Builder addPickup(Shipment shipment){ public Builder addPickup(Shipment shipment){
addPickup(shipment,0.,0.); if(openShipments.contains(shipment)) throw new IllegalStateException("shipment has already been added. cannot add it twice.");
return this;
}
/**
* Adds a the pickup of the specified shipment at specified arrival and end-time.
*
* @param shipment to be picked up and added to this route
* @throws IllegalStateException if method has already been called with the specified shipment.
* @return builder
*/
@Deprecated
public Builder addPickup(Shipment shipment, double arrTime, double endTime){
if(openShipments.contains(shipment)) throw new IllegalStateException("shipment has already been added. cannot add it twice.");
List<AbstractActivity> acts = jobActivityFactory.createActivities(shipment); List<AbstractActivity> acts = jobActivityFactory.createActivities(shipment);
TourActivity act = acts.get(0); TourActivity act = acts.get(0);
act.setArrTime(arrTime);
act.setEndTime(endTime);
tourActivities.addActivity(act); tourActivities.addActivity(act);
openShipments.add(shipment); openShipments.add(shipment);
openActivities.put(shipment,acts.get(1)); openActivities.put(shipment,acts.get(1));
return this; return this;
} }
/** /**
@ -289,32 +276,17 @@ public class VehicleRoute {
* @throws IllegalStateException if specified shipment has not been picked up yet (i.e. method addPickup(shipment) has not been called yet). * @throws IllegalStateException if specified shipment has not been picked up yet (i.e. method addPickup(shipment) has not been called yet).
* @return builder * @return builder
*/ */
@SuppressWarnings("deprecation")
public Builder addDelivery(Shipment shipment){ public Builder addDelivery(Shipment shipment){
addDelivery(shipment,0.,0.); if(openShipments.contains(shipment)){
return this;
}
/**
* Adds a the delivery of the specified shipment at a specified arrival and endTime.
*
* @param shipment to be delivered and added to this route
* @throws IllegalStateException if specified shipment has not been picked up yet (i.e. method addPickup(shipment) has not been called yet).
* @return the builder
*/
@Deprecated
public Builder addDelivery(Shipment shipment, double arrTime, double endTime){
if(openShipments.contains(shipment)){
TourActivity act = openActivities.get(shipment); TourActivity act = openActivities.get(shipment);
act.setArrTime(arrTime);
act.setEndTime(endTime);
tourActivities.addActivity(act); tourActivities.addActivity(act);
openShipments.remove(shipment); openShipments.remove(shipment);
} }
else{ throw new IllegalStateException("cannot deliver shipment. shipment " + shipment + " needs to be picked up first."); } else{ throw new IllegalStateException("cannot deliver shipment. shipment " + shipment + " needs to be picked up first."); }
return this; return this;
} }
/** /**
* Builds the route. * Builds the route.
* *

View file

@ -308,6 +308,20 @@ public class TestVehicleRoute {
} }
@Test
public void whenAddingPickup_itShouldBeAdded(){
Pickup pickup = (Pickup) Pickup.Builder.newInstance("pick").setLocationId("pickLoc").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("vehicle").setStartLocationId("startLoc").build();
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle).addPickup(pickup).build();
TourActivity act = route.getActivities().get(0);
assertTrue(act.getName().equals("pickup"));
assertTrue(act instanceof PickupService);
assertTrue(((TourActivity.JobActivity)act).getJob() instanceof Pickup);
}
@Test @Test
public void whenAddingDelivery_itShouldBeTreatedAsDelivery(){ public void whenAddingDelivery_itShouldBeTreatedAsDelivery(){
@ -321,4 +335,18 @@ public class TestVehicleRoute {
assertTrue(((TourActivity.JobActivity)act).getJob() instanceof Delivery); assertTrue(((TourActivity.JobActivity)act).getJob() instanceof Delivery);
} }
@Test
public void whenAddingDelivery_itShouldBeAdded(){
Delivery delivery = (Delivery) Delivery.Builder.newInstance("delivery").setLocationId("deliveryLoc").build();
VehicleImpl vehicle = VehicleImpl.Builder.newInstance("vehicle").setStartLocationId("startLoc").build();
VehicleRoute route = VehicleRoute.Builder.newInstance(vehicle).addDelivery(delivery).build();
TourActivity act = route.getActivities().get(0);
assertTrue(act.getName().equals("delivery"));
assertTrue(act instanceof DeliverService);
assertTrue(((TourActivity.JobActivity)act).getJob() instanceof Delivery);
}
} }