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-2018 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(SUMOVehicle& veh, double /* oldPos */,
110  double /* newPos */, double newSpeed) {
111  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
112  // check whether another device is present on the vehicle:
113  MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
114  if (otherDevice != nullptr) {
115  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
116  }
117  return true; // keep the device
118 }
119 
120 
121 bool
123  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
124  return true; // keep the device
125 }
126 
127 
128 bool
129 MSDevice_Example::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
130  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
131  return true; // keep the device
132 }
133 
134 
135 void
137  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
138  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
139  os.openTag("example_device");
140  os.writeAttr("customValue1", toString(myCustomValue1));
141  os.writeAttr("customValue2", toString(myCustomValue2));
142  os.closeTag();
143  }
144 }
145 
146 std::string
147 MSDevice_Example::getParameter(const std::string& key) const {
148  if (key == "customValue1") {
149  return toString(myCustomValue1);
150  } else if (key == "customValue2") {
151  return toString(myCustomValue2);
152  } else if (key == "meaningOfLife") {
153  return "42";
154  }
155  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
156 }
157 
158 
159 void
160 MSDevice_Example::setParameter(const std::string& key, const std::string& value) {
161  double doubleValue;
162  try {
163  doubleValue = StringUtils::toDouble(value);
164  } catch (NumberFormatException&) {
165  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
166  }
167  if (key == "customValue1") {
168  myCustomValue1 = doubleValue;
169  } else {
170  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
171  }
172 }
173 
174 
175 /****************************************************************************/
176 
~MSDevice_Example()
Destructor.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:75
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
double myCustomValue1
a value which is initialised based on a commandline/configuration option
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves departure info on insertion.
virtual MSVehicleDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
Notification
Definition of a vehicle state.
MSDevice_Example(SUMOVehicle &holder, const std::string &id, double customValue1, double customValue2, double customValue3)
Constructor.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
const std::string & getID() const
Returns the id.
Definition: Named.h:78
static void insertOptions(OptionsCont &oc)
Inserts MSDevice_Example-options.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSVehicleDevice *> &into)
Build devices for the given vehicle, if needed.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Saves arrival info.
bool knowsParameter(const std::string &key) const
Returns whether the parameter is known.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
bool notifyMove(SUMOVehicle &veh, double oldPos, double newPos, double newSpeed)
Checks for waiting steps when the vehicle moves.
Representation of a vehicle.
Definition: SUMOVehicle.h:60
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key ...
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
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:121
const SUMOVTypeParameter & getParameter() const
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
void generateOutput() const
Called on writing tripinfo output.
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:208
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
std::string getParameter(const std::string &key) const
try to retrieve the given parameter from this device. Throw exception for unsupported key ...
A storage for options typed value containers)
Definition: OptionsCont.h:92
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
Abstract in-vehicle device.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
double myCustomValue2
a value which is initialised based on a vehicle parameter
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
const std::string deviceName() const
return the name for this type of device
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.
double myCustomValue3
a value which is initialised based on a vType parameter