MATSIM
VehicleWriterV1.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.UncheckedIOException;
26 import java.util.ArrayList;
27 import java.util.List;
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.Id;
33 import org.matsim.core.gbl.Gbl;
36 
41 public final class VehicleWriterV1 extends MatsimXmlWriter {
42 
43  private static final Logger log = LogManager.getLogger(VehicleWriterV1.class);
44 
45  private List<Tuple<String, String>> atts = new ArrayList<Tuple<String, String>>();
46  private Map<Id<VehicleType>, VehicleType> vehicleTypes;
47  private Map<Id<Vehicle>, Vehicle> vehicles;
48 
52  @Deprecated
53  public VehicleWriterV1(Vehicles vehicles) {
54  log.warn("VehiclesV1 is deprecated; Please use MatsimVehicleWriter to always write with the currently supported version");
55  this.vehicleTypes = vehicles.getVehicleTypes();
56  this.vehicles = vehicles.getVehicles();
57  }
58 
59  public void writeFile(String filename) throws UncheckedIOException {
60  log.info( Gbl.aboutToWrite( "vehicles", filename) ) ;
61  this.openFile(filename);
62  this.writeXmlHead();
63  this.writeRootElement();
64  this.close();
65  }
66 
67  private void writeRootElement() throws UncheckedIOException {
68  atts.clear();
70  atts.add(this.createTuple(XMLNS + ":xsi", DEFAULTSCHEMANAMESPACELOCATION));
71  atts.add(this.createTuple("xsi:schemaLocation", MATSIM_NAMESPACE + " " + DEFAULT_DTD_LOCATION + "vehicleDefinitions_v1.0.xsd"));
72  this.writeStartTag(VehicleSchemaV1Names.VEHICLEDEFINITIONS, atts);
73  this.writeVehicleTypes(this.vehicleTypes);
74  this.writeVehicles(this.vehicles);
75  this.writeEndTag(VehicleSchemaV1Names.VEHICLEDEFINITIONS);
76  }
77 
78  private void writeVehicles(Map<Id<Vehicle>, Vehicle> veh) throws UncheckedIOException {
79  List<Vehicle> sortedVehicles = veh.values().stream().sorted(comparing(Vehicle::getId)).collect(toList());
80  for (Vehicle v : sortedVehicles) {
81  atts.clear();
82  atts.add(this.createTuple(VehicleSchemaV1Names.ID, v.getId().toString()));
83  atts.add(this.createTuple(VehicleSchemaV1Names.TYPE, v.getType().getId().toString()));
84  this.writeStartTag(VehicleSchemaV1Names.VEHICLE, atts, true);
85  }
86  }
87 
88  private void writeVehicleTypes(Map<Id<VehicleType>, VehicleType> vts) throws UncheckedIOException {
89  List<VehicleType> sortedVehicleTypes = vts.values()
90  .stream()
91  .sorted(comparing(VehicleType::getId))
92  .collect(toList());
93  for (VehicleType vt : sortedVehicleTypes) {
94  atts.clear();
95  atts.add(this.createTuple(VehicleSchemaV1Names.ID, vt.getId().toString()));
96  this.writeStartTag(VehicleSchemaV1Names.VEHICLETYPE, atts);
97  if (vt.getDescription() != null) {
98  this.writeStartTag(VehicleSchemaV1Names.DESCRIPTION, null);
99  this.writeContent(vt.getDescription(), true);
100  this.writeEndTag(VehicleSchemaV1Names.DESCRIPTION);
101  }
102  if (vt.getCapacity() != null) {
103  this.writeCapacity(vt.getCapacity());
104  }
105  if (!Double.isNaN(vt.getLength())){
106  atts.clear();
107  atts.add(this.createTuple(VehicleSchemaV1Names.METER, Double.toString(vt.getLength())));
108  this.writeStartTag(VehicleSchemaV1Names.LENGTH, atts, true);
109  }
110  if (!Double.isNaN(vt.getWidth())){
111  atts.clear();
112  atts.add(this.createTuple(VehicleSchemaV1Names.METER, Double.toString(vt.getWidth())));
113  this.writeStartTag(VehicleSchemaV1Names.WIDTH, atts, true);
114  }
115  if (!Double.isNaN(vt.getMaximumVelocity()) && !Double.isInfinite(vt.getMaximumVelocity())){
116  atts.clear();
117  atts.add(this.createTuple(VehicleSchemaV1Names.METERPERSECOND, Double.toString(vt.getMaximumVelocity())));
118  this.writeStartTag(VehicleSchemaV1Names.MAXIMUMVELOCITY, atts, true);
119  }
120  if (vt.getEngineInformation() != null && !vt.getEngineInformation().getAttributes().isEmpty()) {
121  log.info("try to write EngineInformation for vehType" + vt.getId());
122  this.writeEngineInformation(vt.getEngineInformation());
123  }
124  atts.clear();
125  atts.add(this.createTuple(VehicleSchemaV1Names.SECONDSPERPERSON, VehicleUtils.getAccessTime(vt)));
126  this.writeStartTag(VehicleSchemaV1Names.ACCESSTIME, atts, true);
127  atts.clear();
128  atts.add(this.createTuple(VehicleSchemaV1Names.SECONDSPERPERSON, VehicleUtils.getEgressTime(vt)));
129  this.writeStartTag(VehicleSchemaV1Names.EGRESSTIME, atts, true);
130  atts.clear();
131  atts.add(this.createTuple(VehicleSchemaV1Names.MODE, VehicleUtils.getDoorOperationMode(vt).toString()));
132  this.writeStartTag(VehicleSchemaV1Names.DOOROPERATION, atts, true);
133  atts.clear();
134  atts.add(this.createTuple(VehicleSchemaV1Names.PCE, vt.getPcuEquivalents()));
135  this.writeStartTag(VehicleSchemaV1Names.PASSENGERCAREQUIVALENTS, atts, true);
136  this.writeEndTag(VehicleSchemaV1Names.VEHICLETYPE);
137  }
138  }
139 
140  private void writeEngineInformation( EngineInformation ei ) throws UncheckedIOException {
141  this.writeStartTag(VehicleSchemaV1Names.ENGINEINFORMATION, null);
142  this.writeStartTag(VehicleSchemaV1Names.FUELTYPE, null);
143 // this.writeContent(ei.getFuelType().toString(), false);
144  if (VehicleUtils.getFuelType(ei) != null) {
145  this.writeContent(VehicleUtils.getFuelType(ei).toString(), false);
146  }
147  this.writeEndTag(VehicleSchemaV1Names.FUELTYPE);
148  atts.clear();
149 // log.warn("EngineInformation: " + ei + " fc: " + ei.getFuelConsumption());
150  if((VehicleUtils.getFuelConsumption(ei) != null)) {
151  atts.add(this.createTuple(VehicleSchemaV1Names.LITERPERMETER, VehicleUtils.getFuelConsumption(ei)));
152  this.writeStartTag(VehicleSchemaV1Names.GASCONSUMPTION, atts, true);
153  }
154  this.writeEndTag(VehicleSchemaV1Names.ENGINEINFORMATION);
155  }
156 
157  private void writeCapacity(VehicleCapacity cap) throws UncheckedIOException {
158  this.writeStartTag(VehicleSchemaV1Names.CAPACITY, null);
159  if (cap.getSeats() != null) {
160  atts.clear();
161  atts.add(this.createTuple(VehicleSchemaV1Names.PERSONS, cap.getSeats()));
162  this.writeStartTag(VehicleSchemaV1Names.SEATS, atts, true);
163  }
164  if (cap.getStandingRoom() != null) {
165  atts.clear();
166  atts.add(this.createTuple(VehicleSchemaV1Names.PERSONS, cap.getStandingRoom()));
167  this.writeStartTag(VehicleSchemaV1Names.STANDINGROOM, atts, true);
168  }
169  if( cap.getVolumeInCubicMeters() != null && !Double.isInfinite(cap.getVolumeInCubicMeters())) {
170  this.writeFreightCapacity( cap.getVolumeInCubicMeters() );
171  }
172  this.writeEndTag(VehicleSchemaV1Names.CAPACITY);
173  }
174 
175  private void writeFreightCapacity(double fc) throws UncheckedIOException {
176  this.writeStartTag(VehicleSchemaV1Names.FREIGHTCAPACITY, null);
177  atts.clear();
178  atts.add(this.createTuple(VehicleSchemaV1Names.CUBICMETERS, Double.toString(fc)));
179  this.writeStartTag(VehicleSchemaV1Names.VOLUME, atts, true);
180  this.writeEndTag(VehicleSchemaV1Names.FREIGHTCAPACITY);
181  }
182 
183 }
void writeCapacity(VehicleCapacity cap)
Map< Id< VehicleType >, VehicleType > getVehicleTypes()
static Double getFuelConsumption(VehicleType vehicleType)
static double getAccessTime(VehicleType vehicleType)
static double getEgressTime(VehicleType vehicleType)
final void writeContent(String content, boolean allowWhitespaces)
List< Tuple< String, String > > atts
void writeVehicleTypes(Map< Id< VehicleType >, VehicleType > vts)
Map< Id< Vehicle >, Vehicle > vehicles
final void writeStartTag(String tagname, List< Tuple< String, String >> attributes)
static String aboutToWrite(String what, String filename)
Definition: Gbl.java:254
static Tuple< String, String > createTuple(String one, String two)
final Id< VehicleType > getId()
Map< Id< Vehicle >, Vehicle > getVehicles()
static VehicleType.DoorOperationMode getDoorOperationMode(VehicleType vehicleType)
Map< Id< VehicleType >, VehicleType > vehicleTypes
void writeVehicles(Map< Id< Vehicle >, Vehicle > veh)
void writeEngineInformation(EngineInformation ei)