21 package org.matsim.core.population.io;
23 import java.util.ArrayList;
24 import java.util.Locale;
25 import java.util.Stack;
47 import org.xml.sax.Attributes;
55 class PopulationReaderMatsimV1
extends MatsimXmlParser implements
58 private final static String PLANS =
"plans";
59 private final static String PERSON =
"person";
60 private final static String PLAN =
"plan";
61 private final static String ACT =
"act";
62 private final static String LEG =
"leg";
63 private final static String ROUTE =
"route";
65 private final static String ATTR_X100 =
"x100";
66 private final static String ATTR_Y100 =
"y100";
68 private final CoordinateTransformation coordinateTransformation;
70 private final Population plans;
71 private final Network network;
73 private Person currperson = null;
75 private Plan currplan = null;
77 private Leg currleg = null;
79 private NetworkRoute currroute = null;
80 private String routeNodes = null;
82 private Activity prevAct = null;
84 public PopulationReaderMatsimV1(
final Scenario scenario) {
85 this(
new IdentityTransformation() , scenario );
88 public PopulationReaderMatsimV1(
89 final CoordinateTransformation coordinateTransformation,
90 final Scenario scenario) {
91 super(ValidationType.DTD_ONLY);
92 this.coordinateTransformation = coordinateTransformation;
93 this.plans = scenario.getPopulation();
94 this.network = scenario.getNetwork();
98 public void startTag(
final String name,
final Attributes atts,
99 final Stack<String> context) {
100 if (PLANS.equals(name)) {
103 else if (PERSON.equals(name)) {
106 else if (PLAN.equals(name)) {
109 else if (ACT.equals(name)) {
112 else if (LEG.equals(name)) {
115 else if (ROUTE.equals(name)) {
119 throw new RuntimeException(
this +
"[tag=" + name +
" not known or not supported]");
124 public void endTag(
final String name,
final String content,
125 final Stack<String> context) {
126 if (PERSON.equals(name)) {
127 this.plans.addPerson(this.currperson);
128 this.currperson = null;
130 else if (PLAN.equals(name)) {
131 if (this.currplan.getPlanElements() instanceof ArrayList) {
132 ((ArrayList<?>) this.currplan.getPlanElements()).trimToSize();
134 this.currplan = null;
136 else if (LEG.equals(name)) {
139 else if (ROUTE.equals(name)) {
140 this.routeNodes = content;
144 private void startPlans(
final Attributes atts) {
145 this.plans.setName(atts.getValue(
"name"));
148 private void startPerson(
final Attributes atts) {
149 this.currperson = PopulationUtils.getFactory().createPerson(Id.create(atts.getValue(
"id"), Person.class));
150 PersonUtils.setSex(this.currperson, atts.getValue(
"sex"));
152 PersonUtils.setAge(this.currperson, Integer.parseInt(atts.getValue(
"age")));
154 PersonUtils.setLicence(this.currperson, atts.getValue(
"license"));
155 PersonUtils.setCarAvail(this.currperson, atts.getValue(
"car_avail"));
156 String employed = atts.getValue(
"employed");
157 if (employed == null) {
158 PersonUtils.setEmployed(this.currperson, null);
160 PersonUtils.setEmployed(this.currperson,
"yes".equals(employed));
164 private void startPlan(
final Attributes atts) {
165 String sel = atts.getValue(
"selected");
167 if (sel.equals(
"yes")) {
170 else if (sel.equals(
"no")) {
174 throw new NumberFormatException(
175 "Attribute 'selected' of Element 'Plan' is neither 'yes' nor 'no'.");
177 this.currplan = PersonUtils.createAndAddPlan(this.currperson, selected);
178 this.routeNodes = null;
180 String scoreString = atts.getValue(
"score");
181 if (scoreString != null) {
182 double score = Double.parseDouble(scoreString);
183 this.currplan.setScore(score);
188 private void startAct(
final Attributes atts) {
190 if (atts.getValue(
"link") != null) {
191 final Id<Link> linkId = Id.create(atts.getValue(
"link"), Link.class);
192 act = PopulationUtils.createAndAddActivityFromLinkId(this.currplan, atts.getValue(
"type"), linkId);
193 if (atts.getValue(ATTR_X100) != null && atts.getValue(ATTR_Y100) != null) {
194 final Coord coord = parseCoord( atts );
197 }
else if (atts.getValue(ATTR_X100) != null && atts.getValue(ATTR_Y100) != null) {
198 final Coord coord = parseCoord( atts );
199 act = PopulationUtils.createAndAddActivityFromCoord(this.currplan, atts.getValue(
"type"), coord);
201 throw new IllegalArgumentException(
"Either the coords or the link must be specified for an Act.");
203 Time.parseOptionalTime(atts.getValue(
"start_time"))
204 .ifDefinedOrElse(act::setStartTime, act::setStartTimeUndefined);
205 Time.parseOptionalTime(atts.getValue(
"dur"))
206 .ifDefinedOrElse(act::setMaximumDuration, act::setMaximumDurationUndefined);
207 Time.parseOptionalTime(atts.getValue(
"end_time"))
208 .ifDefinedOrElse(act::setEndTime, act::setEndTimeUndefined);
211 if (this.routeNodes != null) {
212 this.currroute.setLinkIds(this.prevAct.getLinkId(), NetworkUtils.getLinkIds(RouteUtils.getLinksFromNodes(NetworkUtils.getNodes(
this.network,
this.routeNodes))), act.getLinkId());
213 this.routeNodes = null;
214 this.currroute = null;
219 private Coord parseCoord(Attributes atts) {
220 return coordinateTransformation.transform(
222 Double.parseDouble(atts.getValue( ATTR_X100 )),
223 Double.parseDouble(atts.getValue( ATTR_Y100 )) ) );
226 private void startLeg(
final Attributes atts) {
227 this.currleg = PopulationUtils.createAndAddLeg( this.currplan, atts.getValue(
"mode").toLowerCase(Locale.ROOT).intern() );
228 Time.parseOptionalTime(atts.getValue(
"dep_time"))
229 .ifDefinedOrElse(currleg::setDepartureTime, currleg::setDepartureTimeUndefined);
230 Time.parseOptionalTime(atts.getValue(
"trav_time"))
231 .ifDefinedOrElse(currleg::setTravelTime, currleg::setTravelTimeUndefined);
237 private void startRoute(
final Attributes atts) {
238 this.currroute = this.plans.getFactory().getRouteFactories().createRoute(NetworkRoute.class,
this.prevAct.getLinkId(), this.prevAct.getLinkId());
239 this.currleg.setRoute(this.currroute);
240 if (atts.getValue(
"dist") != null) {
241 this.currroute.setDistance(Double.parseDouble(atts.getValue(
"dist")));
243 if (atts.getValue(
"trav_time") != null) {
244 Time.parseOptionalTime(atts.getValue(
"trav_time"))
245 .ifDefinedOrElse(currroute::setTravelTime, currroute::setTravelTimeUndefined);
abstract void startTag(String name, Attributes atts, Stack< String > context)