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-2015 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 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 SUMORouteHandler::SUMORouteHandler(const std::string& file) :
55  SUMOSAXHandler(file),
56  myVehicleParameter(0),
57  myLastDepart(-1),
58  myActiveRouteColor(0),
59  myCurrentVType(0),
60  myBeginDefault(string2time(OptionsCont::getOptions().getString("begin"))),
61  myEndDefault(string2time(OptionsCont::getOptions().getString("end"))),
62  myFirstDepart(-1), myInsertStopEdgesAt(-1) {
63 }
64 
65 
67 }
68 
69 
72  return myLastDepart;
73 }
74 
75 
76 bool
80  WRITE_WARNING("Route file should be sorted by departure time, ignoring '" + myVehicleParameter->id + "'!");
81  return false;
82  }
83  }
84  return true;
85 }
86 
87 
88 void
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  //@todo warn about deprecation of missing trip ids
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  break;
156  }
157 }
158 
159 
160 void
162  switch (element) {
163  case SUMO_TAG_ROUTE:
164  closeRoute();
165  break;
166  case SUMO_TAG_PERSON:
167  closePerson();
168  delete myVehicleParameter;
169  myVehicleParameter = 0;
170  break;
171  case SUMO_TAG_CONTAINER:
172  closeContainer();
173  delete myVehicleParameter;
174  myVehicleParameter = 0;
175  break;
176  case SUMO_TAG_VEHICLE:
178  myVehicleParameter->repetitionNumber++; // for backwards compatibility
179  // it is a flow, thus no break here
180  } else {
181  closeVehicle();
182  delete myVehicleParameter;
183  myVehicleParameter = 0;
184  break;
185  }
186  case SUMO_TAG_FLOW:
187  closeFlow();
188  break;
191  break;
194  break;
195  case SUMO_TAG_VTYPE:
197  break;
198  case SUMO_TAG_INTERVAL:
199  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
200  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
201  break;
202  default:
203  break;
204  }
205 }
206 
207 
208 bool
209 SUMORouteHandler::checkStopPos(SUMOReal& startPos, SUMOReal& endPos, const SUMOReal laneLength,
210  const SUMOReal minLength, const bool friendlyPos) {
211  if (minLength > laneLength) {
212  return false;
213  }
214  if (startPos < 0) {
215  startPos += laneLength;
216  }
217  if (endPos < 0) {
218  endPos += laneLength;
219  }
220  if (endPos < minLength || endPos > laneLength) {
221  if (!friendlyPos) {
222  return false;
223  }
224  if (endPos < minLength) {
225  endPos = minLength;
226  }
227  if (endPos > laneLength) {
228  endPos = laneLength;
229  }
230  }
231  if (startPos < 0 || startPos > endPos - minLength) {
232  if (!friendlyPos) {
233  return false;
234  }
235  if (startPos < 0) {
236  startPos = 0;
237  }
238  if (startPos > endPos - minLength) {
239  startPos = endPos - minLength;
240  }
241  }
242  return true;
243 }
244 
245 
246 void
248  bool ok = true;
249  std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
250  std::string val = attrs.get<std::string>(SUMO_ATTR_VALUE, 0, ok);
251  if (myVehicleParameter != 0) {
252  myVehicleParameter->addParameter(key, val);
253  } else if (myCurrentVType != 0) {
254  myCurrentVType->addParameter(key, val);
255  }
256 }
257 
258 
259 bool
260 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
261  stop.setParameter = 0;
262  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
263  stop.setParameter |= STOP_END_SET;
264  }
265  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
267  }
268  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
270  }
273  }
274  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
276  }
277  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
279  }
282  }
283  bool ok = true;
284  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
285  stop.containerstop = attrs.getOpt<std::string>(SUMO_ATTR_CONTAINER_STOP, 0, ok, "");
286  if (stop.busstop != "") {
287  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
288  } else if (stop.containerstop != "") {
289  errorSuffix = " at '" + stop.containerstop + "'" + errorSuffix;
290  } else {
291  errorSuffix = " on lane '" + stop.lane + "'" + errorSuffix;
292  }
293  // get the standing duration
296  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, true);
297  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
298  } else {
299  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
300  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
301  }
302  stop.duration = -1;
303  stop.until = -1;
304  } else {
305  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
306  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
307  if (!ok || (stop.duration < 0 && stop.until < 0)) {
308  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
309  return false;
310  }
311  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
312  stop.containerTriggered = attrs.getOpt<bool>(SUMO_ATTR_CONTAINER_TRIGGERED, 0, ok, false);
313  }
314  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered || stop.containerTriggered);
315  if (!ok) {
316  errorOutput->inform("Invalid bool for 'triggered', 'containerTriggered' or 'parking' for stop" + errorSuffix);
317  return false;
318  }
319 
320  // expected persons
321  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
322  std::set<std::string> personIDs;
323  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
324  stop.awaitedPersons = personIDs;
325 
326  // expected containers
327  std::string expectedContainersStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED_CONTAINERS, 0, ok, "");
328  std::set<std::string> containerIDs;
329  SUMOSAXAttributes::parseStringSet(expectedContainersStr, containerIDs);
330  stop.awaitedContainers = containerIDs;
331 
332  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
333  if (idx == "end") {
334  stop.index = STOP_INDEX_END;
335  } else if (idx == "fit") {
336  stop.index = STOP_INDEX_FIT;
337  } else {
338  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
339  if (!ok || stop.index < 0) {
340  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
341  return false;
342  }
343  }
344  return true;
345 }
346 
347 /****************************************************************************/
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.
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
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.
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's attributes.
static bool checkStopPos(SUMOReal &startPos, SUMOReal &endPos, const SUMOReal laneLength, const SUMOReal minLength, const bool friendlyPos)
check start and end position of a stop
SUMOVTypeParameter * myCurrentVType
The currently parsed vehicle type.
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...
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
IDSupplier myIdSupplier
generates numerical ids
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
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:63
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's attributes.
std::set< std::string > awaitedContainers
IDs of containers the vehicle has to wait for until departing.
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's departure time.
DepartDefinition departProcedure
Information how the vehicle shall choose the depart time.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
const int STOP_EXPECTED_SET
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
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
const int STOP_TRIGGER_SET
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.
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:89
A storage for options typed value containers)
Definition: OptionsCont.h:108
int index
at which position in the stops list
virtual void closeRouteDistribution()=0
int setParameter
Information for the output which parameter were set.
int SUMOTime
Definition: SUMOTime.h:43
virtual void closeFlow()=0
Ends the processing of a flow.
virtual void closePerson()=0
Ends the processing of a person.
#define SUMOReal
Definition: config.h:218
SUMOTime getLastDepart() const
Returns the last loaded depart time.
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.
static SUMOVTypeParameter * beginVTypeParsing(const SUMOSAXAttributes &attrs, const std::string &file)
Starts to parse a vehicle type.
bool checkLastDepart()
Checks whether the route file is sorted by departure time if needed.
std::string id
The vehicle's id.
virtual void openRoute(const SUMOSAXAttributes &attrs)=0