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(nullptr),
49  myLastDepart(-1),
50  myActiveRouteColor(nullptr),
51  myCurrentCosts(0.),
52  myCurrentVType(nullptr),
53  myBeginDefault(string2time(OptionsCont::getOptions().getString("begin"))),
54  myEndDefault(string2time(OptionsCont::getOptions().getString("end"))),
55  myFirstDepart(-1), myInsertStopEdgesAt(-1) {
56 }
57 
58 
60  delete myCurrentVType;
61 }
62 
63 
66  return myLastDepart;
67 }
68 
69 
70 bool
74  WRITE_WARNING("Route file should be sorted by departure time, ignoring '" + myVehicleParameter->id + "'!");
75  return false;
76  }
77  }
78  return true;
79 }
80 
81 
82 void
84  // register only non public transport to parse all public transport lines in advance
87  if (myFirstDepart == -1) {
89  }
90  }
91  // else: we don't know when this vehicle will depart. keep the previous known depart time
92 }
93 
94 
95 void
97  const SUMOSAXAttributes& attrs) {
98  switch (element) {
99  case SUMO_TAG_VEHICLE:
100  delete myVehicleParameter;
102  break;
103  case SUMO_TAG_PERSON:
104  delete myVehicleParameter;
106  break;
107  case SUMO_TAG_CONTAINER:
108  delete myVehicleParameter;
110  break;
111  case SUMO_TAG_FLOW:
112  delete myVehicleParameter;
114  break;
115  case SUMO_TAG_VTYPE:
116  // 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
118  break;
121  break;
122  case SUMO_TAG_ROUTE:
123  openRoute(attrs);
124  break;
126  openRouteDistribution(attrs);
127  break;
128  case SUMO_TAG_STOP:
129  addStop(attrs);
130  break;
131  case SUMO_TAG_TRIP: {
133  if (myVehicleParameter->id == "") {
134  WRITE_WARNING("Omitting trip ids is deprecated!");
136  }
139  break;
140  }
141  case SUMO_TAG_PERSONTRIP:
142  case SUMO_TAG_WALK:
144  addWalk(attrs);
145  } else {
146  addPersonTrip(attrs);
147  }
148  break;
149  case SUMO_TAG_INTERVAL: {
150  bool ok;
152  myEndDefault = attrs.getSUMOTimeReporting(SUMO_ATTR_END, nullptr, ok);
153  break;
154  }
155  case SUMO_TAG_PARAM:
156  addParam(attrs);
157  break;
158  default:
159  // parse embedded car following model information
160  if (myCurrentVType != nullptr) {
161  WRITE_WARNING("Defining car following parameters in a nested element is deprecated in vType '" + myCurrentVType->id + "', use attributes instead!");
163  }
164  break;
165  }
166 }
167 
168 
169 void
171  switch (element) {
172  case SUMO_TAG_ROUTE:
173  closeRoute();
174  break;
175  case SUMO_TAG_PERSON:
176  closePerson();
177  delete myVehicleParameter;
178  myVehicleParameter = nullptr;
179  break;
180  case SUMO_TAG_CONTAINER:
181  closeContainer();
182  delete myVehicleParameter;
183  myVehicleParameter = nullptr;
184  break;
185  case SUMO_TAG_VEHICLE:
187  myVehicleParameter->repetitionNumber++; // for backwards compatibility
188  // it is a flow, thus no break here
189  FALLTHROUGH;
190  } else {
191  closeVehicle();
192  delete myVehicleParameter;
193  myVehicleParameter = nullptr;
194  break;
195  }
196  case SUMO_TAG_FLOW:
197  closeFlow();
198  break;
201  break;
204  break;
205  case SUMO_TAG_INTERVAL:
206  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
207  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
208  break;
209  default:
210  break;
211  }
212 }
213 
214 
215 bool
216 SUMORouteHandler::checkStopPos(double& startPos, double& endPos, const double laneLength,
217  const double minLength, const bool friendlyPos) {
218  if (minLength > laneLength) {
219  return false;
220  }
221  if (startPos < 0) {
222  startPos += laneLength;
223  }
224  if (endPos < 0) {
225  endPos += laneLength;
226  }
227  if (endPos < minLength || endPos > laneLength) {
228  if (!friendlyPos) {
229  return false;
230  }
231  if (endPos < minLength) {
232  endPos = minLength;
233  }
234  if (endPos > laneLength) {
235  endPos = laneLength;
236  }
237  }
238  if (startPos < 0 || startPos > endPos - minLength) {
239  if (!friendlyPos) {
240  return false;
241  }
242  if (startPos < 0) {
243  startPos = 0;
244  }
245  if (startPos > endPos - minLength) {
246  startPos = endPos - minLength;
247  }
248  }
249  return true;
250 }
251 
252 
253 void
255  bool ok = true;
256  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
257  // circumventing empty string test
258  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
259  if (myVehicleParameter != nullptr) {
260  myVehicleParameter->setParameter(key, val);
261  } else if (myCurrentVType != nullptr) {
262  myCurrentVType->setParameter(key, val);
263  }
264 }
265 
266 
267 bool
268 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
269  stop.parametersSet = 0;
270  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
271  stop.parametersSet |= STOP_END_SET;
272  }
273  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
275  }
276  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
278  }
281  }
282  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
284  }
285  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
287  }
290  }
291  bool ok = true;
292  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, nullptr, ok, "");
293  stop.chargingStation = attrs.getOpt<std::string>(SUMO_ATTR_CHARGING_STATION, nullptr, ok, "");
294  stop.containerstop = attrs.getOpt<std::string>(SUMO_ATTR_CONTAINER_STOP, nullptr, ok, "");
295  stop.parkingarea = attrs.getOpt<std::string>(SUMO_ATTR_PARKING_AREA, nullptr, ok, "");
296  if (stop.busstop != "") {
297  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
298  } else if (stop.chargingStation != "") {
299  errorSuffix = " at '" + stop.chargingStation + "'" + errorSuffix;
300  } else if (stop.containerstop != "") {
301  errorSuffix = " at '" + stop.containerstop + "'" + errorSuffix;
302  } else if (stop.parkingarea != "") {
303  errorSuffix = " at '" + stop.parkingarea + "'" + errorSuffix;
304  } else {
305  errorSuffix = " on lane '" + stop.lane + "'" + errorSuffix;
306  }
307  // get the standing duration
310  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, nullptr, ok, true);
311  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, nullptr, ok, false);
312  } else {
313  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, nullptr, ok, true);
314  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, nullptr, ok, false);
315  }
316  stop.duration = -1;
317  stop.until = -1;
318  } else {
319  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, nullptr, ok, -1);
320  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, nullptr, ok, -1);
321  if (!ok || (stop.duration < 0 && stop.until < 0)) {
322  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
323  return false;
324  }
325  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, nullptr, ok, false);
326  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, nullptr, ok, false);
327  }
328  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, nullptr, ok, stop.triggered || stop.containerTriggered || stop.parkingarea != "");
329  if (stop.parkingarea != "" && !stop.parking) {
330  ok = false;
331  }
332  if (!ok) {
333  errorOutput->inform("Invalid bool for 'triggered', 'containerTriggered' or 'parking' for stop" + errorSuffix);
334  return false;
335  }
336 
337  // expected persons
338  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, nullptr, ok, "");
339  std::set<std::string> personIDs;
340  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
341  stop.awaitedPersons = personIDs;
342 
343  // expected containers
344  std::string expectedContainersStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED_CONTAINERS, nullptr, ok, "");
345  std::set<std::string> containerIDs;
346  SUMOSAXAttributes::parseStringSet(expectedContainersStr, containerIDs);
347  stop.awaitedContainers = containerIDs;
348 
349  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, nullptr, ok, "end");
350  if (idx == "end") {
351  stop.index = STOP_INDEX_END;
352  } else if (idx == "fit") {
353  stop.index = STOP_INDEX_FIT;
354  } else {
355  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, nullptr, ok);
356  if (!ok || stop.index < 0) {
357  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
358  return false;
359  }
360  }
361  return true;
362 }
363 
364 /****************************************************************************/
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:241
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:67
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