SUMO - Simulation of Urban MObility
SUMORouteHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-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 // Parser for routes during their loading
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 
33 #include <string>
34 #include <map>
35 #include <vector>
39 #include <utils/common/ToString.h>
45 #include "SUMORouteHandler.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
51 SUMORouteHandler::SUMORouteHandler(const std::string& file) :
52  SUMOSAXHandler(file),
53  myVehicleParameter(0),
54  myLastDepart(-1),
55  myActiveRouteColor(0),
56  myCurrentVType(0),
57  myBeginDefault(string2time(OptionsCont::getOptions().getString("begin"))),
58  myEndDefault(string2time(OptionsCont::getOptions().getString("end"))),
59  myFirstDepart(-1), myInsertStopEdgesAt(-1), myDefaultCFModel(SUMO_TAG_NOTHING) {
60 }
61 
62 
64  delete myCurrentVType;
65 }
66 
67 
70  return myLastDepart;
71 }
72 
73 
74 bool
78  WRITE_WARNING("Route file should be sorted by departure time, ignoring '" + myVehicleParameter->id + "'!");
79  return false;
80  }
81  }
82  return true;
83 }
84 
85 
86 void
88  // register only non public transport to parse all public transport lines in advance
91  if (myFirstDepart == -1) {
93  }
94  }
95  // else: we don't know when this vehicle will depart. keep the previous known depart time
96 }
97 
98 
99 void
101  const SUMOSAXAttributes& attrs) {
102  switch (element) {
103  case SUMO_TAG_VEHICLE:
104  delete myVehicleParameter;
106  break;
107  case SUMO_TAG_PERSON:
108  delete myVehicleParameter;
110  break;
111  case SUMO_TAG_CONTAINER:
112  delete myVehicleParameter;
114  break;
115  case SUMO_TAG_FLOW:
116  delete myVehicleParameter;
118  break;
119  case SUMO_TAG_VTYPE:
120  // XXX: Where is this deleted? Delegated to subclasses?! MSRouteHandler takes care of this, in case of RORouteHandler this is not obvious. Consider introduction of a shared_ptr
122  break;
125  break;
126  case SUMO_TAG_ROUTE:
127  openRoute(attrs);
128  break;
130  openRouteDistribution(attrs);
131  break;
132  case SUMO_TAG_STOP:
133  addStop(attrs);
134  break;
135  case SUMO_TAG_TRIP: {
137  if (myVehicleParameter->id == "") {
138  WRITE_WARNING("Omitting trip ids is deprecated!");
140  }
143  break;
144  }
145  case SUMO_TAG_PERSONTRIP:
146  case SUMO_TAG_WALK:
148  addWalk(attrs);
149  } else {
150  addPersonTrip(attrs);
151  }
152  break;
153  case SUMO_TAG_INTERVAL: {
154  bool ok;
157  break;
158  }
159  case SUMO_TAG_PARAM:
160  addParam(attrs);
161  break;
162  default:
163  // parse embedded car following model information
164  if (myCurrentVType != 0) {
165  WRITE_WARNING("Defining car following parameters in a nested element is deprecated in vType '" + myCurrentVType->id + "', use attributes instead!");
167  }
168  break;
169  }
170 }
171 
172 
173 void
175  switch (element) {
176  case SUMO_TAG_ROUTE:
177  closeRoute();
178  break;
179  case SUMO_TAG_PERSON:
180  closePerson();
181  delete myVehicleParameter;
182  myVehicleParameter = 0;
183  break;
184  case SUMO_TAG_CONTAINER:
185  closeContainer();
186  delete myVehicleParameter;
187  myVehicleParameter = 0;
188  break;
189  case SUMO_TAG_VEHICLE:
191  myVehicleParameter->repetitionNumber++; // for backwards compatibility
192  // it is a flow, thus no break here
193  } else {
194  closeVehicle();
195  delete myVehicleParameter;
196  myVehicleParameter = 0;
197  break;
198  }
199  case SUMO_TAG_FLOW:
200  closeFlow();
201  break;
204  break;
207  break;
208  case SUMO_TAG_INTERVAL:
209  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
210  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
211  break;
212  default:
213  break;
214  }
215 }
216 
217 
218 bool
219 SUMORouteHandler::checkStopPos(double& startPos, double& endPos, const double laneLength,
220  const double minLength, const bool friendlyPos) {
221  if (minLength > laneLength) {
222  return false;
223  }
224  if (startPos < 0) {
225  startPos += laneLength;
226  }
227  if (endPos < 0) {
228  endPos += laneLength;
229  }
230  if (endPos < minLength || endPos > laneLength) {
231  if (!friendlyPos) {
232  return false;
233  }
234  if (endPos < minLength) {
235  endPos = minLength;
236  }
237  if (endPos > laneLength) {
238  endPos = laneLength;
239  }
240  }
241  if (startPos < 0 || startPos > endPos - minLength) {
242  if (!friendlyPos) {
243  return false;
244  }
245  if (startPos < 0) {
246  startPos = 0;
247  }
248  if (startPos > endPos - minLength) {
249  startPos = endPos - minLength;
250  }
251  }
252  return true;
253 }
254 
255 
256 void
258  bool ok = true;
259  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
260  // circumventing empty string test
261  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
262  if (myVehicleParameter != 0) {
263  myVehicleParameter->setParameter(key, val);
264  } else if (myCurrentVType != 0) {
265  myCurrentVType->setParameter(key, val);
266  }
267 }
268 
269 
270 bool
271 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
272  stop.parametersSet = 0;
273  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
274  stop.parametersSet |= STOP_END_SET;
275  }
276  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
278  }
279  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
281  }
284  }
285  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
287  }
288  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
290  }
293  }
294  bool ok = true;
295  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
296  stop.chargingStation = attrs.getOpt<std::string>(SUMO_ATTR_CHARGING_STATION, 0, ok, "");
297  stop.containerstop = attrs.getOpt<std::string>(SUMO_ATTR_CONTAINER_STOP, 0, ok, "");
298  stop.parkingarea = attrs.getOpt<std::string>(SUMO_ATTR_PARKING_AREA, 0, ok, "");
299  if (stop.busstop != "") {
300  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
301  } else if (stop.chargingStation != "") {
302  errorSuffix = " at '" + stop.chargingStation + "'" + errorSuffix;
303  } else if (stop.containerstop != "") {
304  errorSuffix = " at '" + stop.containerstop + "'" + errorSuffix;
305  } else if (stop.parkingarea != "") {
306  errorSuffix = " at '" + stop.parkingarea + "'" + errorSuffix;
307  } else {
308  errorSuffix = " on lane '" + stop.lane + "'" + errorSuffix;
309  }
310  // get the standing duration
313  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, true);
314  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
315  } else {
316  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
317  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
318  }
319  stop.duration = -1;
320  stop.until = -1;
321  } else {
322  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
323  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
324  if (!ok || (stop.duration < 0 && stop.until < 0)) {
325  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
326  return false;
327  }
328  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
329  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
330  }
331  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered || stop.containerTriggered || stop.parkingarea != "");
332  if (stop.parkingarea != "" && !stop.parking) {
333  ok = false;
334  }
335  if (!ok) {
336  errorOutput->inform("Invalid bool for 'triggered', 'containerTriggered' or 'parking' for stop" + errorSuffix);
337  return false;
338  }
339 
340  // expected persons
341  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
342  std::set<std::string> personIDs;
343  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
344  stop.awaitedPersons = personIDs;
345 
346  // expected containers
347  std::string expectedContainersStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED_CONTAINERS, 0, ok, "");
348  std::set<std::string> containerIDs;
349  SUMOSAXAttributes::parseStringSet(expectedContainersStr, containerIDs);
350  stop.awaitedContainers = containerIDs;
351 
352  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
353  if (idx == "end") {
354  stop.index = STOP_INDEX_END;
355  } else if (idx == "fit") {
356  stop.index = STOP_INDEX_FIT;
357  } else {
358  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
359  if (!ok || stop.index < 0) {
360  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
361  return false;
362  }
363  }
364  return true;
365 }
366 
367 /****************************************************************************/
const int STOP_CONTAINER_TRIGGER_SET
virtual void openVehicleTypeDistribution(const SUMOSAXAttributes &attrs)=0
virtual void addPersonTrip(const SUMOSAXAttributes &attrs)=0
add a routing request for a walking or intermodal person
SumoXMLTag
Numbers representing SUMO-XML - element names.
virtual void myEndElement(int element)
Called when a closing tag occurs.
static SUMOVTypeParameter * beginVTypeParsing(const SUMOSAXAttributes &attrs, const std::string &file, const SumoXMLTag defaultCFModel)
Starts to parse a vehicle type.
const int VEHPARS_FORCE_REROUTE
static void parseStringSet(const std::string &def, std::set< std::string > &into)
Splits the given string, stores it in a set.
description of a vehicle type
The time is given.
std::string containerstop
(Optional) container stop if one is assigned to the stop
int repetitionNumber
The number of times the vehicle shall be repeatedly inserted.
bool parking
whether the vehicle is removed from the net while stopping
SUMOTime myFirstDepart
the first read departure time
a flow definition (used by router)
SUMOTime duration
The stopping duration.
SUMOVehicleParameter * myVehicleParameter
Parameter of the current vehicle, trip, person, container or flow.
SUMOTime myEndDefault
The default value for flow ends.
distribution of a route
virtual void addStop(const SUMOSAXAttributes &attrs)=0
Processing of a stop.
const std::string & getFileName() const
returns the current file name
static SUMOVehicleParameter * parseVehicleAttributes(const SUMOSAXAttributes &attrs, const bool optionalID=false, const bool skipDepart=false, const bool isPerson=false)
Parses a vehicle&#39;s attributes.
static void parseVTypeEmbedded(SUMOVTypeParameter &into, const SumoXMLTag element, const SUMOSAXAttributes &attrs, const bool fromVType=false)
Parses an element embedded in vtype definition.
int parametersSet
Information for the router which parameter were set, TraCI may modify this (whe changing color) ...
SUMOVTypeParameter * myCurrentVType
The currently parsed vehicle type.
weights: time range begin
SUMOTime until
The time at which the vehicle may continue its journey.
virtual void openRouteDistribution(const SUMOSAXAttributes &attrs)=0
const int STOP_INDEX_FIT
void registerLastDepart()
save last depart (only to be used if vehicle is not discarded)
std::set< std::string > awaitedPersons
IDs of persons the vehicle has to wait for until departing.
std::string myActiveRouteID
The id of the current route.
SAX-handler base for SUMO-files.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
std::string parkingarea
(Optional) parking area if one is assigned to the stop
begin/end of the description of a route
SUMOTime getLastDepart() const
Returns the last loaded depart time.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
IDSupplier myIdSupplier
generates numerical ids
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
std::string busstop
(Optional) bus stop if one is assigned to the stop
const int STOP_START_SET
SUMOTime myBeginDefault
The default value for flow begins.
std::string getNext()
Returns the next id.
Definition: IDSupplier.cpp:58
virtual void closeVehicle()=0
Ends the processing of a vehicle.
the edges of a route
virtual void closeContainer()=0
Ends the processing of a container.
Encapsulated SAX-Attributes.
static SUMOVehicleParameter * parseFlowAttributes(const SUMOSAXAttributes &attrs, const SUMOTime beginDefault, const SUMOTime endDefault)
Parses a flow&#39;s attributes.
std::set< std::string > awaitedContainers
IDs of containers the vehicle has to wait for until departing.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
std::string chargingStation
(Optional) charging station if one is assigned to the stop
parameter associated to a certain key
void addParam(const SUMOSAXAttributes &attrs)
assign arbitrary vehicle parameters
bool triggered
whether an arriving person lets the vehicle continue
virtual ~SUMORouteHandler()
standard destructor
const int STOP_INDEX_END
int parametersSet
Information for the output which parameter were set.
SUMOTime depart
The vehicle&#39;s departure time.
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
const int STOP_EXPECTED_SET
SUMORouteHandler(const std::string &file)
standard constructor
bool containerTriggered
whether an arriving container lets the vehicle continue
SumoXMLTag myDefaultCFModel
the default car following model
std::string line
The vehicle&#39;s line (mainly for public transport)
std::string lane
The lane to stop at.
virtual void closeRoute(const bool mayBeDisconnected=false)=0
const int STOP_END_SET
const int STOP_PARKING_SET
stop for vehicles
static bool checkStopPos(double &startPos, double &endPos, const double laneLength, const double minLength, const bool friendlyPos)
check start and end position of a stop
const int STOP_TRIGGER_SET
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
virtual void closeVehicleTypeDistribution()=0
const int STOP_EXPECTED_CONTAINERS_SET
SUMOTime myLastDepart
The insertion time of the vehicle read last.
weights: time range end
SUMOTime getOptSUMOTimeReporting(int attr, const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
Definition of vehicle stop (position and duration)
bool parseStop(SUMOVehicleParameter::Stop &stop, const SUMOSAXAttributes &attrs, std::string errorSuffix, MsgHandler *const errorOutput)
parses attributes common to all stops
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
virtual void closeRouteDistribution()=0
std::string id
The vehicle type&#39;s id.
virtual void closeFlow()=0
Ends the processing of a flow.
description of a vehicle
an aggreagated-output interval
virtual void closePerson()=0
Ends the processing of a person.
long long int SUMOTime
Definition: TraCIDefs.h:51
a single trip definition (used by router)
distribution of a vehicle type
virtual void addWalk(const SUMOSAXAttributes &attrs)=0
add a fully specified walk
bool checkLastDepart()
Checks whether the route file is sorted by departure time if needed.
std::string id
The vehicle&#39;s id.
virtual void openRoute(const SUMOSAXAttributes &attrs)=0