SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2015 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_Person.h"
39 #include "MSDevice_Container.h"
40 #include "MSDevice_Emissions.h"
41 #include "MSDevice_BTreceiver.h"
42 #include "MSDevice_BTsender.h"
43 #include "MSDevice_Example.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // static member variables
52 // ===========================================================================
53 std::map<std::string, std::set<std::string> > MSDevice::myExplicitIDs;
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
59 // ---------------------------------------------------------------------------
60 // static initialisation methods
61 // ---------------------------------------------------------------------------
62 void
69 }
70 
71 
72 void
73 MSDevice::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
81 }
82 
83 
84 void
85 MSDevice::insertDefaultAssignmentOptions(const std::string& deviceName, const std::string& optionsTopic, OptionsCont& oc) {
86  oc.doRegister("device." + deviceName + ".probability", new Option_Float(0.));
87  oc.addDescription("device." + deviceName + ".probability", optionsTopic, "The probability for a vehicle to have a '" + deviceName + "' device");
88 
89  oc.doRegister("device." + deviceName + ".explicit", new Option_String());
90  oc.addSynonyme("device." + deviceName + ".explicit", "device." + deviceName + ".knownveh", true);
91  oc.addDescription("device." + deviceName + ".explicit", optionsTopic, "Assign a '" + deviceName + "' device to named vehicles");
92 
93  oc.doRegister("device." + deviceName + ".deterministic", new Option_Bool(false));
94  oc.addDescription("device." + deviceName + ".deterministic", optionsTopic, "The '" + deviceName + "' devices are set deterministic using a fraction of 1000");
95 }
96 
97 
98 bool
99 MSDevice::equippedByDefaultAssignmentOptions(const OptionsCont& oc, const std::string& deviceName, SUMOVehicle& v) {
100  // assignment by number
101  bool haveByNumber = false;
102  if (oc.exists("device." + deviceName + ".deterministic") && oc.getBool("device." + deviceName + ".deterministic")) {
103  haveByNumber = MSNet::getInstance()->getVehicleControl().getQuota(oc.getFloat("device." + deviceName + ".probability")) == 1;
104  } else {
105  if (oc.exists("device." + deviceName + ".probability") && oc.getFloat("device." + deviceName + ".probability") != 0) {
106  haveByNumber = RandHelper::rand() <= oc.getFloat("device." + deviceName + ".probability");
107  }
108  }
109  // assignment by name
110  bool haveByName = false;
111  if (oc.exists("device." + deviceName + ".explicit") && oc.isSet("device." + deviceName + ".explicit")) {
112  if (myExplicitIDs.find(deviceName) == myExplicitIDs.end()) {
113  myExplicitIDs[deviceName] = std::set<std::string>();
114  const std::vector<std::string> idList = OptionsCont::getOptions().getStringVector("device." + deviceName + ".explicit");
115  myExplicitIDs[deviceName].insert(idList.begin(), idList.end());
116  }
117  haveByName = myExplicitIDs[deviceName].count(v.getID()) > 0;
118  }
119  // assignment by abstract parameters
120  bool haveByParameter = false;
121  if (v.getParameter().knowsParameter("has." + deviceName + ".device")) {
122  haveByParameter = TplConvert::_2bool(v.getParameter().getParameter("has." + deviceName + ".device", "false").c_str());
123  } else {
124  haveByParameter = TplConvert::_2bool(v.getVehicleType().getParameter().getParameter("has." + deviceName + ".device", "false").c_str());
125  }
126  return haveByNumber || haveByName || haveByParameter;
127 }
128 
129 
130 /****************************************************************************/
131 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:84
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 bool _2bool(const E *const data)
Definition: TplConvert.h:282
static std::map< std::string, std::set< std::string > > myExplicitIDs
vehicles which explicitly carry a device, sorted by device, first
Definition: MSDevice.h:159
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:62
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
SUMOReal getFloat(const std::string &name) const
Returns the SUMOReal-value of the named option (only for Option_Float)
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:73
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Routing-options.
const SUMOVTypeParameter & getParameter() const
Representation of a vehicle.
Definition: SUMOVehicle.h:65
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:288
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:85
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTsender-options.
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:99
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle's parameter (including departure definition)
static void insertOptions()
Inserts MSDevice_Emissions-options.
A storage for options typed value containers)
Definition: OptionsCont.h:108
static MSDevice_Vehroutes * buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into, unsigned int maxRoutes=INT_MAX)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts options for building devices.
Definition: MSDevice.cpp:63
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_BTreceiver-options.
virtual const std::string & getID() const =0
Get the vehicle's ID.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
unsigned int getQuota(SUMOReal frac=-1) const
Returns the number of instances of the current vehicle that shall be emitted considering that "frac" ...
bool exists(const std::string &name) const
Returns the information whether the named option is known.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.