SUMO - Simulation of Urban MObility
MSDevice_Example.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A device which stands as an implementation example and which outputs movereminder calls
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2013-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
36 #include <microsim/MSNet.h>
37 #include <microsim/MSLane.h>
38 #include <microsim/MSEdge.h>
39 #include <microsim/MSVehicle.h>
40 #include "MSDevice_Tripinfo.h"
41 #include "MSDevice_Example.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 // ---------------------------------------------------------------------------
48 // static initialisation methods
49 // ---------------------------------------------------------------------------
50 void
52  oc.addOptionSubTopic("Example Device");
53  insertDefaultAssignmentOptions("example", "Example Device", oc);
54 
55  oc.doRegister("device.example.parameter", new Option_Float(0.0));
56  oc.addDescription("device.example.parameter", "Example Device", "An exemplary parameter which can be used by all instances of the example device");
57 }
58 
59 
60 void
61 MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
63  if (equippedByDefaultAssignmentOptions(oc, "example", v)) {
64  // build the device
65  // get custom vehicle parameter
66  double customParameter2 = -1;
67  if (v.getParameter().knowsParameter("example")) {
68  try {
69  customParameter2 = TplConvert::_2double(v.getParameter().getParameter("example", "-1").c_str());
70  } catch (...) {
71  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("example", "-1") + "'for vehicle parameter 'example'");
72  }
73 
74  } else {
75  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n";
76  }
77  // get custom vType parameter
78  double customParameter3 = -1;
79  if (v.getVehicleType().getParameter().knowsParameter("example")) {
80  try {
81  customParameter3 = TplConvert::_2double(v.getVehicleType().getParameter().getParameter("example", "-1").c_str());
82  } catch (...) {
83  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("example", "-1") + "'for vType parameter 'example'");
84  }
85 
86  } else {
87  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n";
88  }
89  MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(),
90  oc.getFloat("device.example.parameter"),
91  customParameter2,
92  customParameter3);
93  into.push_back(device);
94  }
95 }
96 
97 
98 // ---------------------------------------------------------------------------
99 // MSDevice_Example-methods
100 // ---------------------------------------------------------------------------
101 MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id,
102  double customValue1, double customValue2, double customValue3) :
103  MSDevice(holder, id),
104  myCustomValue1(customValue1),
105  myCustomValue2(customValue2),
106  myCustomValue3(customValue3) {
107  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
108 }
109 
110 
112 }
113 
114 
115 bool
116 MSDevice_Example::notifyMove(SUMOVehicle& veh, double /* oldPos */,
117  double /* newPos */, double newSpeed) {
118  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
119  // check whether another device is present on the vehicle:
120  MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
121  if (otherDevice != 0) {
122  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
123  }
124  return true; // keep the device
125 }
126 
127 
128 bool
130  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
131  return true; // keep the device
132 }
133 
134 
135 bool
136 MSDevice_Example::notifyLeave(SUMOVehicle& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
137  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
138  return true; // keep the device
139 }
140 
141 
142 void
144  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
145  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
146  os.openTag("example_device");
147  os.writeAttr("customValue1", toString(myCustomValue1));
148  os.writeAttr("customValue2", toString(myCustomValue2));
149  os.closeTag();
150  }
151 }
152 
153 std::string
154 MSDevice_Example::getParameter(const std::string& key) const {
155  if (key == "customValue1") {
156  return toString(myCustomValue1);
157  } else if (key == "customValue2") {
158  return toString(myCustomValue2);
159  } else if (key == "meaningOfLife") {
160  return "42";
161  }
162  throw InvalidArgument("Parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
163 }
164 
165 
166 void
167 MSDevice_Example::setParameter(const std::string& key, const std::string& value) {
168  double doubleValue;
169  try {
170  doubleValue = TplConvert::_2double(value.c_str());
171  } catch (NumberFormatException) {
172  throw InvalidArgument("Setting parameter '" + key + "' requires a number for device of type '" + deviceName() + "'");
173  }
174  if (key == "customValue1") {
175  myCustomValue1 = doubleValue;
176  } else {
177  throw InvalidArgument("Setting parameter '" + key + "' is not supported for device of type '" + deviceName() + "'");
178  }
179 }
180 
181 
182 /****************************************************************************/
183 
~MSDevice_Example()
Destructor.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:82
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.
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: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
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:56
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:67
void setParameter(const std::string &key, const std::string &value)
try to set the given parameter for this device. Throw exception for unsupported key ...
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
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
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.
Abstract in-vehicle device.
Definition: MSDevice.h:71
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
void generateOutput() const
Called on writing tripinfo output.
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:99
static double _2double(const E *const data)
converts a char-type array into the double value described by it
Definition: TplConvert.h:297
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
virtual MSDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
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:79
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