MATSIM
VehicleUtils.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: matsim
3  * VehicleUtils.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2011 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.vehicles;
22 
23 import java.util.HashMap;
24 import java.util.Map;
25 
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.matsim.api.core.v01.Id;
29 import org.matsim.api.core.v01.Scenario;
33 import org.matsim.core.gbl.Gbl;
35 
36 
41 public final class VehicleUtils {
42  private static final Logger log = LogManager.getLogger( VehicleUtils.class ) ;
43 
44  private static final String DEFAULT_VEHICLE_TYPE_ID = "defaultVehicleType";
45  private static final String VEHICLE_ATTRIBUTE_KEY = "vehicles";
46  private static final String VEHICLE_TYPES_ATTRIBUTE_KEY = "vehicleTypes";
47 
48  // should remain under the hood --> should remain private
49  private static final String DOOR_OPERATION_MODE = "doorOperationMode" ;
50  private static final String EGRESSTIME = "egressTimeInSecondsPerPerson";
51  private static final String ACCESSTIME = "accessTimeInSecondsPerPerson";
52  private static final String FUELCONSUMPTION = "fuelConsumptionLitersPerMeter";
53  private static final String ENERGYCONSUMPTION = "energyConsumptionKWhPerMeter";
54  private static final String ENERGYCAPACITY = "energyCapacityInKWhOrLiters";
55  private static final String HBEFA_VEHICLE_CATEGORY_= "HbefaVehicleCategory";
56  private static final String HBEFA_TECHNOLOGY = "HbefaTechnology";
57  private static final String HBEFA_SIZE_CLASS = "HbefaSizeClass";
58  private static final String HBEFA_EMISSIONS_CONCEPT = "HbefaEmissionsConcept";
59  private static final String COST_PER_SECOND_WAITING = "costsPerSecondWaiting";
60  private static final String COST_PER_SECOND_INSERVICE = "costsPerSecondInService";
61  private static final String FUEL_TYPE = "fuelType";
62  private static final String INITIAL_LINK_ID = "initialLinkId";
63 
65  return new VehicleType( typeId );
66  }
67 
68  public static VehicleType createVehicleType( Id<VehicleType> typeId, String networkMode){
69  return new VehicleType( typeId, networkMode );
70  }
71 
72  public static VehiclesFactory getFactory() {
73  return new VehiclesFactoryImpl();
74  }
75 
77  return new VehiclesImpl();
78  }
79 
81  VehicleType defaultVehicleType = VehicleUtils.getFactory()
82  .createVehicleType(Id.create(DEFAULT_VEHICLE_TYPE_ID, VehicleType.class));
83 
84  defaultVehicleType.getCapacity().setSeats(4);
85  defaultVehicleType.setNetworkMode(TransportMode.car);
86  return defaultVehicleType;
87  }
88 
99  public static Id<Vehicle> createVehicleId(Person person, String mode) {
100  return Id.createVehicleId(person.getId().toString() + "_" + mode);
101  }
102 
103  public static void copyFromTo( VehicleType in, VehicleType out ) {
105  out.setDescription(in.getDescription());
107  out.setLength(in.getLength());
108  out.setWidth(in.getLength());
110  out.setNetworkMode(in.getNetworkMode());
111  // (all the deprecated setters are copied via the attributes!)
113 
115  out.getCostInformation().setCostsPerSecond(cost.getCostsPerSecond()).setCostsPerMeter(cost.getCostsPerMeter()).setFixedCost(cost.getFixedCosts());
117 
118  VehicleCapacity cap = in.getCapacity();
119  out.getCapacity().setWeightInTons(cap.getWeightInTons()).setSeats(cap.getSeats()).setSeats(cap.getStandingRoom()).setVolumeInCubicMeters(cap.getVolumeInCubicMeters());
121 
123 
124  }
125 
136  public static boolean hasVehicleId(Person person, String mode) {
138  return personVehicles != null && personVehicles.getVehicle(mode) != null;
139  }
140 
148  public static Map<String, Id<Vehicle>> getVehicleIds(Person person) {
150  if (personVehicles == null) {
151  throw new RuntimeException("Could not retrieve vehicle id from person: " + person.getId().toString() +
152  ". \nIf you are not using config.qsim().getVehicleSource() with 'defaultVehicle' or 'modeVehicleTypesFromVehiclesData' you have to provide " +
153  "a vehicle for each mode for each person. Attach a PersonVehicles instance (containing a map of mode:String -> id:Id<Vehicle>) with key 'vehicles' as person attribute to each person." +
154  "\n VehicleUtils.insertVehicleIdIntoAttributes does this for you.");
155  }
156  return personVehicles.getModeVehicles();
157  }
158 
163  public static Map<String, Id<VehicleType>> getVehicleTypes(Person person) {
165  return personVehicles != null ? personVehicles.getModeVehicleTypes() : null;
166  }
167 
174  public static Id<Vehicle> getVehicleId(Person person, String mode) {
175  Map<String, Id<Vehicle>> vehicleIds = getVehicleIds(person);
176  if (!vehicleIds.containsKey(mode)) {
177  throw new RuntimeException("Could not retrieve vehicle id from person: " + person.getId().toString() + " for mode: " + mode +
178  ". \nIf you are not using config.qsim().getVehicleSource() with 'defaultVehicle' or 'modeVehicleTypesFromVehiclesData' you have to provide " +
179  "a vehicle for each mode for each person. Attach a PersonVehicles instance (containing a map of mode:String -> id:Id<Vehicle>) with key 'vehicles' as person attribute to each person." +
180  "\n VehicleUtils.insertVehicleIdIntoAttributes does this for you."
181  );
182  }
183  return vehicleIds.get(mode);
184  }
185 
196  @Deprecated
197  public static void insertVehicleIdsIntoAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle){
198  insertVehicleIdsIntoPersonAttributes( person, modeToVehicle );
199  }
208  public static void insertVehicleIdsIntoPersonAttributes(Person person, Map<String, Id<Vehicle>> modeToVehicle) {
209  Object attr = person.getAttributes().getAttribute(VEHICLE_ATTRIBUTE_KEY);
210  // copy in case it's a UnmodifiableMap
211  Map<String, Id<Vehicle>> modeToVehicleCopy = new HashMap<>(modeToVehicle);
212  PersonVehicles personVehicles;
213  if (attr == null) {
214  personVehicles = new PersonVehicles();
215  } else {
216  personVehicles = (PersonVehicles) attr;
217  }
218  personVehicles.addModeVehicleList(modeToVehicleCopy);
219  person.getAttributes().putAttribute(VEHICLE_ATTRIBUTE_KEY, personVehicles);
220  }
221 
227  @Deprecated
228  public static void insertVehicleTypesIntoAttributes(Person person, Map<String, Id<VehicleType>> modeToVehicleType) {
229  insertVehicleTypesIntoPersonAttributes( person, modeToVehicleType );
230  }
235  public static void insertVehicleTypesIntoPersonAttributes(Person person, Map<String, Id<VehicleType>> modeToVehicleType) {
236  Object attr = person.getAttributes().getAttribute(VEHICLE_TYPES_ATTRIBUTE_KEY);
237 
238  Map<String, Id<VehicleType>> modeToTypesCopy = new HashMap<>(modeToVehicleType);
239  PersonVehicleTypes personVehiclesTypes;
240  if (attr == null) {
241  personVehiclesTypes = new PersonVehicleTypes();
242  } else {
243  personVehiclesTypes = (PersonVehicleTypes) attr;
244  }
245  personVehiclesTypes.putModeVehicleTypes(modeToTypesCopy);
246  person.getAttributes().putAttribute(VEHICLE_TYPES_ATTRIBUTE_KEY, personVehiclesTypes);
247  }
248 
249  //******** general VehicleType attributes ************
250 
252  final Object attribute = vehicleType.getAttributes().getAttribute( DOOR_OPERATION_MODE );
253  if ( attribute==null ) {
254  return VehicleType.DoorOperationMode.serial; // this was the default value in V1; could also return null instead.
255  } else if (attribute instanceof VehicleType.DoorOperationMode ){
256  return (VehicleType.DoorOperationMode) attribute;
257  } else if (attribute instanceof String) {
258  String modeString = (String) attribute;
259  if ( VehicleType.DoorOperationMode.serial.toString().equalsIgnoreCase(modeString )) {
261  } else if ( VehicleType.DoorOperationMode.parallel.toString().equalsIgnoreCase(modeString )) {
263  } else {
264  throw new IllegalArgumentException("VehicleType " + vehicleType.getId().toString() + " : Door operation mode " + modeString + "is not supported");
265  }
266  }
267  else {
268  throw new RuntimeException("Type of " + attribute + "is not supported here");
269  }
270  }
271 
272  public static void setDoorOperationMode( VehicleType vehicleType, VehicleType.DoorOperationMode mode ){
273  vehicleType.getAttributes().putAttribute( DOOR_OPERATION_MODE, mode ) ;
274  }
275 
276  public static double getEgressTime(VehicleType vehicleType) {
277  final Object attribute = vehicleType.getAttributes().getAttribute( EGRESSTIME );
278  if ( attribute==null ) {
279  return 1.0 ; // this was the default value in V1; could also return Double-null instead.
280  } else {
281  return (double) attribute;
282  }
283  }
284 
285  public static void setEgressTime(VehicleType vehicleType, double egressTime) {
286  vehicleType.getAttributes().putAttribute(EGRESSTIME, egressTime);
287  }
288 
289  public static double getAccessTime(VehicleType vehicleType) {
290  final Object attribute = vehicleType.getAttributes().getAttribute( ACCESSTIME );
291  if ( attribute==null ) {
292  return 1.0 ; // this was the default value in V1; could also return Double-null instead.
293  } else {
294  return (double) attribute ;
295  }
296  }
297 
298  public static void setAccessTime(VehicleType vehicleType, double accessTime) {
299  vehicleType.getAttributes().putAttribute(ACCESSTIME, accessTime);
300  }
301 
305  @Deprecated
306  public static Double getFuelConsumption(VehicleType vehicleType) {
308  }
309 
313  @Deprecated
314  public static void setFuelConsumption(VehicleType vehicleType, double literPerMeter) {
315  setFuelConsumptionLitersPerMeter(vehicleType.getEngineInformation(), literPerMeter);
316  }
317 
318  //******** EngineInformation attributes ************
319  //TODO create enum for fuel type
320  public static String getHbefaTechnology( EngineInformation ei ){
321  return (String) ei.getAttributes().getAttribute( HBEFA_TECHNOLOGY ) ;
322  }
323  public static void setHbefaTechnology( EngineInformation engineInformation, String hbefaTechnology ) {
324  engineInformation.getAttributes().putAttribute( HBEFA_TECHNOLOGY, hbefaTechnology ) ;
325  }
326 
327  public static String getHbefaVehicleCategory( EngineInformation ei ){
328  return (String) ei.getAttributes().getAttribute( HBEFA_VEHICLE_CATEGORY_ ) ;
329  }
330  public static void setHbefaVehicleCategory( EngineInformation engineInformation, String hbefaVehicleCategory ) {
331  engineInformation.getAttributes().putAttribute( HBEFA_VEHICLE_CATEGORY_, hbefaVehicleCategory ) ;
332  }
333 
334  public static String getHbefaSizeClass( EngineInformation ei ) {
335  return (String) ei.getAttributes().getAttribute(HBEFA_SIZE_CLASS);
336  }
337  public static void setHbefaSizeClass( EngineInformation engineInformation, String hbefaSizeClass ) {
338  engineInformation.getAttributes().putAttribute( HBEFA_SIZE_CLASS, hbefaSizeClass ) ;
339  }
340 
341  public static String getHbefaEmissionsConcept( EngineInformation ei ) {
342  return (String) ei.getAttributes().getAttribute(HBEFA_EMISSIONS_CONCEPT);
343  }
344  public static void setHbefaEmissionsConcept( EngineInformation engineInformation, String emissionsConcept ) {
345  engineInformation.getAttributes().putAttribute( HBEFA_EMISSIONS_CONCEPT, emissionsConcept ) ;
346  }
347 
348  public static Double getEnergyConsumptionKWhPerMeter(EngineInformation engineInformation) {
349  return (Double) engineInformation.getAttributes().getAttribute(ENERGYCONSUMPTION);
350  }
351 
352  public static void setEnergyConsumptionKWhPerMeter(EngineInformation engineInformation, double energyConsumptionKWhPerMeter) {
353  engineInformation.getAttributes().putAttribute(ENERGYCONSUMPTION, energyConsumptionKWhPerMeter);
354  }
355 
356  public static Double getFuelConsumptionLitersPerMeter(EngineInformation engineInformation) {
357  return (Double) engineInformation.getAttributes().getAttribute(FUELCONSUMPTION);
358  }
359 
360  public static void setFuelConsumptionLitersPerMeter(EngineInformation engineInformation, double fuelConsumptionLitersPerMeter) {
361  engineInformation.getAttributes().putAttribute(FUELCONSUMPTION, fuelConsumptionLitersPerMeter);
362  }
363 
364  public static Double getEnergyCapacity(EngineInformation engineInformation) {
365  return (Double) engineInformation.getAttributes().getAttribute(ENERGYCAPACITY);
366  }
367 
368  public static void setEnergyCapacity(EngineInformation engineInformation, double energyCapacityInKWhOrLiters) {
369  engineInformation.getAttributes().putAttribute(ENERGYCAPACITY, energyCapacityInKWhOrLiters);
370  }
371  //******** CostInformation attributes ************
372 
373  public static Double getCostsPerSecondWaiting(CostInformation costInformation) {
374  return (Double) costInformation.getAttributes().getAttribute(COST_PER_SECOND_WAITING);
375  }
376 
377 
378  public static void setCostsPerSecondWaiting(CostInformation costInformation, double costsPerSecond) {
379  costInformation.getAttributes().putAttribute(COST_PER_SECOND_WAITING, costsPerSecond);
380  }
381 
382 
383  public static Double getCostsPerSecondInService(CostInformation costInformation) {
384  return (Double) costInformation.getAttributes().getAttribute(COST_PER_SECOND_INSERVICE);
385  }
386 
387 
388  public static void setCostsPerSecondInService(CostInformation costInformation, double costsPerSecond) {
389  costInformation.getAttributes().putAttribute(COST_PER_SECOND_INSERVICE, costsPerSecond);
390  }
391 
392  public static Vehicle createVehicle( Id<Vehicle> id , VehicleType type ){
393  return new VehicleImpl( id , type );
394  }
395 
399  @Deprecated
400  static EngineInformation.FuelType getFuelType(EngineInformation engineInformation ){
401  return (EngineInformation.FuelType) engineInformation.getAttributes().getAttribute( FUEL_TYPE );
402  }
403 
407  @Deprecated
408  static void setFuelType(EngineInformation engineInformation, EngineInformation.FuelType fuelType ){
409  engineInformation.getAttributes().putAttribute( FUEL_TYPE, fuelType);
410  }
411 
415  @Deprecated
416  static Double getFuelConsumption(EngineInformation engineInformation ){
417  return (Double) engineInformation.getAttributes().getAttribute( FUELCONSUMPTION );
418  }
419 
423  @Deprecated
424  static void setFuelConsumption(EngineInformation engineInformation, double fuelConsumption ){
425  engineInformation.getAttributes().putAttribute( FUELCONSUMPTION, fuelConsumption);
426  }
427 
428  public static Vehicles getOrCreateAllvehicles( Scenario scenario ) {
429  Vehicles map = (Vehicles) scenario.getScenarioElement( "allvehicles" );
430  if ( map==null ) {
431  log.info( "adding scenario element for allvehicles container" );
432  map = new VehiclesImpl();
433  scenario.addScenarioElement("allvehicles" , map);
434  }
435  return map;
436  }
437  private static int tryStdCnt = 5;
438  private static int tryTrnCnt = 5;
439  public static Vehicle findVehicle( Id<Vehicle> vehicleId, Scenario scenario) {
440  Vehicle vehicle = getOrCreateAllvehicles( scenario ).getVehicles().get( vehicleId );
441  if ( vehicle==null ) {
442  if ( tryStdCnt>0){
443  tryStdCnt--;
444  log.info("vehicleId={} not in allVehicles; trying standard vehicles container ...", vehicleId);
445  if ( tryStdCnt==0 ) {
446  log.info( Gbl.FUTURE_SUPPRESSED );
447  }
448  }
449  vehicle = scenario.getVehicles().getVehicles().get( vehicleId );
450  }
451  if ( vehicle==null ) {
452  if ( tryTrnCnt>0 ) {
453  tryTrnCnt--;
454  log.info("vehicleId={} not in allVehicles; trying transit vehicles container ...", vehicleId);
455  if ( tryTrnCnt==0 ) {
456  log.info( Gbl.FUTURE_SUPPRESSED );
457  }
458  }
459  vehicle = scenario.getTransitVehicles().getVehicles().get( vehicleId );
460  }
461  if ( vehicle==null ) {
462  log.info("unable to find vehicle for vehicleId={}; will return null", vehicleId);
463  }
464  return vehicle ;
465  }
466  public static void writeVehicles( Vehicles vehicles, String filename ) {
467  new MatsimVehicleWriter( vehicles ).writeFile( filename );
468 
469  }
470 
471  public static Id<Link> getInitialLinkId(Vehicle vehicle) {
472  String attribute = (String) vehicle.getAttributes().getAttribute(INITIAL_LINK_ID);
473  return attribute == null ? null : Id.createLinkId(attribute);
474  }
475 
476  public static void setInitialLinkId(Vehicle vehicle, Id<Link> initialLinkId) {
477  vehicle.getAttributes().putAttribute(INITIAL_LINK_ID, initialLinkId.toString());
478  }
479 }
static Double getEnergyCapacity(EngineInformation engineInformation)
static final String HBEFA_VEHICLE_CATEGORY_
final EngineInformation getEngineInformation()
static boolean hasVehicleId(Person person, String mode)
static String getHbefaTechnology(EngineInformation ei)
static void setDoorOperationMode(VehicleType vehicleType, VehicleType.DoorOperationMode mode)
final VehicleType setDescription(String desc)
static Id< Vehicle > createVehicleId(final long key)
Definition: Id.java:220
static Id< Link > createLinkId(final long key)
Definition: Id.java:202
static VehicleType createVehicleType(Id< VehicleType > typeId)
static final String HBEFA_EMISSIONS_CONCEPT
static final String VEHICLE_ATTRIBUTE_KEY
static Vehicle findVehicle(Id< Vehicle > vehicleId, Scenario scenario)
static final String VEHICLE_TYPES_ATTRIBUTE_KEY
static Id< Link > getInitialLinkId(Vehicle vehicle)
void addScenarioElement(String name, Object o)
static Double getCostsPerSecondWaiting(CostInformation costInformation)
static void setInitialLinkId(Vehicle vehicle, Id< Link > initialLinkId)
static void copyFromTo(VehicleType in, VehicleType out)
static Double getFuelConsumption(VehicleType vehicleType)
static final String HBEFA_TECHNOLOGY
static void setEgressTime(VehicleType vehicleType, double egressTime)
static< T extends Attributable > void copyAttributesFromTo(T from, T to)
static Vehicles getOrCreateAllvehicles(Scenario scenario)
static Map< String, Id< VehicleType > > getVehicleTypes(Person person)
static Double getEnergyConsumptionKWhPerMeter(EngineInformation engineInformation)
static Double getFuelConsumptionLitersPerMeter(EngineInformation engineInformation)
final double getFlowEfficiencyFactor()
static void setEnergyConsumptionKWhPerMeter(EngineInformation engineInformation, double energyConsumptionKWhPerMeter)
static void setHbefaSizeClass(EngineInformation engineInformation, String hbefaSizeClass)
final VehicleType setLength(double length)
final VehicleType setWidth(double width)
static final String FUTURE_SUPPRESSED
Definition: Gbl.java:44
Object getScenarioElement(String name)
static void setHbefaTechnology(EngineInformation engineInformation, String hbefaTechnology)
static double getAccessTime(VehicleType vehicleType)
static void setCostsPerSecondWaiting(CostInformation costInformation, double costsPerSecond)
CostInformation setCostsPerSecond(Double perSecond)
static void insertVehicleTypesIntoPersonAttributes(Person person, Map< String, Id< VehicleType >> modeToVehicleType)
static< T > Id< T > create(final long key, final Class< T > type)
Definition: Id.java:68
final VehicleCapacity getCapacity()
static Map< String, Id< Vehicle > > getVehicleIds(Person person)
static double getEgressTime(VehicleType vehicleType)
static Vehicle createVehicle(Id< Vehicle > id, VehicleType type)
static final String ENERGYCAPACITY
static final String HBEFA_SIZE_CLASS
VehicleType createVehicleType(Id< VehicleType > type)
static String getHbefaSizeClass(EngineInformation ei)
static Double getCostsPerSecondInService(CostInformation costInformation)
static void insertVehicleIdsIntoPersonAttributes(Person person, Map< String, Id< Vehicle >> modeToVehicle)
static Id< Vehicle > getVehicleId(Person person, String mode)
static final String EGRESSTIME
static void setCostsPerSecondInService(CostInformation costInformation, double costsPerSecond)
static void insertVehicleTypesIntoAttributes(Person person, Map< String, Id< VehicleType >> modeToVehicleType)
static void setFuelConsumption(VehicleType vehicleType, double literPerMeter)
Object putAttribute(final String attribute, final Object value)
static VehicleType createDefaultVehicleType()
final VehicleType setPcuEquivalents(double pcuEquivalents)
static void setAccessTime(VehicleType vehicleType, double accessTime)
VehicleCapacity setWeightInTons(double weightInTons)
final CostInformation getCostInformation()
static final String DEFAULT_VEHICLE_TYPE_ID
static String getHbefaEmissionsConcept(EngineInformation ei)
static void setHbefaEmissionsConcept(EngineInformation engineInformation, String emissionsConcept)
final Attributes getAttributes()
static final String COST_PER_SECOND_WAITING
final VehicleType setMaximumVelocity(double meterPerSecond)
final Id< VehicleType > getId()
static Id< Vehicle > createVehicleId(Person person, String mode)
static final String ENERGYCONSUMPTION
static void setHbefaVehicleCategory(EngineInformation engineInformation, String hbefaVehicleCategory)
static final String INITIAL_LINK_ID
static final String FUELCONSUMPTION
Map< Id< Vehicle >, Vehicle > getVehicles()
static final String ACCESSTIME
static void writeVehicles(Vehicles vehicles, String filename)
static final String DOOR_OPERATION_MODE
static final String COST_PER_SECOND_INSERVICE
static VehicleType.DoorOperationMode getDoorOperationMode(VehicleType vehicleType)
static void setFuelConsumptionLitersPerMeter(EngineInformation engineInformation, double fuelConsumptionLitersPerMeter)
VehicleCapacity setSeats(Integer seats)
final VehicleType setFlowEfficiencyFactor(double flowEfficiencyFactor)
static String getHbefaVehicleCategory(EngineInformation ei)
static void insertVehicleIdsIntoAttributes(Person person, Map< String, Id< Vehicle >> modeToVehicle)
static void setEnergyCapacity(EngineInformation engineInformation, double energyCapacityInKWhOrLiters)
final VehicleType setNetworkMode(String networkMode)
static VehiclesFactory getFactory()
static VehicleType createVehicleType(Id< VehicleType > typeId, String networkMode)
static Vehicles createVehiclesContainer()
Id< Vehicle > getVehicle(String mode)