SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2015 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 
58  oc.doRegister("device.example.explicit", new Option_String());
59  oc.addDescription("device.example.explicit", "Example Device", "Assign a device to named vehicles");
60 
61  oc.doRegister("device.example.parameter", new Option_Float(0.0));
62  oc.addDescription("device.example.parameter", "Example Device", "An exemplary parameter which can be used by all instances of the example device");
63 }
64 
65 
66 void
67 MSDevice_Example::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
69  if (equippedByDefaultAssignmentOptions(oc, "example", v)) {
70  // build the device
71  // get custom vehicle parameter
72  SUMOReal customParameter2 = -1;
73  if (v.getParameter().knowsParameter("example")) {
74  try {
75  customParameter2 = TplConvert::_2SUMOReal(v.getParameter().getParameter("example", "-1").c_str());
76  } catch (...) {
77  WRITE_WARNING("Invalid value '" + v.getParameter().getParameter("example", "-1") + "'for vehicle parameter 'example'");
78  }
79 
80  } else {
81  std::cout << "vehicle '" << v.getID() << "' does not supply vehicle parameter 'example'. Using default of " << customParameter2 << "\n";
82  }
83  // get custom vType parameter
84  SUMOReal customParameter3 = -1;
85  if (v.getVehicleType().getParameter().knowsParameter("example")) {
86  try {
87  customParameter3 = TplConvert::_2SUMOReal(v.getVehicleType().getParameter().getParameter("example", "-1").c_str());
88  } catch (...) {
89  WRITE_WARNING("Invalid value '" + v.getVehicleType().getParameter().getParameter("example", "-1") + "'for vType parameter 'example'");
90  }
91 
92  } else {
93  std::cout << "vehicle '" << v.getID() << "' does not supply vType parameter 'example'. Using default of " << customParameter3 << "\n";
94  }
95  MSDevice_Example* device = new MSDevice_Example(v, "example_" + v.getID(),
96  oc.getFloat("device.example.parameter"),
97  customParameter2,
98  customParameter3);
99  into.push_back(device);
100  }
101 }
102 
103 
104 // ---------------------------------------------------------------------------
105 // MSDevice_Example-methods
106 // ---------------------------------------------------------------------------
107 MSDevice_Example::MSDevice_Example(SUMOVehicle& holder, const std::string& id,
108  SUMOReal customValue1, SUMOReal customValue2, SUMOReal customValue3) :
109  MSDevice(holder, id),
110  myCustomValue1(customValue1),
111  myCustomValue2(customValue2),
112  myCustomValue3(customValue3) {
113  std::cout << "initialized device '" << id << "' with myCustomValue1=" << myCustomValue1 << ", myCustomValue2=" << myCustomValue2 << ", myCustomValue3=" << myCustomValue3 << "\n";
114 }
115 
116 
118 }
119 
120 
121 bool
123  SUMOReal /* newPos */, SUMOReal newSpeed) {
124  std::cout << "device '" << getID() << "' notifyMove: newSpeed=" << newSpeed << "\n";
125  // check whether another device is present on the vehicle:
126  MSDevice_Tripinfo* otherDevice = static_cast<MSDevice_Tripinfo*>(veh.getDevice(typeid(MSDevice_Tripinfo)));
127  if (otherDevice != 0) {
128  std::cout << " veh '" << veh.getID() << " has device '" << otherDevice->getID() << "'\n";
129  }
130  return true; // keep the device
131 }
132 
133 
134 bool
136  std::cout << "device '" << getID() << "' notifyEnter: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
137  return true; // keep the device
138 }
139 
140 
141 bool
144  std::cout << "device '" << getID() << "' notifyLeave: reason=" << reason << " currentEdge=" << veh.getEdge()->getID() << "\n";
145  return true; // keep the device
146 }
147 
148 
149 void
151  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
152  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
153  os.openTag("example_device");
154  os.writeAttr("customValue1", toString(myCustomValue1));
155  os.writeAttr("customValue2", toString(myCustomValue2));
156  os.closeTag();
157  }
158 }
159 
160 
161 
162 /****************************************************************************/
163 
~MSDevice_Example()
Destructor.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:84
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)
Definition: TplConvert.h:223
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:67
SUMOReal myCustomValue2
a value which is initialised based on a vehicle parameter
const std::string & getID() const
Returns the id.
Definition: Named.h:60
const SUMOVTypeParameter & getParameter() const
Representation of a vehicle.
Definition: SUMOVehicle.h:65
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
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:99
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's parameter (including departure definition)
A storage for options typed value containers)
Definition: OptionsCont.h:108
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:218
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's ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.