Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
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-2025 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>
34#include <utils/common/Named.h>
38
39
40// ===========================================================================
41// class declarations
42// ===========================================================================
43class OutputDevice;
44class SUMOVehicle;
45class MSTransportable;
47class MSVehicleDevice;
49
50
51// ===========================================================================
52// class definitions
53// ===========================================================================
62class MSDevice : public Named {
63public:
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 static std::string getDeviceName(const std::string& id);
91
93 return &myEquipmentRNG;
94 }
95
97 virtual const std::string deviceName() const = 0;
98
100 static void cleanupAll();
101
102 static const std::string LOADSTATE_DEVICENAMES;
103
104public:
109 MSDevice(const std::string& id) : Named(id) {
110 }
111
112
114 virtual ~MSDevice() { }
115
116
129 virtual void generateOutput(OutputDevice* /*tripinfoOut*/) const {
130 }
131
137 virtual void saveState(OutputDevice& out) const;
138
144 virtual void loadState(const SUMOSAXAttributes& attrs);
145
147 virtual std::string getParameter(const std::string& key) const {
148 throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
149 }
150
152 virtual void setParameter(const std::string& key, const std::string& value) {
153 UNUSED_PARAMETER(value);
154 throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
155 }
156
157protected:
160
167 static void insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson = false);
168
169
176 template<class DEVICEHOLDER>
177 static bool equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson = false);
179
180
181private:
183 static std::map<std::string, std::set<std::string> > myExplicitIDs;
184
187
188
189private:
192
195
196};
197
198
199template<class DEVICEHOLDER> bool
200MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, DEVICEHOLDER& v, bool outputOptionSet, const bool isPerson) {
201 const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
202 // assignment by number
203 bool numberGiven = ((oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic"))
204 || (oc.exists(prefix + ".probability") && oc.getFloat(prefix + ".probability") >= 0.));
205 double probability = numberGiven ? oc.getFloat(prefix + ".probability") : 0;
206 // assignment by name
207 bool haveByName = false;
208 bool nameGiven = false;
209 if (oc.exists(prefix + ".explicit") && oc.isSet(prefix + ".explicit")) {
210 nameGiven = true;
211 if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
212 myExplicitIDs[deviceName] = std::set<std::string>();
213 const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector(prefix + ".explicit");
214 myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
215 }
216 haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
217 }
218 // assignment by abstract parameters
219 bool haveByParameter = false;
220 bool parameterGiven = false;
221 const std::string key = "has." + deviceName + ".device";
222 if (v.getParameter().hasParameter(key)) {
223 parameterGiven = true;
224 haveByParameter = StringUtils::toBool(v.getParameter().getParameter(key, "false"));
225 } else if (v.getVehicleType().getParameter().hasParameter(key)) {
226 parameterGiven = true;
227 haveByParameter = StringUtils::toBool(v.getVehicleType().getParameter().getParameter(key, "false"));
228 } else if (v.getVehicleType().getParameter().hasParameter(prefix + ".probability")) {
229 // override global options
230 numberGiven = true;
231 probability = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter(prefix + ".probability", "0"));
232 }
233 //std::cout << " deviceName=" << deviceName << " holder=" << v.getID()
234 // << " nameGiven=" << nameGiven << " haveByName=" << haveByName
235 // << " parameterGiven=" << parameterGiven << " haveByParameter=" << haveByParameter
236 // << " numberGiven=" << numberGiven << " haveByNumber=" << haveByNumber
237 // << " outputOptionSet=" << outputOptionSet << "\n";
238 if (haveByName) {
239 return true;
240 } else if (parameterGiven) {
241 return haveByParameter;
242 } else if (numberGiven) {
243 if (oc.exists(prefix + ".deterministic") && oc.getBool(prefix + ".deterministic")) {
244 return MSNet::getInstance()->getVehicleControl().getQuota(probability) == 1;
245 } else if (probability > 0) {
246 if (v.getParameter().hasParameter(LOADSTATE_DEVICENAMES)) {
247 // replicate probabilistic assignment
248 const std::vector<std::string> lsdn = StringTokenizer(v.getParameter().getParameter(LOADSTATE_DEVICENAMES)).getVector();
249 return std::find(lsdn.begin(), lsdn.end(), deviceName) != lsdn.end();
250 } else {
251 return RandHelper::rand(&myEquipmentRNG) < probability;
252 }
253 } else {
254 return false;
255 }
256 } else {
257 return !nameGiven && outputOptionSet;
258 }
259}
Abstract in-vehicle / in-person device.
Definition MSDevice.h:62
static const std::string LOADSTATE_DEVICENAMES
Definition MSDevice.h:102
static SumoRNG * getEquipmentRNG()
Definition MSDevice.h:92
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition MSDevice.cpp:193
virtual const std::string deviceName() const =0
return the name for this type of device
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition MSDevice.cpp:187
virtual void generateOutput(OutputDevice *) const
Called on vehicle deletion to extend tripinfo and other outputs.
Definition MSDevice.h:129
virtual ~MSDevice()
Destructor.
Definition MSDevice.h:114
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:152
static std::string getDeviceName(const std::string &id)
extracts the deviceName from the id (which includes holder id) and is subject to special cases
Definition MSDevice.cpp:173
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition MSDevice.cpp:77
static SumoRNG myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition MSDevice.h:186
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition MSDevice.cpp:114
MSDevice(const std::string &id)
Constructor.
Definition MSDevice.h:109
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:157
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:200
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:147
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition MSDevice.h:183
static void cleanupAll()
perform cleanup for all devices
Definition MSDevice.cpp:149
MSDevice & operator=(const MSDevice &)
Invalidated assignment operator.
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition MSDevice.cpp:139
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition MSNet.cpp:186
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition MSNet.h:392
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.
Static storage of an output device and its base (abstract) implementation.
static double rand(SumoRNG *rng=nullptr)
Returns a random real number in [0, 1)
Encapsulated SAX-Attributes.
Representation of a vehicle.
Definition SUMOVehicle.h:62
std::vector< std::string > getVector()
return vector of strings
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
#define UNUSED_PARAMETER(x)
bool checkOptions()