MATSIM
VehicleWriterV2.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * VehicleDefinitionsWriterV1
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 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 package org.matsim.vehicles;
21 
22 import static java.util.Comparator.comparing;
23 import static java.util.stream.Collectors.toList;
24 
25 import java.io.IOException;
26 import java.io.UncheckedIOException;
27 import java.util.ArrayList;
28 import java.util.Comparator;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.stream.Collectors;
32 
33 import org.apache.logging.log4j.LogManager;
34 import org.apache.logging.log4j.Logger;
35 import org.matsim.api.core.v01.Id;
36 import org.matsim.core.gbl.Gbl;
41 
48 final class VehicleWriterV2 extends MatsimXmlWriter {
49 
50  private static final Logger log = LogManager.getLogger(VehicleWriterV2.class);
51  private AttributesXmlWriterDelegate attributesWriter = new AttributesXmlWriterDelegate();
52 
53  private List<Tuple<String, String>> atts = new ArrayList<Tuple<String, String>>();
54  private Map<Id<VehicleType>, VehicleType> vehicleTypes;
55  private Map<Id<Vehicle>, Vehicle> vehicles;
56 
57 
58  public VehicleWriterV2(Vehicles vehicles) {
59  this.vehicleTypes = vehicles.getVehicleTypes();
60  this.vehicles = vehicles.getVehicles();
61  }
62 
63  public void writeFile(String filename) throws UncheckedIOException, IOException {
64  log.info(Gbl.aboutToWrite("vehicles", filename));
65  this.openFile(filename);
66  this.writeXmlHead();
67  this.writeRootElement();
68  this.close();
69  }
70 
71  private void writeRootElement() throws UncheckedIOException, IOException {
72  atts.clear();
73  atts.add(this.createTuple(XMLNS, MatsimXmlWriter.MATSIM_NAMESPACE));
74  atts.add(this.createTuple(XMLNS + ":xsi", DEFAULTSCHEMANAMESPACELOCATION));
75  atts.add(this.createTuple("xsi:schemaLocation", MATSIM_NAMESPACE + " " + DEFAULT_DTD_LOCATION + "vehicleDefinitions_v2.0.xsd"));
76  this.writeStartTag(VehicleSchemaV2Names.VEHICLEDEFINITIONS, atts);
77  this.writeVehicleTypes(this.vehicleTypes);
78  this.writeVehicles(this.vehicles);
79  this.writeContent("\n", true);
80  this.writeEndTag(VehicleSchemaV2Names.VEHICLEDEFINITIONS);
81  }
82 
83  private void writeVehicles(Map<Id<Vehicle>, Vehicle> veh) throws UncheckedIOException, IOException {
84  List<Vehicle> sortedVehicles = veh.values().stream().sorted(comparing(Vehicle::getId)).collect(toList());
85  for (Vehicle v : sortedVehicles) {
86  atts.clear();
87  atts.add(this.createTuple(VehicleSchemaV2Names.ID, v.getId().toString()));
88  atts.add(this.createTuple(VehicleSchemaV2Names.TYPE, v.getType().getId().toString()));
89 
90  /* The format of the vehicle depends on whether the particular vehicle has attributes specified. */
91  if (v.getAttributes().isEmpty()) {
92  this.writeStartTag(VehicleSchemaV2Names.VEHICLE, atts, true);
93  } else {
94  this.writeStartTag(VehicleSchemaV2Names.VEHICLE, atts, false);
95  this.writer.newLine();
96  attributesWriter.writeAttributes("\t\t", this.writer, v.getAttributes(), false);
97  this.writeEndTag(VehicleSchemaV2Names.VEHICLE);
98  }
99  }
100  }
101 
102  private void writeVehicleTypes(Map<Id<VehicleType>, VehicleType> vts) throws UncheckedIOException, IOException {
103  this.writer.write("\n");
104  List<VehicleType> sortedVehicleTypes = vts.values()
105  .stream()
106  .sorted(Comparator.comparing(VehicleType::getId))
107  .collect(Collectors.toList());
108  for (VehicleType vt : sortedVehicleTypes) {
109  atts.clear();
110  atts.add(this.createTuple(VehicleSchemaV2Names.ID, vt.getId().toString()));
111  this.writeStartTag(VehicleSchemaV2Names.VEHICLETYPE, atts);
112 
113  //Write general vehicleType attributes
114  this.writer.write("\n");
115  attributesWriter.writeAttributes("\t\t", this.writer, vt.getAttributes(), false);
116 
117  //Write vehicleType description, if present
118  if (vt.getDescription() != null) {
119  this.writeElement(VehicleSchemaV2Names.DESCRIPTION, vt.getDescription());
120  }
121 
122  //Write capacity, if present
123  if (vt.getCapacity() != null) {
124  VehicleCapacity vehicleCapacity = vt.getCapacity();
125  atts.clear();
126  if (vehicleCapacity.getSeats() != null) {
127  atts.add(this.createTuple(VehicleSchemaV2Names.SEATS, vehicleCapacity.getSeats()));
128  }
129  if (vehicleCapacity.getStandingRoom() != null) {
130  atts.add(this.createTuple(VehicleSchemaV2Names.STANDINGROOM, vehicleCapacity.getStandingRoom()));
131  }
132  if (vehicleCapacity.getVolumeInCubicMeters() != null && !Double.isNaN(vehicleCapacity.getVolumeInCubicMeters()) && !Double.isInfinite(vehicleCapacity.getVolumeInCubicMeters())) {
133  atts.add(this.createTuple(VehicleSchemaV2Names.VOLUME, vehicleCapacity.getVolumeInCubicMeters()));
134  }
135  if (vehicleCapacity.getWeightInTons() != null && !Double.isNaN(vehicleCapacity.getWeightInTons()) && !Double.isInfinite(vehicleCapacity.getWeightInTons())) {
136  atts.add(this.createTuple(VehicleSchemaV2Names.WEIGHT, vehicleCapacity.getWeightInTons()));
137  }
138  if (vehicleCapacity.getOther() != null && !Double.isNaN(vehicleCapacity.getOther()) && !Double.isInfinite(vehicleCapacity.getOther())) {
139  atts.add(this.createTuple(VehicleSchemaV2Names.OTHER, vehicleCapacity.getOther()));
140  }
141  this.writeStartTag(VehicleSchemaV2Names.CAPACITY, atts);
142  //attributes for capacity
143  this.writer.write("\n");
144  attributesWriter.writeAttributes("\t\t\t", this.writer, vehicleCapacity.getAttributes(), false);
145  this.writeEndTag(VehicleSchemaV2Names.CAPACITY);
146  }
147 
148  //Write length, if present
149  if (!Double.isNaN(vt.getLength())) {
150  atts.clear();
151  atts.add(this.createTuple(VehicleSchemaV2Names.METER, Double.toString(vt.getLength())));
152  this.writeStartTag(VehicleSchemaV2Names.LENGTH, atts, true);
153  }
154 
155  //Write width, if present
156  if (!Double.isNaN(vt.getWidth())) {
157  atts.clear();
158  atts.add(this.createTuple(VehicleSchemaV2Names.METER, Double.toString(vt.getWidth())));
159  this.writeStartTag(VehicleSchemaV2Names.WIDTH, atts, true);
160  }
161 
162  //Write maximumVelocity, if present
163  if (!Double.isNaN(vt.getMaximumVelocity()) && !Double.isInfinite(vt.getMaximumVelocity())) {
164  atts.clear();
165  atts.add(this.createTuple(VehicleSchemaV2Names.METERPERSECOND, Double.toString(vt.getMaximumVelocity())));
166  this.writeStartTag(VehicleSchemaV2Names.MAXIMUMVELOCITY, atts, true);
167  }
168 
169  //Write vehicleType engineInformation, if present
170  if (vt.getEngineInformation() != null & !vt.getEngineInformation().getAttributes().isEmpty()) {
171  atts.clear();
172  this.writeStartTag(VehicleSchemaV2Names.ENGINEINFORMATION, atts);
173  this.writer.write("\n");
174  attributesWriter.writeAttributes("\t\t\t", this.writer, vt.getEngineInformation().getAttributes(), false);
175  this.writeEndTag(VehicleSchemaV2Names.ENGINEINFORMATION);
176  }
177 
178  //Write vehicleType costInformation, if present
179  if (vt.getCostInformation() != null) {
180  CostInformation costInformation = vt.getCostInformation();
181  atts.clear();
182  if (costInformation.getFixedCosts() != null && !Double.isNaN(costInformation.getFixedCosts())) {
183  atts.add(this.createTuple(VehicleSchemaV2Names.FIXEDCOSTSPERDAY, costInformation.getFixedCosts()));
184  }
185  if (costInformation.getCostsPerMeter() != null && !Double.isNaN(costInformation.getCostsPerMeter())) {
186  atts.add(this.createTuple(VehicleSchemaV2Names.COSTSPERMETER, costInformation.getCostsPerMeter()));
187  }
188  if (costInformation.getCostsPerSecond() != null && !Double.isNaN(costInformation.getCostsPerSecond())) {
189  atts.add(this.createTuple(VehicleSchemaV2Names.COSTSPERSECOND, costInformation.getCostsPerSecond()));
190  }
191  this.writeStartTag(VehicleSchemaV2Names.COSTINFORMATION, atts);
192  //attributes for costInformation
193  this.writer.write("\n");
194  attributesWriter.writeAttributes("\t\t\t", this.writer, costInformation.getAttributes(), false);
195  this.writeEndTag(VehicleSchemaV2Names.COSTINFORMATION);
196  }
197 
198  //Write passengerCarEquivalents, if present
199  if (!Double.isNaN(vt.getPcuEquivalents())) {
200  atts.clear();
201  atts.add(this.createTuple(VehicleSchemaV2Names.PCE, vt.getPcuEquivalents()));
202  this.writeStartTag(VehicleSchemaV2Names.PASSENGERCAREQUIVALENTS, atts, true);
203  }
204 
205  //Write networkMode, if present
206  if (vt.getNetworkMode() != null) {
207  atts.clear();
208  atts.add(this.createTuple(VehicleSchemaV2Names.NETWORKMODE, vt.getNetworkMode()));
209  this.writeStartTag(VehicleSchemaV2Names.NETWORKMODE, atts, true);
210  }
211 
212  //Write flowEfficiencyFactor, if present
213  if (!Double.isNaN(vt.getFlowEfficiencyFactor())) {
214  atts.clear();
215  atts.add(this.createTuple(VehicleSchemaV2Names.FACTOR, vt.getFlowEfficiencyFactor()));
216  this.writeStartTag(VehicleSchemaV2Names.FLOWEFFICIENCYFACTOR, atts, true);
217  }
218 
219  this.writeEndTag(VehicleSchemaV2Names.VEHICLETYPE);
220  this.writer.write("\n");
221  }
222  }
223 
224  public void putAttributeConverters(Map<Class<?>, AttributeConverter<?>> converters) {
225  this.attributesWriter.putAttributeConverters(converters);
226  }
227 }
final void writeContent(String content, boolean allowWhitespaces)
final void writeElement(String tagname, String content)
final void writeStartTag(String tagname, List< Tuple< String, String >> attributes)
static Tuple< String, String > createTuple(String one, String two)
final Id< VehicleType > getId()