SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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-sim.org/
13 // Copyright (C) 2001-2014 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 }
63 
64 
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
91  }
92  // else: we don't know when this vehicle will depart. keep the previous known depart time
93 }
94 
95 
96 void
98  const SUMOSAXAttributes& attrs) {
99  switch (element) {
100  case SUMO_TAG_VEHICLE:
101  delete myVehicleParameter;
103  break;
104  case SUMO_TAG_PERSON:
105  delete myVehicleParameter;
107  break;
108  case SUMO_TAG_FLOW:
109  delete myVehicleParameter;
111  break;
112  case SUMO_TAG_VTYPE:
114  break;
117  break;
118  case SUMO_TAG_ROUTE:
119  openRoute(attrs);
120  break;
122  openRouteDistribution(attrs);
123  break;
124  case SUMO_TAG_STOP:
125  addStop(attrs);
126  break;
127  case SUMO_TAG_TRIP: {
129  if (myVehicleParameter->id == "") {
130  //@todo warn about deprecation of missing trip ids
132  }
135  break;
136  }
137  case SUMO_TAG_INTERVAL: {
138  bool ok;
141  break;
142  }
143  case SUMO_TAG_PARAM:
144  addParam(attrs);
145  break;
146  default:
147  break;
148  }
149 }
150 
151 
152 void
154  switch (element) {
155  case SUMO_TAG_ROUTE:
156  closeRoute();
157  break;
158  case SUMO_TAG_PERSON:
159  closePerson();
160  delete myVehicleParameter;
161  myVehicleParameter = 0;
162  break;
163  case SUMO_TAG_VEHICLE:
165  myVehicleParameter->repetitionNumber++; // for backwards compatibility
166  // it is a flow, thus no break here
167  } else {
168  closeVehicle();
169  delete myVehicleParameter;
170  myVehicleParameter = 0;
171  break;
172  }
173  case SUMO_TAG_FLOW:
174  closeFlow();
175  break;
178  break;
181  break;
182  case SUMO_TAG_VTYPE:
184  break;
185  case SUMO_TAG_INTERVAL:
186  myBeginDefault = string2time(OptionsCont::getOptions().getString("begin"));
187  myEndDefault = string2time(OptionsCont::getOptions().getString("end"));
188  break;
189  case SUMO_TAG_TRIP:
190  delete myVehicleParameter;
191  myVehicleParameter = 0;
192  default:
193  break;
194  }
195 }
196 
197 
198 bool
199 SUMORouteHandler::checkStopPos(SUMOReal& startPos, SUMOReal& endPos, const SUMOReal laneLength,
200  const SUMOReal minLength, const bool friendlyPos) {
201  if (minLength > laneLength) {
202  return false;
203  }
204  if (startPos < 0) {
205  startPos += laneLength;
206  }
207  if (endPos < 0) {
208  endPos += laneLength;
209  }
210  if (endPos < minLength || endPos > laneLength) {
211  if (!friendlyPos) {
212  return false;
213  }
214  if (endPos < minLength) {
215  endPos = minLength;
216  }
217  if (endPos > laneLength) {
218  endPos = laneLength;
219  }
220  }
221  if (startPos < 0 || startPos > endPos - minLength) {
222  if (!friendlyPos) {
223  return false;
224  }
225  if (startPos < 0) {
226  startPos = 0;
227  }
228  if (startPos > endPos - minLength) {
229  startPos = endPos - minLength;
230  }
231  }
232  return true;
233 }
234 
235 
236 void
238  bool ok = true;
239  std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
240  std::string val = attrs.get<std::string>(SUMO_ATTR_VALUE, 0, ok);
241  if (myVehicleParameter != 0) {
242  myVehicleParameter->addParameter(key, val);
243  } else if (myCurrentVType != 0) {
244  myCurrentVType->addParameter(key, val);
245  }
246 }
247 
248 
249 bool
250 SUMORouteHandler::parseStop(SUMOVehicleParameter::Stop& stop, const SUMOSAXAttributes& attrs, std::string errorSuffix, MsgHandler* const errorOutput) {
251  stop.setParameter = 0;
252  if (attrs.hasAttribute(SUMO_ATTR_ENDPOS)) {
253  stop.setParameter |= STOP_END_SET;
254  }
255  if (attrs.hasAttribute(SUMO_ATTR_STARTPOS)) {
257  }
258  if (attrs.hasAttribute(SUMO_ATTR_TRIGGERED)) {
260  }
261  if (attrs.hasAttribute(SUMO_ATTR_PARKING)) {
263  }
264  if (attrs.hasAttribute(SUMO_ATTR_EXPECTED)) {
266  }
267  bool ok = true;
268  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
269  if (stop.busstop != "") {
270  errorSuffix = " at '" + stop.busstop + "'" + errorSuffix;
271  } else {
272  errorSuffix = " on lane '" + stop.busstop + "'" + errorSuffix;
273  }
274  // get the standing duration
276  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
277  stop.duration = -1;
278  stop.until = -1;
279  } else {
280  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
281  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
282  if (!ok || (stop.duration < 0 && stop.until < 0)) {
283  errorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
284  return false;
285  }
286  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
287  }
288  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered);
289  if (!ok) {
290  errorOutput->inform("Invalid bool for 'triggered' or 'parking' for stop" + errorSuffix);
291  return false;
292  }
293 
294  // expected persons
295  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
296  std::set<std::string> personIDs;
297  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
298  stop.awaitedPersons = personIDs;
299 
300  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
301  if (idx == "end") {
302  stop.index = STOP_INDEX_END;
303  } else if (idx == "fit") {
304  stop.index = STOP_INDEX_FIT;
305  } else {
306  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
307  if (!ok || stop.index < 0) {
308  errorOutput->inform("Invalid 'index' for stop" + errorSuffix);
309  return false;
310  }
311  }
312  return true;
313 }
314 
315 /****************************************************************************/
virtual void openVehicleTypeDistribution(const SUMOSAXAttributes &attrs)=0
static SUMOVehicleParameter * parseVehicleAttributes(const SUMOSAXAttributes &attrs, bool optionalID=false, bool skipDepart=false)
Parses a vehicle's attributes.
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.
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 duration
The stopping duration.
SUMOVehicleParameter * myVehicleParameter
Parameter of the current vehicle, trip, person, or flow.
SUMOTime myEndDefault
The default value for flow ends.
virtual void addStop(const SUMOSAXAttributes &attrs)=0
Processing of a stop.
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:196
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.
Encapsulated SAX-Attributes.
static SUMOVehicleParameter * parseFlowAttributes(const SUMOSAXAttributes &attrs, const SUMOTime beginDefault, const SUMOTime endDefault)
Parses a flow's attributes.
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.
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
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:215
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