SUMO - Simulation of Urban MObility
MSDevice.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2013-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Abstract in-vehicle device
17 /****************************************************************************/
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
26 #include <microsim/MSVehicle.h>
29 #include "MSDevice.h"
30 #include "MSDevice_Vehroutes.h"
31 #include "MSDevice_Tripinfo.h"
32 #include "MSDevice_Routing.h"
33 #include "MSDevice_Emissions.h"
34 #include "MSDevice_BTreceiver.h"
35 #include "MSDevice_BTsender.h"
36 #include "MSDevice_Example.h"
37 #include "MSDevice_Battery.h"
38 #include "MSDevice_SSM.h"
39 #include "MSDevice_ToC.h"
40 #include "MSDevice_DriverState.h"
41 #include "MSDevice_Bluelight.h"
42 #include "MSDevice_FCD.h"
43 #include "MSPersonDevice_Routing.h"
44 #include "MSRoutingEngine.h"
45 
46 
47 // ===========================================================================
48 // static member variables
49 // ===========================================================================
50 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
51 std::mt19937 MSDevice::myEquipmentRNG;
52 
53 // ===========================================================================
54 // debug flags
55 // ===========================================================================
56 //#define DEBUG_DEVICE_PARAMS
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 // ---------------------------------------------------------------------------
63 // static initialisation methods
64 // ---------------------------------------------------------------------------
65 void
78 }
79 
80 
81 bool
83  bool ok = true;
85  return ok;
86 }
87 
88 
89 void
90 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
104 }
105 
106 
107 void
108 MSDevice::buildPersonDevices(MSTransportable& p, std::vector<MSPersonDevice*>& into) {
110 }
111 
112 
113 void
118 }
119 
120 void
121 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson) {
122  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
123  const std::string object = isPerson ? "person" : "vehicle";
124  oc.doRegister(prefix + ".probability", new Option_Float(-1.0));// (default: no need to call RNG)
125  oc.addDescription(prefix + ".probability", optionsTopic, "The probability for a " + object + " to have a '" + deviceName + "' device");
126 
127  oc.doRegister(prefix + ".explicit", new Option_String());
128  oc.addSynonyme(prefix + ".explicit", prefix + ".knownveh", true);
129  oc.addDescription(prefix + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named " + object + "s");
130 
131  oc.doRegister(prefix + ".deterministic", new Option_Bool(false));
132  oc.addDescription(prefix + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
133 }
134 
135 
136 bool
137 MSDevice::equippedByParameter(const MSTransportable* t, const std::string& deviceName, bool outputOptionSet) {
138  const std::string key = "has." + deviceName + ".device";
139  if (t->getParameter().knowsParameter(key)) {
140  return StringUtils::toBool(t->getParameter().getParameter(key, "false"));
141  } else if (t->getVehicleType().getParameter().knowsParameter(key)) {
142  return StringUtils::toBool(t->getVehicleType().getParameter().getParameter(key, "false"));
143  }
144  return outputOptionSet;
145 }
146 
147 
148 void
150  WRITE_WARNING("Device '" + getID() + "' cannot save state");
151 }
152 
153 
154 void
156 }
157 
158 
159 std::string
160 MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required) {
161  std::string result = deflt;
162  if (v.getParameter().knowsParameter("device." + paramName)) {
163  try {
164  result = v.getParameter().getParameter("device." + paramName, "").c_str();
165  } catch (...) {
166  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
167  }
168  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
169  try {
170  result = v.getVehicleType().getParameter().getParameter("device." + paramName, "").c_str();
171  } catch (...) {
172  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
173  }
174  } else {
175  if (oc.isSet("device." + paramName)) {
176  result = oc.getString("device." + paramName);
177  } else {
178  if (required) {
179  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
180  } else {
181  result = deflt;
182 #ifdef DEBUG_DEVICE_PARAMS
183  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
184 #endif
185  }
186  }
187  }
188  return result;
189 }
190 
191 
192 double
193 MSDevice::getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required) {
194  double result = deflt;
195  if (v.getParameter().knowsParameter("device." + paramName)) {
196  try {
197  result = StringUtils::toDouble(v.getParameter().getParameter("device." + paramName, ""));
198  } catch (...) {
199  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
200  }
201  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
202  try {
203  result = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("device." + paramName, ""));
204  } catch (...) {
205  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
206  }
207  } else {
208  if (oc.isSet("device." + paramName)) {
209  result = oc.getFloat("device." + paramName);
210  } else {
211  if (required) {
212  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
213  } else {
214  result = deflt;
215 #ifdef DEBUG_DEVICE_PARAMS
216  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
217 #endif
218  }
219  }
220  }
221  return result;
222 }
223 
224 
225 bool
226 MSDevice::getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required) {
227  bool result = deflt;
228  if (v.getParameter().knowsParameter("device." + paramName)) {
229  try {
230  result = StringUtils::toBool(v.getParameter().getParameter("device." + paramName, ""));
231  } catch (...) {
232  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
233  }
234  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
235  try {
236  result = StringUtils::toBool(v.getVehicleType().getParameter().getParameter("device." + paramName, ""));
237  } catch (...) {
238  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
239  }
240  } else {
241  if (oc.isSet("device." + paramName)) {
242  result = oc.getBool("device." + paramName);
243  } else {
244  if (required) {
245  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
246  } else {
247  result = deflt;
248 #ifdef DEBUG_DEVICE_PARAMS
249  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
250 #endif
251  }
252  }
253  }
254  return result;
255 }
256 
257 /****************************************************************************/
static void buildDevices(MSTransportable &p, std::vector< MSPersonDevice *> &into)
Build devices for the given person, if needed.
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:226
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:90
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_DriverState-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_SSM-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_ToC-options.
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:191
static void cleanup()
resets the edge filter
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getID() const
Returns the id.
Definition: Named.h:78
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCD-options.
const SUMOVehicleParameter & getParameter() const
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static MSDevice_Vehroutes * buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into, int maxRoutes=std::numeric_limits< int >::max())
Build devices for the given vehicle, if needed.
static bool equippedByParameter(const MSTransportable *t, const std::string &deviceName, bool outputOptionSet)
Determines whether a transportable should get a certain device.
Definition: MSDevice.cpp:137
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter ...
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:96
virtual const std::string deviceName() const =0
return the name for this type of device
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Routing-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
Encapsulated SAX-Attributes.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:149
static std::mt19937 myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:194
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required)
Definition: MSDevice.cpp:193
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc, const bool isPerson=false)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:121
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:114
const SUMOVTypeParameter & getParameter() const
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
static void cleanup()
resets counters
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:82
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Bluelight-options.
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:155
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:160
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
static void insertOptions()
Inserts MSDevice_Emissions-options.
A storage for options typed value containers)
Definition: OptionsCont.h:92
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static void buildPersonDevices(MSTransportable &p, std::vector< MSPersonDevice *> &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:108
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:66
const MSVehicleType & getVehicleType() const
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
static bool checkOptions(OptionsCont &oc)
checks MSDevice_Routing-options
static void cleanup()
deletes the router instance
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.