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

modify reader to read shipments

This commit is contained in:
oblonski 2013-10-27 21:42:18 +01:00
parent 8283a416c1
commit 36e67f6ae6

View file

@ -37,6 +37,7 @@ import util.Resource;
import basics.Delivery;
import basics.Pickup;
import basics.Service;
import basics.Shipment;
import basics.VehicleRoutingProblem;
import basics.VehicleRoutingProblem.FleetComposition;
import basics.VehicleRoutingProblem.FleetSize;
@ -77,6 +78,82 @@ public class VrpXMLReader{
}
}
interface JobConfigReader {
void readConfig(XMLConfiguration vrpProblem);
}
static class ServiceConfigReader implements JobConfigReader{
VehicleRoutingProblem.Builder vrpBuilder;
public ServiceConfigReader(basics.VehicleRoutingProblem.Builder vrpBuilder) {
super();
this.vrpBuilder = vrpBuilder;
}
@Override
public void readConfig(XMLConfiguration config) {
}
}
static class ShipmentConfigReader implements JobConfigReader{
VehicleRoutingProblem.Builder vrpBuilder;
public ShipmentConfigReader(basics.VehicleRoutingProblem.Builder vrpBuilder) {
super();
this.vrpBuilder = vrpBuilder;
}
@Override
public void readConfig(XMLConfiguration config) {
List<HierarchicalConfiguration> shipmentConfigs = config.configurationsAt("shipments.shipment");
for(HierarchicalConfiguration shipmentConfig : shipmentConfigs){
String id = shipmentConfig.getString("[@id]");
if(id == null) throw new IllegalStateException("shipment[@id] is missing.");
int cap = getCap(shipmentConfig);
Shipment.Builder builder = Shipment.Builder.newInstance(id, cap);
String pickupLocationId = shipmentConfig.getString("pickup.locationId");
builder.setPickupLocation(pickupLocationId);
Coordinate pickupCoord = getCoord(shipmentConfig,"pickup.");
builder.setPickupCoord(pickupCoord);
if(pickupCoord != null){
if(pickupLocationId != null){
vrpBuilder.addLocation(pickupLocationId,pickupCoord);
}
else{
vrpBuilder.addLocation(pickupCoord.toString(),pickupCoord);
builder.setPickupLocation(pickupCoord.toString());
}
}
String deliveryLocationId = shipmentConfig.getString("delivery.locationId");
builder.setDeliveryLocation(deliveryLocationId);
Coordinate deliveryCoord = getCoord(shipmentConfig,"delivery.");
builder.setDeliveryCoord(deliveryCoord);
if(deliveryCoord != null){
if(deliveryLocationId != null){
vrpBuilder.addLocation(deliveryLocationId,deliveryCoord);
}
else{
vrpBuilder.addLocation(deliveryCoord.toString(),deliveryCoord);
builder.setPickupLocation(deliveryCoord.toString());
}
}
}
}
}
private static Logger logger = Logger.getLogger(VrpXMLReader.class);
@ -94,6 +171,11 @@ public class VrpXMLReader{
private TourActivityFactory tourActivityFactory = new DefaultTourActivityFactory();
private Collection<JobConfigReader> jobConfigReaders = new ArrayList<VrpXMLReader.JobConfigReader>();
public void addJobConfigReader(JobConfigReader reader){
jobConfigReaders.add(reader);
}
public void setTourActivityFactory(TourActivityFactory tourActivityFactory){
this.tourActivityFactory = tourActivityFactory;
}
@ -160,6 +242,11 @@ public class VrpXMLReader{
}
readProblemType(xmlConfig);
readVehiclesAndTheirTypes(xmlConfig);
// for(JobConfigReader jobConfigReader : jobConfigReaders){
// jobConfigReader.readConfig(xmlConfig);
// }
readServices(xmlConfig);
readSolutions(xmlConfig);
}
@ -237,7 +324,64 @@ public class VrpXMLReader{
}
// private void readShipments(XMLConfiguration)
private void readShipments(XMLConfiguration config){
List<HierarchicalConfiguration> shipmentConfigs = config.configurationsAt("shipments.shipment");
for(HierarchicalConfiguration shipmentConfig : shipmentConfigs){
String id = shipmentConfig.getString("[@id]");
if(id == null) throw new IllegalStateException("shipment[@id] is missing.");
int cap = getCap(shipmentConfig);
Shipment.Builder builder = Shipment.Builder.newInstance(id, cap);
String pickupLocationId = shipmentConfig.getString("pickup.locationId");
builder.setPickupLocation(pickupLocationId);
Coordinate pickupCoord = getCoord(shipmentConfig,"pickup.");
builder.setPickupCoord(pickupCoord);
if(pickupCoord != null){
if(pickupLocationId != null){
vrpBuilder.addLocation(pickupLocationId,pickupCoord);
}
else{
vrpBuilder.addLocation(pickupCoord.toString(),pickupCoord);
builder.setPickupLocation(pickupCoord.toString());
}
}
String deliveryLocationId = shipmentConfig.getString("delivery.locationId");
builder.setDeliveryLocation(deliveryLocationId);
Coordinate deliveryCoord = getCoord(shipmentConfig,"delivery.");
builder.setDeliveryCoord(deliveryCoord);
if(deliveryCoord != null){
if(deliveryLocationId != null){
vrpBuilder.addLocation(deliveryLocationId,deliveryCoord);
}
else{
vrpBuilder.addLocation(deliveryCoord.toString(),deliveryCoord);
builder.setPickupLocation(deliveryCoord.toString());
}
}
}
}
private static Coordinate getCoord(HierarchicalConfiguration serviceConfig, String prefix) {
Coordinate pickupCoord = null;
if(serviceConfig.getString(prefix + "coord[@x]") != null && serviceConfig.getString(prefix + "coord[@y]") != null){
double x = Double.parseDouble(serviceConfig.getString(prefix + "coord[@x]"));
double y = Double.parseDouble(serviceConfig.getString(prefix + "coord[@y]"));
pickupCoord = Coordinate.newInstance(x,y);
}
return pickupCoord;
}
private static int getCap(HierarchicalConfiguration serviceConfig) {
String capacityDemand = serviceConfig.getString("capacity-demand");
int cap = 0;
if(capacityDemand != null) cap = Integer.parseInt(capacityDemand);
return cap;
}
private void readServices(XMLConfiguration vrpProblem) {
List<HierarchicalConfiguration> serviceConfigs = vrpProblem.configurationsAt("services.service");
@ -246,18 +390,11 @@ public class VrpXMLReader{
if(id == null) throw new IllegalStateException("service[@id] is missing.");
String type = serviceConfig.getString("[@type]");
if(type == null) type = "service";
String capacityDemand = serviceConfig.getString("capacity-demand");
int cap = 0;
if(capacityDemand != null) cap = Integer.parseInt(capacityDemand);
int cap = getCap(serviceConfig);
Service.Builder builder = serviceBuilderFactory.createBuilder(type, id, cap);
String serviceLocationId = serviceConfig.getString("locationId");
builder.setLocationId(serviceLocationId);
Coordinate serviceCoord = null;
if(serviceConfig.getString("coord[@x]") != null && serviceConfig.getString("coord[@y]") != null){
double x = Double.parseDouble(serviceConfig.getString("coord[@x]"));
double y = Double.parseDouble(serviceConfig.getString("coord[@y]"));
serviceCoord = Coordinate.newInstance(x,y);
}
Coordinate serviceCoord = getCoord(serviceConfig,"");
builder.setCoord(serviceCoord);
if(serviceCoord != null){
if(serviceLocationId != null){