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-2017 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 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  RORouteDef* route, const SUMOVTypeParameter* type,
54  const RONet* net, MsgHandler* errorHandler)
55  : RORoutable(pars, type), myRoute(route) {
56  myParameter.stops.clear();
57  if (route != 0 && route->getFirstRoute() != 0) {
58  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = route->getFirstRoute()->getStops().begin(); s != route->getFirstRoute()->getStops().end(); ++s) {
59  addStop(*s, net, errorHandler);
60  }
61  }
62  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator s = pars.stops.begin(); s != pars.stops.end(); ++s) {
63  addStop(*s, net, errorHandler);
64  }
65  if (pars.via.size() != 0) {
66  // via takes precedence over stop edges
67  // XXX check for inconsistencies #2275
68  myStopEdges.clear();
69  for (std::vector<std::string>::const_iterator it = pars.via.begin(); it != pars.via.end(); ++it) {
70  assert(net->getEdge(*it) != 0);
71  myStopEdges.push_back(net->getEdge(*it));
72  }
73  }
74 }
75 
76 
77 void
78 ROVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, const RONet* net, MsgHandler* errorHandler) {
79  const ROEdge* stopEdge = net->getEdgeForLaneID(stopPar.lane);
80  assert(stopEdge != 0); // was checked when parsing the stop
81  if (stopEdge->prohibits(this)) {
82  if (errorHandler != 0) {
83  errorHandler->inform("Stop edge '" + stopEdge->getID() + "' does not allow vehicle '" + getID() + "'.");
84  }
85  return;
86  }
87  // where to insert the stop
88  std::vector<SUMOVehicleParameter::Stop>::iterator iter = myParameter.stops.begin();
89  ConstROEdgeVector::iterator edgeIter = myStopEdges.begin();
90  if (stopPar.index == STOP_INDEX_END || stopPar.index >= static_cast<int>(myParameter.stops.size())) {
91  if (myParameter.stops.size() > 0) {
92  iter = myParameter.stops.end();
93  edgeIter = myStopEdges.end();
94  }
95  } else {
96  if (stopPar.index == STOP_INDEX_FIT) {
98  ConstROEdgeVector::const_iterator stopEdgeIt = std::find(edges.begin(), edges.end(), stopEdge);
99  if (stopEdgeIt == edges.end()) {
100  iter = myParameter.stops.end();
101  edgeIter = myStopEdges.end();
102  } else {
103  while (iter != myParameter.stops.end()) {
104  if (edgeIter > stopEdgeIt || (edgeIter == stopEdgeIt && iter->endPos >= stopPar.endPos)) {
105  break;
106  }
107  ++iter;
108  ++edgeIter;
109  }
110  }
111  } else {
112  iter += stopPar.index;
113  edgeIter += stopPar.index;
114  }
115  }
116  myParameter.stops.insert(iter, stopPar);
117  myStopEdges.insert(edgeIter, stopEdge);
118 }
119 
120 
122 
123 
124 const ROEdge*
126  return myRoute->getFirstRoute()->getFirst();
127 }
128 
129 
130 void
132  const bool removeLoops, MsgHandler* errorHandler) {
134  std::string noRouteMsg = "The vehicle '" + getID() + "' has no valid route.";
135  RORouteDef* const routeDef = getRouteDefinition();
136  // check if the route definition is valid
137  if (routeDef == 0) {
138  errorHandler->inform(noRouteMsg);
139  myRoutingSuccess = false;
140  return;
141  }
142  RORoute* current = routeDef->buildCurrentRoute(router, getDepartureTime(), *this);
143  if (current == 0 || current->size() == 0) {
144  delete current;
145  errorHandler->inform(noRouteMsg);
146  myRoutingSuccess = false;
147  return;
148  }
149  // check whether we have to evaluate the route for not containing loops
150  if (removeLoops) {
151  current->recheckForLoops();
152  // check whether the route is still valid
153  if (current->size() == 0) {
154  delete current;
155  errorHandler->inform(noRouteMsg + " (after removing loops)");
156  myRoutingSuccess = false;
157  return;
158  }
159  }
160  // add built route
161  routeDef->addAlternative(router, this, current, getDepartureTime());
162  myRoutingSuccess = true;
163 }
164 
165 
166 void
167 ROVehicle::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
168  if (typeos != 0 && myType != 0 && !myType->saved) {
169  myType->write(*typeos);
170  myType->saved = true;
171  }
172  if (myType != 0 && !myType->saved) {
173  myType->write(os);
174  myType->saved = asAlternatives;
175  }
176 
177  // write the vehicle (new style, with included routes)
178  myParameter.write(os, options);
179 
180  // save the route
181  myRoute->writeXMLDefinition(os, this, asAlternatives, options.getBool("exit-times"));
182  for (std::vector<SUMOVehicleParameter::Stop>::const_iterator stop = myParameter.stops.begin(); stop != myParameter.stops.end(); ++stop) {
183  stop->write(os);
184  }
186  os.closeTag();
187 }
188 
189 
190 /****************************************************************************/
191 
SUMOVehicleParameter myParameter
The vehicle&#39;s parameter.
Definition: RORoutable.h:165
OutputDevice & writeXMLDefinition(OutputDevice &dev, const ROVehicle *const veh, bool asAlternatives, bool withExitTimes) const
Saves the built route / route alternatives.
Definition: RORouteDef.cpp:373
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:280
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:263
void recheckForLoops()
Checks whether this route contains loops and removes such.
Definition: RORoute.cpp:85
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:52
RORouteDef *const myRoute
The route the vehicle takes.
Definition: ROVehicle.h:153
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:91
void addStop(const SUMOVehicleParameter::Stop &stopPar, const RONet *net, MsgHandler *errorHandler)
Adds a stop to this vehicle.
Definition: ROVehicle.cpp:78
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:125
const int STOP_INDEX_END
const std::vector< SUMOVehicleParameter::Stop > & getStops() const
Returns the list of stops this route contains.
Definition: RORoute.h:191
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 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
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:85
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:131
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:121
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete vehicle description.
Definition: ROVehicle.cpp:167
A complete router&#39;s route.
Definition: RORoute.h:62