@Test public void testRouteProfileRoute_NoLink() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); // now the other stuff Id<TransitLine> lineId = Id.create("1", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId.toString()).get(), context); context.push(Constants.TRANSIT_LINE); Id<TransitRoute> routeId1 = Id.create("1", TransitRoute.class); reader.startTag(Constants.TRANSIT_ROUTE, new AttributesBuilder().add(Constants.ID, routeId1.toString()).get(), context); context.push(Constants.TRANSIT_ROUTE); // by definition of the file format, transitRoute *must* have transportMode, routeProfile and departures defined reader.startTag(Constants.TRANSPORT_MODE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.TRANSPORT_MODE, "bus", context); reader.startTag(Constants.ROUTE_PROFILE, AttributesBuilder.getEmpty(), context); // route profile can be empty, but must exist reader.endTag(Constants.ROUTE_PROFILE, EMPTY_STRING, context); reader.startTag(Constants.ROUTE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.ROUTE, EMPTY_STRING, context); reader.endTag(Constants.ROUTE_PROFILE, EMPTY_STRING, context); reader.startTag(Constants.DEPARTURES, AttributesBuilder.getEmpty(), context); // departures can be empty, but must exist reader.endTag(Constants.DEPARTURES, EMPTY_STRING, context); reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_ROUTE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_LINE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_SCHEDULE TransitRoute route = schedule.getTransitLines().get(lineId).getRoutes().get(routeId1); assertNull(route.getRoute()); }
" </transitLine>" + "</transitSchedule>"; new TransitScheduleReaderV1(f.scenario).parse(new ByteArrayInputStream(scheduleXml.getBytes()));
@Test public void testTransitLineName() { String filename = this.utils.getOutputDirectory() + "schedule.xml"; TransitScheduleFactory builder = new TransitScheduleFactoryImpl(); TransitSchedule schedule = builder.createTransitSchedule(); TransitLine line = builder.createTransitLine(Id.create(1, TransitLine.class)); line.setName("Blue line"); schedule.addTransitLine(line); TransitScheduleWriter writer = new TransitScheduleWriter(schedule); writer.writeFile(filename); TransitScheduleFactory builder2 = new TransitScheduleFactoryImpl(); TransitSchedule schedule2 = builder2.createTransitSchedule(); new TransitScheduleReaderV1(schedule2, new RouteFactories()).readFile(filename); Assert.assertEquals(1, schedule2.getTransitLines().size()); Assert.assertEquals("Blue line", schedule2.getTransitLines().get(Id.create(1, TransitLine.class)).getName()); } }
@Override protected void setDoctype(String doctype) { super.setDoctype(doctype); if ("transitSchedule_v2.dtd".equals(doctype)) { this.delegate = new TransitScheduleReaderV2(externalInputCRS, targetCRS, this.scenario); } else if ("transitSchedule_v1.dtd".equals(doctype)) { this.delegate = new TransitScheduleReaderV1( externalInputCRS != null ? TransformationFactory.getCoordinateTransformation(externalInputCRS, targetCRS) : new IdentityTransformation(), this.scenario); } else { throw new IllegalArgumentException("Unsupported doctype: " + doctype); } }
" </transitLine>" + "</transitSchedule>"; new TransitScheduleReaderV1(f.scenario).parse(new ByteArrayInputStream(scheduleXml.getBytes()));
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 testTransitRoute_Description() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); Id<TransitLine> lineId = Id.create("1", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId.toString()).get(), context); context.push(Constants.TRANSIT_LINE); reader.startTag(Constants.TRANSIT_ROUTE, new AttributesBuilder().add(Constants.ID, routeId1.toString()).get(), context); context.push(Constants.TRANSIT_ROUTE); reader.startTag(Constants.DESCRIPTION, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.DESCRIPTION, description, context); reader.startTag(Constants.TRANSPORT_MODE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.TRANSPORT_MODE, "bus", context); reader.startTag(Constants.ROUTE_PROFILE, AttributesBuilder.getEmpty(), context); // route profile can be empty reader.endTag(Constants.ROUTE_PROFILE, EMPTY_STRING, context); reader.startTag(Constants.DEPARTURES, AttributesBuilder.getEmpty(), context); // departures can be empty reader.endTag(Constants.DEPARTURES, EMPTY_STRING, context); reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_ROUTE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_LINE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_SCHEDULE
" </transitLine>" + "</transitSchedule>"; new TransitScheduleReaderV1(f.scenario).parse(new ByteArrayInputStream(scheduleXml.getBytes()));
TransitScheduleFactory builder2 = new TransitScheduleFactoryImpl(); TransitSchedule schedule2 = builder2.createTransitSchedule(); new TransitScheduleReaderV1(schedule2, new RouteFactories()).readFile(filename); TransitScheduleFactory builder3 = new TransitScheduleFactoryImpl(); TransitSchedule schedule3 = builder3.createTransitSchedule(); new TransitScheduleReaderV1(schedule3, new RouteFactories()).readFile(filename);
@Test public void testTransitLine_Single() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); Id<TransitLine> lineId = Id.create("23", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId.toString()).get(), context); reader.endTag(Constants.TRANSIT_LINE, EMPTY_STRING, context);// TRANSIT_LINE reader.endTag(context.pop(), EMPTY_STRING, context);// TRANSIT_SCHEDULE assertEquals(1, schedule.getTransitLines().size()); TransitLine line = schedule.getTransitLines().get(lineId); assertNotNull(line); assertEquals(lineId, line.getId()); }
" </transitLine>" + "</transitSchedule>"; new TransitScheduleReaderV1(f.scenario).parse(new ByteArrayInputStream(scheduleXml.getBytes()));
@Test public void testTransitRoute_Multiple() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); Id<TransitLine> lineId = Id.create("1", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId.toString()).get(), context); context.push(Constants.TRANSIT_LINE); reader.startTag(Constants.TRANSIT_ROUTE, new AttributesBuilder().add(Constants.ID, routeId1.toString()).get(), context); context.push(Constants.TRANSIT_ROUTE); reader.startTag(Constants.TRANSPORT_MODE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.TRANSPORT_MODE, "bus", context); reader.startTag(Constants.ROUTE_PROFILE, AttributesBuilder.getEmpty(), context); // route profile can be empty reader.endTag(Constants.ROUTE_PROFILE, EMPTY_STRING, context); reader.startTag(Constants.DEPARTURES, AttributesBuilder.getEmpty(), context); // departures can be empty reader.endTag(Constants.DEPARTURES, EMPTY_STRING, context); reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_ROUTE reader.startTag(Constants.TRANSIT_ROUTE, new AttributesBuilder().add(Constants.ID, routeId2.toString()).get(), context); context.push(Constants.TRANSIT_ROUTE); reader.startTag(Constants.TRANSPORT_MODE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.TRANSPORT_MODE, "train", context); reader.startTag(Constants.ROUTE_PROFILE, AttributesBuilder.getEmpty(), context); // route profile can be empty reader.endTag(Constants.ROUTE_PROFILE, EMPTY_STRING, context); reader.startTag(Constants.DEPARTURES, AttributesBuilder.getEmpty(), context); // departures can be empty
" </transitLine>" + "</transitSchedule>"; new TransitScheduleReaderV1(f.scenario).parse(new ByteArrayInputStream(scheduleXml.getBytes()));
@Test public void testDepartures_Single() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); Id<TransitLine> lineId = Id.create("1", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId.toString()).get(), context); context.push(Constants.TRANSIT_LINE); reader.startTag(Constants.TRANSIT_ROUTE, new AttributesBuilder().add(Constants.ID, routeId1.toString()).get(), context); context.push(Constants.TRANSIT_ROUTE); reader.startTag(Constants.TRANSPORT_MODE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.TRANSPORT_MODE, "bus", context); reader.startTag(Constants.ROUTE_PROFILE, AttributesBuilder.getEmpty(), context); // route profile can be empty reader.endTag(Constants.ROUTE_PROFILE, EMPTY_STRING, context); reader.startTag(Constants.DEPARTURES, AttributesBuilder.getEmpty(), context); // departures can be empty context.push(Constants.DEPARTURES); reader.startTag(Constants.DEPARTURE, new AttributesBuilder().add(Constants.ID, depId1.toString()). add(Constants.DEPARTURE_TIME, depTime1).get(), context); reader.endTag(Constants.DEPARTURE, EMPTY_STRING, context); reader.endTag(context.pop(), EMPTY_STRING, context); // DEPATURES reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_ROUTE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_LINE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_SCHEDULE
" </transitLine>" + "</transitSchedule>"; new TransitScheduleReaderV1(f.scenario).parse(new ByteArrayInputStream(scheduleXml.getBytes()));
@Test public void testTransitRoute_Single() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); Id<TransitLine> lineId = Id.create("1", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId.toString()).get(), context); context.push(Constants.TRANSIT_LINE); reader.startTag(Constants.TRANSIT_ROUTE, new AttributesBuilder().add(Constants.ID, routeId1.toString()).get(), context); context.push(Constants.TRANSIT_ROUTE); reader.startTag(Constants.TRANSPORT_MODE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.TRANSPORT_MODE, "bus", context); reader.startTag(Constants.ROUTE_PROFILE, AttributesBuilder.getEmpty(), context); // route profile can be empty reader.endTag(Constants.ROUTE_PROFILE, EMPTY_STRING, context); reader.startTag(Constants.DEPARTURES, AttributesBuilder.getEmpty(), context); // departures can be empty reader.endTag(Constants.DEPARTURES, EMPTY_STRING, context); reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_ROUTE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_LINE reader.endTag(context.pop(), EMPTY_STRING, context); // TRANSIT_SCHEDULE
@Test public void testTransitLine_Multiple() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); Id<TransitLine> lineId1 = Id.create("23", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId1.toString()).get(), context); reader.endTag(Constants.TRANSIT_LINE, EMPTY_STRING, context);// TRANSIT_LINE Id<TransitLine> lineId2 = Id.create("42", TransitLine.class); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId2.toString()).get(), context); reader.endTag(Constants.TRANSIT_LINE, EMPTY_STRING, context);// TRANSIT_LINE reader.endTag(context.pop(), EMPTY_STRING, context);// TRANSIT_SCHEDULE assertEquals(2, schedule.getTransitLines().size()); TransitLine line1 = schedule.getTransitLines().get(lineId1); assertNotNull(line1); assertEquals(lineId1, line1.getId()); TransitLine line2 = schedule.getTransitLines().get(lineId2); assertNotNull(line2); assertEquals(lineId2, line2.getId()); }
@Test public void testRouteProfile_SingleStop() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); reader.startTag(Constants.TRANSIT_SCHEDULE, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_SCHEDULE); reader.startTag(Constants.TRANSIT_STOPS, AttributesBuilder.getEmpty(), context); context.push(Constants.TRANSIT_STOPS); Id<TransitStopFacility> stopId = Id.create("stop1", TransitStopFacility.class); Attributes atts = new AttributesBuilder().add(Constants.ID, stopId.toString()). add(Constants.X, "79").add(Constants.Y, "80").get(); reader.startTag(Constants.STOP_FACILITY, atts, context); reader.endTag(Constants.STOP_FACILITY, EMPTY_STRING, context); reader.startTag(Constants.TRANSIT_LINE, new AttributesBuilder().add(Constants.ID, lineId.toString()).get(), context); context.push(Constants.TRANSIT_LINE); reader.startTag(Constants.TRANSIT_ROUTE, new AttributesBuilder().add(Constants.ID, routeId1.toString()).get(), context); context.push(Constants.TRANSIT_ROUTE); reader.startTag(Constants.TRANSPORT_MODE, AttributesBuilder.getEmpty(), context); reader.endTag(Constants.TRANSPORT_MODE, "bus", context); reader.startTag(Constants.ROUTE_PROFILE, AttributesBuilder.getEmpty(), context); // route profile can be empty context.push(Constants.ROUTE_PROFILE); reader.startTag(Constants.STOP, new AttributesBuilder().add(Constants.REF_ID, "stop1").get(), context); reader.endTag(Constants.STOP, EMPTY_STRING, context);
@Test public void testStopFacility_isBlocking() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); Attributes emptyAtts = AttributesBuilder.getEmpty(); reader.startTag(Constants.TRANSIT_SCHEDULE, emptyAtts, context); context.push(Constants.TRANSIT_SCHEDULE); reader.startTag(Constants.TRANSIT_STOPS, emptyAtts, context); context.push(Constants.TRANSIT_STOPS); Attributes atts = new AttributesBuilder().add(Constants.ID, "stop1"). add(Constants.X, "79").add(Constants.Y, "80").add(Constants.IS_BLOCKING, "true").get(); reader.startTag(Constants.STOP_FACILITY, atts, context); reader.endTag(Constants.STOP_FACILITY, EMPTY_STRING, context); reader.endTag(context.pop(), EMPTY_STRING, context);// TRANSIT_STOPS reader.endTag(context.pop(), EMPTY_STRING, context);// TRANSIT_SCHEDULE assertEquals(1, schedule.getFacilities().size()); TransitStopFacility stop = schedule.getFacilities().get(Id.create("stop1", TransitStopFacility.class)); assertNotNull(stop); assertEquals(79.0, stop.getCoord().getX(), MatsimTestCase.EPSILON); assertEquals(80.0, stop.getCoord().getY(), MatsimTestCase.EPSILON); assertTrue(stop.getIsBlockingLane()); }
@Test public void testStopFacility_withName() { TransitSchedule schedule = new TransitScheduleFactoryImpl().createTransitSchedule(); TransitScheduleReaderV1 reader = new TransitScheduleReaderV1(schedule, new RouteFactories()); Stack<String> context = new Stack<String>(); Attributes emptyAtts = AttributesBuilder.getEmpty(); reader.startTag(Constants.TRANSIT_SCHEDULE, emptyAtts, context); context.push(Constants.TRANSIT_SCHEDULE); reader.startTag(Constants.TRANSIT_STOPS, emptyAtts, context); context.push(Constants.TRANSIT_STOPS); Attributes atts = new AttributesBuilder().add(Constants.ID, "stop1"). add(Constants.X, "79").add(Constants.Y, "80").add(Constants.NAME, "some stop name").get(); reader.startTag(Constants.STOP_FACILITY, atts, context); reader.endTag(Constants.STOP_FACILITY, EMPTY_STRING, context); reader.endTag(context.pop(), EMPTY_STRING, context);// TRANSIT_STOPS reader.endTag(context.pop(), EMPTY_STRING, context);// TRANSIT_SCHEDULE assertEquals(1, schedule.getFacilities().size()); TransitStopFacility stop = schedule.getFacilities().get(Id.create("stop1", TransitStopFacility.class)); assertNotNull(stop); assertEquals(79.0, stop.getCoord().getX(), MatsimTestCase.EPSILON); assertEquals(80.0, stop.getCoord().getY(), MatsimTestCase.EPSILON); assertEquals("some stop name", stop.getName()); }