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 /****************************************************************************/
16 // Abstract in-vehicle device
17 /****************************************************************************/
18 #ifndef MSDevice_h
19 #define MSDevice_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <vector>
29 #include <map>
30 #include <set>
31 #include <random>
33 #include <microsim/MSVehicleType.h>
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;
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 buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into);
89 
90  static std::mt19937* getEquipmentRNG() {
91  return &myEquipmentRNG;
92  }
93 
95  virtual const std::string deviceName() const = 0;
96 
98  static void cleanupAll();
99 
100 public:
105  MSDevice(const std::string& id) : Named(id) {
106  }
107 
108 
110  virtual ~MSDevice() { }
111 
112 
124  virtual void generateOutput() const {
125  }
126 
132  virtual void saveState(OutputDevice& out) const;
133 
134 
140  virtual void loadState(const SUMOSAXAttributes& attrs);
141 
143  virtual std::string getParameter(const std::string& key) const {
144  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
145  }
146 
148  virtual void setParameter(const std::string& key, const std::string& value) {
149  UNUSED_PARAMETER(value);
150  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
151  }
152 
153 protected:
156 
163  static void insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson = false);
164 
165 
172  template<class DEVICEHOLDER>
173  static bool equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson = false);
175 
176 
179  static std::string getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required);
180  static double getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required);
181  static bool getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required);
183 
184 private:
186  static std::map<std::string, std::set<std::string> > myExplicitIDs;
187 
189  static std::mt19937 myEquipmentRNG;
190 
191 
192 private:
195 
198 
199 };
200 
201 
202 template<class DEVICEHOLDER> bool
203 MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
204  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
205  // assignment by number
206  bool haveByNumber = false;
207  bool numberGiven = false;
208  if (oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic")) {
209  numberGiven = true;
210  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat(prefix + ".probability")) == 1;
211  } else {
212  if (oc.exists(prefix + ".probability") && oc.getFloat(prefix + ".probability") >= 0) {
213  numberGiven = true;
214  haveByNumber = RandHelper::rand(&myEquipmentRNG) <= oc.getFloat(prefix + ".probability");
215  }
216  }
217  // assignment by name
218  bool haveByName = false;
219  bool nameGiven = false;
220  if (oc.exists(prefix + ".explicit") && oc.isSet(prefix + ".explicit")) {
221  nameGiven = true;
222  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
223  myExplicitIDs[deviceName] = std::set<std::string>();
224  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
225  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
226  }
227  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
228  }
229  // assignment by abstract parameters
230  bool haveByParameter = false;
231  bool parameterGiven = false;
232  const std::string key = "has." + deviceName + ".device";
233  if (v.getParameter().knowsParameter(key)) {
234  parameterGiven = true;
235  haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
236  } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
237  parameterGiven = true;
238  haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
239  }
240  //std::cout << " deviceName=" << deviceName << " holder=" << v.getID()
241  // << " nameGiven=" << nameGiven << " haveByName=" << haveByName
242  // << " parameterGiven=" << parameterGiven << " haveByParameter=" << haveByParameter
243  // << " numberGiven=" << numberGiven << " haveByNumber=" << haveByNumber
244  // << " outputOptionSet=" << outputOptionSet << "\n";
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 /****************************************************************************/
MSDevice::setParameter
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:148
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:31
MSDevice::buildTransportableDevices
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:112
StringUtils::toBool
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
Definition: StringUtils.cpp:374
Named
Base class for objects which have an id.
Definition: Named.h:56
MSDevice::generateOutput
virtual void generateOutput() const
Called on writing tripinfo output.
Definition: MSDevice.h:124
MSDevice::MSDevice
MSDevice(const std::string &id)
Constructor.
Definition: MSDevice.h:105
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
OptionsCont.h
MSDevice::getEquipmentRNG
static std::mt19937 * getEquipmentRNG()
Definition: MSDevice.h:90
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
OptionsCont::exists
bool exists(const std::string &name) const
Returns the information whether the named option is known.
Definition: OptionsCont.cpp:129
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSDevice::myExplicitIDs
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:186
MSTransportable
Definition: MSTransportable.h:58
MSDevice::MSDevice
MSDevice(const MSDevice &)
Invalidated copy constructor.
MSVehicleType.h
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
MSDevice::saveState
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:142
MSDevice::getParameter
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:143
MSMoveReminder.h
RandHelper::rand
static double rand(std::mt19937 *rng=0)
Returns a random real number in [0, 1)
Definition: RandHelper.h:53
MSDevice::insertDefaultAssignmentOptions
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
MSDevice::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:94
Named.h
MSDevice::checkOptions
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:86
MSDevice
Abstract in-vehicle / in-person device.
Definition: MSDevice.h:63
MSDevice::cleanupAll
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:119
MSDevice::myEquipmentRNG
static std::mt19937 myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:189
UtilExceptions.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MSDevice::deviceName
virtual const std::string deviceName() const =0
return the name for this type of device
MSVehicleControl::getQuota
int getQuota(double frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
Definition: MSVehicleControl.cpp:437
MSDevice::getFloatParam
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required)
Definition: MSDevice.cpp:186
MSTransportableDevice
Abstract in-person device.
Definition: MSTransportableDevice.h:52
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
StringUtils.h
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSDevice::equippedByDefaultAssignmentOptions
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:203
InvalidArgument
Definition: UtilExceptions.h:56
MSDevice::getStringParam
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:153
MSDevice::operator=
MSDevice & operator=(const MSDevice &)
Invalidated assignment operator.
config.h
MSDevice::loadState
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:148
MSDevice::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:66
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
MSVehicleControl.h
MSNet::getVehicleControl
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:336
MSDevice::getBoolParam
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:219
MSDevice::~MSDevice
virtual ~MSDevice()
Destructor.
Definition: MSDevice.h:110
MSVehicleDevice
Abstract in-vehicle device.
Definition: MSVehicleDevice.h:54