String scenarioName = scenario.getAttribute("name");
String playerName = scenario.getAttribute("pName");
double balance = Double.parseDouble(scenario.getAttribute("balance"));
int psgSatisfaction = Integer.parseInt(scenario.getAttribute("satisfaction"));
//Now create the scenario object.
Scenario myScenario = createScenarioObject(scenarioName, playerName, balance, psgSatisfaction);
//Seventhly, create the simulation object.
theSimulator = new Simulator(myScenario, document.getDocumentElement().getAttribute("time"), Integer.parseInt(document.getDocumentElement().getAttribute("increment")));
//Set the difficulty level.
theSimulator.setDifficultyLevel(document.getDocumentElement().getAttribute("difficulty"));
//Now add the messages!!!!
NodeList messageNode = (NodeList) xpath.evaluate("//scenario/message", document.getDocumentElement(), XPathConstants.NODESET);
for ( int i = 0; i < messageNode.getLength(); i++ ) {
Element messageElement = (Element) messageNode.item(i);
//Create message element.
theSimulator.addMessage(createMessageObject(messageElement.getAttribute("subject"), messageElement.getAttribute("text"), messageElement.getAttribute("sender"), messageElement.getAttribute("folder"), messageElement.getAttribute("date"), messageElement.getAttribute("type")));
}
//Fourthly, get the route details and create the route object.
//theRoutes = new LinkedList<Route>();
NodeList routeNode = (NodeList) xpath.evaluate("//scenario/route", document.getDocumentElement(), XPathConstants.NODESET);
for ( int i = 0; i < routeNode.getLength(); i++ ) {
Element routeElement = (Element) routeNode.item(i);
//Create the route by creating the route number and valid from and valid to dates.
String routeNumber = routeElement.getAttribute("number");
//Create route object.
Route route = new Route();
route.setRouteNumber(routeNumber);
//Now get the outward stops.
NodeList outwardStopNodes = routeElement.getElementsByTagName("outstop");
for ( int j = 0; j < outwardStopNodes.getLength(); j++ ) {
//Add each stop to the route object.
Element stopElement = (Element) outwardStopNodes.item(j);
route.addStop(stopElement.getAttribute("name"), Route.OUTWARDSTOPS);
}
//Now get the inward stops.
NodeList inwardStopNodes = routeElement.getElementsByTagName("instop");
for ( int j = 0; j < inwardStopNodes.getLength(); j++ ) {
//Add each stop to the route object.
Element stopElement = (Element) inwardStopNodes.item(j);
route.addStop(stopElement.getAttribute("name"), Route.RETURNSTOPS);
}
//Now go through and get the timetable elements.
NodeList timetableNodes = routeElement.getElementsByTagName("timetable");
for ( int j = 0; j < timetableNodes.getLength(); j++ ) {
Element timetableElement = (Element) timetableNodes.item(j);
//Get timetable information.
String[] validFromDates = timetableElement.getAttribute("validFrom").split("-");
Calendar validFrom = new GregorianCalendar(Integer.parseInt(validFromDates[0]), Integer.parseInt(validFromDates[1])-1, Integer.parseInt(validFromDates[2]));
String[] validToDates = timetableElement.getAttribute("validTo").split("-");
Calendar validTo = new GregorianCalendar(Integer.parseInt(validToDates[0]), Integer.parseInt(validToDates[1])-1, Integer.parseInt(validToDates[2]));
//Create timetable object.
Timetable myTimetable = new Timetable(timetableElement.getAttribute("name"), validFrom, validTo);
//Now add all service patterns.
NodeList serviceNodes = timetableElement.getElementsByTagName("servicePattern");
for ( int k = 0; k < serviceNodes.getLength(); k++ ) {
Element serviceElement = (Element) serviceNodes.item(k);
//Get service information.
String daysOfOperation = serviceElement.getAttribute("days");
String[] timeFrom = serviceElement.getAttribute("startTime").split(":");
Calendar startTime = new GregorianCalendar(2009,Calendar.AUGUST,5,Integer.parseInt(timeFrom[0]),Integer.parseInt(timeFrom[1]));
String[] timeTo = serviceElement.getAttribute("endTime").split(":");
Calendar endTime = new GregorianCalendar(2009,Calendar.AUGUST,5,Integer.parseInt(timeTo[0]),Integer.parseInt(timeTo[1]));
//Create service object.
ServicePattern myService = new ServicePattern(serviceElement.getAttribute("name"), daysOfOperation, serviceElement.getAttribute("returnTerminus"), serviceElement.getAttribute("outgoingTerminus"), startTime, endTime, Integer.parseInt(serviceElement.getAttribute("frequency")), Integer.parseInt(serviceElement.getAttribute("duration")));
//Add to timetable.
myTimetable.addServicePattern(serviceElement.getAttribute("name"), myService);
}
//Finally add this timetable to the route.
logger.debug("Adding " + myTimetable.getName() + " to " + route.getRouteNumber());
route.addTimetable(timetableElement.getAttribute("name"), myTimetable);
}
//Generate timetables.
List<Service> outgoingServices = route.generateServiceTimetables(getSimulator().getCurrentSimTime(), myScenario, Route.OUTWARDSTOPS);
List<Service> returnServices = route.generateServiceTimetables(getSimulator().getCurrentSimTime(), myScenario, Route.RETURNSTOPS);
route.generateRouteSchedules(outgoingServices, returnServices);
//Add route.
//theRoutes.add(route);
myScenario.addRoute(route);
}
//Sixthly, get the vehicles and create relevant vehicle objects.
NodeList vehicleList = (NodeList) xpath.evaluate("//scenario/vehicles/vehicle", document.getDocumentElement(), XPathConstants.NODESET);
for ( int i = 0; i < vehicleList.getLength(); i++ ) {
Element thisElem = (Element) vehicleList.item(i);
String[] deliveryDates = thisElem.getAttribute("deliveryDate").split("-");
Calendar deliveryDate = new GregorianCalendar(Integer.parseInt(deliveryDates[0]), Integer.parseInt(deliveryDates[1])-1, Integer.parseInt(deliveryDates[2]));
Vehicle myVeh = createVehicleObject(thisElem.getAttribute("id"), thisElem.getAttribute("type"), deliveryDate);
myVeh.setAssignedSchedule(null);
myScenario.addVehicle(myVeh);
//Add allocation to route.
String schedId = thisElem.getAttribute("route") + "/" + thisElem.getAttribute("schedId");
for ( int j = 0; j < myScenario.getNumberRoutes(); j++ ) {
if ( myScenario.getRoute(j).getRouteNumber().equalsIgnoreCase(thisElem.getAttribute("route")) ) {
String[] dates = document.getDocumentElement().getAttribute("time").split("-");
String day = dates[2];
if ( day.substring(0,1).equalsIgnoreCase("0") ) { day = day.substring(1,2); }
myScenario.getRoute(j).addAllocation(schedId, myVeh);
break;
}
}
//logger.debug("Adding vehicle with id " + v.getLast().getVehicleID() + " type " + v.getLast().getVehicleType() + " age " + v.getLast().getVehicleAge());
}