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 
44 
45 // ===========================================================================
46 // static member variables
47 // ===========================================================================
48 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
49 std::mt19937 MSDevice::myEquipmentRNG;
50 
51 // ===========================================================================
52 // debug flags
53 // ===========================================================================
54 //#define DEBUG_DEVICE_PARAMS
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 // ---------------------------------------------------------------------------
61 // static initialisation methods
62 // ---------------------------------------------------------------------------
63 void
76 }
77 
78 
79 bool
81  bool ok = true;
83  return ok;
84 }
85 
86 
87 void
88 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
102 }
103 
104 void
109 }
110 
111 void
112 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc) {
113  oc.doRegister("device." + deviceName + ".probability", new Option_Float(-1.0));// (default: no need to call RNG)
114  oc.addDescription("device." + deviceName + ".probability", optionsTopic, "The probability for a vehicle to have a '" + deviceName + "' device");
115 
116  oc.doRegister("device." + deviceName + ".explicit", new Option_String());
117  oc.addSynonyme("device." + deviceName + ".explicit", "device." + deviceName + ".knownveh", true);
118  oc.addDescription("device." + deviceName + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named vehicles");
119 
120  oc.doRegister("device." + deviceName + ".deterministic", new Option_Bool(false));
121  oc.addDescription("device." + deviceName + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
122 }
123 
124 
125 bool
126 MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, SUMOVehicle& v, bool outputOptionSet) {
127  // assignment by number
128  bool haveByNumber = false;
129  bool numberGiven = false;
130  if (oc.exists("device." + deviceName + ".deterministic") && oc.getBool("device." + deviceName + ".deterministic")) {
131  numberGiven = true;
132  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat("device." + deviceName + ".probability")) == 1;
133  } else {
134  if (oc.exists("device." + deviceName + ".probability") && oc.getFloat("device." + deviceName + ".probability") >= 0) {
135  numberGiven = true;
136  haveByNumber = RandHelper::rand(&myEquipmentRNG) <= oc.getFloat("device." + deviceName + ".probability");
137  }
138  }
139  // assignment by name
140  bool haveByName = false;
141  bool nameGiven = false;
142  if (oc.exists("device." + deviceName + ".explicit") && oc.isSet("device." + deviceName + ".explicit")) {
143  nameGiven = true;
144  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
145  myExplicitIDs[deviceName] = std::set<std::string>();
146  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector("device." + deviceName + ".explicit");
147  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
148  }
149  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
150  }
151  // assignment by abstract parameters
152  bool haveByParameter = false;
153  bool parameterGiven = false;
154  const std::string key = "has." + deviceName + ".device";
155  if (v.getParameter().knowsParameter(key)) {
156  parameterGiven = true;
157  haveByParameter = TplConvert::_2bool(v.getParameter().getParameter(key, "false").c_str());
158  } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
159  parameterGiven = true;
160  haveByParameter = TplConvert::_2bool(v.getVehicleType().getParameter().getParameter(key, "false").c_str());
161  }
162  if (haveByName) {
163  return true;
164  } else if (parameterGiven) {
165  return haveByParameter;
166  } else if (numberGiven) {
167  return haveByNumber;
168  } else {
169  return !nameGiven && outputOptionSet;
170  }
171 }
172 
173 
174 bool
175 MSDevice::equippedByParameter(const MSTransportable* t, const std::string& deviceName, bool outputOptionSet) {
176  const std::string key = "has." + deviceName + ".device";
177  if (t->getParameter().knowsParameter(key)) {
178  return TplConvert::_2bool(t->getParameter().getParameter(key, "false").c_str());
179  } else if (t->getVehicleType().getParameter().knowsParameter(key)) {
180  return TplConvert::_2bool(t->getVehicleType().getParameter().getParameter(key, "false").c_str());
181  }
182  return outputOptionSet;
183 }
184 
185 
186 void
188  WRITE_WARNING("Device '" + getID() + "' cannot save state");
189 }
190 
191 
192 void
194 }
195 
196 
197 std::string
198 MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required) {
199  std::string result = deflt;
200  if (v.getParameter().knowsParameter("device." + paramName)) {
201  try {
202  result = v.getParameter().getParameter("device." + paramName, "").c_str();
203  } catch (...) {
204  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
205  }
206  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
207  try {
208  result = v.getVehicleType().getParameter().getParameter("device." + paramName, "").c_str();
209  } catch (...) {
210  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
211  }
212  } else {
213  if (oc.isSet("device." + paramName)) {
214  result = oc.getString("device." + paramName);
215  } else {
216  if (required) {
217  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
218  } else {
219  result = deflt;
220 #ifdef DEBUG_DEVICE_PARAMS
221  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
222 #endif
223  }
224  }
225  }
226  return result;
227 }
228 
229 
230 double
231 MSDevice::getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required) {
232  double result = deflt;
233  if (v.getParameter().knowsParameter("device." + paramName)) {
234  try {
235  result = TplConvert::_2double(v.getParameter().getParameter("device." + paramName, "").c_str());
236  } catch (...) {
237  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
238  }
239  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
240  try {
241  result = TplConvert::_2double(v.getVehicleType().getParameter().getParameter("device." + paramName, "").c_str());
242  } catch (...) {
243  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
244  }
245  } else {
246  if (oc.isSet("device." + paramName)) {
247  result = oc.getFloat("device." + paramName);
248  } else {
249  if (required) {
250  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
251  } else {
252  result = deflt;
253 #ifdef DEBUG_DEVICE_PARAMS
254  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
255 #endif
256  }
257  }
258  }
259  return result;
260 }
261 
262 
263 bool
264 MSDevice::getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required) {
265  bool result = deflt;
266  if (v.getParameter().knowsParameter("device." + paramName)) {
267  try {
268  result = TplConvert::_2bool(v.getParameter().getParameter("device." + paramName, "").c_str());
269  } catch (...) {
270  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
271  }
272  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
273  try {
274  result = TplConvert::_2bool(v.getVehicleType().getParameter().getParameter("device." + paramName, "").c_str());
275  } catch (...) {
276  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
277  }
278  } else {
279  if (oc.isSet("device." + paramName)) {
280  result = oc.getBool("device." + paramName);
281  } else {
282  if (required) {
283  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
284  } else {
285  result = deflt;
286 #ifdef DEBUG_DEVICE_PARAMS
287  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
288 #endif
289  }
290  }
291  }
292  return result;
293 }
294 
295 /****************************************************************************/
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:264
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< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:382
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &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 buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
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:199
static void cleanup()
resets the edge filter
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
int getQuota(double frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
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:69
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:242
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
static bool equippedByParameter(const MSTransportable *t, const std::string &deviceName, bool outputOptionSet)
Determines whether a transportable should get a certain device.
Definition: MSDevice.cpp:175
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:88
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 buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Routing-options.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
Encapsulated SAX-Attributes.
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:187
static std::mt19937 myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:202
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:309
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
bool exists(const std::string &name) const
Returns the information whether the named option is known.
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required)
Definition: MSDevice.cpp:231
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:112
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:105
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 buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
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:80
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:193
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:198
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
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:305
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v, bool outputOptionSet)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:126
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:64
static MSDevice_Vehroutes * buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into, int maxRoutes=std::numeric_limits< int >::max())
Build devices for the given vehicle, if needed.
const MSVehicleType & getVehicleType() const
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
static void cleanup()
deletes the router instance
static bool checkOptions(OptionsCont &oc)
checks MSDevice_Routing-options
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.