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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // A vehicle as used by router
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
28 #include <utils/common/ToString.h>
33 #include <string>
34 #include <iostream>
35 #include "RORouteDef.h"
36 #include "ROVehicle.h"
37 #include "RORoute.h"
38 #include "ROHelper.h"
39 #include "RONet.h"
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46  RORouteDef* route, const SUMOVTypeParameter* type,
47  const RONet* net, MsgHandler* errorHandler)
48  : RORoutable(pars, type), myRoute(route) {
49  getParameter().stops.clear();
50  if (route != nullptr && route->getFirstRoute() != nullptr) {
51  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = route->getFirstRoute()->getStops().begin(); s != route->getFirstRoute()->getStops().end(); ++s) {
52  addStop(*s, net, errorHandler);
53  }
54  }
55  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = pars.stops.begin(); s != pars.stops.end(); ++s) {
56  addStop(*s, net, errorHandler);
57  }
58  if (pars.via.size() != 0) {
59  // via takes precedence over stop edges
60  // XXX check for inconsistencies #2275
61  myStopEdges.clear();
62  for (std::vector<std::string>::const_iterator it = pars.via.begin(); it != pars.via.end(); ++it) {
63  assert(net->getEdge(*it) != 0);
64  myStopEdges.push_back(net->getEdge(*it));
65  }
66  }
67 }
68 
69 
70 void
71 ROVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, const RONet* net, MsgHandler* errorHandler) {
72  const ROEdge* stopEdge = net->getEdgeForLaneID(stopPar.lane);
73  assert(stopEdge != 0); // was checked when parsing the stop
74  if (stopEdge->prohibits(this)) {
75  if (errorHandler != nullptr) {
76  errorHandler->inform("Stop edge '" + stopEdge->getID() + "' does not allow vehicle '" + getID() + "'.");
77  }
78  return;
79  }
80  // where to insert the stop
81  std::vector<SUMOVehicleParameter::Stop>::iterator iter = getParameter().stops.begin();
82  ConstROEdgeVector::iterator edgeIter = myStopEdges.begin();
83  if (stopPar.index == STOP_INDEX_END || stopPar.index >= static_cast<int>(getParameter().stops.size())) {
84  if (getParameter().stops.size() > 0) {
85  iter = getParameter().stops.end();
86  edgeIter = myStopEdges.end();
87  }
88  } else {
89  if (stopPar.index == STOP_INDEX_FIT) {
91  ConstROEdgeVector::const_iterator stopEdgeIt = std::find(edges.begin(), edges.end(), stopEdge);
92  if (stopEdgeIt == edges.end()) {
93  iter = getParameter().stops.end();
94  edgeIter = myStopEdges.end();
95  } else {
96  while (iter != getParameter().stops.end()) {
97  if (edgeIter > stopEdgeIt || (edgeIter == stopEdgeIt && iter->endPos >= stopPar.endPos)) {
98  break;
99  }
100  ++iter;
101  ++edgeIter;
102  }
103  }
104  } else {
105  iter += stopPar.index;
106  edgeIter += stopPar.index;
107  }
108  }
109  getParameter().stops.insert(iter, stopPar);
110  myStopEdges.insert(edgeIter, stopEdge);
111 }
112 
113 
115 
116 
117 const ROEdge*
119  return myRoute->getFirstRoute()->getFirst();
120 }
121 
122 
123 void
125  const bool removeLoops, MsgHandler* errorHandler) {
127  std::string noRouteMsg = "The vehicle '" + getID() + "' has no valid route.";
128  RORouteDef* const routeDef = getRouteDefinition();
129  // check if the route definition is valid
130  if (routeDef == nullptr) {
131  errorHandler->inform(noRouteMsg);
132  myRoutingSuccess = false;
133  return;
134  }
135  RORoute* current = routeDef->buildCurrentRoute(router, getDepartureTime(), *this);
136  if (current == nullptr || current->size() == 0) {
137  delete current;
138  errorHandler->inform(noRouteMsg);
139  myRoutingSuccess = false;
140  return;
141  }
142  // check whether we have to evaluate the route for not containing loops
143  if (removeLoops) {
144  const ROEdge* requiredStart = (getParameter().departPosProcedure == DEPART_POS_GIVEN
145  || getParameter().departLaneProcedure == DEPART_LANE_GIVEN ? current->getEdgeVector().front() : 0);
146  const ROEdge* requiredEnd = (getParameter().arrivalPosProcedure == ARRIVAL_POS_GIVEN
147  || getParameter().arrivalLaneProcedure == ARRIVAL_LANE_GIVEN ? current->getEdgeVector().back() : 0);
148  current->recheckForLoops(getMandatoryEdges(requiredStart, requiredEnd));
149  // check whether the route is still valid
150  if (current->size() == 0) {
151  delete current;
152  errorHandler->inform(noRouteMsg + " (after removing loops)");
153  myRoutingSuccess = false;
154  return;
155  }
156  }
157  // add built route
158  routeDef->addAlternative(router, this, current, getDepartureTime());
159  myRoutingSuccess = true;
160 }
161 
162 
164 ROVehicle::getMandatoryEdges(const ROEdge* requiredStart, const ROEdge* requiredEnd) const {
165  ConstROEdgeVector mandatory;
166  if (requiredStart) {
167  mandatory.push_back(requiredStart);
168  }
169  for (const ROEdge* e : getStopEdges()) {
170  if (e->isInternal()) {
171  // the edges before and after the internal edge are mandatory
172  const ROEdge* before = e->getNormalBefore();
173  const ROEdge* after = e->getNormalAfter();
174  if (mandatory.size() == 0 || after != mandatory.back()) {
175  mandatory.push_back(before);
176  mandatory.push_back(after);
177  }
178  } else {
179  if (mandatory.size() == 0 || e != mandatory.back()) {
180  mandatory.push_back(e);
181  }
182  }
183  }
184  if (requiredEnd) {
185  if (mandatory.size() < 2 || mandatory.back() != requiredEnd) {
186  mandatory.push_back(requiredEnd);
187  }
188  }
189  return mandatory;
190 }
191 
192 
193 void
194 ROVehicle::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
195  if (typeos != nullptr && getType() != nullptr && !getType()->saved) {
196  getType()->write(*typeos);
197  getType()->saved = true;
198  }
199  if (getType() != nullptr && !getType()->saved) {
200  getType()->write(os);
201  getType()->saved = asAlternatives;
202  }
203 
204  // write the vehicle (new style, with included routes)
205  getParameter().write(os, options);
206 
207  // save the route
208  myRoute->writeXMLDefinition(os, this, asAlternatives, options.getBool("exit-times"));
209  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator stop = getParameter().stops.begin(); stop != getParameter().stops.end(); ++stop) {
210  stop->write(os);
211  }
213  os.closeTag();
214 }
215 
216 
217 /****************************************************************************/
218 
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:355
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:167
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
Structure representing possible vehicle parameter.
const SUMOVehicleParameter & getParameter() const
Returns the definition of the vehicle / person parameter.
Definition: RORoutable.h:74
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:262
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
const RORoute * getFirstRoute() const
Definition: RORouteDef.h:101
The position is given.
bool prohibits(const ROVehicle *const vehicle) const
Returns whether this edge prohibits the given vehicle to pass it.
Definition: ROEdge.h:264
const ROEdge * getFirst() const
Returns the first edge in the route.
Definition: RORoute.h:94
const int STOP_INDEX_FIT
std::vector< const ROEdge * > ConstROEdgeVector
Definition: ROEdge.h:56
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:182
const std::string & getID() const
Returns the id.
Definition: Named.h:78
The arrival position is given.
ROVehicle(const SUMOVehicleParameter &pars, RORouteDef *route, const SUMOVTypeParameter *type, const RONet *net, MsgHandler *errorHandler=0)
Constructor.
Definition: ROVehicle.cpp:45
RORouteDef *const myRoute
The route the vehicle takes.
Definition: ROVehicle.h:141
The lane is given.
std::vector< Stop > stops
List of the stops the vehicle will make, TraCI may add entries here.
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:84
void addStop(const SUMOVehicleParameter::Stop &stopPar, const RONet *net, MsgHandler *errorHandler)
Adds a stop to this vehicle.
Definition: ROVehicle.cpp:71
A routable thing such as a vehicle or person.
Definition: RORoutable.h:55
const ROEdge * getDepartEdge() const
Returns the first edge the vehicle takes.
Definition: ROVehicle.cpp:118
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
const ROEdge * getNormalAfter() const
if this edge is an internal edge, return its first normal successor, otherwise the edge itself ...
Definition: ROEdge.cpp:264
ConstROEdgeVector getMandatoryEdges(const ROEdge *requiredStart, const ROEdge *requiredEnd) const
compute mandatory edges
Definition: ROVehicle.cpp:164
const int STOP_INDEX_END
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:184
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.
const ConstROEdgeVector & getStopEdges() const
Definition: ROVehicle.h:100
double endPos
The stopping position end.
const std::string & getID() const
Returns the id of the routable.
Definition: RORoutable.h:94
SUMOTime getDepartureTime() const
Returns the time the vehicle starts at, 0 for triggered vehicles.
Definition: ROVehicle.h:95
A basic edge for routing applications.
Definition: ROEdge.h:72
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:76
void writeParams(OutputDevice &device) const
write Params in the given outputdevice
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:146
void write(OutputDevice &dev) const
Writes the vtype.
The router&#39;s network representation.
Definition: RONet.h:68
Structure representing possible vehicle parameter.
const SUMOVTypeParameter * getType() const
Returns the type of the routable.
Definition: RORoutable.h:85
ConstROEdgeVector myStopEdges
The edges where the vehicle stops.
Definition: ROVehicle.h:144
Definition of vehicle stop (position and duration)
const ConstROEdgeVector & getEdgeVector() const
Returns the list of edges this route consists of.
Definition: RORoute.h:155
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:113
A storage for options typed value containers)
Definition: OptionsCont.h:92
int index
at which position in the stops list
The arrival lane is given.
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROVehicle.cpp:124
Base class for a vehicle&#39;s route definition.
Definition: RORouteDef.h:56
void recheckForLoops(const ConstROEdgeVector &mandatory)
Checks whether this route contains loops and removes such.
Definition: RORoute.cpp:78
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
const ROEdge * getNormalBefore() const
if this edge is an internal edge, return its first normal predecessor, otherwise the edge itself ...
Definition: ROEdge.cpp:253
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:157
virtual ~ROVehicle()
Destructor.
Definition: ROVehicle.cpp:114
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete vehicle description.
Definition: ROVehicle.cpp:194
A complete router&#39;s route.
Definition: RORoute.h:55
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.