SUMO - Simulation of Urban MObility
ROVehicle.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A vehicle as used by router
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2002-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
35 #include <utils/common/ToString.h>
40 #include <string>
41 #include <iostream>
42 #include "RORouteDef.h"
43 #include "ROVehicle.h"
44 #include "RORoute.h"
45 #include "ROHelper.h"
46 #include "RONet.h"
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
57  RORouteDef* route, const SUMOVTypeParameter* type,
58  const RONet* net, MsgHandler* errorHandler)
59  : RORoutable(pars, type), myRoute(route) {
60  myParameter.stops.clear();
61  if (route != 0 && route->getFirstRoute() != 0) {
62  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = route->getFirstRoute()->getStops().begin(); s != route->getFirstRoute()->getStops().end(); ++s) {
63  addStop(*s, net, errorHandler);
64  }
65  }
66  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = pars.stops.begin(); s != pars.stops.end(); ++s) {
67  addStop(*s, net, errorHandler);
68  }
69 }
70 
71 
72 void
73 ROVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, const RONet* net, MsgHandler* errorHandler) {
74  const ROEdge* stopEdge = net->getEdgeForLaneID(stopPar.lane);
75  assert(stopEdge != 0); // was checked when parsing the stop
76  if (stopEdge->prohibits(this)) {
77  if (errorHandler != 0) {
78  errorHandler->inform("Stop edge '" + stopEdge->getID() + "' does not allow vehicle '" + getID() + "'.");
79  }
80  return;
81  }
82  // where to insert the stop
83  std::vector<SUMOVehicleParameter::Stop>::iterator iter = myParameter.stops.begin();
84  ConstROEdgeVector::iterator edgeIter = myStopEdges.begin();
85  if (stopPar.index == STOP_INDEX_END || stopPar.index >= static_cast<int>(myParameter.stops.size())) {
86  if (myParameter.stops.size() > 0) {
87  iter = myParameter.stops.end();
88  edgeIter = myStopEdges.end();
89  }
90  } else {
91  if (stopPar.index == STOP_INDEX_FIT) {
93  ConstROEdgeVector::const_iterator stopEdgeIt = std::find(edges.begin(), edges.end(), stopEdge);
94  if (stopEdgeIt == edges.end()) {
95  iter = myParameter.stops.end();
96  edgeIter = myStopEdges.end();
97  } else {
98  while (iter != myParameter.stops.end()) {
99  if (edgeIter > stopEdgeIt || (edgeIter == stopEdgeIt && iter->endPos >= stopPar.endPos)) {
100  break;
101  }
102  ++iter;
103  ++edgeIter;
104  }
105  }
106  } else {
107  iter += stopPar.index;
108  edgeIter += stopPar.index;
109  }
110  }
111  myParameter.stops.insert(iter, stopPar);
112  myStopEdges.insert(edgeIter, stopEdge);
113 }
114 
115 
117 
118 
119 const ROEdge*
121  return myRoute->getFirstRoute()->getFirst();
122 }
123 
124 
125 void
127  const bool removeLoops, MsgHandler* errorHandler) {
129  std::string noRouteMsg = "The vehicle '" + getID() + "' has no valid route.";
130  RORouteDef* const routeDef = getRouteDefinition();
131  // check if the route definition is valid
132  if (routeDef == 0) {
133  errorHandler->inform(noRouteMsg);
134  myRoutingSuccess = false;
135  return;
136  }
137  RORoute* current = routeDef->buildCurrentRoute(router, getDepartureTime(), *this);
138  if (current == 0 || current->size() == 0) {
139  delete current;
140  errorHandler->inform(noRouteMsg);
141  myRoutingSuccess = false;
142  return;
143  }
144  // check whether we have to evaluate the route for not containing loops
145  if (removeLoops) {
146  current->recheckForLoops();
147  // check whether the route is still valid
148  if (current->size() == 0) {
149  delete current;
150  errorHandler->inform(noRouteMsg + " (after removing loops)");
151  myRoutingSuccess = false;
152  return;
153  }
154  }
155  // add built route
156  routeDef->addAlternative(router, this, current, getDepartureTime());
157  myRoutingSuccess = true;
158 }
159 
160 
161 void
162 ROVehicle::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
163  if (typeos != 0 && myType != 0 && !myType->saved) {
164  myType->write(*typeos);
165  myType->saved = true;
166  }
167  if (myType != 0 && !myType->saved) {
168  myType->write(os);
169  myType->saved = asAlternatives;
170  }
171 
172  // write the vehicle (new style, with included routes)
173  myParameter.write(os, options);
174 
175  // save the route
176  myRoute->writeXMLDefinition(os, this, asAlternatives, options.getBool("exit-times"));
177  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator stop = myParameter.stops.begin(); stop != myParameter.stops.end(); ++stop) {
178  stop->write(os);
179  }
180  for (std::map<std::string, std::string>::const_iterator j = myParameter.getMap().begin(); j != myParameter.getMap().end(); ++j) {
182  os.writeAttr(SUMO_ATTR_KEY, (*j).first);
183  os.writeAttr(SUMO_ATTR_VALUE, (*j).second);
184  os.closeTag();
185  }
186  os.closeTag();
187 }
188 
189 
190 /****************************************************************************/
191 
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at, 0 for triggered vehicles.
Definition: ROVehicle.h:111
SUMOVehicleParameter myParameter
The vehicle&#39;s parameter.
Definition: RORoutable.h:165
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete vehicle description.
Definition: ROVehicle.cpp:162
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
unsigned int size() const
Returns the number of edges in this route.
Definition: RORoute.h:153
Structure representing possible vehicle parameter.
bool saved
Information whether this type was already saved (needed by routers)
void addAlternative(SUMOAbstractRouter< ROEdge, ROVehicle > &router, const ROVehicle *const, RORoute *current, SUMOTime begin)
Adds an alternative to the list of routes.
Definition: RORouteDef.cpp:288
RORoute * buildCurrentRoute(SUMOAbstractRouter< ROEdge, ROVehicle > &router, SUMOTime begin, const ROVehicle &veh) const
Triggers building of the complete route (via preComputeCurrentRoute) or returns precomputed route...
Definition: RORouteDef.cpp:95
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:162
void recheckForLoops()
Checks whether this route contains loops and removes such.
Definition: RORoute.cpp:89
const int STOP_INDEX_FIT
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:62
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:171
const ROEdge * getDepartEdge() const
Returns the first edge the vehicle takes.
Definition: ROVehicle.cpp:120
ROVehicle(const SUMOVehicleParameter &pars, RORouteDef *route, const SUMOVTypeParameter *type, const RONet *net, MsgHandler *errorHandler=0)
Constructor.
Definition: ROVehicle.cpp:56
RORouteDef *const myRoute
The route the vehicle takes.
Definition: ROVehicle.h:153
std::vector< Stop > stops
List of the stops the vehicle will make.
void addStop(const SUMOVehicleParameter::Stop &stopPar, const RONet *net, MsgHandler *errorHandler)
Adds a stop to this vehicle.
Definition: ROVehicle.cpp:73
A routable thing such as a vehicle or person.
Definition: RORoutable.h:62
const std::string & getID() const
Returns the id.
Definition: Named.h:65
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:381
SUMOReal endPos
The stopping position end.
const int STOP_INDEX_END
void write(OutputDevice &dev, const OptionsCont &oc, const SumoXMLTag tag=SUMO_TAG_VEHICLE) const
Writes the parameters as a beginning element.
SUMOAbstractRouter< E, V > & getVehicleRouter() const
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:101
A basic edge for routing applications.
Definition: ROEdge.h:77
std::string lane
The lane to stop at.
void write(OutputDevice &dev) const
Writes the vtype.
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:175
const SUMOVTypeParameter *const myType
The type of the vehicle.
Definition: RORoutable.h:168
The router&#39;s network representation.
Definition: RONet.h:76
Structure representing possible vehicle parameter.
ConstROEdgeVector myStopEdges
The edges where the vehicle stops.
Definition: ROVehicle.h:156
bool prohibits(const ROVehicle *const vehicle) const
Returns whether this edge prohibits the given vehicle to pass it.
Definition: ROEdge.h:239
const std::string & getID() const
Returns the id of the vehicle.
Definition: RORoutable.h:92
Definition of vehicle stop (position and duration)
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
int index
at which position in the stops list
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROVehicle.cpp:126
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:63
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
const RORoute * getFirstRoute() const
Definition: RORouteDef.h:108
virtual ~ROVehicle()
Destructor.
Definition: ROVehicle.cpp:116
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:83
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:191
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router&#39;s route.
Definition: RORoute.h:62