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