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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // Abstract in-vehicle device
21 /****************************************************************************/
22 #pragma once
23 #include <config.h>
24 
25 #include <string>
26 #include <vector>
27 #include <map>
28 #include <set>
29 #include <random>
31 #include <microsim/MSVehicleType.h>
33 #include <utils/common/Named.h>
37 
38 
39 // ===========================================================================
40 // class declarations
41 // ===========================================================================
42 class OutputDevice;
43 class SUMOVehicle;
44 class MSTransportable;
45 class SUMOSAXAttributes;
46 class MSVehicleDevice;
48 
49 
50 // ===========================================================================
51 // class definitions
52 // ===========================================================================
61 class MSDevice : public Named {
62 public:
66  static void insertOptions(OptionsCont& oc);
67 
71  static bool checkOptions(OptionsCont& oc);
72 
73 
79  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
80 
86  static void buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into);
87 
89  return &myEquipmentRNG;
90  }
91 
93  virtual const std::string deviceName() const = 0;
94 
96  static void cleanupAll();
97 
98 public:
103  MSDevice(const std::string& id) : Named(id) {
104  }
105 
106 
108  virtual ~MSDevice() { }
109 
110 
123  virtual void generateOutput(OutputDevice* /*tripinfoOut*/) const {
124  }
125 
131  virtual void saveState(OutputDevice& out) const;
132 
138  virtual void loadState(const SUMOSAXAttributes& attrs);
139 
141  virtual std::string getParameter(const std::string& key) const {
142  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
143  }
144 
146  virtual void setParameter(const std::string& key, const std::string& value) {
147  UNUSED_PARAMETER(value);
148  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
149  }
150 
152  virtual void notifyParking() {}
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);
183  static SUMOTime getTimeParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, SUMOTime deflt, bool required);
185 
186 private:
188  static std::map<std::string, std::set<std::string> > myExplicitIDs;
189 
192 
193 
194 private:
197 
200 
201 };
202 
203 
204 template<class DEVICEHOLDER> bool
205 MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
206  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
207  // assignment by number
208  bool haveByNumber = false;
209  bool numberGiven = false;
210  if (oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic")) {
211  numberGiven = true;
212  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat(prefix + ".probability")) == 1;
213  } else {
214  if (oc.exists(prefix + ".probability") && oc.getFloat(prefix + ".probability") >= 0.) {
215  numberGiven = true;
216  haveByNumber = RandHelper::rand(&myEquipmentRNG) < oc.getFloat(prefix + ".probability");
217  }
218  }
219  // assignment by name
220  bool haveByName = false;
221  bool nameGiven = false;
222  if (oc.exists(prefix + ".explicit") && oc.isSet(prefix + ".explicit")) {
223  nameGiven = true;
224  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
225  myExplicitIDs[deviceName] = std::set<std::string>();
226  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
227  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
228  }
229  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
230  }
231  // assignment by abstract parameters
232  bool haveByParameter = false;
233  bool parameterGiven = false;
234  const std::string key = "has." + deviceName + ".device";
235  if (v.getParameter().knowsParameter(key)) {
236  parameterGiven = true;
237  haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
238  } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
239  parameterGiven = true;
240  haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
241  } else if (v.getVehicleType().getParameter().knowsParameter(prefix + ".probability")) {
242  // override global options
243  numberGiven = true;
244  haveByNumber = RandHelper::rand(&myEquipmentRNG) < StringUtils::toDouble(v.getVehicleType().getParameter().getParameter(prefix + ".probability", "0"));
245  }
246  //std::cout << " deviceName=" << deviceName << " holder=" << v.getID()
247  // << " nameGiven=" << nameGiven << " haveByName=" << haveByName
248  // << " parameterGiven=" << parameterGiven << " haveByParameter=" << haveByParameter
249  // << " numberGiven=" << numberGiven << " haveByNumber=" << haveByNumber
250  // << " outputOptionSet=" << outputOptionSet << "\n";
251  if (haveByName) {
252  return true;
253  } else if (parameterGiven) {
254  return haveByParameter;
255  } else if (numberGiven) {
256  return haveByNumber;
257  } else {
258  return !nameGiven && outputOptionSet;
259  }
260 }
long long int SUMOTime
Definition: SUMOTime.h:32
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
Abstract in-vehicle / in-person device.
Definition: MSDevice.h:61
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:159
virtual const std::string deviceName() const =0
return the name for this type of device
MSDevice & operator=(const MSDevice &)
Invalidated assignment operator.
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:153
virtual void generateOutput(OutputDevice *) const
Called on vehicle deletion to extend tripinfo and other outputs.
Definition: MSDevice.h:123
static SUMOTime getTimeParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, SUMOTime deflt, bool required)
Definition: MSDevice.cpp:214
virtual ~MSDevice()
Destructor.
Definition: MSDevice.h:108
MSDevice(const MSDevice &)
Invalidated copy constructor.
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:146
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:201
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required)
Definition: MSDevice.cpp:188
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:69
static SumoRNG myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:191
static SumoRNG * getEquipmentRNG()
Definition: MSDevice.h:88
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:101
MSDevice(const std::string &id)
Constructor.
Definition: MSDevice.h:103
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:137
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:205
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:164
virtual void notifyParking()
called to update state for parking vehicles
Definition: MSDevice.h:152
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:141
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:188
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:93
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:129
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:122
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:174
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:376
Abstract in-person device.
int getQuota(double frac=-1, int loaded=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
Abstract in-vehicle device.
Base class for objects which have an id.
Definition: Named.h:54
A storage for options typed value containers)
Definition: OptionsCont.h:89
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.h:119
Encapsulated SAX-Attributes.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter