SUMO - Simulation of Urban MObility
MSDevice.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Abstract in-vehicle device
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2013-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
33 #include <microsim/MSVehicle.h>
34 #include "MSDevice.h"
35 #include "MSDevice_Vehroutes.h"
36 #include "MSDevice_Tripinfo.h"
37 #include "MSDevice_Routing.h"
38 #include "MSDevice_Emissions.h"
39 #include "MSDevice_BTreceiver.h"
40 #include "MSDevice_BTsender.h"
41 #include "MSDevice_Example.h"
42 #include "MSDevice_Battery.h"
43 #include "MSDevice_SSM.h"
44 
45 
46 // ===========================================================================
47 // static member variables
48 // ===========================================================================
49 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 // ---------------------------------------------------------------------------
56 // static initialisation methods
57 // ---------------------------------------------------------------------------
58 void
67 }
68 
69 
70 bool
72  bool ok = true;
74  return ok;
75 }
76 
77 
78 void
79 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
89 }
90 
91 
92 void
93 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc) {
94  oc.doRegister("device." + deviceName + ".probability", new Option_Float(0.));
95  oc.addDescription("device." + deviceName + ".probability", optionsTopic, "The probability for a vehicle to have a '" + deviceName + "' device");
96 
97  oc.doRegister("device." + deviceName + ".explicit", new Option_String());
98  oc.addSynonyme("device." + deviceName + ".explicit", "device." + deviceName + ".knownveh", true);
99  oc.addDescription("device." + deviceName + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named vehicles");
100 
101  oc.doRegister("device." + deviceName + ".deterministic", new Option_Bool(false));
102  oc.addDescription("device." + deviceName + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
103 }
104 
105 
106 bool
108  // assignment by number
109  bool haveByNumber = false;
110  if (oc.exists("device." + deviceName + ".deterministic") && oc.getBool("device." + deviceName + ".deterministic")) {
111  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat("device." + deviceName + ".probability")) == 1;
112  } else {
113  if (oc.exists("device." + deviceName + ".probability") && oc.getFloat("device." + deviceName + ".probability") != 0) {
114  haveByNumber = myEquipmentRNG.rand() <= oc.getFloat("device." + deviceName + ".probability");
115  }
116  }
117  // assignment by name
118  bool haveByName = false;
119  if (oc.exists("device." + deviceName + ".explicit") && oc.isSet("device." + deviceName + ".explicit")) {
120  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
121  myExplicitIDs[deviceName] = std::set<std::string>();
122  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector("device." + deviceName + ".explicit");
123  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
124  }
125  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
126  }
127  // assignment by abstract parameters
128  bool haveByParameter = false;
129  bool parameterGiven = false;
130  const std::string key = "has." + deviceName + ".device";
131  if (v.getParameter().knowsParameter(key)) {
132  parameterGiven = true;
133  haveByParameter = TplConvert::_2bool(v.getParameter().getParameter(key, "false").c_str());
134  } else if (v.getVehicleType().getParameter().knowsParameter(key)) {
135  parameterGiven = true;
136  haveByParameter = TplConvert::_2bool(v.getVehicleType().getParameter().getParameter(key, "false").c_str());
137  }
138  return (haveByNumber && !parameterGiven) || haveByName || haveByParameter;
139 }
140 
141 
142 void
144  WRITE_WARNING("Device '" + getID() + "' cannot save state");
145 }
146 
147 
148 void
150 }
151 
152 
153 /****************************************************************************/
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
double rand()
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static bool _2bool(const E *const data)
converts a 0-terminated char-type array into the boolean value described by it
Definition: TplConvert.h:371
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_SSM-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:191
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
int getQuota(double frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:79
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
virtual const std::string deviceName() const =0
return the name for this type of device
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Routing-options.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
Representation of a vehicle.
Definition: SUMOVehicle.h:67
Encapsulated SAX-Attributes.
virtual void saveState(OutputDevice &out) const
Saves the state of the device.
Definition: MSDevice.cpp:143
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:310
bool exists(const std::string &name) const
Returns the information whether the named option is known.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static MSDevice_Vehroutes * buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into, int maxRoutes=INT_MAX)
Build devices for the given vehicle, if needed.
static void insertDefaultAssignmentOptions(const std::string &deviceName, const std::string &optionsTopic, OptionsCont &oc)
Adds common command options that allow to assign devices to vehicles.
Definition: MSDevice.cpp:93
const SUMOVTypeParameter & getParameter() const
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
static bool checkOptions(OptionsCont &oc)
check device-specific options
Definition: MSDevice.cpp:71
static MTRand myEquipmentRNG
A random number generator used to choose from vtype/route distributions and computing the speed facto...
Definition: MSDevice.h:194
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:107
virtual void loadState(const SUMOSAXAttributes &attrs)
Loads the state of the device from the given description.
Definition: MSDevice.cpp:149
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
static void insertOptions()
Inserts MSDevice_Emissions-options.
A storage for options typed value containers)
Definition: OptionsCont.h:99
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:59
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
static bool checkOptions(OptionsCont &oc)
checks MSDevice_Routing-options
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice *> &into)
Build devices for the given vehicle, if needed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.