MATSIM
PopulationReaderMatsimV1.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlansReaderMatsimV1.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.population.io;
22 
23 import java.util.ArrayList;
24 import java.util.Locale;
25 import java.util.Stack;
26 
27 import org.matsim.api.core.v01.Coord;
28 import org.matsim.api.core.v01.Id;
29 import org.matsim.api.core.v01.Scenario;
46 import org.matsim.core.utils.misc.Time;
47 import org.xml.sax.Attributes;
48 
55 /*package*/ class PopulationReaderMatsimV1 extends MatsimXmlParser implements
56  MatsimReader {
57 
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";
64 
65  private final static String ATTR_X100 = "x100";
66  private final static String ATTR_Y100 = "y100";
67 
68  private final CoordinateTransformation coordinateTransformation;
69 
70  private final Population plans;
71  private final Network network;
72 
73  private Person currperson = null;
74 
75  private Plan currplan = null;
76 
77  private Leg currleg = null;
78 
79  private NetworkRoute currroute = null;
80  private String routeNodes = null;
81 
82  private Activity prevAct = null;
83 
84  public PopulationReaderMatsimV1(final Scenario scenario) {
85  this( new IdentityTransformation() , scenario );
86  }
87 
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();
95  }
96 
97  @Override
98  public void startTag(final String name, final Attributes atts,
99  final Stack<String> context) {
100  if (PLANS.equals(name)) {
101  startPlans(atts);
102  }
103  else if (PERSON.equals(name)) {
104  startPerson(atts);
105  }
106  else if (PLAN.equals(name)) {
107  startPlan(atts);
108  }
109  else if (ACT.equals(name)) {
110  startAct(atts);
111  }
112  else if (LEG.equals(name)) {
113  startLeg(atts);
114  }
115  else if (ROUTE.equals(name)) {
116  startRoute(atts);
117  }
118  else {
119  throw new RuntimeException(this + "[tag=" + name + " not known or not supported]");
120  }
121  }
122 
123  @Override
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;
129  }
130  else if (PLAN.equals(name)) {
131  if (this.currplan.getPlanElements() instanceof ArrayList) {
132  ((ArrayList<?>) this.currplan.getPlanElements()).trimToSize();
133  }
134  this.currplan = null;
135  }
136  else if (LEG.equals(name)) {
137  this.currleg = null;
138  }
139  else if (ROUTE.equals(name)) {
140  this.routeNodes = content;
141  }
142  }
143 
144  private void startPlans(final Attributes atts) {
145  this.plans.setName(atts.getValue("name"));
146  }
147 
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"));
151 
152  PersonUtils.setAge(this.currperson, Integer.parseInt(atts.getValue("age")));
153 
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);
159  } else {
160  PersonUtils.setEmployed(this.currperson, "yes".equals(employed));
161  }
162  }
163 
164  private void startPlan(final Attributes atts) {
165  String sel = atts.getValue("selected");
166  boolean selected;
167  if (sel.equals("yes")) {
168  selected = true;
169  }
170  else if (sel.equals("no")) {
171  selected = false;
172  }
173  else {
174  throw new NumberFormatException(
175  "Attribute 'selected' of Element 'Plan' is neither 'yes' nor 'no'.");
176  }
177  this.currplan = PersonUtils.createAndAddPlan(this.currperson, selected);
178  this.routeNodes = null;
179 
180  String scoreString = atts.getValue("score");
181  if (scoreString != null) {
182  double score = Double.parseDouble(scoreString);
183  this.currplan.setScore(score);
184  }
185 
186  }
187 
188  private void startAct(final Attributes atts) {
189  Activity act = null;
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 );
195  act.setCoord(coord);
196  }
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);
200  } else {
201  throw new IllegalArgumentException("Either the coords or the link must be specified for an Act.");
202  }
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);
209 
210 
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;
215  }
216  this.prevAct = act;
217  }
218 
219  private Coord parseCoord(Attributes atts) {
220  return coordinateTransformation.transform(
221  new Coord(
222  Double.parseDouble(atts.getValue( ATTR_X100 )),
223  Double.parseDouble(atts.getValue( ATTR_Y100 )) ) );
224  }
225 
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);
232 // LegImpl r = this.currleg;
233 // r.setTravelTime( Time.parseTime(atts.getValue("arr_time")) - r.getDepartureTime() );
234  // arrival time is in dtd, but no longer evaluated in code (according to not being in API). kai, jun'16
235  }
236 
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")));
242  }
243  if (atts.getValue("trav_time") != null) {
244  Time.parseOptionalTime(atts.getValue("trav_time"))
245  .ifDefinedOrElse(currroute::setTravelTime, currroute::setTravelTimeUndefined);
246  }
247  }
248 
249 }
abstract void startTag(String name, Attributes atts, Stack< String > context)