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>
46 #include "SUMORouteHandler.h"
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 SUMORouteHandler::SUMORouteHandler(const std::string& file) :
53  SUMOSAXHandler(file),
54  myVehicleParameter(0),
55  myLastDepart(-1),
56  myActiveRouteColor(0),
57  myCurrentVType(0),
58  myBeginDefault(string2time(OptionsCont::getOptions().getString("begin"))),
59  myEndDefault(string2time(OptionsCont::getOptions().getString("end"))),
60  myFirstDepart(-1), myInsertStopEdgesAt(-1), myDefaultCFModel(SUMO_TAG_NOTHING) {
61 }
62 
63 
65  delete myCurrentVType;
66 }
67 
68 
71  return myLastDepart;
72 }
73 
74 
75 bool
79  WRITE_WARNING("Route file should be sorted by departure time, ignoring '" + myVehicleParameter->id + "'!");
80  return false;
81  }
82  }
83  return true;
84 }
85 
86 
87 void
89  // register only non public transport to parse all public transport lines in advance
92  if (myFirstDepart == -1) {
94  }
95  }
96  // else: we don't know when this vehicle will depart. keep the previous known depart time
97 }
98 
99 
100 void
102  const SUMOSAXAttributes& attrs) {
103  switch (element) {
104  case SUMO_TAG_VEHICLE:
105  delete myVehicleParameter;
107  break;
108  case SUMO_TAG_PERSON:
109  delete myVehicleParameter;
111  break;
112  case SUMO_TAG_CONTAINER:
113  delete myVehicleParameter;
115  break;
116  case SUMO_TAG_FLOW:
117  delete myVehicleParameter;
119  break;
120  case SUMO_TAG_VTYPE:
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_INTERVAL: {
146  bool ok;
149  break;
150  }
151  case SUMO_TAG_PARAM:
152  addParam(attrs);
153  break;
154  default:
155  // parse embedded car following model information
156  if (myCurrentVType != 0) {
157  WRITE_WARNING("Defining car following parameters in a nested element is deprecated in vType '" + myCurrentVType->id + "', use attributes instead!");
159  }
160  break;
161  }
162 }
163 
164 
165 void
167  switch (element) {
168  case SUMO_TAG_ROUTE:
169  closeRoute();
170  break;
171  case SUMO_TAG_PERSON:
172  closePerson();
173  delete myVehicleParameter;
174  myVehicleParameter = 0;
175  break;
176  case SUMO_TAG_CONTAINER:
177  closeContainer();
178  delete myVehicleParameter;
179  myVehicleParameter = 0;
180  break;
181  case SUMO_TAG_VEHICLE:
183  myVehicleParameter->repetitionNumber++; // for backwards compatibility
184  // it is a flow, thus no break here
185  } else {
186  closeVehicle();
187  delete myVehicleParameter;
188  myVehicleParameter = 0;
189  break;
190  }
191  case SUMO_TAG_FLOW:
192  closeFlow();
193  break;
196  break;
199  break;
200  case SUMO_TAG_INTERVAL:
201  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
202  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
203  break;
204  default:
205  break;
206  }
207 }
208 
209 
210 bool
211 SUMORouteHandler::checkStopPos(double& startPos, double& endPos, const double laneLength,
212  const double minLength, const bool friendlyPos) {
213  if (minLength > laneLength) {
214  return false;
215  }
216  if (startPos < 0) {
217  startPos += laneLength;
218  }
219  if (endPos < 0) {
220  endPos += laneLength;
221  }
222  if (endPos < minLength || endPos > laneLength) {
223  if (!friendlyPos) {
224  return false;
225  }
226  if (endPos < minLength) {
227  endPos = minLength;
228  }
229  if (endPos > laneLength) {
230  endPos = laneLength;
231  }
232  }
233  if (startPos < 0 || startPos > endPos - minLength) {
234  if (!friendlyPos) {
235  return false;
236  }
237  if (startPos < 0) {
238  startPos = 0;
239  }
240  if (startPos > endPos - minLength) {
241  startPos = endPos - minLength;
242  }
243  }
244  return true;
245 }
246 
247 
248 void
250  bool ok = true;
251  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
252  // circumventing empty string test
253  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
254  if (myVehicleParameter != 0) {
255  myVehicleParameter->addParameter(key, val);
256  } else if (myCurrentVType != 0) {
257  myCurrentVType->addParameter(key, val);
258  }
259 }
260 
261 
262 bool
263 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
264  stop.setParameter = 0;
265  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
266  stop.setParameter |= STOP_END_SET;
267  }
268  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
270  }
271  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
273  }
276  }
277  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
279  }
280  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
282  }
285  }
286  bool ok = true;
287  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
288  stop.chargingStation = attrs.getOpt<std::string>(SUMO_ATTR_CHARGING_STATION, 0, ok, "");
289  stop.containerstop = attrs.getOpt<std::string>(SUMO_ATTR_CONTAINER_STOP, 0, ok, "");
290  stop.parkingarea = attrs.getOpt<std::string>(SUMO_ATTR_PARKING_AREA, 0, ok, "");
291  if (stop.busstop != "") {
292  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
293  } else if (stop.chargingStation != "") {
294  errorSuffix = " at '" + stop.chargingStation + "'" + errorSuffix;
295  } else if (stop.containerstop != "") {
296  errorSuffix = " at '" + stop.containerstop + "'" + errorSuffix;
297  } else if (stop.parkingarea != "") {
298  errorSuffix = " at '" + stop.parkingarea + "'" + errorSuffix;
299  } else {
300  errorSuffix = " on lane '" + stop.lane + "'" + errorSuffix;
301  }
302  // get the standing duration
305  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, true);
306  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
307  } else {
308  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
309  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
310  }
311  stop.duration = -1;
312  stop.until = -1;
313  } else {
314  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
315  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
316  if (!ok || (stop.duration < 0 && stop.until < 0)) {
317  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
318  return false;
319  }
320  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
321  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
322  }
323  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered || stop.containerTriggered || stop.parkingarea != "");
324  if (stop.parkingarea != "" && !stop.parking) {
325  ok = false;
326  }
327  if (!ok) {
328  errorOutput->inform("Invalid bool for 'triggered', 'containerTriggered' or 'parking' for stop" + errorSuffix);
329  return false;
330  }
331 
332  // expected persons
333  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
334  std::set<std::string> personIDs;
335  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
336  stop.awaitedPersons = personIDs;
337 
338  // expected containers
339  std::string expectedContainersStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED_CONTAINERS, 0, ok, "");
340  std::set<std::string> containerIDs;
341  SUMOSAXAttributes::parseStringSet(expectedContainersStr, containerIDs);
342  stop.awaitedContainers = containerIDs;
343 
344  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
345  if (idx == "end") {
346  stop.index = STOP_INDEX_END;
347  } else if (idx == "fit") {
348  stop.index = STOP_INDEX_FIT;
349  } else {
350  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
351  if (!ok || stop.index < 0) {
352  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
353  return false;
354  }
355  }
356  return true;
357 }
358 
359 /****************************************************************************/
const int STOP_CONTAINER_TRIGGER_SET
virtual void openVehicleTypeDistribution(const SUMOSAXAttributes &attrs)=0
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.
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.
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.
std::string parkingarea
(Optional) parking area if one is assigned to the stop
begin/end of the description of a route
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
IDSupplier myIdSupplier
generates numerical ids
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
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.
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.
const std::string & getFileName() const
returns the current file name
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.
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
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
SUMORouteHandler(const std::string &file)
standard constructor
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
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)
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
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
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
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.
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.
SUMOTime getLastDepart() const
Returns the last loaded depart time.
long long int SUMOTime
Definition: TraCIDefs.h:52
a single trip definition (used by router)
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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
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