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