SUMO - Simulation of Urban MObility
SUMORouteHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Parser for routes during their loading
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2001-2017 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 
34 #include <string>
35 #include <map>
36 #include <vector>
40 #include <utils/common/ToString.h>
44 #include "SUMORouteHandler.h"
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 SUMORouteHandler::SUMORouteHandler(const std::string& file) :
51  SUMOSAXHandler(file),
52  myVehicleParameter(0),
53  myLastDepart(-1),
54  myActiveRouteColor(0),
55  myCurrentVType(0),
56  myBeginDefault(string2time(OptionsCont::getOptions().getString("begin"))),
57  myEndDefault(string2time(OptionsCont::getOptions().getString("end"))),
58  myFirstDepart(-1), myInsertStopEdgesAt(-1) {
59 }
60 
61 
63  delete myCurrentVType;
64 }
65 
66 
69  return myLastDepart;
70 }
71 
72 
73 bool
77  WRITE_WARNING("Route file should be sorted by departure time, ignoring '" + myVehicleParameter->id + "'!");
78  return false;
79  }
80  }
81  return true;
82 }
83 
84 
85 void
87  // register only non public transport to parse all public transport lines in advance
90  if (myFirstDepart == -1) {
92  }
93  }
94  // else: we don't know when this vehicle will depart. keep the previous known depart time
95 }
96 
97 
98 void
100  const SUMOSAXAttributes& attrs) {
101  switch (element) {
102  case SUMO_TAG_VEHICLE:
103  delete myVehicleParameter;
105  break;
106  case SUMO_TAG_PERSON:
107  delete myVehicleParameter;
109  break;
110  case SUMO_TAG_CONTAINER:
111  delete myVehicleParameter;
113  break;
114  case SUMO_TAG_FLOW:
115  delete myVehicleParameter;
117  break;
118  case SUMO_TAG_VTYPE:
120  break;
123  break;
124  case SUMO_TAG_ROUTE:
125  openRoute(attrs);
126  break;
128  openRouteDistribution(attrs);
129  break;
130  case SUMO_TAG_STOP:
131  addStop(attrs);
132  break;
133  case SUMO_TAG_TRIP: {
135  if (myVehicleParameter->id == "") {
136  //@todo warn about deprecation of missing trip ids
138  }
141  break;
142  }
143  case SUMO_TAG_INTERVAL: {
144  bool ok;
147  break;
148  }
149  case SUMO_TAG_PARAM:
150  addParam(attrs);
151  break;
152  default:
153  break;
154  }
155 }
156 
157 
158 void
160  switch (element) {
161  case SUMO_TAG_ROUTE:
162  closeRoute();
163  break;
164  case SUMO_TAG_PERSON:
165  closePerson();
166  delete myVehicleParameter;
167  myVehicleParameter = 0;
168  break;
169  case SUMO_TAG_CONTAINER:
170  closeContainer();
171  delete myVehicleParameter;
172  myVehicleParameter = 0;
173  break;
174  case SUMO_TAG_VEHICLE:
176  myVehicleParameter->repetitionNumber++; // for backwards compatibility
177  // it is a flow, thus no break here
178  } else {
179  closeVehicle();
180  delete myVehicleParameter;
181  myVehicleParameter = 0;
182  break;
183  }
184  case SUMO_TAG_FLOW:
185  closeFlow();
186  break;
189  break;
192  break;
193  case SUMO_TAG_VTYPE:
195  break;
196  case SUMO_TAG_INTERVAL:
197  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
198  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
199  break;
200  default:
201  break;
202  }
203 }
204 
205 
206 bool
207 SUMORouteHandler::checkStopPos(double& startPos, double& endPos, const double laneLength,
208  const double minLength, const bool friendlyPos) {
209  if (minLength > laneLength) {
210  return false;
211  }
212  if (startPos < 0) {
213  startPos += laneLength;
214  }
215  if (endPos < 0) {
216  endPos += laneLength;
217  }
218  if (endPos < minLength || endPos > laneLength) {
219  if (!friendlyPos) {
220  return false;
221  }
222  if (endPos < minLength) {
223  endPos = minLength;
224  }
225  if (endPos > laneLength) {
226  endPos = laneLength;
227  }
228  }
229  if (startPos < 0 || startPos > endPos - minLength) {
230  if (!friendlyPos) {
231  return false;
232  }
233  if (startPos < 0) {
234  startPos = 0;
235  }
236  if (startPos > endPos - minLength) {
237  startPos = endPos - minLength;
238  }
239  }
240  return true;
241 }
242 
243 
244 void
246  bool ok = true;
247  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
248  // circumventing empty string test
249  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
250  if (myVehicleParameter != 0) {
251  myVehicleParameter->addParameter(key, val);
252  } else if (myCurrentVType != 0) {
253  myCurrentVType->addParameter(key, val);
254  }
255 }
256 
257 
258 bool
259 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
260  stop.setParameter = 0;
261  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
262  stop.setParameter |= STOP_END_SET;
263  }
264  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
266  }
267  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
269  }
272  }
273  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
275  }
276  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
278  }
281  }
282  bool ok = true;
283  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
284  stop.chargingStation = attrs.getOpt<std::string>(SUMO_ATTR_CHARGING_STATION, 0, ok, "");
285  stop.containerstop = attrs.getOpt<std::string>(SUMO_ATTR_CONTAINER_STOP, 0, ok, "");
286  stop.parkingarea = attrs.getOpt<std::string>(SUMO_ATTR_PARKING_AREA, 0, ok, "");
287  if (stop.busstop != "") {
288  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
289  } else if (stop.chargingStation != "") {
290  errorSuffix = " at '" + stop.chargingStation + "'" + errorSuffix;
291  } else if (stop.containerstop != "") {
292  errorSuffix = " at '" + stop.containerstop + "'" + errorSuffix;
293  } else if (stop.parkingarea != "") {
294  errorSuffix = " at '" + stop.parkingarea + "'" + errorSuffix;
295  } else {
296  errorSuffix = " on lane '" + stop.lane + "'" + errorSuffix;
297  }
298  // get the standing duration
301  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, true);
302  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
303  } else {
304  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
305  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
306  }
307  stop.duration = -1;
308  stop.until = -1;
309  } else {
310  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
311  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
312  if (!ok || (stop.duration < 0 && stop.until < 0)) {
313  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
314  return false;
315  }
316  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
317  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
318  }
319  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered || stop.containerTriggered || stop.parkingarea != "");
320  if (stop.parkingarea != "" && !stop.parking) {
321  ok = false;
322  }
323  if (!ok) {
324  errorOutput->inform("Invalid bool for 'triggered', 'containerTriggered' or 'parking' for stop" + errorSuffix);
325  return false;
326  }
327 
328  // expected persons
329  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
330  std::set<std::string> personIDs;
331  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
332  stop.awaitedPersons = personIDs;
333 
334  // expected containers
335  std::string expectedContainersStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED_CONTAINERS, 0, ok, "");
336  std::set<std::string> containerIDs;
337  SUMOSAXAttributes::parseStringSet(expectedContainersStr, containerIDs);
338  stop.awaitedContainers = containerIDs;
339 
340  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
341  if (idx == "end") {
342  stop.index = STOP_INDEX_END;
343  } else if (idx == "fit") {
344  stop.index = STOP_INDEX_FIT;
345  } else {
346  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
347  if (!ok || stop.index < 0) {
348  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
349  return false;
350  }
351  }
352  return true;
353 }
354 
355 /****************************************************************************/
const int STOP_CONTAINER_TRIGGER_SET
virtual void openVehicleTypeDistribution(const SUMOSAXAttributes &attrs)=0
virtual void myEndElement(int element)
Called when a closing tag occurs.
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.
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:200
IDSupplier myIdSupplier
generates numerical ids
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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:59
virtual void closeVehicle()=0
Ends the processing of a vehicle.
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
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:47
const int STOP_EXPECTED_SET
SUMORouteHandler(const std::string &file)
standard constructor
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
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
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.
static void closeVTypeParsing(SUMOVTypeParameter &vtype)
Closes parsing of the vehicle type.
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.
int setParameter
Information for the router which parameter were set, TraCI may modify this (whe changing color) ...
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:85
A storage for options typed value containers)
Definition: OptionsCont.h:99
int index
at which position in the stops list
virtual void closeRouteDistribution()=0
int setParameter
Information for the output which parameter were set.
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:52
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
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