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  FALLTHROUGH;
189  } else {
190  closeVehicle();
191  delete myVehicleParameter;
192  myVehicleParameter = 0;
193  break;
194  }
195  case SUMO_TAG_FLOW:
196  closeFlow();
197  break;
200  break;
203  break;
204  case SUMO_TAG_INTERVAL:
205  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
206  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
207  break;
208  default:
209  break;
210  }
211 }
212 
213 
214 bool
215 SUMORouteHandler::checkStopPos(double& startPos, double& endPos, const double laneLength,
216  const double minLength, const bool friendlyPos) {
217  if (minLength > laneLength) {
218  return false;
219  }
220  if (startPos < 0) {
221  startPos += laneLength;
222  }
223  if (endPos < 0) {
224  endPos += laneLength;
225  }
226  if (endPos < minLength || endPos > laneLength) {
227  if (!friendlyPos) {
228  return false;
229  }
230  if (endPos < minLength) {
231  endPos = minLength;
232  }
233  if (endPos > laneLength) {
234  endPos = laneLength;
235  }
236  }
237  if (startPos < 0 || startPos > endPos - minLength) {
238  if (!friendlyPos) {
239  return false;
240  }
241  if (startPos < 0) {
242  startPos = 0;
243  }
244  if (startPos > endPos - minLength) {
245  startPos = endPos - minLength;
246  }
247  }
248  return true;
249 }
250 
251 
252 void
254  bool ok = true;
255  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
256  // circumventing empty string test
257  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
258  if (myVehicleParameter != 0) {
259  myVehicleParameter->setParameter(key, val);
260  } else if (myCurrentVType != 0) {
261  myCurrentVType->setParameter(key, val);
262  }
263 }
264 
265 
266 bool
267 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
268  stop.parametersSet = 0;
269  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
270  stop.parametersSet |= STOP_END_SET;
271  }
272  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
274  }
275  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
277  }
280  }
281  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
283  }
284  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
286  }
289  }
290  bool ok = true;
291  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
292  stop.chargingStation = attrs.getOpt<std::string>(SUMO_ATTR_CHARGING_STATION, 0, ok, "");
293  stop.containerstop = attrs.getOpt<std::string>(SUMO_ATTR_CONTAINER_STOP, 0, ok, "");
294  stop.parkingarea = attrs.getOpt<std::string>(SUMO_ATTR_PARKING_AREA, 0, ok, "");
295  if (stop.busstop != "") {
296  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
297  } else if (stop.chargingStation != "") {
298  errorSuffix = " at '" + stop.chargingStation + "'" + errorSuffix;
299  } else if (stop.containerstop != "") {
300  errorSuffix = " at '" + stop.containerstop + "'" + errorSuffix;
301  } else if (stop.parkingarea != "") {
302  errorSuffix = " at '" + stop.parkingarea + "'" + errorSuffix;
303  } else {
304  errorSuffix = " on lane '" + stop.lane + "'" + errorSuffix;
305  }
306  // get the standing duration
309  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, true);
310  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
311  } else {
312  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
313  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
314  }
315  stop.duration = -1;
316  stop.until = -1;
317  } else {
318  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
319  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
320  if (!ok || (stop.duration < 0 && stop.until < 0)) {
321  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
322  return false;
323  }
324  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
325  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
326  }
327  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered || stop.containerTriggered || stop.parkingarea != "");
328  if (stop.parkingarea != "" && !stop.parking) {
329  ok = false;
330  }
331  if (!ok) {
332  errorOutput->inform("Invalid bool for 'triggered', 'containerTriggered' or 'parking' for stop" + errorSuffix);
333  return false;
334  }
335 
336  // expected persons
337  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
338  std::set<std::string> personIDs;
339  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
340  stop.awaitedPersons = personIDs;
341 
342  // expected containers
343  std::string expectedContainersStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED_CONTAINERS, 0, ok, "");
344  std::set<std::string> containerIDs;
345  SUMOSAXAttributes::parseStringSet(expectedContainersStr, containerIDs);
346  stop.awaitedContainers = containerIDs;
347 
348  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
349  if (idx == "end") {
350  stop.index = STOP_INDEX_END;
351  } else if (idx == "fit") {
352  stop.index = STOP_INDEX_FIT;
353  } else {
354  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
355  if (!ok || stop.index < 0) {
356  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
357  return false;
358  }
359  }
360  return true;
361 }
362 
363 /****************************************************************************/
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
#define FALLTHROUGH
Definition: StdDefs.h:38