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.dev/sumo
3 // Copyright (C) 2007-2024 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/MSNet.h>
32 #include <microsim/MSVehicleType.h>
34 #include <utils/common/Named.h>
38 
39 
40 // ===========================================================================
41 // class declarations
42 // ===========================================================================
43 class OutputDevice;
44 class SUMOVehicle;
45 class MSTransportable;
46 class SUMOSAXAttributes;
47 class MSVehicleDevice;
49 
50 
51 // ===========================================================================
52 // class definitions
53 // ===========================================================================
62 class MSDevice : public Named {
63 public:
67  static void insertOptions(OptionsCont& oc);
68 
72  static bool checkOptions(OptionsCont& oc);
73 
74 
80  static void buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into);
81 
87  static void buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into);
88 
90  return &myEquipmentRNG;
91  }
92 
94  virtual const std::string deviceName() const = 0;
95 
97  static void cleanupAll();
98 
99 public:
104  MSDevice(const std::string& id) : Named(id) {
105  }
106 
107 
109  virtual ~MSDevice() { }
110 
111 
124  virtual void generateOutput(OutputDevice* /*tripinfoOut*/) const {
125  }
126 
132  virtual void saveState(OutputDevice& out) const;
133 
139  virtual void loadState(const SUMOSAXAttributes& attrs);
140 
142  virtual std::string getParameter(const std::string& key) const {
143  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
144  }
145 
147  virtual void setParameter(const std::string& key, const std::string& value) {
148  UNUSED_PARAMETER(value);
149  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
150  }
151 
152 protected:
155 
162  static void insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson = false);
163 
164 
171  template<class DEVICEHOLDER>
172  static bool equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson = false);
174 
175 
176 private:
178  static std::map<std::string, std::set<std::string> > myExplicitIDs;
179 
182 
183 
184 private:
187 
190 
191 };
192 
193 
194 template<class DEVICEHOLDER> bool
195 MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
196  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
197  // assignment by number
198  bool haveByNumber = false;
199  bool numberGiven = false;
200  if (oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic")) {
201  numberGiven = true;
202  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat(prefix + ".probability")) == 1;
203  } else {
204  if (oc.exists(prefix + ".probability") && oc.getFloat(prefix + ".probability") >= 0.) {
205  numberGiven = true;
206  haveByNumber = RandHelper::rand(&myEquipmentRNG) < oc.getFloat(prefix + ".probability");
207  }
208  }
209  // assignment by name
210  bool haveByName = false;
211  bool nameGiven = false;
212  if (oc.exists(prefix + ".explicit") && oc.isSet(prefix + ".explicit")) {
213  nameGiven = true;
214  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
215  myExplicitIDs[deviceName] = std::set<std::string>();
216  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
217  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
218  }
219  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
220  }
221  // assignment by abstract parameters
222  bool haveByParameter = false;
223  bool parameterGiven = false;
224  const std::string key = "has." + deviceName + ".device";
225  if (v.getParameter().hasParameter(key)) {
226  parameterGiven = true;
227  haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
228  } else if (v.getVehicleType().getParameter().hasParameter(key)) {
229  parameterGiven = true;
230  haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
231  } else if (v.getVehicleType().getParameter().hasParameter(prefix + ".probability")) {
232  // override global options
233  numberGiven = true;
234  haveByNumber = RandHelper::rand(&myEquipmentRNG) < StringUtils::toDouble(v.getVehicleType().getParameter().getParameter(prefix + ".probability", "0"));
235  }
236  //std::cout << " deviceName=" << deviceName << " holder=" << v.getID()
237  // << " nameGiven=" << nameGiven << " haveByName=" << haveByName
238  // << " parameterGiven=" << parameterGiven << " haveByParameter=" << haveByParameter
239  // << " numberGiven=" << numberGiven << " haveByNumber=" << haveByNumber
240  // << " outputOptionSet=" << outputOptionSet << "\n";
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 }
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
Abstract in-vehicle / in-person device.
Definition: MSDevice.h:62
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:177
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:171
virtual void generateOutput(OutputDevice *) const
Called on vehicle deletion to extend tripinfo and other outputs.
Definition: MSDevice.h:124
virtual ~MSDevice()
Destructor.
Definition: MSDevice.h:109
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:147
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:75
static SumoRNG myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:181
static SumoRNG * getEquipmentRNG()
Definition: MSDevice.h:89
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:112
MSDevice(const std::string &id)
Constructor.
Definition: MSDevice.h:104
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:155
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:195
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:142
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:178
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:104
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:147
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:137
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:184
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:378
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:60
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.cpp:94
Encapsulated SAX-Attributes.
Representation of a vehicle.
Definition: SUMOVehicle.h:62
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