MATSIM
PopulationReaderMatsimV0.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlansReaderMatsimV0.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.Locale;
24 import java.util.Stack;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Coord;
29 import org.matsim.api.core.v01.Id;
30 import org.matsim.api.core.v01.Scenario;
47 import org.matsim.core.utils.misc.Time;
48 import org.xml.sax.Attributes;
49 
56 /*package*/ class PopulationReaderMatsimV0 extends MatsimXmlParser implements MatsimReader {
57 
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";
68 
69  private final static String ATTR_X100 = "x100";
70  private final static String ATTR_Y100 = "y100";
71 
72  private final CoordinateTransformation coordinateTransformation;
73 
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;
80 
81  private Activity prevAct = null;
82  private String routeNodes = null;
83 
84  private static final Logger log = LogManager.getLogger(PopulationReaderMatsimV0.class);
85 
86  protected PopulationReaderMatsimV0(
87  final Scenario scenario) {
88  this( new IdentityTransformation() , scenario );
89  }
90 
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();
98  }
99 
100  @Override
101  public void startTag(final String name, final Attributes atts, final Stack<String> context) {
102  if (PERSON.equals(name)) {
103  startPerson(atts);
104  } else if (PLAN.equals(name)) {
105  startPlan(atts);
106  } else if (ACT.equals(name)) {
107  startAct(atts);
108  } else if (LEG.equals(name)) {
109  startLeg(atts);
110  } else if (ROUTE.equals(name)) {
111  startRoute();
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]");
116  }
117  }
118 
119  @Override
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)) {
127  this.currleg = null;
128  } else if (ROUTE.equals(name)) {
129  this.routeNodes = content;
130  }
131  }
132 
133  private void startPerson(final Attributes atts) {
134  this.currperson = this.plans.getFactory().createPerson(Id.create(atts.getValue("id"), Person.class));
135  }
136 
137  private void startPlan(final Attributes atts) {
138  String sel = atts.getValue("selected");
139  boolean selected;
140  if (sel.equals("yes")) {
141  selected = true;
142  }
143  else if (sel.equals("no")) {
144  selected = false;
145  }
146  else {
147  throw new NumberFormatException("Attribute 'selected' of Element 'Plan' is neither 'yes' nor 'no'.");
148  }
149  this.currplan = PersonUtils.createAndAddPlan(this.currperson, selected);
150  this.routeNodes = null;
151 
152  String scoreString = atts.getValue("score");
153  if (scoreString != null) {
154  double score = Double.parseDouble(scoreString);
155  this.currplan.setScore(score);
156  }
157 
158  }
159 
160  private void startAct(final Attributes atts) {
161  if (atts.getValue("zone") != null) {
162  log.info("The attribute 'zone' of <act> will be ignored");
163  }
164 
165  Activity act;
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 );
172  act.setCoord(coord);
173  }
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);
177  } else {
178  throw new IllegalArgumentException("Either the coords or the link must be specified for an Act.");
179  }
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);
186 
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;
191  }
192  this.prevAct = act;
193  }
194 
195  private Coord parseCoord(Attributes atts) {
196  return coordinateTransformation.transform(
197  new Coord(
198  Double.parseDouble(atts.getValue( ATTR_X100 )),
199  Double.parseDouble(atts.getValue( ATTR_Y100 )) ) );
200  }
201 
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);
208 // LegImpl r = this.currleg;
209 // r.setTravelTime( Time.parseTime(atts.getValue("arr_time")) - r.getDepartureTime() );
210  // arrival time is in dtd, but no longer evaluated in code (according to not being in API). kai, jun'16
211  }
212 
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);
216  }
217 
218 }
abstract void startTag(String name, Attributes atts, Stack< String > context)