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  if (pars.via.size() != 0) {
70  // via takes precedence over stop edges
71  // XXX check for inconsistencies #2275
72  myStopEdges.clear();
73  for (std::vector<std::string>::const_iterator it = pars.via.begin(); it != pars.via.end(); ++it) {
74  assert(net->getEdge(*it) != 0);
75  myStopEdges.push_back(net->getEdge(*it));
76  }
77  }
78 }
79 
80 
81 void
82 ROVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, const RONet* net, MsgHandler* errorHandler) {
83  const ROEdge* stopEdge = net->getEdgeForLaneID(stopPar.lane);
84  assert(stopEdge != 0); // was checked when parsing the stop
85  if (stopEdge->prohibits(this)) {
86  if (errorHandler != 0) {
87  errorHandler->inform("Stop edge '" + stopEdge->getID() + "' does not allow vehicle '" + getID() + "'.");
88  }
89  return;
90  }
91  // where to insert the stop
92  std::vector<SUMOVehicleParameter::Stop>::iterator iter = myParameter.stops.begin();
93  ConstROEdgeVector::iterator edgeIter = myStopEdges.begin();
94  if (stopPar.index == STOP_INDEX_END || stopPar.index >= static_cast<int>(myParameter.stops.size())) {
95  if (myParameter.stops.size() > 0) {
96  iter = myParameter.stops.end();
97  edgeIter = myStopEdges.end();
98  }
99  } else {
100  if (stopPar.index == STOP_INDEX_FIT) {
102  ConstROEdgeVector::const_iterator stopEdgeIt = std::find(edges.begin(), edges.end(), stopEdge);
103  if (stopEdgeIt == edges.end()) {
104  iter = myParameter.stops.end();
105  edgeIter = myStopEdges.end();
106  } else {
107  while (iter != myParameter.stops.end()) {
108  if (edgeIter > stopEdgeIt || (edgeIter == stopEdgeIt && iter->endPos >= stopPar.endPos)) {
109  break;
110  }
111  ++iter;
112  ++edgeIter;
113  }
114  }
115  } else {
116  iter += stopPar.index;
117  edgeIter += stopPar.index;
118  }
119  }
120  myParameter.stops.insert(iter, stopPar);
121  myStopEdges.insert(edgeIter, stopEdge);
122 }
123 
124 
126 
127 
128 const ROEdge*
130  return myRoute->getFirstRoute()->getFirst();
131 }
132 
133 
134 void
136  const bool removeLoops, MsgHandler* errorHandler) {
138  std::string noRouteMsg = "The vehicle '" + getID() + "' has no valid route.";
139  RORouteDef* const routeDef = getRouteDefinition();
140  // check if the route definition is valid
141  if (routeDef == 0) {
142  errorHandler->inform(noRouteMsg);
143  myRoutingSuccess = false;
144  return;
145  }
146  RORoute* current = routeDef->buildCurrentRoute(router, getDepartureTime(), *this);
147  if (current == 0 || current->size() == 0) {
148  delete current;
149  errorHandler->inform(noRouteMsg);
150  myRoutingSuccess = false;
151  return;
152  }
153  // check whether we have to evaluate the route for not containing loops
154  if (removeLoops) {
155  current->recheckForLoops();
156  // check whether the route is still valid
157  if (current->size() == 0) {
158  delete current;
159  errorHandler->inform(noRouteMsg + " (after removing loops)");
160  myRoutingSuccess = false;
161  return;
162  }
163  }
164  // add built route
165  routeDef->addAlternative(router, this, current, getDepartureTime());
166  myRoutingSuccess = true;
167 }
168 
169 
170 void
171 ROVehicle::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
172  if (typeos != 0 && myType != 0 && !myType->saved) {
173  myType->write(*typeos);
174  myType->saved = true;
175  }
176  if (myType != 0 && !myType->saved) {
177  myType->write(os);
178  myType->saved = asAlternatives;
179  }
180 
181  // write the vehicle (new style, with included routes)
182  myParameter.write(os, options);
183 
184  // save the route
185  myRoute->writeXMLDefinition(os, this, asAlternatives, options.getBool("exit-times"));
186  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator stop = myParameter.stops.begin(); stop != myParameter.stops.end(); ++stop) {
187  stop->write(os);
188  }
189  for (std::map<std::string, std::string>::const_iterator j = myParameter.getMap().begin(); j != myParameter.getMap().end(); ++j) {
191  os.writeAttr(SUMO_ATTR_KEY, (*j).first);
192  os.writeAttr(SUMO_ATTR_VALUE, (*j).second);
193  os.closeTag();
194  }
195  os.closeTag();
196 }
197 
198 
199 /****************************************************************************/
200 
SUMOVehicleParameter myParameter
The vehicle&#39;s parameter.
Definition: RORoutable.h:165
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:377
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:175
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:284
const RORoute * getFirstRoute() const
Definition: RORouteDef.h:108
bool prohibits(const ROVehicle *const vehicle) const
Returns whether this edge prohibits the given vehicle to pass it.
Definition: ROEdge.h:253
void recheckForLoops()
Checks whether this route contains loops and removes such.
Definition: RORoute.cpp:89
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:101
const int STOP_INDEX_FIT
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:62
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
bool myRoutingSuccess
Whether the last routing was successful.
Definition: RORoutable.h:171
const std::string & getID() const
Returns the id.
Definition: Named.h:66
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.
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
void addStop(const SUMOVehicleParameter::Stop &stopPar, const RONet *net, MsgHandler *errorHandler)
Adds a stop to this vehicle.
Definition: ROVehicle.cpp:82
A routable thing such as a vehicle or person.
Definition: RORoutable.h:62
const ROEdge * getDepartEdge() const
Returns the first edge the vehicle takes.
Definition: ROVehicle.cpp:129
SUMOReal endPos
The stopping position end.
void write(OutputDevice &dev, const OptionsCont &oc, const SumoXMLTag tag=SUMO_TAG_VEHICLE) const
Writes the parameters as a beginning element.
const int STOP_INDEX_END
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:191
const std::string & getID() const
Returns the id of the vehicle.
Definition: RORoutable.h:92
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at, 0 for triggered vehicles.
Definition: ROVehicle.h:111
A basic edge for routing applications.
Definition: ROEdge.h:77
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:83
std::string lane
The lane to stop at.
std::vector< std::string > via
List of the via-edges the vehicle must visit.
SUMOAbstractRouter< E, V > & getVehicleRouter() const
const SUMOVTypeParameter *const myType
The type of the vehicle.
Definition: RORoutable.h:168
int size() const
Returns the number of edges in this route.
Definition: RORoute.h:153
void write(OutputDevice &dev) const
Writes the vtype.
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
const std::map< std::string, std::string > & getMap() const
Returns the inner key/value map.
Definition of vehicle stop (position and duration)
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:162
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:99
int index
at which position in the stops list
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROVehicle.cpp:135
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.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:165
virtual ~ROVehicle()
Destructor.
Definition: ROVehicle.cpp:125
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete vehicle description.
Definition: ROVehicle.cpp:171
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router&#39;s route.
Definition: RORoute.h:62