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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Abstract in-vehicle device
16 /****************************************************************************/
17 
18 // ===========================================================================
19 // included modules
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"
39 #include "MSDevice_DriverState.h"
40 #include "MSDevice_Bluelight.h"
41 #include "MSDevice_FCD.h"
44 #include "MSRoutingEngine.h"
45 
46 
47 // ===========================================================================
48 // static member variables
49 // ===========================================================================
50 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
51 std::mt19937 MSDevice::myEquipmentRNG;
52 
53 // ===========================================================================
54 // debug flags
55 // ===========================================================================
56 //#define DEBUG_DEVICE_PARAMS
57 
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 // ---------------------------------------------------------------------------
63 // static initialisation methods
64 // ---------------------------------------------------------------------------
65 void
79 
82 }
83 
84 
85 bool
87  bool ok = true;
89  return ok;
90 }
91 
92 
93 void
94 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
108 }
109 
110 
111 void
112 MSDevice::buildTransportableDevices(MSTransportable& p, std::vector<MSTransportableDevice*>& into) {
115 }
116 
117 
118 void
123 }
124 
125 void
126 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc, const bool isPerson) {
127  const std::string prefix = (isPerson ? "person-device." : "device.") + deviceName;
128  const std::string object = isPerson ? "person" : "vehicle";
129  oc.doRegister(prefix + ".probability", new Option_Float(-1.0));// (default: no need to call RNG)
130  oc.addDescription(prefix + ".probability", optionsTopic, "The probability for a " + object + " to have a '" + deviceName + "' device");
131 
132  oc.doRegister(prefix + ".explicit", new Option_StringVector());
133  oc.addSynonyme(prefix + ".explicit", prefix + ".knownveh", true);
134  oc.addDescription(prefix + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named " + object + "s");
135 
136  oc.doRegister(prefix + ".deterministic", new Option_Bool(false));
137  oc.addDescription(prefix + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
138 }
139 
140 
141 void
143  WRITE_WARNING("Device '" + getID() + "' cannot save state");
144 }
145 
146 
147 void
149 }
150 
151 
152 std::string
153 MSDevice::getStringParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, std::string deflt, bool required) {
154  std::string result = deflt;
155  if (v.getParameter().knowsParameter("device." + paramName)) {
156  try {
157  result = v.getParameter().getParameter("device." + paramName, "").c_str();
158  } catch (...) {
159  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
160  }
161  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
162  try {
163  result = v.getVehicleType().getParameter().getParameter("device." + paramName, "").c_str();
164  } catch (...) {
165  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
166  }
167  } else {
168  if (oc.isSet("device." + paramName)) {
169  result = oc.getString("device." + paramName);
170  } else {
171  if (required) {
172  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
173  } else {
174  result = deflt;
175 #ifdef DEBUG_DEVICE_PARAMS
176  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
177 #endif
178  }
179  }
180  }
181  return result;
182 }
183 
184 
185 double
186 MSDevice::getFloatParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, double deflt, bool required) {
187  double result = deflt;
188  if (v.getParameter().knowsParameter("device." + paramName)) {
189  try {
190  result = StringUtils::toDouble(v.getParameter().getParameter("device." + paramName, ""));
191  } catch (...) {
192  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
193  }
194  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
195  try {
196  result = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("device." + paramName, ""));
197  } catch (...) {
198  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
199  }
200  } else {
201  if (oc.isSet("device." + paramName)) {
202  result = oc.getFloat("device." + paramName);
203  } else {
204  if (required) {
205  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
206  } else {
207  result = deflt;
208 #ifdef DEBUG_DEVICE_PARAMS
209  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
210 #endif
211  }
212  }
213  }
214  return result;
215 }
216 
217 
218 bool
219 MSDevice::getBoolParam(const SUMOVehicle& v, const OptionsCont& oc, std::string paramName, bool deflt, bool required) {
220  bool result = deflt;
221  if (v.getParameter().knowsParameter("device." + paramName)) {
222  try {
223  result = StringUtils::toBool(v.getParameter().getParameter("device." + paramName, ""));
224  } catch (...) {
225  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("device." + paramName, "") + "'for vehicle parameter 'toc." + paramName + "'");
226  }
227  } else if (v.getVehicleType().getParameter().knowsParameter("device." + paramName)) {
228  try {
229  result = StringUtils::toBool(v.getVehicleType().getParameter().getParameter("device." + paramName, ""));
230  } catch (...) {
231  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("device." + paramName, "") + "'for vType parameter 'toc." + paramName + "'");
232  }
233  } else {
234  if (oc.isSet("device." + paramName)) {
235  result = oc.getBool("device." + paramName);
236  } else {
237  if (required) {
238  throw ProcessError("Missing parameter 'device." + paramName + "' for vehicle '" + v.getID());
239  } else {
240  result = deflt;
241 #ifdef DEBUG_DEVICE_PARAMS
242  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'device." + paramName + "'. Using default of '" << result << "'\n";
243 #endif
244  }
245  }
246  }
247  return result;
248 }
249 
250 /****************************************************************************/
MSDevice_FCD::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_FCD-options.
Definition: MSDevice_FCD.cpp:47
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
MSDevice_DriverState.h
MSDevice_Tripinfo::cleanup
static void cleanup()
resets counters
Definition: MSDevice_Tripinfo.cpp:121
MSDevice_Emissions::insertOptions
static void insertOptions()
Inserts MSDevice_Emissions-options.
Definition: MSDevice_Emissions.cpp:40
MSTransportableDevice_Routing::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_Routing-options.
Definition: MSTransportableDevice_Routing.cpp:42
MSDevice::buildTransportableDevices
static void buildTransportableDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSDevice.cpp:112
StringUtils::toBool
static bool toBool(const std::string &sData)
converts a string into the bool value described by it by calling the char-type converter
Definition: StringUtils.cpp:374
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
Option_Bool
Definition: Option.h:538
MSDevice_Example::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
Definition: MSDevice_Example.cpp:43
SUMOVehicle::getParameter
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
MSDevice_Bluelight::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Bluelight.cpp:59
MSDevice_Example.h
OptionsCont.h
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:345
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
OptionsCont::getString
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
Definition: OptionsCont.cpp:201
MSDevice_FCD::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_FCD.cpp:57
MSTransportableDevice_Routing::buildDevices
static void buildDevices(MSTransportable &p, std::vector< MSTransportableDevice * > &into)
Build devices for the given person, if needed.
Definition: MSTransportableDevice_Routing.cpp:51
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
OptionsCont::getBool
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Definition: OptionsCont.cpp:222
MSDevice::myExplicitIDs
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:186
MSTransportableDevice_Routing.h
MSTransportable
Definition: MSTransportable.h:58
MSDevice_ToC::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_ToC.cpp:154
MSDevice_Battery.h
MSDevice_Example::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Example.cpp:53
MSDevice.h
MSDevice_DriverState::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_DriverState-options.
Definition: MSDevice_DriverState.cpp:62
Parameterised::getParameter
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Definition: Parameterised.cpp:72
MSVehicle.h
MSRoutingEngine::cleanup
static void cleanup()
deletes the router instance
Definition: MSRoutingEngine.cpp:327
MSDevice::saveState
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:142
MSDevice_Routing::checkOptions
static bool checkOptions(OptionsCont &oc)
checks MSDevice_Routing-options
Definition: MSDevice_Routing.cpp:92
MSDevice_Tripinfo::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Tripinfo-options.
Definition: MSDevice_Tripinfo.cpp:74
OptionsCont::addDescription
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
Definition: OptionsCont.cpp:469
MSDevice::insertDefaultAssignmentOptions
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:126
OptionsCont::addSynonyme
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:95
MSDevice_Tripinfo::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Tripinfo.cpp:81
MSDevice::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:94
MSDevice_SSM::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_SSM.cpp:230
MSDevice_FCD::cleanup
static void cleanup()
resets the edge filter
Definition: MSDevice_FCD.cpp:104
MSDevice_Vehroutes::buildVehicleDevices
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.
Definition: MSDevice_Vehroutes.cpp:83
MSDevice::checkOptions
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:86
MSDevice_BTsender::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
Definition: MSDevice_BTsender.cpp:49
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:74
ProcessError
Definition: UtilExceptions.h:39
MSTransportableDevice_FCD::buildDevices
static void buildDevices(MSTransportable &t, std::vector< MSTransportableDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSTransportableDevice_FCD.cpp:53
MSDevice_Bluelight.h
MSDevice::cleanupAll
static void cleanupAll()
perform cleanup for all devices
Definition: MSDevice.cpp:119
MSDevice_DriverState::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_DriverState.cpp:87
MSDevice::myEquipmentRNG
static std::mt19937 myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:189
MSDevice_SSM::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_SSM-options.
Definition: MSDevice_SSM.cpp:208
MSDevice_Routing.h
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MSDevice::deviceName
virtual const std::string deviceName() const =0
return the name for this type of device
MSDevice_BTsender.h
MSRoutingEngine.h
MSDevice::getFloatParam
static double getFloatParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, double deflt, bool required)
Definition: MSDevice.cpp:186
MSTransportableDevice_FCD::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSTransportableDevice_FCD-options.
Definition: MSTransportableDevice_FCD.cpp:44
MSDevice_BTreceiver::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
Definition: MSDevice_BTreceiver.cpp:57
MSDevice_BTreceiver.h
MSDevice_BTreceiver::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_BTreceiver.cpp:74
MSDevice_ToC::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_ToC-options.
Definition: MSDevice_ToC.cpp:108
OptionsCont::getFloat
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
Definition: OptionsCont.cpp:208
StringUtils.h
Option_StringVector
Definition: Option.h:720
Option_Float
Definition: Option.h:470
MSTransportable.h
MSDevice::getStringParam
static std::string getStringParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, std::string deflt, bool required)
Definition: MSDevice.cpp:153
MSDevice_Routing::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Routing-options.
Definition: MSDevice_Routing.cpp:49
MSVehicleType::getParameter
const SUMOVTypeParameter & getParameter() const
Definition: MSVehicleType.h:560
MSDevice_Vehroutes.h
MSDevice_SSM.h
config.h
MSDevice_Battery::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
Definition: MSDevice_Battery.cpp:47
MSDevice_Battery::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Battery.cpp:53
MSDevice_Tripinfo.h
MSDevice_FCD.h
MSDevice::loadState
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:148
MSDevice::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:66
MSDevice_Emissions.h
MSDevice_Emissions::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Emissions.cpp:46
MSTransportableDevice_FCD.h
MSDevice_Routing::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_Routing.cpp:125
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
MSDevice_BTsender::buildVehicleDevices
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice_BTsender.cpp:55
MSDevice_Bluelight::insertOptions
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Bluelight-options.
Definition: MSDevice_Bluelight.cpp:48
MSVehicleControl.h
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSDevice_ToC.h
MSDevice::getBoolParam
static bool getBoolParam(const SUMOVehicle &v, const OptionsCont &oc, std::string paramName, bool deflt, bool required)
Definition: MSDevice.cpp:219
Parameterised::knowsParameter
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
Definition: Parameterised.cpp:66