MATSIM
PopulationWriterHandlerImplV5.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * PlansWriterHandlerImplV4.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 
24 
25 import java.io.BufferedWriter;
26 import java.io.IOException;
27 
28 import org.apache.logging.log4j.LogManager;
29 import org.apache.logging.log4j.Logger;
30 import org.matsim.api.core.v01.Coord;
31 import org.matsim.api.core.v01.Id;
44 import org.matsim.core.utils.misc.Time;
45 
50 /*package*/ class PopulationWriterHandlerImplV5 implements PopulationWriterHandler {
51  private static final Logger log = LogManager.getLogger( PopulationWriterHandlerImplV5.class );
52 
53  private final CoordinateTransformation coordinateTransformation;
54 
55  PopulationWriterHandlerImplV5(CoordinateTransformation coordinateTransformation) {
56  this.coordinateTransformation = coordinateTransformation;
57  }
58 
59  @Override
60  public void writeHeaderAndStartElement(final BufferedWriter out) throws IOException {
61  out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
62  out.write("<!DOCTYPE population SYSTEM \"" + MatsimXmlWriter.DEFAULT_DTD_LOCATION + "population_v5.dtd\">\n\n");
63  }
64 
65  @Override
66  public void startPlans(final Population plans, final BufferedWriter out) throws IOException {
67  out.write("<population");
68  if (plans.getName() != null) {
69  out.write(" desc=\"" + encodeAttributeValue(plans.getName()) + "\"");
70  }
71  out.write(">\n\n");
72  }
73 
74  @Override
75  public void writePerson(final Person person, final BufferedWriter out) throws IOException {
76  PopulationWriterHandlerImplV5.startPerson(person, out);
77  for (Plan plan : person.getPlans()) {
78  PopulationWriterHandlerImplV5.startPlan(plan, out);
79  // act/leg
80  for (PlanElement pe : plan.getPlanElements()) {
81  if (pe instanceof Activity) {
82  Activity act = (Activity) pe;
83  this.writeAct(act, out);
84  }
85  else if (pe instanceof Leg) {
86  Leg leg = (Leg) pe;
87  PopulationWriterHandlerImplV5.startLeg(leg, out);
88  // route
89  Route route = leg.getRoute();
90  if (route != null) {
91  PopulationWriterHandlerImplV5.startRoute(route, out);
92  PopulationWriterHandlerImplV5.endRoute(out);
93  }
94  PopulationWriterHandlerImplV5.endLeg(out);
95  }
96  }
97  PopulationWriterHandlerImplV5.endPlan(out);
98  }
99  PopulationWriterHandlerImplV5.endPerson(out);
100  this.writeSeparator(out);
101  }
102 
103  @Override
104  public void endPlans(final BufferedWriter out) throws IOException {
105  out.write("</population>\n");
106  out.flush();
107  }
108 
109  private static void startPerson(final Person person, final BufferedWriter out) throws IOException {
110  out.write("\t<person id=\"");
111  out.write(encodeAttributeValue(person.getId().toString()));
112  out.write("\"");
113  if (PersonUtils.getSex(person) != null) {
114  out.write(" sex=\"");
115  out.write(PersonUtils.getSex(person));
116  out.write("\"");
117  }
118  if (PersonUtils.getAge(person) != null) {
119  out.write(" age=\"");
120  out.write(Integer.toString(PersonUtils.getAge(person)));
121  out.write("\"");
122  }
123  if (PersonUtils.getLicense(person) != null) {
124  out.write(" license=\"");
125  out.write(PersonUtils.getLicense(person));
126  out.write("\"");
127  }
128  if (PersonUtils.getCarAvail(person) != null) {
129  out.write(" car_avail=\"");
130  out.write(PersonUtils.getCarAvail(person));
131  out.write("\"");
132  }
133  if (PersonUtils.isEmployed(person) != null) {
134  out.write(" employed=\"");
135  out.write((PersonUtils.isEmployed(person) ? "yes" : "no"));
136  out.write("\"");
137  }
138  out.write(">\n");
139  }
140 
141  private static void endPerson(final BufferedWriter out) throws IOException {
142  out.write("\t</person>\n\n");
143  }
144 
145  private static void startPlan(final Plan plan, final BufferedWriter out) throws IOException {
146  out.write("\t\t<plan");
147  if (plan.getScore() != null) {
148  out.write(" score=\"");
149  out.write(plan.getScore().toString());
150  out.write("\"");
151  }
152  if (PersonUtils.isSelected(plan))
153  out.write(" selected=\"yes\"");
154  else
155  out.write(" selected=\"no\"");
156  if ((plan.getType() != null)) {
157  out.write(" type=\"");
158  out.write(encodeAttributeValue(plan.getType()));
159  out.write("\"");
160  }
161  out.write(">\n");
162  }
163 
164  private static void endPlan(final BufferedWriter out) throws IOException {
165  out.write("\t\t</plan>\n\n");
166  }
167 
168  private void writeAct(final Activity act, final BufferedWriter out) throws IOException {
169  out.write("\t\t\t<act type=\"");
170  out.write(encodeAttributeValue(act.getType()));
171  out.write("\"");
172  if (act.getLinkId() != null) {
173  out.write(" link=\"");
174  out.write(encodeAttributeValue(act.getLinkId().toString()));
175  out.write("\"");
176  }
177  if (act.getFacilityId() != null) {
178  out.write(" facility=\"");
179  out.write(encodeAttributeValue(act.getFacilityId().toString()));
180  out.write("\"");
181  }
182  if (act.getCoord() != null) {
183  final Coord coord = this.coordinateTransformation.transform( act.getCoord() );
184  out.write(" x=\"");
185  out.write(Double.toString( coord.getX() ));
186  out.write("\" y=\"");
187  out.write(Double.toString( coord.getY() ));
188  out.write("\"");
189  }
190  if (act.getStartTime().isDefined()) {
191  out.write(" start_time=\"");
192  out.write(Time.writeTime(act.getStartTime().seconds()));
193  out.write("\"");
194  }
195  if (act.getMaximumDuration().isDefined()) {
196  out.write(" max_dur=\"");
197  out.write(Time.writeTime(act.getMaximumDuration().seconds()));
198  out.write("\"");
199  }
200  if (act.getEndTime().isDefined()) {
201  out.write(" end_time=\"");
202  out.write(Time.writeTime(act.getEndTime().seconds()));
203  out.write("\"");
204  }
205  out.write(" />\n");
206  }
207 
208  private static void startLeg(final Leg leg, final BufferedWriter out) throws IOException {
209  out.write("\t\t\t<leg mode=\"");
210  out.write(encodeAttributeValue(leg.getMode()));
211  out.write("\"");
212  if (leg.getDepartureTime().isDefined()) {
213  out.write(" dep_time=\"");
214  out.write(Time.writeTime(leg.getDepartureTime().seconds()));
215  out.write("\"");
216  }
217  if (leg.getTravelTime().isDefined()) {
218  out.write(" trav_time=\"");
219  out.write(Time.writeTime(leg.getTravelTime().seconds()));
220  out.write("\"");
221  }
222 // if (leg instanceof LegImpl) {
223 // LegImpl l = (LegImpl)leg;
224 // if (l.getDepartureTime() + l.getTravelTime() != Time.getUndefinedTime()) {
225 // out.write(" arr_time=\"");
226 // out.write(Time.writeTime(l.getDepartureTime() + l.getTravelTime()));
227 // out.write("\"");
228 // }
229 // }
230  // arrival time is in dtd, but no longer evaluated in code (according to not being in API). kai, jun'16
231 
232  out.write(">\n");
233  }
234 
235  private static void endLeg(final BufferedWriter out) throws IOException {
236  out.write("\t\t\t</leg>\n");
237  }
238 
239  private static void startRoute(final Route route, final BufferedWriter out) throws IOException {
240  out.write("\t\t\t\t<route ");
241  out.write("type=\"");
242  out.write(encodeAttributeValue(route.getRouteType()));
243  out.write("\"");
244  out.write(" start_link=\"");
245  out.write(encodeAttributeValue(route.getStartLinkId().toString()));
246  out.write("\"");
247  out.write(" end_link=\"");
248  out.write(encodeAttributeValue(route.getEndLinkId().toString()));
249  out.write("\"");
250  out.write(" trav_time=\"");
251  out.write(Time.writeTime(route.getTravelTime()));
252  out.write("\"");
253  out.write(" distance=\"");
254  out.write(Double.toString(route.getDistance()));
255  out.write("\"");
256 
257  // yyyy would be good if we could make the following stuff automatic. kai, jun'18
258 
259  if ( route instanceof NetworkRoute ) {
260  out.write(" vehicleRefId=\"");
261  out.write(encodeAttributeValue(Id.writeId(((NetworkRoute) route).getVehicleId())));
262  out.write("\"");
263  }
264  out.write(">");
265  String rd = route.getRouteDescription();
266  if (rd != null) {
267  out.write(XmlUtils.encodeContent(rd));
268  }
269  }
270 
271  private static void endRoute(final BufferedWriter out) throws IOException {
272  out.write("</route>\n");
273  }
274 
275  @Override
276  public void writeSeparator(final BufferedWriter out) throws IOException {
277  out.write("<!-- ====================================================================== -->\n\n");
278  }
279 
280 }
static String encodeAttributeValue(final String attributeValue)
Definition: XmlUtils.java:41