SUMO - Simulation of Urban MObility
ROVehicle.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A vehicle as used by router
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 
34 #include <utils/common/ToString.h>
39 #include <string>
40 #include <iostream>
41 #include "RORouteDef.h"
42 #include "ROVehicle.h"
43 #include "RORoute.h"
44 #include "ROHelper.h"
45 #include "RONet.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52  RORouteDef* route, const SUMOVTypeParameter* type,
53  const RONet* net, MsgHandler* errorHandler)
54  : RORoutable(pars, type), myRoute(route) {
55  getParameter().stops.clear();
56  if (route != 0 && route->getFirstRoute() != 0) {
57  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = route->getFirstRoute()->getStops().begin(); s != route->getFirstRoute()->getStops().end(); ++s) {
58  addStop(*s, net, errorHandler);
59  }
60  }
61  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = pars.stops.begin(); s != pars.stops.end(); ++s) {
62  addStop(*s, net, errorHandler);
63  }
64  if (pars.via.size() != 0) {
65  // via takes precedence over stop edges
66  // XXX check for inconsistencies #2275
67  myStopEdges.clear();
68  for (std::vector<std::string>::const_iterator it = pars.via.begin(); it != pars.via.end(); ++it) {
69  assert(net->getEdge(*it) != 0);
70  myStopEdges.push_back(net->getEdge(*it));
71  }
72  }
73 }
74 
75 
76 void
77 ROVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, const RONet* net, MsgHandler* errorHandler) {
78  const ROEdge* stopEdge = net->getEdgeForLaneID(stopPar.lane);
79  assert(stopEdge != 0); // was checked when parsing the stop
80  if (stopEdge->prohibits(this)) {
81  if (errorHandler != 0) {
82  errorHandler->inform("Stop edge '" + stopEdge->getID() + "' does not allow vehicle '" + getID() + "'.");
83  }
84  return;
85  }
86  // where to insert the stop
87  std::vector<SUMOVehicleParameter::Stop>::iterator iter = getParameter().stops.begin();
88  ConstROEdgeVector::iterator edgeIter = myStopEdges.begin();
89  if (stopPar.index == STOP_INDEX_END || stopPar.index >= static_cast<int>(getParameter().stops.size())) {
90  if (getParameter().stops.size() > 0) {
91  iter = getParameter().stops.end();
92  edgeIter = myStopEdges.end();
93  }
94  } else {
95  if (stopPar.index == STOP_INDEX_FIT) {
97  ConstROEdgeVector::const_iterator stopEdgeIt = std::find(edges.begin(), edges.end(), stopEdge);
98  if (stopEdgeIt == edges.end()) {
99  iter = getParameter().stops.end();
100  edgeIter = myStopEdges.end();
101  } else {
102  while (iter != getParameter().stops.end()) {
103  if (edgeIter > stopEdgeIt || (edgeIter == stopEdgeIt && iter->endPos >= stopPar.endPos)) {
104  break;
105  }
106  ++iter;
107  ++edgeIter;
108  }
109  }
110  } else {
111  iter += stopPar.index;
112  edgeIter += stopPar.index;
113  }
114  }
115  getParameter().stops.insert(iter, stopPar);
116  myStopEdges.insert(edgeIter, stopEdge);
117 }
118 
119 
121 
122 
123 const ROEdge*
125  return myRoute->getFirstRoute()->getFirst();
126 }
127 
128 
129 void
131  const bool removeLoops, MsgHandler* errorHandler) {
133  std::string noRouteMsg = "The vehicle '" + getID() + "' has no valid route.";
134  RORouteDef* const routeDef = getRouteDefinition();
135  // check if the route definition is valid
136  if (routeDef == 0) {
137  errorHandler->inform(noRouteMsg);
138  myRoutingSuccess = false;
139  return;
140  }
141  RORoute* current = routeDef->buildCurrentRoute(router, getDepartureTime(), *this);
142  if (current == 0 || current->size() == 0) {
143  delete current;
144  errorHandler->inform(noRouteMsg);
145  myRoutingSuccess = false;
146  return;
147  }
148  // check whether we have to evaluate the route for not containing loops
149  if (removeLoops) {
150  current->recheckForLoops();
151  // check whether the route is still valid
152  if (current->size() == 0) {
153  delete current;
154  errorHandler->inform(noRouteMsg + " (after removing loops)");
155  myRoutingSuccess = false;
156  return;
157  }
158  }
159  // add built route
160  routeDef->addAlternative(router, this, current, getDepartureTime());
161  myRoutingSuccess = true;
162 }
163 
164 
165 void
166 ROVehicle::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
167  if (typeos != 0 && getType() != 0 && !getType()->saved) {
168  getType()->write(*typeos);
169  getType()->saved = true;
170  }
171  if (getType() != 0 && !getType()->saved) {
172  getType()->write(os);
173  getType()->saved = asAlternatives;
174  }
175 
176  // write the vehicle (new style, with included routes)
177  getParameter().write(os, options);
178 
179  // save the route
180  myRoute->writeXMLDefinition(os, this, asAlternatives, options.getBool("exit-times"));
181  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator stop = getParameter().stops.begin(); stop != getParameter().stops.end(); ++stop) {
182  stop->write(os);
183  }
185  os.closeTag();
186 }
187 
188 
189 /****************************************************************************/
190 
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:382
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:173
Structure representing possible vehicle parameter.
const SUMOVehicleParameter & getParameter() const
Returns the definition of the vehicle / person parameter.
Definition: RORoutable.h:80
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:289
const RORoute * getFirstRoute() const
Definition: RORouteDef.h:107
bool prohibits(const ROVehicle *const vehicle) const
Returns whether this edge prohibits the given vehicle to pass it.
Definition: ROEdge.h:269
void recheckForLoops()
Checks whether this route contains loops and removes such.
Definition: RORoute.cpp:84
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:100
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:188
const std::string & getID() const
Returns the id.
Definition: Named.h:65
ROVehicle(const SUMOVehicleParameter &pars, RORouteDef *route, const SUMOVTypeParameter *type, const RONet *net, MsgHandler *errorHandler=0)
Constructor.
Definition: ROVehicle.cpp:51
RORouteDef *const myRoute
The route the vehicle takes.
Definition: ROVehicle.h:143
std::vector< Stop > stops
List of the stops the vehicle will make, TraCI may add entries here.
void writeParams(OutputDevice &out) const
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:90
void addStop(const SUMOVehicleParameter::Stop &stopPar, const RONet *net, MsgHandler *errorHandler)
Adds a stop to this vehicle.
Definition: ROVehicle.cpp:77
A routable thing such as a vehicle or person.
Definition: RORoutable.h:61
const ROEdge * getDepartEdge() const
Returns the first edge the vehicle takes.
Definition: ROVehicle.cpp:124
const int STOP_INDEX_END
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:190
void write(OutputDevice &dev, const OptionsCont &oc, const SumoXMLTag tag=SUMO_TAG_VEHICLE, const std::string &typeID="") const
Writes the parameters as a beginning element.
double endPos
The stopping position end.
const std::string & getID() const
Returns the id of the routable.
Definition: RORoutable.h:100
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at, 0 for triggered vehicles.
Definition: ROVehicle.h:101
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:82
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
int size() const
Returns the number of edges in this route.
Definition: RORoute.h:152
void write(OutputDevice &dev) const
Writes the vtype.
The router&#39;s network representation.
Definition: RONet.h:74
Structure representing possible vehicle parameter.
const SUMOVTypeParameter * getType() const
Returns the type of the routable.
Definition: RORoutable.h:91
ConstROEdgeVector myStopEdges
The edges where the vehicle stops.
Definition: ROVehicle.h:146
Definition of vehicle stop (position and duration)
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:161
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:84
A storage for options typed value containers)
Definition: OptionsCont.h:98
int index
at which position in the stops list
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROVehicle.cpp:130
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:62
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:163
virtual ~ROVehicle()
Destructor.
Definition: ROVehicle.cpp:120
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete vehicle description.
Definition: ROVehicle.cpp:166
A complete router&#39;s route.
Definition: RORoute.h:61