List<HierarchicalConfiguration> vehicleConfigs = vrpProblem.configurationsAt("vehicles.vehicle");
boolean doNotWarnAgain = false;
for(HierarchicalConfiguration vehicleConfig : vehicleConfigs){
String vehicleId = vehicleConfig.getString("id");
if(vehicleId == null) throw new IllegalStateException("vehicleId is missing.");
Builder builder = VehicleImpl.Builder.newInstance(vehicleId);
String typeId = vehicleConfig.getString("typeId");
if(typeId == null) throw new IllegalStateException("typeId is missing.");
String vType = vehicleConfig.getString("[@type]");
if(vType!=null){
if(vType.equals("penalty")){
typeId+="_penalty";
}
}
VehicleType type = types.get(typeId);
if(type == null) throw new IllegalStateException("vehicleType with typeId " + typeId + " is missing.");
builder.setType(type);
//read startlocation
String locationId = vehicleConfig.getString("location.id");
if(locationId == null) {
locationId = vehicleConfig.getString("startLocation.id");
}
if(locationId == null) throw new IllegalStateException("location.id is missing.");
builder.setStartLocationId(locationId);
String coordX = vehicleConfig.getString("location.coord[@x]");
String coordY = vehicleConfig.getString("location.coord[@y]");
if(coordX == null || coordY == null) {
coordX = vehicleConfig.getString("startLocation.coord[@x]");
coordY = vehicleConfig.getString("startLocation.coord[@y]");
}
if(coordX == null || coordY == null) {
if(!doNotWarnAgain) {
logger.warn("location.coord is missing. will not warn you again.");
doNotWarnAgain = true;
}
}
else{
Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(coordX), Double.parseDouble(coordY));
builder.setStartLocationCoordinate(coordinate);
}
//read endlocation
String endLocationId = vehicleConfig.getString("endLocation.id");
if(endLocationId != null) builder.setEndLocationId(endLocationId);
String endCoordX = vehicleConfig.getString("endLocation.coord[@x]");
String endCoordY = vehicleConfig.getString("endLocation.coord[@y]");
if(endCoordX == null || endCoordY == null) {
if(!doNotWarnAgain) {
logger.warn("endLocation.coord is missing. will not warn you again.");
doNotWarnAgain = true;
}
}
else{
Coordinate coordinate = Coordinate.newInstance(Double.parseDouble(endCoordX), Double.parseDouble(endCoordY));
builder.setEndLocationCoordinate(coordinate);
}
//read timeSchedule
String start = vehicleConfig.getString("timeSchedule.start");
String end = vehicleConfig.getString("timeSchedule.end");
if(start != null) builder.setEarliestStart(Double.parseDouble(start));
if(end != null) builder.setLatestArrival(Double.parseDouble(end));
//read return2depot
String returnToDepot = vehicleConfig.getString("returnToDepot");
if(returnToDepot != null){
builder.setReturnToDepot(vehicleConfig.getBoolean("returnToDepot"));
}
//read skills
String skillString = vehicleConfig.getString("skills");
if(skillString != null){
String cleaned = skillString.replaceAll("\\s", "");
String[] skillTokens = cleaned.split("[,;]");
for(String skill : skillTokens) builder.addSkill(skill.toLowerCase());
}
//build vehicle
VehicleImpl vehicle = builder.build();
vrpBuilder.addVehicle(vehicle);
vehicleMap.put(vehicleId, vehicle);
}
}