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-2023 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>
33#include <utils/common/Named.h>
37
38
39// ===========================================================================
40// class declarations
41// ===========================================================================
42class OutputDevice;
43class SUMOVehicle;
44class MSTransportable;
46class MSVehicleDevice;
48
49
50// ===========================================================================
51// class definitions
52// ===========================================================================
61class MSDevice : public Named {
62public:
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
98public:
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
151protected:
154
161 static void insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson = false);
162
163
170 template<class DEVICEHOLDER>
171 static bool equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson = false);
173
174
177 static std::string getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required = false);
178 static double getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required = false);
179 static bool getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required = false);
180 static SUMOTime getTimeParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, SUMOTime deflt, bool required = false);
182
183private:
185 static std::map<std::string, std::set<std::string> > myExplicitIDs;
186
189
190
191private:
194
197
198};
199
200
201template<class DEVICEHOLDER> bool
202MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
203 const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
204 // assignment by number
205 bool haveByNumber = false;
206 bool numberGiven = false;
207 if (oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic")) {
208 numberGiven = true;
209 haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat(prefix + ".probability")) == 1;
210 } else {
211 if (oc.exists(prefix + ".probability") && oc.getFloat(prefix + ".probability") >= 0.) {
212 numberGiven = true;
213 haveByNumber = RandHelper::rand(&myEquipmentRNG) < oc.getFloat(prefix + ".probability");
214 }
215 }
216 // assignment by name
217 bool haveByName = false;
218 bool nameGiven = false;
219 if (oc.exists(prefix + ".explicit") && oc.isSet(prefix + ".explicit")) {
220 nameGiven = true;
221 if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
222 myExplicitIDs[deviceName] = std::set<std::string>();
223 const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
224 myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
225 }
226 haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
227 }
228 // assignment by abstract parameters
229 bool haveByParameter = false;
230 bool parameterGiven = false;
231 const std::string key = "has." + deviceName + ".device";
232 if (v.getParameter().knowsParameter(key)) {
233 parameterGiven = true;
234 haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
235 } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
236 parameterGiven = true;
237 haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
238 } else if (v.getVehicleType().getParameter().knowsParameter(prefix + ".probability")) {
239 // override global options
240 numberGiven = true;
241 haveByNumber = RandHelper::rand(&myEquipmentRNG) < StringUtils::toDouble(v.getVehicleType().getParameter().getParameter(prefix + ".probability", "0"));
242 }
243 //std::cout << " deviceName=" << deviceName << " holder=" << v.getID()
244 // << " nameGiven=" << nameGiven << " haveByName=" << haveByName
245 // << " parameterGiven=" << parameterGiven << " haveByParameter=" << haveByParameter
246 // << " numberGiven=" << numberGiven << " haveByNumber=" << haveByNumber
247 // << " outputOptionSet=" << outputOptionSet << "\n";
248 if (haveByName) {
249 return true;
250 } else if (parameterGiven) {
251 return haveByParameter;
252 } else if (numberGiven) {
253 return haveByNumber;
254 } else {
255 return !nameGiven && outputOptionSet;
256 }
257}
long long int SUMOTime
Definition: GUI.h:36
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:30
Abstract in-vehicle / in-person device.
Definition: MSDevice.h:61
static SumoRNG * getEquipmentRNG()
Definition: MSDevice.h:88
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:170
virtual const std::string deviceName() const =0
return the name for this type of device
static SUMOTime getTimeParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, SUMOTime deflt, bool required=false)
Definition: MSDevice.cpp:225
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required=false)
Definition: MSDevice.cpp:212
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:164
virtual void generateOutput(OutputDevice *) const
Called on vehicle deletion to extend tripinfo and other outputs.
Definition: MSDevice.h:123
virtual ~MSDevice()
Destructor.
Definition: MSDevice.h:108
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required=false)
Definition: MSDevice.cpp:199
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 void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:71
static SumoRNG myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:188
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:107
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:148
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:202
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:185
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:99
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:140
MSDevice & operator=(const MSDevice &)
Invalidated assignment operator.
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required=false)
Definition: MSDevice.cpp:175
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:131
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:380
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