mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add json stuff
This commit is contained in:
parent
a32ccf3efa
commit
025316b5a8
2 changed files with 29 additions and 5 deletions
|
|
@ -69,6 +69,8 @@ public class JsonConstants {
|
||||||
public static final String PICKUP = "pickup";
|
public static final String PICKUP = "pickup";
|
||||||
|
|
||||||
public static final String DELIVERY = "delivery";
|
public static final String DELIVERY = "delivery";
|
||||||
|
|
||||||
|
public static final String SERVICE = "service";
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Vehicle {
|
public static class Vehicle {
|
||||||
|
|
|
||||||
|
|
@ -58,19 +58,40 @@ public class VrpJsonReader {
|
||||||
* @throws java.lang.IllegalStateException if there is a service without id and proper location spec OR
|
* @throws java.lang.IllegalStateException if there is a service without id and proper location spec OR
|
||||||
* if there is a vehicle without id and proper location spec OR if there is a vehicle type without id
|
* if there is a vehicle without id and proper location spec OR if there is a vehicle type without id
|
||||||
*/
|
*/
|
||||||
public void read(String jsonFile){
|
public void read(File jsonFile){
|
||||||
JsonNode root = buildTree_and_getRoot(jsonFile);
|
JsonNode root = buildTree_and_getRoot_fromFile(jsonFile);
|
||||||
|
parse(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void read(String jsonContent){
|
||||||
|
JsonNode root = buildTree_and_getRoot_fromContent(jsonContent);
|
||||||
|
parse(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parse(JsonNode root) {
|
||||||
setFleetSize(root);
|
setFleetSize(root);
|
||||||
parse_and_map_vehicle_types(root);
|
parse_and_map_vehicle_types(root);
|
||||||
parse_vehicles(root);
|
parse_vehicles(root);
|
||||||
parse_services(root);
|
parse_services(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JsonNode buildTree_and_getRoot(String jsonFile){
|
private JsonNode buildTree_and_getRoot_fromContent(String jsonContent){
|
||||||
JsonNode node = null;
|
JsonNode node = null;
|
||||||
try {
|
try {
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
node = objectMapper.readTree(new File(jsonFile));
|
node = objectMapper.readTree(jsonContent);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
|
||||||
|
private JsonNode buildTree_and_getRoot_fromFile(File jsonFile){
|
||||||
|
JsonNode node = null;
|
||||||
|
try {
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
node = objectMapper.readTree(jsonFile);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
|
|
@ -93,6 +114,7 @@ public class VrpJsonReader {
|
||||||
|
|
||||||
Service.Builder serviceBuilder;
|
Service.Builder serviceBuilder;
|
||||||
if(typeNode.isMissingNode()) serviceBuilder = Service.Builder.newInstance(jobIdNode.asText());
|
if(typeNode.isMissingNode()) serviceBuilder = Service.Builder.newInstance(jobIdNode.asText());
|
||||||
|
else if(typeNode.asText().equals(JsonConstants.Job.SERVICE)) serviceBuilder = Service.Builder.newInstance(jobIdNode.asText());
|
||||||
else if(typeNode.asText().equals(JsonConstants.Job.PICKUP)) serviceBuilder = Pickup.Builder.newInstance(jobIdNode.asText());
|
else if(typeNode.asText().equals(JsonConstants.Job.PICKUP)) serviceBuilder = Pickup.Builder.newInstance(jobIdNode.asText());
|
||||||
else if(typeNode.asText().equals(JsonConstants.Job.DELIVERY)) serviceBuilder = Delivery.Builder.newInstance(jobIdNode.asText());
|
else if(typeNode.asText().equals(JsonConstants.Job.DELIVERY)) serviceBuilder = Delivery.Builder.newInstance(jobIdNode.asText());
|
||||||
else throw new IllegalStateException("type of service ("+typeNode.asText()+") is not supported");
|
else throw new IllegalStateException("type of service ("+typeNode.asText()+") is not supported");
|
||||||
|
|
@ -244,7 +266,7 @@ public class VrpJsonReader {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
|
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
|
||||||
new VrpJsonReader(vrpBuilder).read("output/vrp.json");
|
new VrpJsonReader(vrpBuilder).read(new File("output/vrp.json"));
|
||||||
VehicleRoutingProblem problem = vrpBuilder.build();
|
VehicleRoutingProblem problem = vrpBuilder.build();
|
||||||
for(Job j : problem.getJobs().values()) System.out.println(j);
|
for(Job j : problem.getJobs().values()) System.out.println(j);
|
||||||
for(Vehicle v : problem.getVehicles()) System.out.println(v);
|
for(Vehicle v : problem.getVehicles()) System.out.println(v);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue