SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ROPerson.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>
41 #include <string>
42 #include <iostream>
43 #include "RORouteDef.h"
44 #include "ROPerson.h"
45 #include "RORoute.h"
46 #include "ROVehicle.h"
47 #include "ROHelper.h"
48 #include "RONet.h"
49 
50 #ifdef CHECK_MEMORY_LEAKS
51 #include <foreign/nvwa/debug_new.h>
52 #endif // CHECK_MEMORY_LEAKS
53 
54 
55 // ===========================================================================
56 // method definitions
57 // ===========================================================================
59  : RORoutable(pars, type) {
60 }
61 
62 
64  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
65  delete *it;
66  }
67 }
68 
69 
70 void
71 ROPerson::addTrip(const ROEdge* const from, const ROEdge* const to, const SVCPermissions modeSet,
72  const std::string& vTypes, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string& busStop) {
73  PersonTrip* trip = new PersonTrip(from, to, modeSet, departPos, arrivalPos, busStop);
74  RONet* net = RONet::getInstance();
77 
78  for (StringTokenizer st(vTypes); st.hasNext();) {
79  pars.vtypeid = st.next();
82  if (type == 0) {
83  delete trip;
84  throw InvalidArgument("The vehicle type '" + pars.vtypeid + "' in a trip for person '" + getID() + "' is not known.");
85  }
86  pars.id = getID() + "_" + toString(trip->getVehicles().size());
87  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), type, net));
88  }
89  if ((modeSet & SVC_PASSENGER) != 0 && trip->getVehicles().empty()) {
90  pars.id = getID() + "_0";
91  trip->addVehicle(new ROVehicle(pars, new RORouteDef("!" + pars.id, 0, false, false), net->getVehicleTypeSecure(DEFAULT_VTYPE_ID), net));
92  }
93  myPlan.push_back(trip);
94 }
95 
96 
97 void
98 ROPerson::addRide(const ROEdge* const from, const ROEdge* const to, const std::string& lines, const std::string& destStop) {
99  if (myPlan.empty() || myPlan.back()->isStop()) {
100  myPlan.push_back(new PersonTrip());
101  }
102  myPlan.back()->addTripItem(new Ride(from, to, lines, destStop));
103 }
104 
105 
106 void
107 ROPerson::addWalk(const ConstROEdgeVector& edges, const SUMOReal duration, const SUMOReal speed, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string& busStop) {
108  if (myPlan.empty() || myPlan.back()->isStop()) {
109  myPlan.push_back(new PersonTrip());
110  }
111  myPlan.back()->addTripItem(new Walk(edges, duration, speed, departPos, arrivalPos, busStop));
112 }
113 
114 
115 void
116 ROPerson::addStop(const SUMOVehicleParameter::Stop& stopPar, const ROEdge* const stopEdge) {
117  myPlan.push_back(new Stop(stopPar, stopEdge));
118 }
119 
120 
121 void
124  if (from != 0) {
126  }
127  if (to != 0) {
128  os.writeAttr(SUMO_ATTR_TO, to->getID());
129  }
130  if (destStop != "") {
132  }
134  os.closeTag();
135 }
136 
137 
138 void
141  if (dur > 0) {
142  os.writeAttr(SUMO_ATTR_DURATION, dur);
143  }
144  if (v > 0) {
145  os.writeAttr(SUMO_ATTR_SPEED, v);
146  }
147  os.writeAttr(SUMO_ATTR_EDGES, edges);
148  if (dep != std::numeric_limits<SUMOReal>::infinity()) {
150  }
151  if (arr != std::numeric_limits<SUMOReal>::infinity()) {
153  }
154  if (destStop != "") {
155  os.writeAttr(SUMO_ATTR_BUS_STOP, destStop);
156  }
157  os.closeTag();
158 }
159 
160 
161 void
162 ROPerson::PersonTrip::saveVehicles(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
163  for (std::vector<ROVehicle*>::const_iterator it = myVehicles.begin(); it != myVehicles.end(); ++it) {
164  (*it)->saveAsXML(os, typeos, asAlternatives, options);
165  }
166 }
167 
168 
169 bool
170 ROPerson::computeIntermodal(const RORouterProvider& provider, PersonTrip* const trip, const ROVehicle* const veh, MsgHandler* const errorHandler) {
171  std::vector<ROIntermodalRouter::TripItem> result;
172  provider.getIntermodalRouter().compute(trip->getOrigin(), trip->getDestination(), trip->getDepartPos(), trip->getArrivalPos(),
173  myType->maxSpeed, veh, trip->getModes(), 0, result);
174  bool carUsed = false;
175  for (std::vector<ROIntermodalRouter::TripItem>::const_iterator it = result.begin(); it != result.end(); ++it) {
176  if (!it->edges.empty()) {
177  if (it->line == "") {
178  trip->addTripItem(new Walk(it->edges, it->destStop));
179  } else if (veh != 0 && it->line == veh->getID()) {
180  trip->addTripItem(new Ride(it->edges.front(), it->edges.back(), veh->getID(), it->destStop));
181  veh->getRouteDefinition()->addLoadedAlternative(new RORoute(veh->getID() + "_RouteDef", it->edges));
182  carUsed = true;
183  } else {
184  trip->addTripItem(new Ride(0, 0, it->line, it->destStop));
185  }
186  }
187  }
188  if (result.empty()) {
189  errorHandler->inform("No route for trip in person '" + getID() + "'.");
190  myRoutingSuccess = false;
191  }
192  return carUsed;
193 }
194 
195 
196 void
198  const bool /* removeLoops */, MsgHandler* errorHandler) {
199  myRoutingSuccess = true;
200  for (std::vector<PlanItem*>::iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
201  if ((*it)->needsRouting()) {
202  PersonTrip* trip = static_cast<PersonTrip*>(*it);
203  ConstROEdgeVector edges;
204  std::vector<ROVehicle*>& vehicles = trip->getVehicles();
205  if (vehicles.empty()) {
206  computeIntermodal(provider, trip, 0, errorHandler);
207  } else {
208  for (std::vector<ROVehicle*>::iterator v = vehicles.begin(); v != vehicles.end();) {
209  if (!computeIntermodal(provider, trip, *v, errorHandler)) {
210  v = vehicles.erase(v);
211  } else {
212  ++v;
213  }
214  }
215  }
216  }
217  }
218 }
219 
220 
221 void
222 ROPerson::saveAsXML(OutputDevice& os, OutputDevice* const typeos, bool asAlternatives, OptionsCont& options) const {
223  // write the person's vehicles
224  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
225  (*it)->saveVehicles(os, typeos, asAlternatives, options);
226  }
227 
228  if (typeos != 0 && myType != 0 && !myType->saved) {
229  myType->write(*typeos);
230  myType->saved = true;
231  }
232  if (myType != 0 && !myType->saved) {
233  myType->write(os);
234  myType->saved = asAlternatives;
235  }
236 
237  // write the person
238  myParameter.write(os, options, SUMO_TAG_PERSON);
239 
240  for (std::vector<PlanItem*>::const_iterator it = myPlan.begin(); it != myPlan.end(); ++it) {
241  (*it)->saveAsXML(os);
242  }
243 
244  for (std::map<std::string, std::string>::const_iterator j = myParameter.getMap().begin(); j != myParameter.getMap().end(); ++j) {
246  os.writeAttr(SUMO_ATTR_KEY, (*j).first);
247  os.writeAttr(SUMO_ATTR_VALUE, (*j).second);
248  os.closeTag();
249  }
250  os.closeTag();
251 }
252 
253 
254 /****************************************************************************/
255 
The departure is person triggered.
SUMOVehicleParameter myParameter
The vehicle's parameter.
Definition: RORoutable.h:165
void addRide(const ROEdge *const from, const ROEdge *const to, const std::string &lines, const std::string &destStop)
Definition: ROPerson.cpp:98
bool compute(const E *from, const E *to, SUMOReal departPos, SUMOReal arrivalPos, SUMOReal speed, const V *const vehicle, const SVCPermissions modeSet, SUMOTime msTime, std::vector< TripItem > &into)
Builds the route between the given edges using the minimum effort at the given time The definition of...
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
const std::string lines
Definition: ROPerson.h:172
std::string vtypeid
The vehicle's type id.
void addVehicle(ROVehicle *veh)
Definition: ROPerson.h:236
SUMOReal getArrivalPos() const
Definition: ROPerson.h:251
void saveAsXML(OutputDevice &os) const
Definition: ROPerson.cpp:122
Structure representing possible vehicle parameter.
bool saved
Information whether this type was already saved (needed by routers)
int SVCPermissions
A planItem can be a Stop.
Definition: ROPerson.h:110
void addWalk(const ConstROEdgeVector &edges, const SUMOReal duration, const SUMOReal speed, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:107
const ROEdge *const from
Definition: ROPerson.h:170
bool computeIntermodal(const RORouterProvider &provider, PersonTrip *const trip, const ROVehicle *const veh, MsgHandler *const errorHandler)
Definition: ROPerson.cpp:170
const ROEdge * getDestination() const
Definition: ROPerson.h:245
SUMOReal getDepartPos() const
Definition: ROPerson.h:248
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 std::string DEFAULT_VTYPE_ID
A planItem can be a Trip which contains multiple tripItems.
Definition: ROPerson.h:215
static RONet * getInstance()
Returns the pointer to the unique instance of RONet (singleton).
Definition: RONet.cpp:66
A routable thing such as a vehicle or person.
Definition: RORoutable.h:62
const std::string & getID() const
Returns the id.
Definition: Named.h:66
A ride is part of a trip, e.g., go from here to here by car or bus.
Definition: ROPerson.h:155
A vehicle as used by router.
Definition: ROVehicle.h:60
the edges of a route
std::vector< PlanItem * > myPlan
The plan of the person.
Definition: ROPerson.h:321
void addStop(const SUMOVehicleParameter::Stop &stopPar, const ROEdge *const stopEdge)
Definition: ROPerson.cpp:116
void addTrip(const ROEdge *const from, const ROEdge *const to, const SVCPermissions modeSet, const std::string &vTypes, const SUMOReal departPos, const SUMOReal arrivalPos, const std::string &busStop)
Definition: ROPerson.cpp:71
const ROEdge * getOrigin() const
Definition: ROPerson.h:242
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
void write(OutputDevice &dev, const OptionsCont &oc, const SumoXMLTag tag=SUMO_TAG_VEHICLE) const
Writes the parameters as a beginning element.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
vehicle is a passenger car (a "normal" car)
A walk is part of a trip, e.g., go from here to here by foot.
Definition: ROPerson.h:185
A basic edge for routing applications.
Definition: ROEdge.h:77
virtual ~ROPerson()
Destructor.
Definition: ROPerson.cpp:63
ROPerson(const SUMOVehicleParameter &pars, const SUMOVTypeParameter *type)
Constructor.
Definition: ROPerson.cpp:58
void write(OutputDevice &dev) const
Writes the vtype.
SUMOReal maxSpeed
The vehicle type's maximum speed [m/s].
SVCPermissions getModes() const
Definition: ROPerson.h:254
const SUMOVTypeParameter *const myType
The type of the vehicle.
Definition: RORoutable.h:168
void computeRoute(const RORouterProvider &provider, const bool removeLoops, MsgHandler *errorHandler)
Definition: ROPerson.cpp:197
The router's network representation.
Definition: RONet.h:76
Structure representing possible vehicle parameter.
int setParameter
Information for the router which parameter were set.
void saveAsXML(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Saves the complete person description.
Definition: ROPerson.cpp:222
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:99
const std::string destStop
Definition: ROPerson.h:173
Base class for a vehicle's route definition.
Definition: RORouteDef.h:63
void addLoadedAlternative(RORoute *alternative)
Adds a single alternative loaded from the file An alternative may also be generated during DUA...
Definition: RORouteDef.cpp:80
const int VEHPARS_VTYPE_SET
IntermodalRouter< E, L, N, V > & getIntermodalRouter() const
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
std::vector< ROVehicle * > & getVehicles()
Definition: ROPerson.h:239
virtual void addTripItem(TripItem *tripIt)
Definition: ROPerson.h:233
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:214
SUMOVTypeParameter * getVehicleTypeSecure(const std::string &id)
Retrieves the named vehicle type.
Definition: RONet.cpp:272
void saveAsXML(OutputDevice &os) const
Definition: ROPerson.cpp:139
RORouteDef * getRouteDefinition() const
Returns the definition of the route the vehicle takes.
Definition: ROVehicle.h:83
const ROEdge *const to
Definition: ROPerson.h:171
void saveVehicles(OutputDevice &os, OutputDevice *const typeos, bool asAlternatives, OptionsCont &options) const
Definition: ROPerson.cpp:162
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
A complete router's route.
Definition: RORoute.h:62
std::string id
The vehicle's id.