SUMO - Simulation of Urban MObility
MSDevice.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2007-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 /****************************************************************************/
17 // Abstract in-vehicle device
18 /****************************************************************************/
19 #ifndef MSDevice_h
20 #define MSDevice_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <vector>
30 #include <map>
31 #include <set>
32 #include <random>
35 #include <utils/common/Named.h>
39 
40 
41 // ===========================================================================
42 // class declarations
43 // ===========================================================================
44 class OutputDevice;
45 class SUMOVehicle;
46 class MSTransportable;
47 class SUMOSAXAttributes;
48 class MSVehicleDevice;
49 class MSPersonDevice;
50 
51 
52 // ===========================================================================
53 // class definitions
54 // ===========================================================================
63 class MSDevice : public Named {
64 public:
68  static void insertOptions(OptionsCont& oc);
69 
73  static bool checkOptions(OptionsCont& oc);
74 
75 
81  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
82 
88  static void buildPersonDevices(MSTransportable& p, std::vector<MSPersonDevice*>& into);
89 
90  static std::mt19937* getEquipmentRNG() {
91  return &myEquipmentRNG;
92  }
93 
95  virtual const std::string deviceName() const = 0;
96 
99  static bool equippedByParameter(const MSTransportable* t, const std::string& deviceName, bool outputOptionSet);
100 
102  static void cleanupAll();
103 
104 public:
110  MSDevice(const std::string& id) : Named(id) {
111  }
112 
113 
115  virtual ~MSDevice() { }
116 
117 
129  virtual void generateOutput() const {
130  }
131 
137  virtual void saveState(OutputDevice& out) const;
138 
139 
145  virtual void loadState(const SUMOSAXAttributes& attrs);
146 
148  virtual std::string getParameter(const std::string& key) const {
149  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
150  }
151 
153  virtual void setParameter(const std::string& key, const std::string& value) {
154  UNUSED_PARAMETER(value);
155  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
156  }
157 
158 protected:
161 
168  static void insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson=false);
169 
170 
177  template<class DEVICEHOLDER>
178  static bool equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson = false);
180 
181 
184  static std::string getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required);
185  static double getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required);
186  static bool getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required);
188 
189 private:
191  static std::map<std::string, std::set<std::string> > myExplicitIDs;
192 
194  static std::mt19937 myEquipmentRNG;
195 
196 
197 private:
199  MSDevice(const MSDevice&);
200 
202  MSDevice& operator=(const MSDevice&);
203 
204 };
205 
206 
207 template<class DEVICEHOLDER> bool
208 MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
209  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
210  // assignment by number
211  bool haveByNumber = false;
212  bool numberGiven = false;
213  if (oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic")) {
214  numberGiven = true;
215  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat(prefix + ".probability")) == 1;
216  } else {
217  if (oc.exists(prefix + ".probability") && oc.getFloat(prefix + ".probability") >= 0) {
218  numberGiven = true;
219  haveByNumber = RandHelper::rand(&myEquipmentRNG) <= oc.getFloat(prefix + ".probability");
220  }
221  }
222  // assignment by name
223  bool haveByName = false;
224  bool nameGiven = false;
225  if (oc.exists(prefix + ".explicit") && oc.isSet(prefix + ".explicit")) {
226  nameGiven = true;
227  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
228  myExplicitIDs[deviceName] = std::set<std::string>();
229  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
230  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
231  }
232  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
233  }
234  // assignment by abstract parameters
235  bool haveByParameter = false;
236  bool parameterGiven = false;
237  const std::string key = "has." + deviceName + ".device";
238  if (v.getParameter().knowsParameter(key)) {
239  parameterGiven = true;
240  haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
241  } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
242  parameterGiven = true;
243  haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
244  }
245  if (haveByName) {
246  return true;
247  } else if (parameterGiven) {
248  return haveByParameter;
249  } else if (numberGiven) {
250  return haveByNumber;
251  } else {
252  return !nameGiven && outputOptionSet;
253  }
254 }
255 
256 
257 #endif
258 
259 /****************************************************************************/
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:226
Abstract in-person device.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:90
virtual void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key ...
Definition: MSDevice.h:153
virtual ~MSDevice()
Destructor.
Definition: MSDevice.h:115
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 double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
virtual std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...
Definition: MSDevice.h:148
int getQuota(double frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
virtual void generateOutput() const
Called on writing tripinfo output.
Definition: MSDevice.h:129
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static std::mt19937 * getEquipmentRNG()
Definition: MSDevice.h:90
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:33
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:137
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter ...
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.
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: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
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:316
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:193
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, 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
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Base class for objects which have an id.
Definition: Named.h:58
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:82
Abstract in-vehicle / in-person device.
Definition: MSDevice.h:63
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:155
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:160
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, DEVICEHOLDER &v, bool outputOptionSet, const bool isPerson=false)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.h:208
A storage for options typed value containers)
Definition: OptionsCont.h:92
static void buildPersonDevices(MSTransportable &p, std::vector< MSPersonDevice *> &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:108
Abstract in-vehicle device.
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:66
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
MSDevice & operator=(const MSDevice &)
Invalidated assignment operator.
MSDevice(const std::string &id)
Constructor.
Definition: MSDevice.h:110