Eclipse 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-2019 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"
45 #include "MSRoutingEngine.h"
46 
47 
48 // ===========================================================================
49 // static member variables
50 // ===========================================================================
51 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
52 std::mt19937 MSDevice::myEquipmentRNG;
53 
54 // ===========================================================================
55 // debug flags
56 // ===========================================================================
57 //#define DEBUG_DEVICE_PARAMS
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
63 // ---------------------------------------------------------------------------
64 // static initialisation methods
65 // ---------------------------------------------------------------------------
66 void
79 
82 }
83 
84 
85 bool
87  bool ok = true;
89  return ok;
90 }
91 
92 
93 void
94 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
108 }
109 
110 
111 void
112 MSDevice::buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into) {
115 }
116 
117 
118 void
123 }
124 
125 void
126 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson) {
127  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
128  const std::string object = isPerson ? "person" : "vehicle";
129  oc.doRegister(prefix + ".probability", new Option_Float(-1.0));// (default: no need to call RNG)
130  oc.addDescription(prefix + ".probability", optionsTopic, "The probability for a " + object + " to have a '" + deviceName + "' device");
131 
132  oc.doRegister(prefix + ".explicit", new Option_String());
133  oc.addSynonyme(prefix + ".explicit", prefix + ".knownveh", true);
134  oc.addDescription(prefix + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named " + object + "s");
135 
136  oc.doRegister(prefix + ".deterministic", new Option_Bool(false));
137  oc.addDescription(prefix + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
138 }
139 
140 
141 void
143  WRITE_WARNING("Device '" + getID() + "' cannot save state");
144 }
145 
146 
147 void
149 }
150 
151 
152 std::string
153 MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required) {
154  std::string result = deflt;
155  if (v.getParameter().knowsParameter("device." + paramName)) {
156  try {
157  result = v.getParameter().getParameter("device." + paramName, "").c_str();
158  } catch (...) {
159  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
160  }
161  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
162  try {
163  result = v.getVehicleType().getParameter().getParameter("device." + paramName, "").c_str();
164  } catch (...) {
165  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
166  }
167  } else {
168  if (oc.isSet("device." + paramName)) {
169  result = oc.getString("device." + paramName);
170  } else {
171  if (required) {
172  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
173  } else {
174  result = deflt;
175 #ifdef DEBUG_DEVICE_PARAMS
176  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
177 #endif
178  }
179  }
180  }
181  return result;
182 }
183 
184 
185 double
186 MSDevice::getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required) {
187  double result = deflt;
188  if (v.getParameter().knowsParameter("device." + paramName)) {
189  try {
190  result = StringUtils::toDouble(v.getParameter().getParameter("device." + paramName, ""));
191  } catch (...) {
192  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
193  }
194  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
195  try {
196  result = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("device." + paramName, ""));
197  } catch (...) {
198  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
199  }
200  } else {
201  if (oc.isSet("device." + paramName)) {
202  result = oc.getFloat("device." + paramName);
203  } else {
204  if (required) {
205  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
206  } else {
207  result = deflt;
208 #ifdef DEBUG_DEVICE_PARAMS
209  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
210 #endif
211  }
212  }
213  }
214  return result;
215 }
216 
217 
218 bool
219 MSDevice::getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required) {
220  bool result = deflt;
221  if (v.getParameter().knowsParameter("device." + paramName)) {
222  try {
223  result = StringUtils::toBool(v.getParameter().getParameter("device." + paramName, ""));
224  } catch (...) {
225  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
226  }
227  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
228  try {
229  result = StringUtils::toBool(v.getVehicleType().getParameter().getParameter("device." + paramName, ""));
230  } catch (...) {
231  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
232  }
233  } else {
234  if (oc.isSet("device." + paramName)) {
235  result = oc.getBool("device." + paramName);
236  } else {
237  if (required) {
238  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
239  } else {
240  result = deflt;
241 #ifdef DEBUG_DEVICE_PARAMS
242  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
243 #endif
244  }
245  }
246  }
247  return result;
248 }
249 
250 /****************************************************************************/
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:219
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:94
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice *> &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:112
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:187
static void cleanup()
resets the edge filter
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_Routing-options.
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:77
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCD-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
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 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:61
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:142
static std::mt19937 myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:190
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:186
static void buildDevices(MSTransportable &p, std::vector< MSTransportableDevice *> &into)
Build devices for the given person, if needed.
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:126
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:119
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:86
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:148
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:153
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.
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_FCD-options.
A storage for options typed value containers)
Definition: OptionsCont.h:90
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:67
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.
static void buildDevices(MSTransportable &t, std::vector< MSTransportableDevice *> &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.