Eclipse SUMO - Simulation of Urban MObility
MSDevice_Example.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 /****************************************************************************/
16 // A device which stands as an implementation example and which outputs movereminder calls
17 /****************************************************************************/
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
28 #include <microsim/MSNet.h>
29 #include <microsim/MSLane.h>
30 #include <microsim/MSEdge.h>
31 #include <microsim/MSVehicle.h>
32 #include "MSDevice_Tripinfo.h"
33 #include "MSDevice_Example.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 // ---------------------------------------------------------------------------
40 // static initialisation methods
41 // ---------------------------------------------------------------------------
42 void
44  oc.addOptionSubTopic("Example Device");
45  insertDefaultAssignmentOptions("example", "Example Device", oc);
46 
47  oc.doRegister("device.example.parameter", new Option_Float(0.0));
48  oc.addDescription("device.example.parameter", "Example Device", "An exemplary parameter which can be used by all instances of the example device");
49 }
50 
51 
52 void
53 MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into) {
55  if (equippedByDefaultAssignmentOptions(oc, "example", v, false)) {
56  // build the device
57  // get custom vehicle parameter
58  double customParameter2 = -1;
59  if (v.getParameter().knowsParameter("example")) {
60  try {
61  customParameter2 = StringUtils::toDouble(v.getParameter().getParameter("example", "-1"));
62  } catch (...) {
63  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("example", "-1") + "'for vehicle parameter 'example'");
64  }
65 
66  } else {
67  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n";
68  }
69  // get custom vType parameter
70  double customParameter3 = -1;
71  if (v.getVehicleType().getParameter().knowsParameter("example")) {
72  try {
73  customParameter3 = StringUtils::toDouble(v.getVehicleType().getParameter().getParameter("example", "-1"));
74  } catch (...) {
75  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("example", "-1") + "'for vType parameter 'example'");
76  }
77 
78  } else {
79  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n";
80  }
81  MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(),
82  oc.getFloat("device.example.parameter"),
83  customParameter2,
84  customParameter3);
85  into.push_back(device);
86  }
87 }
88 
89 
90 // ---------------------------------------------------------------------------
91 // MSDevice_Example-methods
92 // ---------------------------------------------------------------------------
93 MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id,
94  double customValue1, double customValue2, double customValue3) :
95  MSVehicleDevice(holder, id),
96  myCustomValue1(customValue1),
97  myCustomValue2(customValue2),
98  myCustomValue3(customValue3) {
99  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
100 }
101 
102 
104 }
105 
106 
107 bool
108 MSDevice_Example::notifyMove(SUMOTrafficObject& tObject, double /* oldPos */,
109  double /* newPos */, double newSpeed) {
110  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
111  if (tObject.isVehicle()) {
112  SUMOVehicle& veh = static_cast<SUMOVehicle&>(tObject);
113  // check whether another device is present on the vehicle:
114  MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
115  if (otherDevice != nullptr) {
116  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
117  }
118  }
119  return true; // keep the device
120 }
121 
122 
123 bool
125  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
126  return true; // keep the device
127 }
128 
129 
130 bool
131 MSDevice_Example::notifyLeave(SUMOTrafficObject& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
132  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
133  return true; // keep the device
134 }
135 
136 
137 void
139  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
140  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
141  os.openTag("example_device");
142  os.writeAttr("customValue1", toString(myCustomValue1));
143  os.writeAttr("customValue2", toString(myCustomValue2));
144  os.closeTag();
145  }
146 }
147 
148 std::string
149 MSDevice_Example::getParameter(const std::string& key) const {
150  if (key == "customValue1") {
151  return toString(myCustomValue1);
152  } else if (key == "customValue2") {
153  return toString(myCustomValue2);
154  } else if (key == "meaningOfLife") {
155  return "42";
156  }
157  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
158 }
159 
160 
161 void
162 MSDevice_Example::setParameter(const std::string& key, const std::string& value) {
163  double doubleValue;
164  try {
165  doubleValue = StringUtils::toDouble(value);
166  } catch (NumberFormatException&) {
167  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
168  }
169  if (key == "customValue1") {
170  myCustomValue1 = doubleValue;
171  } else {
172  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
173  }
174 }
175 
176 
177 /****************************************************************************/
178 
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
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_Example.h
OptionsCont.h
SUMOTrafficObject::getEdge
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
SUMOTrafficObject::isVehicle
virtual bool isVehicle() const =0
Get the vehicle's ID.
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
MSDevice_Example::myCustomValue3
double myCustomValue3
a value which is initialised based on a vType parameter
Definition: MSDevice_Example.h:157
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:60
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSEdge.h
MSDevice_Example::myCustomValue2
double myCustomValue2
a value which is initialised based on a vehicle parameter
Definition: MSDevice_Example.h:154
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_Example::setParameter
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key
Definition: MSDevice_Example.cpp:162
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
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
MSDevice_Tripinfo
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_Tripinfo.h:47
NumberFormatException
Definition: UtilExceptions.h:95
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
SUMOVehicle.h
MSDevice_Example
A device which collects info on the vehicle trip (mainly on departure and arrival)
Definition: MSDevice_Example.h:47
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
MSDevice_Example::getParameter
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key
Definition: MSDevice_Example.cpp:149
MSDevice_Example::MSDevice_Example
MSDevice_Example(SUMOVehicle &holder, const std::string &id, double customValue1, double customValue2, double customValue3)
Constructor.
Definition: MSDevice_Example.cpp:93
SUMOVehicle::getDevice
virtual MSVehicleDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
OutputDevice.h
OptionsCont::doRegister
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:74
MSDevice_Example::notifyEnter
bool notifyEnter(SUMOTrafficObject &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
Definition: MSDevice_Example.cpp:124
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
MSDevice_Example::deviceName
const std::string deviceName() const
return the name for this type of device
Definition: MSDevice_Example.h:116
MSDevice_Example::notifyLeave
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves arrival info.
Definition: MSDevice_Example.cpp:131
OptionsCont::addOptionSubTopic
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
Definition: OptionsCont.cpp:519
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
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
MSDevice::equippedByDefaultAssignmentOptions
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, DEVICEHOLDER &v, bool outputOptionSet, const bool isPerson=false)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.h:203
InvalidArgument
Definition: UtilExceptions.h:56
Option_Float
Definition: Option.h:470
MSDevice_Example::~MSDevice_Example
~MSDevice_Example()
Destructor.
Definition: MSDevice_Example.cpp:103
MSVehicleType::getParameter
const SUMOVTypeParameter & getParameter() const
Definition: MSVehicleType.h:560
config.h
MSDevice_Tripinfo.h
MSDevice_Example::myCustomValue1
double myCustomValue1
a value which is initialised based on a commandline/configuration option
Definition: MSDevice_Example.h:151
MSDevice_Example::notifyMove
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
Definition: MSDevice_Example.cpp:108
MSLane.h
MSDevice_Example::generateOutput
void generateOutput() const
Called on writing tripinfo output.
Definition: MSDevice_Example.cpp:138
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:91
OutputDevice::getDeviceByOption
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
Definition: OutputDevice.cpp:116
Parameterised::knowsParameter
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
Definition: Parameterised.cpp:66
MSVehicleDevice
Abstract in-vehicle device.
Definition: MSVehicleDevice.h:54