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-2016 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 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 // ---------------------------------------------------------------------------
52 // static initialisation methods
53 // ---------------------------------------------------------------------------
54 void
56  oc.addOptionSubTopic("Example Device");
57  insertDefaultAssignmentOptions("example", "Example Device", oc);
58 
59  oc.doRegister("device.example.parameter", new Option_Float(0.0));
60  oc.addDescription("device.example.parameter", "Example Device", "An exemplary parameter which can be used by all instances of the example device");
61 }
62 
63 
64 void
65 MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
67  if (equippedByDefaultAssignmentOptions(oc, "example", v)) {
68  // build the device
69  // get custom vehicle parameter
70  SUMOReal customParameter2 = -1;
71  if (v.getParameter().knowsParameter("example")) {
72  try {
73  customParameter2 = TplConvert::_2SUMOReal(v.getParameter().getParameter("example", "-1").c_str());
74  } catch (...) {
75  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("example", "-1") + "'for vehicle parameter 'example'");
76  }
77 
78  } else {
79  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n";
80  }
81  // get custom vType parameter
82  SUMOReal customParameter3 = -1;
83  if (v.getVehicleType().getParameter().knowsParameter("example")) {
84  try {
85  customParameter3 = TplConvert::_2SUMOReal(v.getVehicleType().getParameter().getParameter("example", "-1").c_str());
86  } catch (...) {
87  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("example", "-1") + "'for vType parameter 'example'");
88  }
89 
90  } else {
91  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n";
92  }
93  MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(),
94  oc.getFloat("device.example.parameter"),
95  customParameter2,
96  customParameter3);
97  into.push_back(device);
98  }
99 }
100 
101 
102 // ---------------------------------------------------------------------------
103 // MSDevice_Example-methods
104 // ---------------------------------------------------------------------------
105 MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id,
106  SUMOReal customValue1, SUMOReal customValue2, SUMOReal customValue3) :
107  MSDevice(holder, id),
108  myCustomValue1(customValue1),
109  myCustomValue2(customValue2),
110  myCustomValue3(customValue3) {
111  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
112 }
113 
114 
116 }
117 
118 
119 bool
121  SUMOReal /* newPos */, SUMOReal newSpeed) {
122  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
123  // check whether another device is present on the vehicle:
124  MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
125  if (otherDevice != 0) {
126  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
127  }
128  return true; // keep the device
129 }
130 
131 
132 bool
134  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
135  return true; // keep the device
136 }
137 
138 
139 bool
142  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
143  return true; // keep the device
144 }
145 
146 
147 void
149  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
150  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
151  os.openTag("example_device");
152  os.writeAttr("customValue1", toString(myCustomValue1));
153  os.writeAttr("customValue2", toString(myCustomValue2));
154  os.closeTag();
155  }
156 }
157 
158 
159 
160 /****************************************************************************/
161 
~MSDevice_Example()
Destructor.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Saves departure info on insertion.
SUMOReal myCustomValue3
a value which is initialised based on a vType parameter
static SUMOReal _2SUMOReal(const E *const data)
converts a char-type array into the SUMOReal value described by it
Definition: TplConvert.h:290
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.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
Notification
Definition of a vehicle state.
bool notifyMove(SUMOVehicle &veh, SUMOReal oldPos, SUMOReal newPos, SUMOReal newSpeed)
Checks for waiting steps when the vehicle moves.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
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.
bool notifyLeave(SUMOVehicle &veh, SUMOReal lastPos, MSMoveReminder::Notification reason)
Saves arrival info.
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:69
SUMOReal myCustomValue2
a value which is initialised based on a vehicle parameter
const std::string & getID() const
Returns the id.
Definition: Named.h:66
const SUMOVTypeParameter & getParameter() const
Representation of a vehicle.
Definition: SUMOVehicle.h:66
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:94
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
Abstract in-vehicle device.
Definition: MSDevice.h:69
static bool equippedByDefaultAssignmentOptions(const OptionsCont &oc, const std::string &deviceName, SUMOVehicle &v)
Determines whether a vehicle should get a certain device.
Definition: MSDevice.cpp:108
void generateOutput() const
Called on writing tripinfo output.
MSDevice_Example(SUMOVehicle &holder, const std::string &id, SUMOReal customValue1, SUMOReal customValue2, SUMOReal customValue3)
Constructor.
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)
A storage for options typed value containers)
Definition: OptionsCont.h:99
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
virtual MSDevice * getDevice(const std::type_info &type) const =0
Returns a device of the given type if it exists or 0.
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.
SUMOReal myCustomValue1
a value which is initialised based on a commandline/configuration option
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.