Eclipse 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-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 /****************************************************************************/
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>
34 #include <microsim/MSVehicleType.h>
36 #include <utils/common/Named.h>
40 
41 
42 // ===========================================================================
43 // class declarations
44 // ===========================================================================
45 class OutputDevice;
46 class SUMOVehicle;
47 class MSTransportable;
48 class SUMOSAXAttributes;
49 class MSVehicleDevice;
51 
52 
53 // ===========================================================================
54 // class definitions
55 // ===========================================================================
64 class MSDevice : public Named {
65 public:
69  static void insertOptions(OptionsCont& oc);
70 
74  static bool checkOptions(OptionsCont& oc);
75 
76 
82  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
83 
89  static void buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into);
90 
91  static std::mt19937* getEquipmentRNG() {
92  return &myEquipmentRNG;
93  }
94 
96  virtual const std::string deviceName() const = 0;
97 
99  static void cleanupAll();
100 
101 public:
106  MSDevice(const std::string& id) : Named(id) {
107  }
108 
109 
111  virtual ~MSDevice() { }
112 
113 
125  virtual void generateOutput() const {
126  }
127 
133  virtual void saveState(OutputDevice& out) const;
134 
135 
141  virtual void loadState(const SUMOSAXAttributes& attrs);
142 
144  virtual std::string getParameter(const std::string& key) const {
145  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
146  }
147 
149  virtual void setParameter(const std::string& key, const std::string& value) {
150  UNUSED_PARAMETER(value);
151  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
152  }
153 
154 protected:
157 
164  static void insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson = false);
165 
166 
173  template<class DEVICEHOLDER>
174  static bool equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson = false);
176 
177 
180  static std::string getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required);
181  static double getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required);
182  static bool getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required);
184 
185 private:
187  static std::map<std::string, std::set<std::string> > myExplicitIDs;
188 
190  static std::mt19937 myEquipmentRNG;
191 
192 
193 private:
195  MSDevice(const MSDevice&);
196 
198  MSDevice& operator=(const MSDevice&);
199 
200 };
201 
202 
203 template<class DEVICEHOLDER> bool
204 MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
205  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
206  // assignment by number
207  bool haveByNumber = false;
208  bool numberGiven = false;
209  if (oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic")) {
210  numberGiven = true;
211  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat(prefix + ".probability")) == 1;
212  } else {
213  if (oc.exists(prefix + ".probability") && oc.getFloat(prefix + ".probability") >= 0) {
214  numberGiven = true;
215  haveByNumber = RandHelper::rand(&myEquipmentRNG) <= oc.getFloat(prefix + ".probability");
216  }
217  }
218  // assignment by name
219  bool haveByName = false;
220  bool nameGiven = false;
221  if (oc.exists(prefix + ".explicit") && oc.isSet(prefix + ".explicit")) {
222  nameGiven = true;
223  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
224  myExplicitIDs[deviceName] = std::set<std::string>();
225  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
226  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
227  }
228  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
229  }
230  // assignment by abstract parameters
231  bool haveByParameter = false;
232  bool parameterGiven = false;
233  const std::string key = "has." + deviceName + ".device";
234  if (v.getParameter().knowsParameter(key)) {
235  parameterGiven = true;
236  haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
237  } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
238  parameterGiven = true;
239  haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
240  }
241  if (haveByName) {
242  return true;
243  } else if (parameterGiven) {
244  return haveByParameter;
245  } else if (numberGiven) {
246  return haveByNumber;
247  } else {
248  return !nameGiven && outputOptionSet;
249  }
250 }
251 
252 
253 #endif
254 
255 /****************************************************************************/
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:219
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:94
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:149
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice *> &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:112
virtual ~MSDevice()
Destructor.
Definition: MSDevice.h:111
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 double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:60
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:144
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:168
virtual void generateOutput() const
Called on writing tripinfo output.
Definition: MSDevice.h:125
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:91
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
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:61
Encapsulated SAX-Attributes.
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
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:337
Abstract in-person device.
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:186
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:126
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:119
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:57
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:86
Abstract in-vehicle / in-person device.
Definition: MSDevice.h:64
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:148
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:153
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:204
A storage for options typed value containers)
Definition: OptionsCont.h:90
Abstract in-vehicle device.
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
MSDevice & operator=(const MSDevice &)
Invalidated assignment operator.
MSDevice(const std::string &id)
Constructor.
Definition: MSDevice.h:106