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-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 // Parser for routes during their loading
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <map>
29 #include <vector>
31 #include <utils/common/ToString.h>
39 #include <utils/xml/XMLSubSys.h>
40 #include "SUMORouteHandler.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
46 SUMORouteHandler::SUMORouteHandler(const std::string& file, const std::string& expectedRoot) :
47  SUMOSAXHandler(file, XMLSubSys::isValidating() ? expectedRoot : ""),
48  myVehicleParameter(0),
49  myLastDepart(-1),
50  myActiveRouteColor(0),
51  myCurrentVType(0),
52  myBeginDefault(string2time(OptionsCont::getOptions().getString("begin"))),
53  myEndDefault(string2time(OptionsCont::getOptions().getString("end"))),
54  myFirstDepart(-1), myInsertStopEdgesAt(-1) {
55 }
56 
57 
59  delete myCurrentVType;
60 }
61 
62 
65  return myLastDepart;
66 }
67 
68 
69 bool
73  WRITE_WARNING("Route file should be sorted by departure time, ignoring '" + myVehicleParameter->id + "'!");
74  return false;
75  }
76  }
77  return true;
78 }
79 
80 
81 void
83  // register only non public transport to parse all public transport lines in advance
86  if (myFirstDepart == -1) {
88  }
89  }
90  // else: we don't know when this vehicle will depart. keep the previous known depart time
91 }
92 
93 
94 void
96  const SUMOSAXAttributes& attrs) {
97  switch (element) {
98  case SUMO_TAG_VEHICLE:
99  delete myVehicleParameter;
101  break;
102  case SUMO_TAG_PERSON:
103  delete myVehicleParameter;
105  break;
106  case SUMO_TAG_CONTAINER:
107  delete myVehicleParameter;
109  break;
110  case SUMO_TAG_FLOW:
111  delete myVehicleParameter;
113  break;
114  case SUMO_TAG_VTYPE:
115  // 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
117  break;
120  break;
121  case SUMO_TAG_ROUTE:
122  openRoute(attrs);
123  break;
125  openRouteDistribution(attrs);
126  break;
127  case SUMO_TAG_STOP:
128  addStop(attrs);
129  break;
130  case SUMO_TAG_TRIP: {
132  if (myVehicleParameter->id == "") {
133  WRITE_WARNING("Omitting trip ids is deprecated!");
135  }
138  break;
139  }
140  case SUMO_TAG_PERSONTRIP:
141  case SUMO_TAG_WALK:
143  addWalk(attrs);
144  } else {
145  addPersonTrip(attrs);
146  }
147  break;
148  case SUMO_TAG_INTERVAL: {
149  bool ok;
152  break;
153  }
154  case SUMO_TAG_PARAM:
155  addParam(attrs);
156  break;
157  default:
158  // parse embedded car following model information
159  if (myCurrentVType != 0) {
160  WRITE_WARNING("Defining car following parameters in a nested element is deprecated in vType '" + myCurrentVType->id + "', use attributes instead!");
162  }
163  break;
164  }
165 }
166 
167 
168 void
170  switch (element) {
171  case SUMO_TAG_ROUTE:
172  closeRoute();
173  break;
174  case SUMO_TAG_PERSON:
175  closePerson();
176  delete myVehicleParameter;
177  myVehicleParameter = 0;
178  break;
179  case SUMO_TAG_CONTAINER:
180  closeContainer();
181  delete myVehicleParameter;
182  myVehicleParameter = 0;
183  break;
184  case SUMO_TAG_VEHICLE:
186  myVehicleParameter->repetitionNumber++; // for backwards compatibility
187  // it is a flow, thus no break here
188  } else {
189  closeVehicle();
190  delete myVehicleParameter;
191  myVehicleParameter = 0;
192  break;
193  }
194  case SUMO_TAG_FLOW:
195  closeFlow();
196  break;
199  break;
202  break;
203  case SUMO_TAG_INTERVAL:
204  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
205  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
206  break;
207  default:
208  break;
209  }
210 }
211 
212 
213 bool
214 SUMORouteHandler::checkStopPos(double& startPos, double& endPos, const double laneLength,
215  const double minLength, const bool friendlyPos) {
216  if (minLength > laneLength) {
217  return false;
218  }
219  if (startPos < 0) {
220  startPos += laneLength;
221  }
222  if (endPos < 0) {
223  endPos += laneLength;
224  }
225  if (endPos < minLength || endPos > laneLength) {
226  if (!friendlyPos) {
227  return false;
228  }
229  if (endPos < minLength) {
230  endPos = minLength;
231  }
232  if (endPos > laneLength) {
233  endPos = laneLength;
234  }
235  }
236  if (startPos < 0 || startPos > endPos - minLength) {
237  if (!friendlyPos) {
238  return false;
239  }
240  if (startPos < 0) {
241  startPos = 0;
242  }
243  if (startPos > endPos - minLength) {
244  startPos = endPos - minLength;
245  }
246  }
247  return true;
248 }
249 
250 
251 void
253  bool ok = true;
254  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
255  // circumventing empty string test
256  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
257  if (myVehicleParameter != 0) {
258  myVehicleParameter->setParameter(key, val);
259  } else if (myCurrentVType != 0) {
260  myCurrentVType->setParameter(key, val);
261  }
262 }
263 
264 
265 bool
266 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
267  stop.parametersSet = 0;
268  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
269  stop.parametersSet |= STOP_END_SET;
270  }
271  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
273  }
274  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
276  }
279  }
280  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
282  }
283  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
285  }
288  }
289  bool ok = true;
290  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
291  stop.chargingStation = attrs.getOpt<std::string>(SUMO_ATTR_CHARGING_STATION, 0, ok, "");
292  stop.containerstop = attrs.getOpt<std::string>(SUMO_ATTR_CONTAINER_STOP, 0, ok, "");
293  stop.parkingarea = attrs.getOpt<std::string>(SUMO_ATTR_PARKING_AREA, 0, ok, "");
294  if (stop.busstop != "") {
295  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
296  } else if (stop.chargingStation != "") {
297  errorSuffix = " at '" + stop.chargingStation + "'" + errorSuffix;
298  } else if (stop.containerstop != "") {
299  errorSuffix = " at '" + stop.containerstop + "'" + errorSuffix;
300  } else if (stop.parkingarea != "") {
301  errorSuffix = " at '" + stop.parkingarea + "'" + errorSuffix;
302  } else {
303  errorSuffix = " on lane '" + stop.lane + "'" + errorSuffix;
304  }
305  // get the standing duration
308  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, true);
309  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
310  } else {
311  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
312  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
313  }
314  stop.duration = -1;
315  stop.until = -1;
316  } else {
317  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
318  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
319  if (!ok || (stop.duration < 0 && stop.until < 0)) {
320  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
321  return false;
322  }
323  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
324  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
325  }
326  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered || stop.containerTriggered || stop.parkingarea != "");
327  if (stop.parkingarea != "" && !stop.parking) {
328  ok = false;
329  }
330  if (!ok) {
331  errorOutput->inform("Invalid bool for 'triggered', 'containerTriggered' or 'parking' for stop" + errorSuffix);
332  return false;
333  }
334 
335  // expected persons
336  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
337  std::set<std::string> personIDs;
338  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
339  stop.awaitedPersons = personIDs;
340 
341  // expected containers
342  std::string expectedContainersStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED_CONTAINERS, 0, ok, "");
343  std::set<std::string> containerIDs;
344  SUMOSAXAttributes::parseStringSet(expectedContainersStr, containerIDs);
345  stop.awaitedContainers = containerIDs;
346 
347  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
348  if (idx == "end") {
349  stop.index = STOP_INDEX_END;
350  } else if (idx == "fit") {
351  stop.index = STOP_INDEX_FIT;
352  } else {
353  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
354  if (!ok || stop.index < 0) {
355  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
356  return false;
357  }
358  }
359  return true;
360 }
361 
362 /****************************************************************************/
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.
long long int SUMOTime
Definition: SUMOTime.h:36
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:242
IDSupplier myIdSupplier
generates numerical ids
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
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:52
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:42
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
const int STOP_EXPECTED_SET
bool containerTriggered
whether an arriving container lets the vehicle continue
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
SUMORouteHandler(const std::string &file, const std::string &expectedRoot)
standard constructor
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:113
A storage for options typed value containers)
Definition: OptionsCont.h:92
Utility methods for initialising, closing and using the XML-subsystem.
Definition: XMLSubSys.h:66
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.
a single trip definition (used by router)
static SUMOVTypeParameter * beginVTypeParsing(const SUMOSAXAttributes &attrs, const std::string &file)
Starts to parse a vehicle type.
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