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-2020 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 
88  static std::mt19937* getEquipmentRNG() {
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);
184 
185 private:
187  static std::map<std::string, std::set<std::string> > myExplicitIDs;
188 
190  static std::mt19937 myEquipmentRNG;
191 
192 
193 private:
196 
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  //std::cout << " deviceName=" << deviceName << " holder=" << v.getID()
242  // << " nameGiven=" << nameGiven << " haveByName=" << haveByName
243  // << " parameterGiven=" << parameterGiven << " haveByParameter=" << haveByParameter
244  // << " numberGiven=" << numberGiven << " haveByNumber=" << haveByNumber
245  // << " outputOptionSet=" << outputOptionSet << "\n";
246  if (haveByName) {
247  return true;
248  } else if (parameterGiven) {
249  return haveByParameter;
250  } else if (numberGiven) {
251  return haveByNumber;
252  } else {
253  return !nameGiven && outputOptionSet;
254  }
255 }
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:29
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:156
static std::mt19937 * getEquipmentRNG()
Definition: MSDevice.h:88
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:150
virtual void generateOutput(OutputDevice *) const
Called on vehicle deletion to extend tripinfo and other outputs.
Definition: MSDevice.h:123
static std::mt19937 myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:190
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:198
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required)
Definition: MSDevice.cpp:185
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:68
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:99
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:134
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
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:161
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:187
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:91
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:126
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:119
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:371
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:53
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:60
static double rand(std::mt19937 *rng=nullptr)
Returns a random real number in [0, 1)
Definition: RandHelper.h:51
Encapsulated SAX-Attributes.
Representation of a vehicle.
Definition: SUMOVehicle.h:58
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter