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

add JsonReader

This commit is contained in:
oblonski 2014-11-04 16:13:01 +01:00
parent 26c42e83ae
commit 69951579a2

View file

@ -21,9 +21,12 @@ package jsprit.core.problem.io;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.vehicle.Vehicle;
import jsprit.core.problem.vehicle.VehicleImpl;
import jsprit.core.problem.vehicle.VehicleType;
import jsprit.core.problem.vehicle.VehicleTypeImpl;
import jsprit.core.util.Coordinate;
import java.io.File;
import java.io.IOException;
@ -68,7 +71,38 @@ public class VrpJsonReader {
vehicleBuilder.setType(type);
vehicleBuilder.setEarliestStart(vehicleNode.path(JsonConstants.Vehicle.EARLIEST_START).asDouble());
vehicleBuilder.setLatestArrival(vehicleNode.path(JsonConstants.Vehicle.LATEST_END).asDouble());
vehicleBuilder.setStartLocationId(vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.ID).asText());
String startLocationId = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.ID).asText();
vehicleBuilder.setStartLocationId(startLocationId);
String endLocationId = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.ID).asText();
if(!startLocationId.equals(endLocationId)){
vehicleBuilder.setEndLocationId(endLocationId);
}
{
Double lon = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.LON).asDouble();
Double lat = vehicleNode.path(JsonConstants.Vehicle.START_ADDRESS).path(JsonConstants.Address.LAT).asDouble();
if (lon != null && lat != null) {
vehicleBuilder.setStartLocationCoordinate(Coordinate.newInstance(lon, lat));
}
}
{
Double lon = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.LON).asDouble();
Double lat = vehicleNode.path(JsonConstants.Vehicle.END_ADDRESS).path(JsonConstants.Address.LAT).asDouble();
if (lon != null && lat != null) {
vehicleBuilder.setStartLocationCoordinate(Coordinate.newInstance(lon, lat));
}
}
JsonNode skillsNode = vehicleNode.path(JsonConstants.Vehicle.SKILLS);
Iterator<JsonNode> skill_iterator = skillsNode.iterator();
while(skill_iterator.hasNext()){
JsonNode skillNode = skill_iterator.next();
String skill = skillNode.asText();
vehicleBuilder.addSkill(skill);
}
VehicleImpl vehicle = vehicleBuilder.build();
vrpBuilder.addVehicle(vehicle);
}
}
@ -102,5 +136,13 @@ public class VrpJsonReader {
else vrpBuilder.setFleetSize(VehicleRoutingProblem.FleetSize.FINITE);
}
public static void main(String[] args) {
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
new VrpJsonReader(vrpBuilder).read("output/vrp.json");
VehicleRoutingProblem problem = vrpBuilder.build();
for(Job j : problem.getJobs().values()) System.out.println(j);
for(Vehicle v : problem.getVehicles()) System.out.println(v);
}
}