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.dev/sumo
3 // Copyright (C) 2013-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 /****************************************************************************/
19 // Abstract in-vehicle device
20 /****************************************************************************/
21 #include <config.h>
22 
25 #include <microsim/MSVehicle.h>
28 #include <microsim/MSEdge.h>
29 
30 #include "MSDevice_Vehroutes.h"
31 #include "MSDevice_Tripinfo.h"
32 #include "MSDevice_Routing.h"
33 #include "MSDevice_Emissions.h"
34 #include "MSDevice_BTreceiver.h"
35 #include "MSDevice_BTsender.h"
36 #include "MSDevice_Example.h"
37 #include "MSDevice_StationFinder.h"
38 #include "MSDevice_Battery.h"
39 #include "MSDevice_SSM.h"
40 #include "MSDevice_ToC.h"
41 #include "MSDevice_DriverState.h"
42 #include "MSDevice_Bluelight.h"
43 #include "MSDevice_FCD.h"
44 #include "MSDevice_Taxi.h"
45 #include "MSDevice_GLOSA.h"
46 #include "MSDevice_ElecHybrid.h"
50 #include "MSRoutingEngine.h"
51 #include "MSDevice_Friction.h"
52 #include "MSDevice_FCDReplay.h"
53 #include "MSDevice.h"
54 
55 
56 // ===========================================================================
57 // static member variables
58 // ===========================================================================
59 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
60 SumoRNG MSDevice::myEquipmentRNG("deviceEquipment");
61 
62 // ===========================================================================
63 // debug flags
64 // ===========================================================================
65 //#define DEBUG_DEVICE_PARAMS
66 
67 
68 // ===========================================================================
69 // method definitions
70 // ===========================================================================
71 // ---------------------------------------------------------------------------
72 // static initialisation methods
73 // ---------------------------------------------------------------------------
74 void
95 
100 }
101 
102 
103 bool
105  bool ok = true;
107  return ok;
108 }
109 
110 
111 void
112 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
120  const size_t numBefore = into.size();
122  MSDevice_Battery::buildVehicleDevices(v, into, into.size() == numBefore ? nullptr : static_cast<MSDevice_StationFinder*>(into.back()));
133 }
134 
135 
136 void
137 MSDevice::buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into) {
143 }
144 
145 
146 void
152 }
153 
154 void
155 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson) {
156  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
157  const std::string object = isPerson ? "person" : "vehicle";
158  oc.doRegister(prefix + ".probability", new Option_Float(-1.0));// (default: no need to call RNG)
159  oc.addDescription(prefix + ".probability", optionsTopic, "The probability for a " + object + " to have a '" + deviceName + "' device");
160 
161  oc.doRegister(prefix + ".explicit", new Option_StringVector());
162  oc.addSynonyme(prefix + ".explicit", prefix + ".knownveh", true);
163  oc.addDescription(prefix + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named " + object + "s");
164 
165  oc.doRegister(prefix + ".deterministic", new Option_Bool(false));
166  oc.addDescription(prefix + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
167 }
168 
169 
170 void
172  WRITE_WARNINGF(TL("Device '%' cannot save state"), getID());
173 }
174 
175 
176 void
178 }
179 
180 
181 std::string
182 MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const std::string& deflt, bool required) {
183  const std::string key = "device." + paramName;
184  if (v.getParameter().hasParameter(key)) {
185  return v.getParameter().getParameter(key, "");
186  } else if (v.getVehicleType().getParameter().hasParameter(key)) {
187  return v.getVehicleType().getParameter().getParameter(key, "");
188  } else {
189  if (oc.exists(key) && oc.isSet(key)) {
190  return oc.getValueString(key);
191  } else {
192  if (required) {
193  throw ProcessError("Missing parameter '" + key + "' for vehicle '" + v.getID());
194  } else {
195 #ifdef DEBUG_DEVICE_PARAMS
196  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter '" + key + "'. Using default of '" << result << "'\n";
197 #endif
198  return deflt;
199  }
200  }
201  }
202 }
203 
204 
205 double
206 MSDevice::getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const double deflt, bool required) {
207  const std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
208  try {
209  return Distribution_Parameterized(val).sample();
210  } catch (const ProcessError&) {
211  WRITE_ERRORF(TL("Invalid distribution / float value '%' for parameter '%' in vehicle '%'."), val, "device." + paramName, v.getID());
212  return deflt;
213  }
214 }
215 
216 
217 bool
218 MSDevice::getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const bool deflt, bool required) {
219  const std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
220  try {
221  return StringUtils::toBool(val);
222  } catch (const ProcessError&) {
223  WRITE_ERRORF(TL("Invalid boolean value '%' for parameter '%' in vehicle '%'."), val, "device." + paramName, v.getID());
224  return deflt;
225  }
226 }
227 
228 
229 SUMOTime
230 MSDevice::getTimeParam(const SUMOVehicle& v, const OptionsCont& oc, const std::string& paramName, const SUMOTime deflt, bool required) {
231  const std::string val = getStringParam(v, oc, paramName, toString(deflt), required);
232  try {
233  return string2time(val);
234  } catch (const ProcessError&) {
235  WRITE_ERRORF(TL("Invalid time value '%' for parameter '%' in vehicle '%'."), val, "device." + paramName, v.getID());
236  return deflt;
237  }
238 }
239 
240 
241 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:296
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:46
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
double sample(SumoRNG *which=0) const
Draw a sample of the distribution.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into, MSDevice_StationFinder *sf)
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 buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCDReplay-options.
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.
A device which triggers rerouting to nearby charging stations.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_StationFinder-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
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.
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, const std::string &paramName, const double deflt, bool required=false)
Definition: MSDevice.cpp:206
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, const std::string &paramName, const bool deflt, bool required=false)
Definition: MSDevice.cpp:218
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, const std::string &paramName, const std::string &deflt, bool required=false)
Definition: MSDevice.cpp:182
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
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:171
static SUMOTime getTimeParam(const SUMOVehicle &v, const OptionsCont &oc, const std::string &paramName, const SUMOTime deflt, bool required=false)
Definition: MSDevice.cpp:230
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:189
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:112
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 std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:186
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 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 &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:76
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
bool hasParameter(const std::string &key) const
Returns whether the parameter is set.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
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:60
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter