/** * Lazy initialization, as the vehicle info may not be available from the beginning. */ private void collectHeadingsInfo() { Map<Id, Departure> map = new HashMap<Id, Departure>(this.transitRoute.getDepartures().size()*2); for (Departure departure : this.transitRoute.getDepartures().values()) { if (departure.getVehicleId() != null) { map.put(departure.getVehicleId(), departure); } } /* try to make it thread-safe by assigning class-member at the end. * if two threads enter this method, nothing bad should happen, * as both threads should generated the same initialization. */ this.headings = map; }
/** * Lazy initialization, as the vehicle info may not be available from the beginning. */ private void collectVehiclesInfo() { Set<Id> set = new HashSet<Id>(this.transitRoute.getDepartures().size()*2); for (Departure departure : this.transitRoute.getDepartures().values()) { if (departure.getVehicleId() != null) { set.add(departure.getVehicleId()); } } /* try to make it thread-safe by assigning class-member at the end. * if two threads enter this method, nothing bad should happen, * as both threads should generated the same initialization. */ this.routeVehicles = set; }
public void setLine(TransitLine line) { this.line = line; this.vehicleIds = new TreeSet<>(); for (TransitRoute route : this.line.getRoutes().values()) { for (Departure departure : route.getDepartures().values()) { this.vehicleIds.add(departure.getVehicleId()); } } this.nVehicles = this.vehicleIds.size(); }
private static void printStatistic(TransitSchedule transitSchedule){ int nStops = 0; int nLines = 0; int nRoutes = 0; int nDepartures = 0; nStops = transitSchedule.getFacilities().values().size(); for (TransitLine transitLine : transitSchedule.getTransitLines().values()) { nLines++; for (TransitRoute transitRoute : transitLine.getRoutes().values()) { nRoutes++; nDepartures += transitRoute.getDepartures().size(); } } log.info("Transit schedule stats: " + nStops + " stops, " + nLines + " lines, " + nRoutes + " routes, " + nDepartures + " departures."); }
private void createUmlaufStuecke() { this.umlaufStuecke = new ArrayList<>(); log.info("Generating UmlaufStuecke"); int cnt = 0; for (TransitLine line : transitLines) { for (TransitRoute route : line.getRoutes().values()) { Gbl.assertNotNull(route.getRoute()); // will fail much later if this is null. kai, may'17 for (Departure departure : route.getDepartures().values()) { UmlaufStueck umlaufStueck = new UmlaufStueck(line, route, departure); umlaufStuecke.add(umlaufStueck); cnt++; printStatus(cnt); } } } Collections.sort(this.umlaufStuecke, departureTimeComparator); }
for (Departure oldDeparture : transitRoute.getDepartures().values()) { Id<Departure> newDepartureId = Id.create("new_" + mergeDestination.getDepartures().values().size(), Departure.class); Departure newDeparture = transitScheduleFactory.createDeparture(newDepartureId, oldDeparture.getDepartureTime()); newDeparture.setVehicleId(oldDeparture.getVehicleId());
public StopStopTimeCalculatorTuple(final TransitSchedule transitSchedule, final int timeSlot, final int totalTime) { this.timeSlot = timeSlot; Map<Tuple<Id<TransitStopFacility>, Id<TransitStopFacility>>, Integer> numObservations = new HashMap<Tuple<Id<TransitStopFacility>, Id<TransitStopFacility>>, Integer>(); for(TransitLine line:transitSchedule.getTransitLines().values()) for(TransitRoute route:line.getRoutes().values()) { for(int s=0; s<route.getStops().size()-1; s++) { Tuple<Id<TransitStopFacility>, Id<TransitStopFacility>> key = new Tuple<Id<TransitStopFacility>, Id<TransitStopFacility>>(route.getStops().get(s).getStopFacility().getId(), route.getStops().get(s+1).getStopFacility().getId()); StopStopTimeData data = stopStopTimes.get(key); if(data==null) stopStopTimes.put(key, new StopStopTimeDataArray((int) (totalTime/timeSlot)+1)); Double sTime = scheduledStopStopTimes.get(key); Integer num = numObservations.get(key); if(sTime==null) { sTime = 0.0; scheduledStopStopTimes.put(key, sTime); num = 0; numObservations.put(key, num); } scheduledStopStopTimes.put(key, (num*sTime+route.getStops().get(s+1).getArrivalOffset()-route.getStops().get(s).getDepartureOffset())/++num); numObservations.put(key, num); } for(Departure departure:route.getDepartures().values()) vehicleIds.add(departure.getVehicleId()); } System.out.println(stopStopTimes.size()); } public static void main(String[] args) {
public void testGetDeparturesImmutable() { Fixture f = new Fixture(); Departure dep1 = new DepartureImpl(Id.create(1, Departure.class), 7.0*3600); assertEquals(0, f.tRoute.getDepartures().size()); try { f.tRoute.getDepartures().put(dep1.getId(), dep1); fail("missing exception"); } catch (UnsupportedOperationException e) { log.info("catched expected exception.", e); } }
public void run() { VehiclesFactory vb = this.vehicles.getFactory(); VehicleType vehicleType = vb.createVehicleType(Id.create("defaultTransitVehicleType", VehicleType.class)); VehicleCapacity capacity = new VehicleCapacityImpl(); capacity.setSeats(Integer.valueOf(101)); capacity.setStandingRoom(Integer.valueOf(0)); vehicleType.setCapacity(capacity); this.vehicles.addVehicleType(vehicleType); long vehId = 0; for (TransitLine line : this.schedule.getTransitLines().values()) { for (TransitRoute route : line.getRoutes().values()) { for (Departure departure : route.getDepartures().values()) { Vehicle veh = vb.createVehicle(Id.create("tr_" + Long.toString(vehId++), Vehicle.class), vehicleType); this.vehicles.addVehicle(veh); departure.setVehicleId(veh.getId()); } } } } }
public void testAddDepartures() { Fixture f = new Fixture(); Departure dep1 = new DepartureImpl(Id.create(1, Departure.class), 7.0*3600); Departure dep2 = new DepartureImpl(Id.create(2, Departure.class), 8.0*3600); Departure dep3 = new DepartureImpl(Id.create(3, Departure.class), 9.0*3600); assertEquals(0, f.tRoute.getDepartures().size()); f.tRoute.addDeparture(dep1); assertEquals(1, f.tRoute.getDepartures().size()); assertEquals(dep1, f.tRoute.getDepartures().get(dep1.getId())); f.tRoute.addDeparture(dep2); assertEquals(2, f.tRoute.getDepartures().size()); assertEquals(dep1, f.tRoute.getDepartures().get(dep1.getId())); assertEquals(dep2, f.tRoute.getDepartures().get(dep2.getId())); assertNull(f.tRoute.getDepartures().get(dep3.getId())); f.tRoute.addDeparture(dep3); assertEquals(3, f.tRoute.getDepartures().size()); assertEquals(dep3, f.tRoute.getDepartures().get(dep3.getId())); }
public void testRemoveDepartures() { Fixture f = new Fixture(); Departure dep1 = new DepartureImpl(Id.create(1, Departure.class), 7.0*3600); Departure dep2 = new DepartureImpl(Id.create(2, Departure.class), 8.0*3600); f.tRoute.addDeparture(dep1); f.tRoute.addDeparture(dep2); assertEquals(2, f.tRoute.getDepartures().size()); assertNotNull(f.tRoute.getDepartures().get(dep1.getId())); assertNotNull(f.tRoute.getDepartures().get(dep2.getId())); assertTrue(f.tRoute.removeDeparture(dep1)); assertEquals(1, f.tRoute.getDepartures().size()); assertNull(f.tRoute.getDepartures().get(dep1.getId())); assertNotNull(f.tRoute.getDepartures().get(dep2.getId())); assertTrue(f.tRoute.removeDeparture(dep2)); assertEquals(0, f.tRoute.getDepartures().size()); f.tRoute.addDeparture(dep1); assertEquals(1, f.tRoute.getDepartures().size()); assertFalse(f.tRoute.removeDeparture(dep2)); assertEquals(1, f.tRoute.getDepartures().size()); assertNotNull(f.tRoute.getDepartures().get(dep1.getId())); }
@Override public ArrayList<Umlauf> build() { int id = 0; ArrayList<Umlauf> umlaeufe = new ArrayList<Umlauf>(); for (TransitLine line : transitLines) { for (TransitRoute route : line.getRoutes().values()) { Gbl.assertNotNull(route.getRoute()); // will fail much later if this is null. kai, may'17 for (Departure departure : route.getDepartures().values()) { UmlaufStueck umlaufStueck = new UmlaufStueck(line, route, departure); Umlauf umlauf = new UmlaufImpl(Id.create(id++, Umlauf.class)); umlauf.getUmlaufStuecke().add(umlaufStueck); umlaeufe.add(umlauf); } } } return umlaeufe; }
public void testAddDeparturesException() { Fixture f = new Fixture(); Departure dep1a = new DepartureImpl(Id.create(1, Departure.class), 7.0*3600); Departure dep1b = new DepartureImpl(Id.create(1, Departure.class), 7.0*3600); assertEquals(0, f.tRoute.getDepartures().size()); f.tRoute.addDeparture(dep1a); assertEquals(1, f.tRoute.getDepartures().size()); try { f.tRoute.addDeparture(dep1b); fail("missing exception."); } catch (IllegalArgumentException e) { log.info("catched expected exception.", e); } }
private void writeTransitRoute(final TransitRoute route) throws UncheckedIOException { List<Tuple<String, String>> attributes = new ArrayList<Tuple<String, String>>(1); attributes.add(this.createTuple(Constants.ID, route.getId().toString())); this.writeStartTag(Constants.TRANSIT_ROUTE, attributes); if (route.getDescription() != null) { this.writeStartTag(Constants.DESCRIPTION, null); this.writeContent(route.getDescription(), false); this.writeEndTag(Constants.DESCRIPTION); } this.writeStartTag(Constants.TRANSPORT_MODE, null); this.writeContent(route.getTransportMode(), false); this.writeEndTag(Constants.TRANSPORT_MODE); this.writeRouteProfile(route.getStops()); this.writeRoute(route.getRoute()); this.writeDepartures(route.getDepartures()); this.writeEndTag(Constants.TRANSIT_ROUTE); }
private void writeTransitRoute(final TransitRoute route) throws IOException, UncheckedIOException { List<Tuple<String, String>> attributes = new ArrayList<>(1); attributes.add(createTuple(Constants.ID, route.getId().toString())); this.writeStartTag(Constants.TRANSIT_ROUTE, attributes); if (!AttributesUtils.isEmpty(route.getAttributes())) { this.writer.write(NL); this.attributesWriter.writeAttributes("\t\t\t", this.writer, route.getAttributes()); } if (route.getDescription() != null) { this.writeStartTag(Constants.DESCRIPTION, null); this.writeContent(route.getDescription(), false); this.writeEndTag(Constants.DESCRIPTION); } this.writeStartTag(Constants.TRANSPORT_MODE, null); this.writeContent(route.getTransportMode(), false); this.writeEndTag(Constants.TRANSPORT_MODE); this.writeRouteProfile(route.getStops()); this.writeRoute(route.getRoute()); this.writeDepartures(route.getDepartures()); this.writeEndTag(Constants.TRANSIT_ROUTE); }
for (Departure departure : route.getDepartures().values()) { if (!vehicles.getVehicles().keySet().contains(departure.getVehicleId())) { Vehicle vehicle = vehFactory.createVehicle(departure.getVehicleId(), vehType);
assertEquals(1, route.getDepartures().size()); Departure dep1 = route.getDepartures().get(depId1); assertNotNull(dep1); assertEquals(depId1, dep1.getId());
assertEquals(0, route1.getStops().size()); assertNull(route1.getRoute()); assertEquals(0, route1.getDepartures().size());
public void testReadFileV1() throws SAXException, ParserConfigurationException, IOException { final String inputDir = getClassInputDirectory(); Scenario scenario = ScenarioUtils.createScenario(ConfigUtils.createConfig()); Network network = scenario.getNetwork(); new MatsimNetworkReader(scenario.getNetwork()).readFile(inputDir + INPUT_TEST_FILE_NETWORK); TransitScheduleFactory builder = new TransitScheduleFactoryImpl(); TransitSchedule schedule = builder.createTransitSchedule(); new TransitScheduleReaderV1(schedule, scenario.getPopulation().getFactory().getRouteFactories()).readFile(inputDir + INPUT_TEST_FILE_TRANSITSCHEDULE); assertEquals("wrong number of transit lines.", 1, schedule.getTransitLines().size()); assertEquals("wrong line id.", Id.create("T1", TransitLine.class), schedule.getTransitLines().keySet().iterator().next()); TransitLine lineT1 = schedule.getTransitLines().get(Id.create("T1", TransitLine.class)); assertNotNull("could not find line with id T1.", lineT1); TransitRoute route1 = lineT1.getRoutes().get(Id.create("1", TransitRoute.class)); assertNotNull("could not find route 1 in line T1.", route1); Map<Id<Departure>, Departure> departures = route1.getDepartures(); assertNotNull("could not get departures of route 1 in line T1.", departures); assertEquals("wrong number of departures.", 3, departures.size()); List<TransitRouteStop> stops = route1.getStops(); assertNotNull("could not get transit route stops.", stops); assertEquals("wrong number of stops.", 6, stops.size()); NetworkRoute route = route1.getRoute(); assertNotNull("could not get route.", route); assertEquals("wrong start link.", network.getLinks().get(Id.create("1", Link.class)).getId(), route.getStartLinkId()); assertEquals("wrong end link.", network.getLinks().get(Id.create("8", Link.class)).getId(), route.getEndLinkId()); assertEquals("wrong number of links in route.", 4, route.getLinkIds().size()); }
@Test public void testIntegration() { final Config cfg = this.util.loadConfig("test/scenarios/pt-tutorial/0.config.xml"); cfg.controler().setLastIteration(0); final Scenario s = ScenarioUtils.loadScenario(cfg); final Controler c = new Controler(s); final TransitLoad transitload = new TransitLoad(); c.addControlerListener(new StartupListener() { @Override public void notifyStartup(StartupEvent event) { c.getEvents().addHandler(transitload); } }); cfg.controler().setWritePlansInterval(0); c.getConfig().controler().setCreateGraphs(false); c.getConfig().controler().setWriteEventsInterval(0); c.getConfig().controler().setDumpDataAtEnd(false); c.run(); TransitLine line = s.getTransitSchedule().getTransitLines().get(Id.create("Blue Line", TransitLine.class)); TransitRoute route = line.getRoutes().get(Id.create("1to3", TransitRoute.class)); TransitStopFacility stopFacility = s.getTransitSchedule().getFacilities().get(Id.create("2a", TransitStopFacility.class)); Departure departure = route.getDepartures().get(Id.create("07", Departure.class)); int load = transitload.getLoadAtDeparture(line, route, stopFacility, departure); Assert.assertEquals("wrong number of passengers.", 4, load); } }