SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RORouteHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Parser and container for routes during their loading
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2013 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>
37 #include <iostream>
47 #include <utils/xml/XMLSubSys.h>
49 #include "RONet.h"
50 #include "RORouteHandler.h"
51 
52 #ifdef CHECK_MEMORY_LEAKS
53 #include <foreign/nvwa/debug_new.h>
54 #endif // CHECK_MEMORY_LEAKS
55 
56 
57 // ===========================================================================
58 // method definitions
59 // ===========================================================================
60 RORouteHandler::RORouteHandler(RONet& net, const std::string& file,
61  const bool tryRepair,
62  const bool emptyDestinationsAllowed,
63  const bool ignoreErrors) :
64  SUMORouteHandler(file),
65  myNet(net),
66  myActivePlan(0),
67  myTryRepair(tryRepair),
68  myEmptyDestinationsAllowed(emptyDestinationsAllowed),
69  myErrorOutput(ignoreErrors ? MsgHandler::getWarningInstance() : MsgHandler::getErrorInstance()),
70  myCurrentVTypeDistribution(0),
71  myCurrentAlternatives(0) {
72  myActiveRoute.reserve(100);
73 }
74 
75 
77 }
78 
79 
80 void
81 RORouteHandler::parseFromTo(std::string element,
82  const SUMOSAXAttributes& attrs) {
83  bool useTaz = OptionsCont::getOptions().getBool("with-taz");
84  if (useTaz && !myVehicleParameter->wasSet(VEHPARS_TAZ_SET)) {
85  WRITE_WARNING("Taz usage was requested but no taz present in " + element + " '" + myVehicleParameter->id + "'!");
86  useTaz = false;
87  } else if (!useTaz && !attrs.hasAttribute(SUMO_ATTR_FROM) && myVehicleParameter->wasSet(VEHPARS_TAZ_SET)) {
88  WRITE_WARNING("'from' attribute missing using taz for " + element + " '" + myVehicleParameter->id + "'!");
89  useTaz = true;
90  }
91  if (useTaz) {
92  const ROEdge* fromTaz = myNet.getEdge(myVehicleParameter->fromTaz + "-source");
93  if (fromTaz == 0) {
94  myErrorOutput->inform("Source taz '" + myVehicleParameter->fromTaz + "' not known for " + element + " '" + myVehicleParameter->id + "'!");
95  } else if (fromTaz->getNoFollowing() == 0) {
96  myErrorOutput->inform("Source taz '" + myVehicleParameter->fromTaz + "' has no outgoing edges for " + element + " '" + myVehicleParameter->id + "'!");
97  } else {
98  myActiveRoute.push_back(fromTaz);
99  }
100  const ROEdge* toTaz = myNet.getEdge(myVehicleParameter->toTaz + "-sink");
101  if (toTaz == 0) {
102  myErrorOutput->inform("Sink taz '" + myVehicleParameter->toTaz + "' not known for " + element + " '" + myVehicleParameter->id + "'!");
103  } else {
104  myActiveRoute.push_back(toTaz);
105  }
106  } else {
107  bool ok = true;
108  parseEdges(attrs.get<std::string>(SUMO_ATTR_FROM, myVehicleParameter->id.c_str(), ok),
109  myActiveRoute, "for " + element + " '" + myVehicleParameter->id + "'");
110  parseEdges(attrs.get<std::string>(SUMO_ATTR_TO, myVehicleParameter->id.c_str(), ok, !myEmptyDestinationsAllowed),
111  myActiveRoute, "for " + element + " '" + myVehicleParameter->id + "'");
112  }
114  if (myVehicleParameter->routeid == "") {
116  }
117  closeRoute(true);
118 }
119 
120 
121 
122 void
124  const SUMOSAXAttributes& attrs) {
125  SUMORouteHandler::myStartElement(element, attrs);
126  switch (element) {
127  case SUMO_TAG_PERSON:
128  myActivePlan = new OutputDevice_String(false, 1);
130  (*myActivePlan) << attrs;
131  break;
132  case SUMO_TAG_RIDE: {
134  (*myActivePlan) << attrs;
136  break;
137  }
138  case SUMO_TAG_WALK: {
140  (*myActivePlan) << attrs;
142  break;
143  }
144  case SUMO_TAG_FLOW:
145  parseFromTo("flow", attrs);
146  break;
147  case SUMO_TAG_TRIP: {
148  parseFromTo("trip", attrs);
149  closeVehicle();
150  }
151  break;
152  default:
153  break;
154  }
155  // parse embedded vtype information
156  if (myCurrentVType != 0 && element != SUMO_TAG_VTYPE) {
158  return;
159  }
160 }
161 
162 
163 void
165  bool ok = true;
166  myCurrentVTypeDistributionID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
167  if (ok) {
169  if (attrs.hasAttribute(SUMO_ATTR_VTYPES)) {
170  const std::string vTypes = attrs.get<std::string>(SUMO_ATTR_VTYPES, myCurrentVTypeDistributionID.c_str(), ok);
171  StringTokenizer st(vTypes);
172  while (st.hasNext()) {
173  SUMOVTypeParameter* type = myNet.getVehicleTypeSecure(st.next());
174  myCurrentVTypeDistribution->add(1., type);
175  }
176  }
177  }
178 }
179 
180 
181 void
183  if (myCurrentVTypeDistribution != 0) {
186  myErrorOutput->inform("Vehicle type distribution '" + myCurrentVTypeDistributionID + "' is empty.");
189  myErrorOutput->inform("Another vehicle type (or distribution) with the id '" + myCurrentVTypeDistributionID + "' exists.");
190  }
192  }
193 }
194 
195 
196 void
198  // check whether the id is really necessary
199  std::string rid;
200  if (myCurrentAlternatives != 0) {
202  rid = "distribution '" + myCurrentAlternatives->getID() + "'";
203  } else if (myVehicleParameter != 0) {
204  // ok, a vehicle is wrapping the route,
205  // we may use this vehicle's id as default
206  myVehicleParameter->routeid = myActiveRouteID = "!" + myVehicleParameter->id; // !!! document this
207  if (attrs.hasAttribute(SUMO_ATTR_ID)) {
208  WRITE_WARNING("Ids of internal routes are ignored (vehicle '" + myVehicleParameter->id + "').");
209  }
210  } else {
211  bool ok = true;
212  myActiveRouteID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
213  if (!ok) {
214  return;
215  }
216  rid = "'" + myActiveRouteID + "'";
217  }
218  if (myVehicleParameter != 0) { // have to do this here for nested route distributions
219  rid = "for vehicle '" + myVehicleParameter->id + "'";
220  }
221  bool ok = true;
222  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
223  parseEdges(attrs.get<std::string>(SUMO_ATTR_EDGES, myActiveRouteID.c_str(), ok), myActiveRoute, rid);
224  }
225  myActiveRouteRefID = attrs.getOpt<std::string>(SUMO_ATTR_REFID, myActiveRouteID.c_str(), ok, "");
227  myErrorOutput->inform("Invalid reference to route '" + myActiveRouteRefID + "' in route " + rid + ".");
228  }
229  if (myCurrentAlternatives != 0 && !attrs.hasAttribute(SUMO_ATTR_PROB)) {
230  WRITE_WARNING("No probability for a route in '" + rid + "', using default.");
231  }
233  if (ok && myActiveRouteProbability < 0) {
234  myErrorOutput->inform("Invalid probability for route '" + myActiveRouteID + "'.");
235  }
237  ok = true;
238  myCurrentCosts = attrs.getOpt<SUMOReal>(SUMO_ATTR_COST, myActiveRouteID.c_str(), ok, -1);
239  if (ok && myCurrentCosts != -1 && myCurrentCosts < 0) {
240  myErrorOutput->inform("Invalid cost for route '" + myActiveRouteID + "'.");
241  }
242 }
243 
244 
245 void
248  switch (element) {
249  case SUMO_TAG_VTYPE: {
251  if (myCurrentVTypeDistribution != 0) {
253  }
254  }
255  myCurrentVType = 0;
256  }
257  break;
258  default:
259  break;
260  }
261 }
262 
263 
264 void
265 RORouteHandler::closeRoute(const bool mayBeDisconnected) {
266  if (myActiveRoute.size() == 0) {
267  if (myActiveRouteRefID != "" && myCurrentAlternatives != 0) {
269  myActiveRouteID = "";
270  myActiveRouteRefID = "";
271  return;
272  }
273  if (myVehicleParameter != 0) {
274  myErrorOutput->inform("Vehicle's '" + myVehicleParameter->id + "' route has no edges.");
275  } else {
276  myErrorOutput->inform("Route '" + myActiveRouteID + "' has no edges.");
277  }
278  myActiveRouteID = "";
279  myActiveRouteStops.clear();
280  return;
281  }
284  myActiveRoute.clear();
285  if (myCurrentAlternatives == 0) {
286  if (myNet.getRouteDef(myActiveRouteID) != 0) {
287  delete route;
288  if (myVehicleParameter != 0) {
289  myErrorOutput->inform("Another route for vehicle '" + myVehicleParameter->id + "' exists.");
290  } else {
291  myErrorOutput->inform("Another route (or distribution) with the id '" + myActiveRouteID + "' exists.");
292  }
293  myActiveRouteID = "";
294  myActiveRouteStops.clear();
295  return;
296  } else {
297  myCurrentAlternatives = new RORouteDef(myActiveRouteID, 0, mayBeDisconnected || myTryRepair);
301  }
302  } else {
304  }
305  myActiveRouteID = "";
306  myActiveRouteStops.clear();
307 }
308 
309 
310 void
312  // check whether the id is really necessary
313  bool ok = true;
314  std::string id;
315  if (myVehicleParameter != 0) {
316  // ok, a vehicle is wrapping the route,
317  // we may use this vehicle's id as default
318  myVehicleParameter->routeid = id = "!" + myVehicleParameter->id; // !!! document this
319  if (attrs.hasAttribute(SUMO_ATTR_ID)) {
320  WRITE_WARNING("Ids of internal route distributions are ignored (vehicle '" + myVehicleParameter->id + "').");
321  }
322  } else {
323  id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
324  if (!ok) {
325  return;
326  }
327  }
328  // try to get the index of the last element
329  int index = attrs.get<int>(SUMO_ATTR_LAST, id.c_str(), ok);
330  if (ok && index < 0) {
331  myErrorOutput->inform("Negative index of a route alternative (id='" + id + "').");
332  return;
333  }
334  // build the alternative cont
335  myCurrentAlternatives = new RORouteDef(id, index, myTryRepair);
336  if (attrs.hasAttribute(SUMO_ATTR_ROUTES)) {
337  ok = true;
338  StringTokenizer st(attrs.get<std::string>(SUMO_ATTR_ROUTES, id.c_str(), ok));
339  while (st.hasNext()) {
340  const std::string routeID = st.next();
341  const RORouteDef* route = myNet.getRouteDef(routeID);
342  if (route == 0) {
343  myErrorOutput->inform("Unknown route '" + routeID + "' in distribution '" + id + "'.");
344  } else {
346  }
347  }
348  }
349 }
350 
351 
352 void
354  if (myCurrentAlternatives != 0) {
356  myErrorOutput->inform("Route distribution '" + myCurrentAlternatives->getID() + "' is empty.");
357  delete myCurrentAlternatives;
358  } else if (!myNet.addRouteDef(myCurrentAlternatives)) {
359  myErrorOutput->inform("Another route (or distribution) with the id '" + myCurrentAlternatives->getID() + "' exists.");
360  delete myCurrentAlternatives;
361  }
363  }
364 }
365 
366 
367 void
369  // get the vehicle id
371  return;
372  }
373  // get vehicle type
375  // get the route
377  if (route == 0) {
378  myErrorOutput->inform("The route of the vehicle '" + myVehicleParameter->id + "' is not known.");
379  return;
380  }
381  if (route->getID()[0] != '!') {
382  route = route->copy("!" + myVehicleParameter->id);
383  }
384  // build the vehicle
385  if (!MsgHandler::getErrorInstance()->wasInformed()) {
386  ROVehicle* veh = new ROVehicle(*myVehicleParameter, route, type);
389  }
390 }
391 
392 
393 void
398  delete myVehicleParameter;
399  myVehicleParameter = 0;
400  delete myActivePlan;
401  myActivePlan = 0;
402 }
403 
404 
405 void
407  // @todo: consider myScale?
408  // let's check whether vehicles had to depart before the simulation starts
410  SUMOTime offsetToBegin = string2time(OptionsCont::getOptions().getString("begin")) - myVehicleParameter->depart;
414  delete myVehicleParameter;
415  return;
416  }
417  }
420  if (type == 0) {
421  myErrorOutput->inform("The vehicle type '" + myVehicleParameter->vtypeid + "' for vehicle '" + myVehicleParameter->id + "' is not known.");
422  delete myVehicleParameter;
423  return;
424  }
425  if (route == 0) {
426  myErrorOutput->inform("Vehicle '" + myVehicleParameter->id + "' has no route.");
427  delete myVehicleParameter;
428  return;
429  }
430  myActiveRouteID = "";
431  myNet.addFlow(myVehicleParameter, OptionsCont::getOptions().getBool("randomize-flows"));
433  myVehicleParameter = 0;
434 }
435 
436 
437 void
439  if (myActivePlan) {
441  (*myActivePlan) << attrs;
443  return;
444  }
445  bool ok = true;
446  std::string errorSuffix;
447  if (myActiveRouteID != "") {
448  errorSuffix = " in route '" + myActiveRouteID + "'.";
449  } else {
450  errorSuffix = " in vehicle '" + myVehicleParameter->id + "'.";
451  }
454  // try to parse the assigned bus stop
455  stop.busstop = attrs.getOpt<std::string>(SUMO_ATTR_BUS_STOP, 0, ok, "");
456  if (stop.busstop == "") {
457  // no, the lane and the position should be given
458  stop.lane = attrs.getOpt<std::string>(SUMO_ATTR_LANE, 0, ok, "");
459  if (!ok || stop.lane == "") {
460  myErrorOutput->inform("A stop must be placed on a bus stop or a lane" + errorSuffix);
461  return;
462  }
463  ROEdge* edge = myNet.getEdge(stop.lane.substr(0, stop.lane.rfind('_')));
464  if (edge == 0) {
465  myErrorOutput->inform("The lane '" + stop.lane + "' for a stop is not known" + errorSuffix);
466  return;
467  }
468  stop.endPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, 0, ok, edge->getLength());
469  stop.startPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, 0, ok, stop.endPos - 2 * POSITION_EPS);
470  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, 0, ok, false);
471  if (!ok || !checkStopPos(stop.startPos, stop.endPos, edge->getLength(), POSITION_EPS, friendlyPos)) {
472  myErrorOutput->inform("Invalid start or end position for stop" + errorSuffix);
473  return;
474  }
475  }
476 
477  // get the standing duration
479  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, true);
480  stop.duration = -1;
481  stop.until = -1;
482  } else {
483  stop.duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, 0, ok, -1);
484  stop.until = attrs.getOptSUMOTimeReporting(SUMO_ATTR_UNTIL, 0, ok, -1);
485  if (!ok || (stop.duration < 0 && stop.until < 0)) {
486  myErrorOutput->inform("Invalid duration or end time is given for a stop" + errorSuffix);
487  return;
488  }
489  stop.triggered = attrs.getOpt<bool>(SUMO_ATTR_TRIGGERED, 0, ok, false);
490  }
491  stop.parking = attrs.getOpt<bool>(SUMO_ATTR_PARKING, 0, ok, stop.triggered);
492  if (!ok) {
493  myErrorOutput->inform("Invalid bool for 'triggered' or 'parking' for stop" + errorSuffix);
494  return;
495  }
496 
497  // expected persons
498  std::string expectedStr = attrs.getOpt<std::string>(SUMO_ATTR_EXPECTED, 0, ok, "");
499  std::set<std::string> personIDs;
500  SUMOSAXAttributes::parseStringSet(expectedStr, personIDs);
501  stop.awaitedPersons = personIDs;
502 
503  const std::string idx = attrs.getOpt<std::string>(SUMO_ATTR_INDEX, 0, ok, "end");
504  if (idx == "end") {
505  stop.index = STOP_INDEX_END;
506  } else if (idx == "fit") {
507  stop.index = STOP_INDEX_FIT;
508  } else {
509  stop.index = attrs.get<int>(SUMO_ATTR_INDEX, 0, ok);
510  if (!ok || stop.index < 0) {
511  myErrorOutput->inform("Invalid 'index' for stop" + errorSuffix);
512  return;
513  }
514  }
515  if (myVehicleParameter != 0) {
516  myVehicleParameter->stops.push_back(stop);
517  } else {
518  myActiveRouteStops.push_back(stop);
519  }
520 }
521 
522 
523 void
524 RORouteHandler::parseEdges(const std::string& desc, std::vector<const ROEdge*>& into,
525  const std::string& rid) {
526  if (desc[0] == BinaryFormatter::BF_ROUTE) {
527  std::istringstream in(desc, std::ios::binary);
528  char c;
529  in >> c;
530  FileHelpers::readEdgeVector(in, into, rid);
531  } else {
532  for (StringTokenizer st(desc); st.hasNext();) {
533  const std::string id = st.next();
534  const ROEdge* edge = myNet.getEdge(id);
535  if (edge == 0) {
536  myErrorOutput->inform("The edge '" + id + "' within the route " + rid + " is not known."
537  + "\n The route can not be build.");
538  } else {
539  into.push_back(edge);
540  }
541  }
542  }
543 }
544 
545 
546 /****************************************************************************/