Eclipse SUMO - Simulation of Urban MObility
MSDevice.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3// Copyright (C) 2013-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/****************************************************************************/
19// Abstract in-vehicle device
20/****************************************************************************/
21#include <config.h>
22
25#include <microsim/MSVehicle.h>
28#include "MSDevice.h"
29#include "MSDevice_Vehroutes.h"
30#include "MSDevice_Tripinfo.h"
31#include "MSDevice_Routing.h"
32#include "MSDevice_Emissions.h"
33#include "MSDevice_BTreceiver.h"
34#include "MSDevice_BTsender.h"
35#include "MSDevice_Example.h"
36#include "MSDevice_Battery.h"
37#include "MSDevice_SSM.h"
38#include "MSDevice_ToC.h"
40#include "MSDevice_Bluelight.h"
41#include "MSDevice_FCD.h"
42#include "MSDevice_Taxi.h"
43#include "MSDevice_GLOSA.h"
44#include "MSDevice_ElecHybrid.h"
47#include "MSRoutingEngine.h"
48#include "MSDevice_Friction.h"
49
50
51// ===========================================================================
52// static member variables
53// ===========================================================================
54std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
55SumoRNG MSDevice::myEquipmentRNG("deviceEquipment");
56
57// ===========================================================================
58// debug flags
59// ===========================================================================
60//#define DEBUG_DEVICE_PARAMS
61
62
63// ===========================================================================
64// method definitions
65// ===========================================================================
66// ---------------------------------------------------------------------------
67// static initialisation methods
68// ---------------------------------------------------------------------------
69void
88
93}
94
95
96bool
98 bool ok = true;
100 return ok;
101}
102
103
104void
105MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
123}
124
125
126void
127MSDevice::buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into) {
132}
133
134
135void
141}
142
143void
144MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson) {
145 const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
146 const std::string object = isPerson ? "person" : "vehicle";
147 oc.doRegister(prefix + ".probability", new Option_Float(-1.0));// (default: no need to call RNG)
148 oc.addDescription(prefix + ".probability", optionsTopic, "The probability for a " + object + " to have a '" + deviceName + "' device");
149
150 oc.doRegister(prefix + ".explicit", new Option_StringVector());
151 oc.addSynonyme(prefix + ".explicit", prefix + ".knownveh", true);
152 oc.addDescription(prefix + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named " + object + "s");
153
154 oc.doRegister(prefix + ".deterministic", new Option_Bool(false));
155 oc.addDescription(prefix + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
156}
157
158
159void
161 WRITE_WARNINGF(TL("Device '%' cannot save state"), getID());
162}
163
164
165void
167}
168
169
170std::string
171MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required) {
172 const std::string key = "device." + paramName;
173 if (v.getParameter().knowsParameter(key)) {
174 return v.getParameter().getParameter(key, "");
175 } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
176 return v.getVehicleType().getParameter().getParameter(key, "");
177 } else {
178 if (oc.exists(key) && oc.isSet(key)) {
179 return oc.getValueString(key);
180 } else {
181 if (required) {
182 throw ProcessError("Missing parameter '" + key + "' for vehicle '" + v.getID());
183 } else {
184#ifdef DEBUG_DEVICE_PARAMS
185 std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter '" + key + "'. Using default of '" << result << "'\n";
186#endif
187 return deflt;
188 }
189 }
190 }
191}
192
193
194double
195MSDevice::getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required) {
196 const std::string key = "device." + paramName;
197 std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
198 try {
199 return StringUtils::toDouble(val);
200 } catch (...) {
201 WRITE_ERRORF(TL("Invalid float value '%'for parameter '%'"), val, key);
202 return deflt;
203 }
204}
205
206
207bool
208MSDevice::getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required) {
209 const std::string key = "device." + paramName;
210 std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
211 try {
212 return StringUtils::toBool(val);
213 } catch (...) {
214 WRITE_ERRORF(TL("Invalid bool value '%'for parameter '%'"), val, key);
215 return deflt;
216 }
217}
218
219
221MSDevice::getTimeParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, SUMOTime deflt, bool required) {
222 const std::string key = "device." + paramName;
223 std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
224 try {
225 return string2time(val);
226 } catch (...) {
227 WRITE_ERRORF(TL("Invalid time value '%'for parameter '%'"), val, key);
228 return deflt;
229 }
230}
231
232
233/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:268
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:277
#define TL(string)
Definition: MsgHandler.h:284
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Bluelight-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_DriverState-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_ElecHybrid-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Emissions-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void cleanup()
resets the edge filter
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCD-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Friction-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_GLOSA-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Routing-options.
static bool checkOptions(OptionsCont &oc)
checks MSDevice_Routing-options
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_SSM-options.
static void cleanup()
resets counters
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Taxi-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_ToC-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void cleanup()
resets counters
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Tripinfo-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCD-options.
static MSDevice_Vehroutes * buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into, int maxRoutes=std::numeric_limits< int >::max())
Build devices for the given vehicle, if needed.
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:166
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:160
static SUMOTime getTimeParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, SUMOTime deflt, bool required)
Definition: MSDevice.cpp:221
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:208
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required)
Definition: MSDevice.cpp:195
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:70
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:105
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:144
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:171
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:97
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:136
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:127
static void cleanup()
deletes the router instance
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
static void buildDevices(MSTransportable &t, std::vector< MSTransportableDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
static void buildDevices(MSTransportable &t, std::vector< MSTransportableDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_FCD-options.
static void buildDevices(MSTransportable &t, std::vector< MSTransportableDevice * > &into)
Build devices for the given vehicle, if needed.
static void buildDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_Routing-options.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
const SUMOVTypeParameter & getParameter() const
const std::string & getID() const
Returns the id.
Definition: Named.h:74
A storage for options typed value containers)
Definition: OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
void doRegister(const std::string &name, Option *o)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
bool exists(const std::string &name) const
Returns the information whether the named option is known.
std::string getValueString(const std::string &name) const
Returns the string-value of the named option (all options)
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
Encapsulated SAX-Attributes.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
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