case POPULATION_V5: this.delegate = new PopulationReaderMatsimV5( transformation, this.scenario);
@Override public void endTag(final String name, final String content, final Stack<String> context) { if (PERSON.equals(name)) { this.plans.addPerson(this.currperson); this.currperson = null; } else if (PLAN.equals(name)) { if (this.currplan.getPlanElements() instanceof ArrayList<?>) { ((ArrayList<?>) this.currplan.getPlanElements()).trimToSize(); } this.currplan = null; } else if (ACT.equals(name)) { this.prevAct = this.curract; this.curract = null; } else if (ROUTE.equals(name)) { endRoute(content); } }
private void startAct(final Attributes atts) { if (atts.getValue(ATTR_ACT_LINK) != null) { Id<Link> linkId = Id.create(atts.getValue(ATTR_ACT_LINK), Link.class); final Id<Link> linkId1 = linkId; this.curract = PopulationUtils.createAndAddActivityFromLinkId(this.currplan, atts.getValue(ATTR_ACT_TYPE), linkId1); if ((atts.getValue(ATTR_ACT_X) != null) && (atts.getValue(ATTR_ACT_Y) != null)) { final Coord coord = parseCoord( atts ); this.curract.setCoord(coord); } } else if ((atts.getValue(ATTR_ACT_X) != null) && (atts.getValue(ATTR_ACT_Y) != null)) { final Coord coord = parseCoord( atts ); this.curract = PopulationUtils.createAndAddActivityFromCoord(this.currplan, atts.getValue(ATTR_ACT_TYPE), coord); } else { throw new IllegalArgumentException("In this version of MATSim either the coords or the link must be specified for an Act."); } this.curract.setStartTime(Time.parseTime(atts.getValue(ATTR_ACT_STARTTIME))); this.curract.setMaximumDuration(Time.parseTime(atts.getValue(ATTR_ACT_MAXDUR))); this.curract.setEndTime(Time.parseTime(atts.getValue(ATTR_ACT_ENDTIME))); String fId = atts.getValue(ATTR_ACT_FACILITY); if (fId != null) { this.curract.setFacilityId(Id.create(fId, ActivityFacility.class)); } if (this.routeDescription != null) { finishLastRoute(); } }
@Override public void startTag(final String name, final Attributes atts, final Stack<String> context) { if (POPULATION.equals(name)) { startPopulation(atts); } else if (PERSON.equals(name)) { startPerson(atts); } else if (PLAN.equals(name)) { startPlan(atts); } else if (ACT.equals(name)) { startAct(atts); } else if (LEG.equals(name)) { startLeg(atts); } else if (ROUTE.equals(name)) { startRoute(atts); } else { throw new RuntimeException(this + "[tag=" + name + " not known or not supported]"); } }
@Test public void testRepeatingActs() { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); PopulationReaderMatsimV5 reader = new PopulationReaderMatsimV5(scenario); final Population population = scenario.getPopulation(); String str = "<?xml version=\"1.0\" ?>"+ "<!DOCTYPE population SYSTEM \"http://www.matsim.org/files/dtd/population_v5.dtd\">"+ "<population>"+ "<person id=\"1\">"+ " <plan>"+ " <act type=\"h\" x=\"-25000\" y=\"0\" end_time=\"06:00\" />"+ " <leg mode=\"walk\" />"+ " <act type=\"w\" x=\"10000\" y=\"0\" end_time=\"12:00\" />"+ " <act type=\"l\" x=\"10000\" y=\"0\" />"+ " </plan>"+ "</person>"+ "</population>"; reader.parse(new ByteArrayInputStream(str.getBytes())); Plan plan = population.getPersons().get(Id.create(1, Person.class)).getSelectedPlan(); Assert.assertEquals(4, plan.getPlanElements().size()); Assert.assertTrue(plan.getPlanElements().get(0) instanceof Activity); Assert.assertTrue(plan.getPlanElements().get(1) instanceof Leg); Assert.assertTrue(plan.getPlanElements().get(2) instanceof Activity); Assert.assertTrue(plan.getPlanElements().get(3) instanceof Activity); }
/** * @author mrieser */ @Test public void testReadActivity() { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); final Population population = scenario.getPopulation(); PopulationReaderMatsimV5 reader = new PopulationReaderMatsimV5(scenario); Stack<String> context = new Stack<String>(); // not sure the context is ever used in the reader... reader.startTag("population", AttributesBuilder.getEmpty(), context); reader.startTag("person", new AttributesBuilder().add("id", "2").get(), context); reader.startTag("plan", new AttributesBuilder().add("selected", "no").get(), context); reader.startTag("act", new AttributesBuilder().add("type", "h").add("link", "3").get(), context); reader.endTag("act", "", context); reader.startTag("leg", new AttributesBuilder().add("mode", "car").get(), context); reader.endTag("leg", "", context); reader.startTag("act", new AttributesBuilder().add("type", "h").add("link", "2").get(), context); reader.endTag("plan", "", context); reader.endTag("person", "", context); reader.endTag("population", "", context); Assert.assertEquals(1, population.getPersons().size()); Person person = population.getPersons().get(Id.create("2", Person.class)); Plan plan = person.getPlans().get(0); Assert.assertEquals("3", ((Activity) plan.getPlanElements().get(0)).getLinkId().toString()); Assert.assertEquals("2", ((Activity) plan.getPlanElements().get(2)).getLinkId().toString()); }
PopulationReaderMatsimV5 popReader = new PopulationReaderMatsimV5(scenario); popReader.readFile(filename); Person person2 = pop2.getPersons().get(Id.create(1, Person.class)); Leg leg2 = (Leg) person2.getPlans().get(0).getPlanElements().get(1);
private void startLeg(final Attributes atts) { if (this.routeDescription != null) { finishLastRoute(); } String mode = atts.getValue(ATTR_LEG_MODE); if (VALUE_UNDEF.equals(mode)) { mode = "undefined"; } this.currleg = PopulationUtils.createAndAddLeg( this.currplan, mode.intern() ); this.currleg.setDepartureTime(Time.parseTime(atts.getValue(ATTR_LEG_DEPTIME))); this.currleg.setTravelTime(Time.parseTime(atts.getValue(ATTR_LEG_TRAVTIME))); // LegImpl r = this.currleg; // r.setTravelTime( Time.parseTime(atts.getValue(ATTR_LEG_ARRTIME)) - r.getDepartureTime() ); // arrival time is in dtd, but no longer evaluated in code (according to not being in API). kai, jun'16 }
@Test public void testRepeatingLegs() { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); PopulationReaderMatsimV5 reader = new PopulationReaderMatsimV5(scenario); final Population population = scenario.getPopulation(); String str = "<?xml version=\"1.0\" ?>"+ "<!DOCTYPE population SYSTEM \"http://www.matsim.org/files/dtd/population_v5.dtd\">"+ "<population>"+ "<person id=\"1\">"+ " <plan>"+ " <act type=\"h\" x=\"-25000\" y=\"0\" end_time=\"06:00\" />"+ " <leg mode=\"walk\" />"+ " <leg mode=\"pt\" />"+ " <leg mode=\"walk\" />"+ " <act type=\"w\" x=\"10000\" y=\"0\" />"+ " </plan>"+ "</person>"+ "</population>"; reader.parse(new ByteArrayInputStream(str.getBytes())); Plan plan = population.getPersons().get(Id.create(1, Person.class)).getSelectedPlan(); Assert.assertEquals(5, plan.getPlanElements().size()); Assert.assertTrue(plan.getPlanElements().get(0) instanceof Activity); Assert.assertTrue(plan.getPlanElements().get(1) instanceof Leg); Assert.assertTrue(plan.getPlanElements().get(2) instanceof Leg); Assert.assertTrue(plan.getPlanElements().get(3) instanceof Leg); Assert.assertTrue(plan.getPlanElements().get(4) instanceof Activity); }
final Population population = scenario.getPopulation(); PopulationReaderMatsimV5 parser = new PopulationReaderMatsimV5(scenario); parser.startTag("population", new AttributesBuilder().add("name", "").get(), context); parser.startTag("person", new AttributesBuilder().add("id", "981").get(), context); parser.startTag("plan", new AttributesBuilder().add("selected", "yes").get(), context); parser.startTag("act", new AttributesBuilder().add("type", "h").add("x", "125").add("y", "500").add("end_time", "08:00:00").get(), context); parser.endTag("act", null, context); parser.startTag("leg", new AttributesBuilder().add("mode", "pt").get(), context); parser.startTag("route", new AttributesBuilder().add("type", "links").get(), context); parser.endTag("route", " ", context); parser.endTag("leg", null, context); parser.startTag("act", new AttributesBuilder().add("type", "w").add("x", "500").add("y", "1100").add("start_time", "10:05:00").get(), context); parser.endTag("act", null, context); parser.endTag("plan", null, context); parser.endTag("person", null, context); parser.endTag("population", null, context);
@Test public void testVehicleIdInRoute() { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); PopulationReaderMatsimV5 reader = new PopulationReaderMatsimV5(scenario); final Population population = scenario.getPopulation(); String str = "<?xml version=\"1.0\" ?>"+ "<!DOCTYPE population SYSTEM \"http://www.matsim.org/files/dtd/population_v5.dtd\">"+ "<population>"+ "<person id=\"1\">"+ " <plan>"+ " <act type=\"h\" x=\"-25000\" y=\"0\" end_time=\"06:00\" />"+ " <leg mode=\"car\" >"+ " <route type=\"links\" vehicleRefId=\"123\"/>" + " </leg>" + " <act type=\"w\" x=\"10000\" y=\"0\" end_time=\"12:00\" />"+ " </plan>"+ "</person>"+ "</population>"; reader.parse(new ByteArrayInputStream(str.getBytes())); Plan plan = population.getPersons().get(Id.create(1, Person.class)).getSelectedPlan(); Assert.assertEquals(3, plan.getPlanElements().size()); Assert.assertTrue(plan.getPlanElements().get(0) instanceof Activity); Assert.assertTrue(plan.getPlanElements().get(1) instanceof Leg); Leg leg = (Leg) plan.getPlanElements().get(1) ; NetworkRoute route = (NetworkRoute) leg.getRoute() ; Assert.assertEquals(Id.create("123", Vehicle.class), route.getVehicleId() ) ; Assert.assertTrue(plan.getPlanElements().get(2) instanceof Activity); }
RouteInterceptingPopulationReader popReader = new RouteInterceptingPopulationReader(new PopulationReaderMatsimV5(scenario)); popReader.readFile(filename); Person person2 = pop2.getPersons().get(Id.create(1, Person.class));
@Test public void testReadingOldRoutesWithoutType() { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); PopulationReaderMatsimV5 reader = new PopulationReaderMatsimV5(scenario); final Population population = scenario.getPopulation(); "</person>"+ "</population>"; reader.parse(new ByteArrayInputStream(str.getBytes()));
@Test public void testReadRoute_sameLinkRoute() throws SAXException, ParserConfigurationException, IOException { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); final Population population = scenario.getPopulation(); MatsimXmlParser parser = new PopulationReaderMatsimV5(scenario); XmlParserTestHelper tester = new XmlParserTestHelper(parser); tester.startTag("population"); tester.startTag("person", new String[][]{{"id", "1"}}); tester.startTag("plan", new String[][]{{"selected", "no"}}); tester.startTag("act", new String[][]{{"type", "h"}, {"x", "-25000"}, {"y", "0"}, {"link", "1"}, {"end_time", "06:00"}}); tester.endTag(); tester.startTag("leg", new String[][]{{"mode", "car"}}); tester.startTag("route", new String[][]{{"type", "links"}}); tester.endTag("1"); tester.endTag(); tester.startTag("act", new String[][]{{"type", "w"}, {"x", "10000"}, {"y", "0"}, {"link", "1"}, {"dur", "00:10"}}); tester.endTag(); tester.endTag(); tester.endTag(); tester.endTag(); Person person1 = population.getPersons().get(Id.create("1", Person.class)); Plan plan1 = person1.getPlans().get(0); Leg leg1a = (Leg) plan1.getPlanElements().get(1); Route route1a = leg1a.getRoute(); Assert.assertEquals("different startLink for first leg.", "1", route1a.getStartLinkId().toString()); Assert.assertEquals("different endLink for first leg.", "1", route1a.getEndLinkId().toString()); Assert.assertTrue(route1a instanceof NetworkRoute); NetworkRoute nr = (NetworkRoute) route1a; Assert.assertEquals(0, nr.getLinkIds().size()); }
@Test public void testReadRoute_consequentLinks() throws SAXException, ParserConfigurationException, IOException { final MutableScenario scenario = (MutableScenario) ScenarioUtils.createScenario(ConfigUtils.createConfig()); final Population population = scenario.getPopulation(); MatsimXmlParser parser = new PopulationReaderMatsimV5(scenario); XmlParserTestHelper tester = new XmlParserTestHelper(parser); tester.startTag("population"); tester.startTag("person", new String[][]{{"id", "1"}}); tester.startTag("plan", new String[][]{{"selected", "no"}}); tester.startTag("act", new String[][]{{"type", "h"}, {"x", "-25000"}, {"y", "0"}, {"link", "1"}, {"end_time", "06:00"}}); tester.endTag(); tester.startTag("leg", new String[][]{{"mode", "car"}}); tester.startTag("route", new String[][]{{"type", "links"}}); tester.endTag("1 2"); tester.endTag(); tester.startTag("act", new String[][]{{"type", "w"}, {"x", "10000"}, {"y", "0"}, {"link", "2"}, {"dur", "00:10"}}); tester.endTag(); tester.endTag(); tester.endTag(); tester.endTag(); Person person1 = population.getPersons().get(Id.create("1", Person.class)); Plan plan1 = person1.getPlans().get(0); Leg leg1a = (Leg) plan1.getPlanElements().get(1); Route route1a = leg1a.getRoute(); Assert.assertEquals("different startLink for first leg.", "1", route1a.getStartLinkId().toString()); Assert.assertEquals("different endLink for first leg.", "2", route1a.getEndLinkId().toString()); Assert.assertTrue(route1a instanceof NetworkRoute); NetworkRoute nr = (NetworkRoute) route1a; Assert.assertEquals(0, nr.getLinkIds().size()); }
final Population population = scenario.getPopulation(); MatsimXmlParser parser = new PopulationReaderMatsimV5(scenario); XmlParserTestHelper tester = new XmlParserTestHelper(parser);