20 package org.matsim.vehicles;
22 import static java.util.Comparator.comparing;
23 import static java.util.stream.Collectors.toList;
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;
31 import java.util.stream.Collectors;
33 import org.apache.logging.log4j.LogManager;
34 import org.apache.logging.log4j.Logger;
48 final class VehicleWriterV2
extends MatsimXmlWriter {
50 private static final Logger log = LogManager.getLogger(VehicleWriterV2.class);
51 private AttributesXmlWriterDelegate attributesWriter =
new AttributesXmlWriterDelegate();
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;
58 public VehicleWriterV2(Vehicles vehicles) {
59 this.vehicleTypes = vehicles.getVehicleTypes();
60 this.vehicles = vehicles.getVehicles();
63 public void writeFile(String filename)
throws UncheckedIOException, IOException {
64 log.info(Gbl.aboutToWrite(
"vehicles", filename));
67 this.writeRootElement();
71 private void writeRootElement() throws UncheckedIOException, IOException {
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);
80 this.
writeEndTag(VehicleSchemaV2Names.VEHICLEDEFINITIONS);
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) {
87 atts.add(this.
createTuple(VehicleSchemaV2Names.ID, v.getId().toString()));
88 atts.add(this.
createTuple(VehicleSchemaV2Names.TYPE, v.getType().getId().toString()));
91 if (v.getAttributes().isEmpty()) {
94 this.
writeStartTag(VehicleSchemaV2Names.VEHICLE, atts,
false);
96 attributesWriter.writeAttributes(
"\t\t", this.writer, v.getAttributes(),
false);
102 private void writeVehicleTypes(Map<Id<VehicleType>, VehicleType> vts)
throws UncheckedIOException, IOException {
104 List<VehicleType> sortedVehicleTypes = vts.values()
107 .collect(Collectors.toList());
108 for (VehicleType vt : sortedVehicleTypes) {
110 atts.add(this.
createTuple(VehicleSchemaV2Names.ID, vt.getId().toString()));
115 attributesWriter.writeAttributes(
"\t\t", this.writer, vt.getAttributes(),
false);
118 if (vt.getDescription() != null) {
119 this.
writeElement(VehicleSchemaV2Names.DESCRIPTION, vt.getDescription());
123 if (vt.getCapacity() != null) {
124 VehicleCapacity vehicleCapacity = vt.getCapacity();
126 if (vehicleCapacity.getSeats() != null) {
127 atts.add(this.
createTuple(VehicleSchemaV2Names.SEATS, vehicleCapacity.getSeats()));
129 if (vehicleCapacity.getStandingRoom() != null) {
130 atts.add(this.
createTuple(VehicleSchemaV2Names.STANDINGROOM, vehicleCapacity.getStandingRoom()));
132 if (vehicleCapacity.getVolumeInCubicMeters() != null && !Double.isNaN(vehicleCapacity.getVolumeInCubicMeters()) && !Double.isInfinite(vehicleCapacity.getVolumeInCubicMeters())) {
133 atts.add(this.
createTuple(VehicleSchemaV2Names.VOLUME, vehicleCapacity.getVolumeInCubicMeters()));
135 if (vehicleCapacity.getWeightInTons() != null && !Double.isNaN(vehicleCapacity.getWeightInTons()) && !Double.isInfinite(vehicleCapacity.getWeightInTons())) {
136 atts.add(this.
createTuple(VehicleSchemaV2Names.WEIGHT, vehicleCapacity.getWeightInTons()));
138 if (vehicleCapacity.getOther() != null && !Double.isNaN(vehicleCapacity.getOther()) && !Double.isInfinite(vehicleCapacity.getOther())) {
139 atts.add(this.
createTuple(VehicleSchemaV2Names.OTHER, vehicleCapacity.getOther()));
144 attributesWriter.writeAttributes(
"\t\t\t", this.writer, vehicleCapacity.getAttributes(),
false);
149 if (!Double.isNaN(vt.getLength())) {
151 atts.add(this.
createTuple(VehicleSchemaV2Names.METER, Double.toString(vt.getLength())));
156 if (!Double.isNaN(vt.getWidth())) {
158 atts.add(this.
createTuple(VehicleSchemaV2Names.METER, Double.toString(vt.getWidth())));
163 if (!Double.isNaN(vt.getMaximumVelocity()) && !Double.isInfinite(vt.getMaximumVelocity())) {
165 atts.add(this.
createTuple(VehicleSchemaV2Names.METERPERSECOND, Double.toString(vt.getMaximumVelocity())));
166 this.
writeStartTag(VehicleSchemaV2Names.MAXIMUMVELOCITY, atts,
true);
170 if (vt.getEngineInformation() != null & !vt.getEngineInformation().getAttributes().isEmpty()) {
172 this.
writeStartTag(VehicleSchemaV2Names.ENGINEINFORMATION, atts);
174 attributesWriter.writeAttributes(
"\t\t\t", this.writer, vt.getEngineInformation().getAttributes(),
false);
175 this.
writeEndTag(VehicleSchemaV2Names.ENGINEINFORMATION);
179 if (vt.getCostInformation() != null) {
180 CostInformation costInformation = vt.getCostInformation();
182 if (costInformation.getFixedCosts() != null && !Double.isNaN(costInformation.getFixedCosts())) {
183 atts.add(this.
createTuple(VehicleSchemaV2Names.FIXEDCOSTSPERDAY, costInformation.getFixedCosts()));
185 if (costInformation.getCostsPerMeter() != null && !Double.isNaN(costInformation.getCostsPerMeter())) {
186 atts.add(this.
createTuple(VehicleSchemaV2Names.COSTSPERMETER, costInformation.getCostsPerMeter()));
188 if (costInformation.getCostsPerSecond() != null && !Double.isNaN(costInformation.getCostsPerSecond())) {
189 atts.add(this.
createTuple(VehicleSchemaV2Names.COSTSPERSECOND, costInformation.getCostsPerSecond()));
191 this.
writeStartTag(VehicleSchemaV2Names.COSTINFORMATION, atts);
194 attributesWriter.writeAttributes(
"\t\t\t", this.writer, costInformation.getAttributes(),
false);
195 this.
writeEndTag(VehicleSchemaV2Names.COSTINFORMATION);
199 if (!Double.isNaN(vt.getPcuEquivalents())) {
201 atts.add(this.
createTuple(VehicleSchemaV2Names.PCE, vt.getPcuEquivalents()));
202 this.
writeStartTag(VehicleSchemaV2Names.PASSENGERCAREQUIVALENTS, atts,
true);
206 if (vt.getNetworkMode() != null) {
208 atts.add(this.
createTuple(VehicleSchemaV2Names.NETWORKMODE, vt.getNetworkMode()));
209 this.
writeStartTag(VehicleSchemaV2Names.NETWORKMODE, atts,
true);
213 if (!Double.isNaN(vt.getFlowEfficiencyFactor())) {
215 atts.add(this.
createTuple(VehicleSchemaV2Names.FACTOR, vt.getFlowEfficiencyFactor()));
216 this.
writeStartTag(VehicleSchemaV2Names.FLOWEFFICIENCYFACTOR, atts,
true);
219 this.
writeEndTag(VehicleSchemaV2Names.VEHICLETYPE);
224 public void putAttributeConverters(Map<Class<?>, AttributeConverter<?>> converters) {
225 this.attributesWriter.putAttributeConverters(converters);
final void writeXmlHead()
final void writeContent(String content, boolean allowWhitespaces)
final void openFile(final String filename)
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()
final void writeEndTag(String tagname)