21 package org.matsim.core.population.io;
23 import java.util.Locale;
24 import java.util.Stack;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
48 import org.xml.sax.Attributes;
56 class PopulationReaderMatsimV0
extends MatsimXmlParser implements MatsimReader {
58 private final static String PLANS =
"plans";
59 private final static String DEMAND =
"demand";
60 private final static String SEGMENT =
"segment";
61 private final static String MODEL =
"model";
62 private final static String PARAM =
"param";
63 private final static String PERSON =
"person";
64 private final static String PLAN =
"plan";
65 private final static String ACT =
"act";
66 private final static String LEG =
"leg";
67 private final static String ROUTE =
"route";
69 private final static String ATTR_X100 =
"x100";
70 private final static String ATTR_Y100 =
"y100";
72 private final CoordinateTransformation coordinateTransformation;
74 private final Population plans;
75 private final Network network;
76 private Person currperson = null;
77 private Plan currplan = null;
78 private Leg currleg = null;
79 private NetworkRoute currroute = null;
81 private Activity prevAct = null;
82 private String routeNodes = null;
84 private static final Logger log = LogManager.getLogger(PopulationReaderMatsimV0.class);
86 protected PopulationReaderMatsimV0(
87 final Scenario scenario) {
88 this(
new IdentityTransformation() , scenario );
91 protected PopulationReaderMatsimV0(
92 final CoordinateTransformation coordinateTransformation,
93 final Scenario scenario) {
94 super(ValidationType.DTD_ONLY);
95 this.coordinateTransformation = coordinateTransformation;
96 this.plans = scenario.getPopulation();
97 this.network = scenario.getNetwork();
101 public void startTag(
final String name,
final Attributes atts,
final Stack<String> context) {
102 if (PERSON.equals(name)) {
104 }
else if (PLAN.equals(name)) {
106 }
else if (ACT.equals(name)) {
108 }
else if (LEG.equals(name)) {
110 }
else if (ROUTE.equals(name)) {
112 }
else if (DEMAND.equals(name)) {
113 log.info(
"The tag <demand> is not supported");
114 }
else if (!SEGMENT.equals(name) && !MODEL.equals(name) && !PARAM.equals(name) && !PLANS.equals(name)) {
115 throw new RuntimeException(
this +
"[tag=" + name +
" not known or not supported]");
120 public void endTag(
final String name,
final String content,
final Stack<String> context) {
121 if (PERSON.equals(name)) {
122 this.plans.addPerson(this.currperson);
123 this.currperson = null;
124 }
else if (PLAN.equals(name)) {
125 this.currplan = null;
126 }
else if (LEG.equals(name)) {
128 }
else if (ROUTE.equals(name)) {
129 this.routeNodes = content;
133 private void startPerson(
final Attributes atts) {
134 this.currperson = this.plans.getFactory().createPerson(Id.create(atts.getValue(
"id"), Person.class));
137 private void startPlan(
final Attributes atts) {
138 String sel = atts.getValue(
"selected");
140 if (sel.equals(
"yes")) {
143 else if (sel.equals(
"no")) {
147 throw new NumberFormatException(
"Attribute 'selected' of Element 'Plan' is neither 'yes' nor 'no'.");
149 this.currplan = PersonUtils.createAndAddPlan(this.currperson, selected);
150 this.routeNodes = null;
152 String scoreString = atts.getValue(
"score");
153 if (scoreString != null) {
154 double score = Double.parseDouble(scoreString);
155 this.currplan.setScore(score);
160 private void startAct(
final Attributes atts) {
161 if (atts.getValue(
"zone") != null) {
162 log.info(
"The attribute 'zone' of <act> will be ignored");
166 if (atts.getValue(
"link") != null) {
167 Id<Link> linkId = Id.create(atts.getValue(
"link"), Link.class);
168 final Id<Link> linkId1 = linkId;
169 act = PopulationUtils.createAndAddActivityFromLinkId(this.currplan, atts.getValue(
"type"), linkId1);
170 if (atts.getValue(ATTR_X100) != null && atts.getValue(ATTR_Y100) != null) {
171 final Coord coord = parseCoord( atts );
174 }
else if (atts.getValue(ATTR_X100) != null && atts.getValue(ATTR_Y100) != null) {
175 final Coord coord = parseCoord( atts );
176 act = PopulationUtils.createAndAddActivityFromCoord(this.currplan, atts.getValue(
"type"), coord);
178 throw new IllegalArgumentException(
"Either the coords or the link must be specified for an Act.");
180 Time.parseOptionalTime(atts.getValue(
"start_time"))
181 .ifDefinedOrElse(act::setStartTime, act::setStartTimeUndefined);
182 Time.parseOptionalTime(atts.getValue(
"dur"))
183 .ifDefinedOrElse(act::setMaximumDuration, act::setMaximumDurationUndefined);
184 Time.parseOptionalTime(atts.getValue(
"end_time"))
185 .ifDefinedOrElse(act::setEndTime, act::setEndTimeUndefined);
187 if (this.routeNodes != null) {
188 this.currroute.setLinkIds(this.prevAct.getLinkId(), NetworkUtils.getLinkIds(RouteUtils.getLinksFromNodes(NetworkUtils.getNodes(
this.network,
this.routeNodes))), act.getLinkId());
189 this.routeNodes = null;
190 this.currroute = null;
195 private Coord parseCoord(Attributes atts) {
196 return coordinateTransformation.transform(
198 Double.parseDouble(atts.getValue( ATTR_X100 )),
199 Double.parseDouble(atts.getValue( ATTR_Y100 )) ) );
202 private void startLeg(
final Attributes atts) {
203 this.currleg = PopulationUtils.createAndAddLeg( this.currplan, atts.getValue(
"mode").toLowerCase(Locale.ROOT).intern() );
204 Time.parseOptionalTime(atts.getValue(
"dep_time"))
205 .ifDefinedOrElse(currleg::setDepartureTime, currleg::setDepartureTimeUndefined);
206 Time.parseOptionalTime(atts.getValue(
"trav_time"))
207 .ifDefinedOrElse(currleg::setTravelTime, currleg::setTravelTimeUndefined);
213 private void startRoute() {
214 this.currroute = this.plans.getFactory().getRouteFactories().createRoute(NetworkRoute.class,
this.prevAct.getLinkId(), this.prevAct.getLinkId());
215 this.currleg.setRoute(this.currroute);
abstract void startTag(String name, Attributes atts, Stack< String > context)