SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSBaseVehicle.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A base class for vehicle implementations
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 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 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <iostream>
34 #include <cassert>
35 #include <utils/common/StdDefs.h>
39 #include "MSVehicleType.h"
40 #include "MSEdge.h"
41 #include "MSLane.h"
42 #include "MSMoveReminder.h"
43 #include "MSBaseVehicle.h"
44 #include "MSNet.h"
45 #include "devices/MSDevice.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 // ===========================================================================
52 // static members
53 // ===========================================================================
55 #ifdef _DEBUG
56 std::set<std::string> MSBaseVehicle::myShallTraceMoveReminders;
57 #endif
58 
59 // ===========================================================================
60 // method definitions
61 // ===========================================================================
62 MSBaseVehicle::MSBaseVehicle(SUMOVehicleParameter* pars, const MSRoute* route, const MSVehicleType* type, const SUMOReal speedFactor) :
63  myParameter(pars),
64  myRoute(route),
65  myType(type),
66  myCurrEdge(route->begin()),
67  myChosenSpeedFactor(speedFactor),
68  myMoveReminders(0),
69  myDeparture(NOT_YET_DEPARTED),
70  myArrivalPos(-1),
71  myNumberReroutes(0)
72 #ifdef _DEBUG
73  , myTraceMoveReminders(myShallTraceMoveReminders.count(pars->id) > 0)
74 #endif
75 {
76  // init devices
78  //
79  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
80  myMoveReminders.push_back(std::make_pair(*dev, 0.));
81  }
84 }
85 
87  myRoute->release();
88  delete myParameter;
89  for (std::vector< MSDevice* >::iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
90  delete(*dev);
91  }
92 }
93 
94 
95 const std::string&
97  return myParameter->id;
98 }
99 
100 
103  return *myParameter;
104 }
105 
106 
107 SUMOReal
109  return myType->getMaxSpeed();
110 }
111 
112 
113 const MSEdge*
114 MSBaseVehicle::succEdge(unsigned int nSuccs) const {
115  if (myCurrEdge + nSuccs < myRoute->end()) {
116  return *(myCurrEdge + nSuccs);
117  } else {
118  return 0;
119  }
120 }
121 
122 
123 const MSEdge*
125  return *myCurrEdge;
126 }
127 
128 
129 void
131  // check whether to reroute
132  std::vector<const MSEdge*> edges;
133  if (withTaz && MSEdge::dictionary(myParameter->fromTaz + "-source") && MSEdge::dictionary(myParameter->toTaz + "-sink")) {
134  router.compute(MSEdge::dictionary(myParameter->fromTaz + "-source"), MSEdge::dictionary(myParameter->toTaz + "-sink"), this, t, edges);
135  if (edges.size() >= 2) {
136  edges.erase(edges.begin());
137  edges.pop_back();
138  }
139  } else {
140  router.compute(*myCurrEdge, myRoute->getLastEdge(), this, t, edges);
141  }
142  if (edges.empty()) {
143  WRITE_WARNING("No route for vehicle '" + getID() + "' found.");
144  return;
145  }
146  replaceRouteEdges(edges, withTaz);
147 }
148 
149 
150 bool
151 MSBaseVehicle::replaceRouteEdges(const MSEdgeVector& edges, bool onInit) {
152  // build a new id, first
153  std::string id = getID();
154  if (id[0] != '!') {
155  id = "!" + id;
156  }
157  if (myRoute->getID().find("!var#") != std::string::npos) {
158  id = myRoute->getID().substr(0, myRoute->getID().rfind("!var#") + 5) + toString(getNumberReroutes() + 1);
159  } else {
160  id = id + "!var#1";
161  }
162  const RGBColor& c = myRoute->getColor();
163  MSRoute* newRoute = new MSRoute(id, edges, 0, &c == &RGBColor::DEFAULT_COLOR ? 0 : new RGBColor(c), myRoute->getStops());
164  if (!MSRoute::dictionary(id, newRoute)) {
165  delete newRoute;
166  return false;
167  }
168  if (!replaceRoute(newRoute, onInit)) {
169  newRoute->addReference();
170  newRoute->release();
171  return false;
172  }
173  return true;
174 }
175 
176 
177 SUMOReal
179  return 0;
180 }
181 
182 
183 void
187 }
188 
189 
190 bool
192  return myDeparture != NOT_YET_DEPARTED;
193 }
194 
195 
196 bool
198  return succEdge(1) == 0;
199 }
200 
201 void
203 }
204 
205 
206 bool
207 MSBaseVehicle::hasValidRoute(std::string& msg) const {
208  MSRouteIterator last = myRoute->end() - 1;
209  // check connectivity, first
210  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
211  if ((*e)->allowedLanes(**(e + 1), myType->getVehicleClass()) == 0) {
212  msg = "No connection between '" + (*e)->getID() + "' and '" + (*(e + 1))->getID() + "'.";
213  return false;
214  }
215  }
216  last = myRoute->end();
217  // check usable lanes, then
218  for (MSRouteIterator e = myCurrEdge; e != last; ++e) {
219  if ((*e)->prohibits(this)) {
220  msg = "Edge '" + (*e)->getID() + "' prohibits.";
221  return false;
222  }
223  }
224  return true;
225 }
226 
227 
228 void
230 #ifdef _DEBUG
231  if (myTraceMoveReminders) {
232  traceMoveReminder("add", rem, 0, true);
233  }
234 #endif
235  myMoveReminders.push_back(std::make_pair(rem, 0.));
236 }
237 
238 
239 void
241  for (MoveReminderCont::iterator r = myMoveReminders.begin(); r != myMoveReminders.end(); ++r) {
242  if (r->first == rem) {
243 #ifdef _DEBUG
244  if (myTraceMoveReminders) {
245  traceMoveReminder("remove", rem, 0, false);
246  }
247 #endif
248  myMoveReminders.erase(r);
249  return;
250  }
251  }
252 }
253 
254 
255 void
257  for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
258  if (rem->first->notifyEnter(*this, reason)) {
259 #ifdef _DEBUG
260  if (myTraceMoveReminders) {
261  traceMoveReminder("notifyEnter", rem->first, rem->second, true);
262  }
263 #endif
264  ++rem;
265  } else {
266 #ifdef _DEBUG
267  if (myTraceMoveReminders) {
268  traceMoveReminder("notifyEnter", rem->first, rem->second, false);
269  }
270 #endif
271  rem = myMoveReminders.erase(rem);
272  }
273  }
274 }
275 
276 
277 void
279  const SUMOReal lastLaneLength = (myRoute->getLastEdge()->getLanes())[0]->getLength();
280  switch (myParameter->arrivalPosProcedure) {
281  case ARRIVAL_POS_GIVEN:
282  if (fabs(myParameter->arrivalPos) > lastLaneLength) {
283  WRITE_WARNING("Vehicle '" + getID() + "' will not be able to arrive at the given position!");
284  }
285  // Maybe we should warn the user about invalid inputs!
286  myArrivalPos = MIN2(myParameter->arrivalPos, lastLaneLength);
287  if (myArrivalPos < 0) {
288  myArrivalPos = MAX2(myArrivalPos + lastLaneLength, static_cast<SUMOReal>(0));
289  }
290  break;
291  case ARRIVAL_POS_RANDOM:
292  myArrivalPos = RandHelper::rand(static_cast<SUMOReal>(0), lastLaneLength);
293  break;
294  default:
295  myArrivalPos = lastLaneLength;
296  break;
297  }
298 }
299 
300 
301 MSDevice*
302 MSBaseVehicle::getDevice(const std::type_info& type) const {
303  for (std::vector<MSDevice*>::const_iterator dev = myDevices.begin(); dev != myDevices.end(); ++dev) {
304  if (typeid(**dev) == type) {
305  return *dev;
306  }
307  }
308  return 0;
309 }
310 
311 
312 void
318  // here starts the vehicle internal part (see loading)
319  // @note: remember to close the vehicle tag when calling this in a subclass!
320 }
321 
322 
323 #ifdef _DEBUG
324 void
325 MSBaseVehicle::initMoveReminderOutput(const OptionsCont& oc) {
326  if (oc.isSet("movereminder-output.vehicles")) {
327  const std::vector<std::string> vehicles = oc.getStringVector("movereminder-output.vehicles");
328  myShallTraceMoveReminders.insert(vehicles.begin(), vehicles.end());
329  }
330 }
331 
332 
333 void
334 MSBaseVehicle::traceMoveReminder(const std::string& type, MSMoveReminder* rem, SUMOReal pos, bool keep) const {
335  OutputDevice& od = OutputDevice::getDeviceByOption("movereminder-output");
336  od.openTag("movereminder");
337  od.writeAttr(SUMO_ATTR_TIME, STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()));
338  od.writeAttr("veh", getID());
340  od.writeAttr("type", type);
341  od.writeAttr("pos", toString(pos));
342  od.writeAttr("keep", toString(keep));
343  od.closeTag();
344 }
345 #endif
346 
347 /****************************************************************************/
348 
void removeReminder(MSMoveReminder *rem)
Removes a MoveReminder dynamically.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
const MSVehicleType * myType
This Vehicle&#39;s type.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
MSDevice * getDevice(const std::type_info &type) const
Returns a device of the given type if it exists or 0.
SUMOReal getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
void reroute(SUMOTime t, SUMOAbstractRouter< MSEdge, SUMOVehicle > &router, bool withTaz=false)
Performs a rerouting using the given router.
unsigned int getNumberReroutes() const
Returns the number of new routes this vehicle got.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
const std::string & getDescription() const
SUMOReal myArrivalPos
the position on the destination lane where the vehicle stops
MoveReminderCont myMoveReminders
Current lane&#39;s move reminder.
SUMOReal getMaxSpeed() const
Returns the maximum speed.
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:167
virtual void addPerson(MSPerson *person)
Adds a person to this vehicle.
MSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:59
virtual bool replaceRoute(const MSRoute *route, bool onInit=false)=0
Replaces the current route by the given one.
const MSEdge * getLastEdge() const
returns the destination edge
Definition: MSRoute.cpp:93
Notification
Definition of a vehicle state.
static SUMOReal rand()
Returns a random real number in [0, 1)
Definition: RandHelper.h:61
bool replaceRouteEdges(const MSEdgeVector &edges, bool onInit=false)
Replaces the current route by the given edges.
SUMOReal arrivalPos
(optional) The position the vehicle shall arrive on
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
T MAX2(T a, T b)
Definition: StdDefs.h:63
virtual ~MSBaseVehicle()
Destructor.
const MSRoute * myRoute
This Vehicle&#39;s route.
bool hasDeparted() const
Returns whether this vehicle has already departed.
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:461
const SUMOVehicleParameter * myParameter
This Vehicle&#39;s parameter.
The arrival position is given.
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the stops.
Definition: MSRoute.cpp:302
SUMOTime getCurrentTimeStep() const
Returns the current simulation step (in s)
Definition: MSNet.cpp:500
MSBaseVehicle(SUMOVehicleParameter *pars, const MSRoute *route, const MSVehicleType *type, const SUMOReal speedFactor)
Constructor.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
Definition: MSDevice.cpp:66
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual void saveState(OutputDevice &out)
Saves the (common) state of a vehicle.
std::string toTaz
The vehicle&#39;s destination zone (district)
const std::string & getID() const
Returns the id.
Definition: Named.h:60
A road/street connecting two junctions.
Definition: MSEdge.h:73
virtual SUMOReal getAcceleration() const
Returns the vehicle&#39;s acceleration.
std::vector< const MSEdge * > MSEdgeVector
Definition: MSPerson.h:53
static const RGBColor DEFAULT_COLOR
The default color (for vehicle types and vehicles)
Definition: RGBColor.h:198
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:248
const MSEdge * succEdge(unsigned int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
SUMOTime depart
The vehicle&#39;s departure time.
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
T MIN2(T a, T b)
Definition: StdDefs.h:57
std::string fromTaz
The vehicle&#39;s origin zone (district)
Something on a lane to be noticed about vehicle movement.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:100
Abstract in-vehicle device.
Definition: MSDevice.h:68
Structure representing possible vehicle parameter.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:81
#define SUMOTime_MAX
Definition: SUMOTime.h:44
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
virtual void activateReminders(const MSMoveReminder::Notification reason)
&quot;Activates&quot; all current move reminder
void onDepart()
Called when the vehicle is inserted into the network.
A storage for options typed value containers)
Definition: OptionsCont.h:108
virtual bool hasArrived() const
Returns whether this vehicle has already arived (by default this is true if the vehicle has reached i...
void calculateArrivalPos()
(Re-)Calculates the arrival position from the vehicle parameters
const std::string & getID() const
Returns the name of the vehicle type.
int SUMOTime
Definition: SUMOTime.h:43
const SUMOVehicleParameter & getParameter() const
Returns the vehicle&#39;s parameter (including departure definition)
bool hasValidRoute(std::string &msg) const
Validates the current route.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
const RGBColor & getColor() const
Returns the color.
Definition: MSRoute.cpp:293
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:221
virtual void compute(const E *from, const E *to, const V *const vehicle, SUMOTime msTime, std::vector< const E * > &into)=0
Builds the route between the given edges using the minimum effort at the given time The definition of...
MSRouteIterator myCurrEdge
Iterator to current route-edge.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
void vehicleDeparted(const SUMOVehicle &v)
Informs this control about a vehicle&#39;s departure.
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:106
void addReminder(MSMoveReminder *rem)
Adds a MoveReminder dynamically.
std::vector< MSDevice * > myDevices
The devices this vehicle has.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
SUMOTime myDeparture
The real departure time.
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
static const SUMOTime NOT_YET_DEPARTED
std::string id
The vehicle&#39;s id.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
const std::string & getID() const
Returns the name of the vehicle.
static bool dictionary(const std::string &id, const MSRoute *route)
Adds a route to the dictionary.
Definition: MSRoute.cpp:116
The arrival position is chosen randomly.