SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RORDLoader_SUMOBase.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // The base class for SUMO-native route handlers
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "RORDLoader_SUMOBase.h"
35 #include "RORouteDef.h"
36 #include "RONet.h"
40 #include <utils/common/ToString.h>
41 #include "ROVehicle.h"
43 #include "RORouteDef_Complete.h"
44 #include "RORoute.h"
46 
47 #ifdef CHECK_MEMORY_LEAKS
48 #include <foreign/nvwa/debug_new.h>
49 #endif // CHECK_MEMORY_LEAKS
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
56  SUMOTime begin, SUMOTime end, const int maxRouteNumber, const bool tryRepair,
57  const bool withTaz, const bool keepRoutes,
58  const bool skipRouteCalculation, const std::string& file)
59  : ROTypedXMLRoutesLoader(net, begin, end, file),
60  myVehicleParameter(0), myCurrentIsOk(true), myAltIsValid(true),
61  myCurrentAlternatives(0), myMaxRouteNumber(maxRouteNumber),
62  myCurrentRoute(0), myTryRepair(tryRepair), myWithTaz(withTaz), myKeepRoutes(keepRoutes),
63  mySkipRouteCalculation(skipRouteCalculation), myColor(0), myCurrentVType(0),
64  myHaveWarnedAboutDeprecatedVType(false), myHaveWarnedAboutDeprecatedRoute(false) {
65 }
66 
67 
69  // clean up (on failure)
70  delete myCurrentAlternatives;
71  delete myCurrentRoute;
72  delete myVehicleParameter;
73  delete myColor;
74 }
75 
76 
77 void
79  const SUMOSAXAttributes& attrs) {
80  switch (element) {
81  case SUMO_TAG_ROUTE:
82  startRoute(attrs);
83  break;
84  case SUMO_TAG_VEHICLE:
85  // try to parse the vehicle definition
86  delete myVehicleParameter;
89  if (myVehicleParameter != 0) {
91  }
93  break;
97  WRITE_WARNING("'" + toString(SUMO_TAG_VTYPE__DEPRECATED) + "' is deprecated; please use '" + toString(SUMO_TAG_VTYPE) + "'.");
98  }
99  case SUMO_TAG_VTYPE:
101  break;
103  myAltIsValid = true;
104  startAlternative(attrs);
105  if (!myCurrentIsOk) {
106  myAltIsValid = false;
107  }
108  break;
109  default:
110  break;
111  }
112  // parse embedded vtype information
113  if (myCurrentVType != 0 && element != SUMO_TAG_VTYPE && element != SUMO_TAG_VTYPE__DEPRECATED) {
115  return;
116  }
117  if (!myCurrentIsOk) {
118  throw ProcessError();
119  }
120 }
121 
122 
123 void
125  delete myColor;
126  myColor = 0;
127  if (!myAltIsValid) {
128  return;
129  }
130  if (myCurrentAlternatives == 0) {
131  myCurrentIsOk = true;
132  if (myVehicleParameter != 0) {
133  if (attrs.hasAttribute(SUMO_ATTR_ID)) {
134  WRITE_ERROR("Internal routes do not have an id (vehicle '" + myVehicleParameter->id + "').");
135  myCurrentIsOk = false;
136  return;
137  }
139  } else {
141  }
142  } else {
143  // parse route alternative...
146  if (myCurrentIsOk && myCost < 0 && myCost != -1) {
147  WRITE_ERROR("Invalid cost in alternative for route '" + myCurrentAlternatives->getID() + "' (" + toString<SUMOReal>(myCost) + ").");
148  myCurrentIsOk = false;
149  return;
150  }
151  if (myCurrentIsOk && myProbability < 0) {
152  WRITE_ERROR("Invalid probability in alternative for route '" + myCurrentAlternatives->getID() + "' (" + toString<SUMOReal>(myProbability) + ").");
153  myCurrentIsOk = false;
154  return;
155  }
156  }
157  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
159  attrs.getString(SUMO_ATTR_COLOR),
160  attrs.getObjectType(), myCurrentRouteName.c_str(), true, myCurrentIsOk));
161  }
162  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
164  } else {
166  WRITE_WARNING("Defining routes as a nested string is deprecated, use the edges attribute instead.");
168  }
169  }
170 }
171 
172 
173 void
175  // try to get the id
176  myCurrentIsOk = true;
177  std::string id;
178  if (myVehicleParameter != 0) {
179  id = myVehicleParameter->id;
180  if (id == "") {
181  WRITE_ERROR("Missing 'id' of a routeDistribution.");
182  myCurrentIsOk = false;
183  return;
184  }
185  id = "!" + id;
186  } else {
188  if (!myCurrentIsOk) {
189  return;
190  }
191  }
192  // try to get the index of the last element
193  int index = attrs.getIntReporting(SUMO_ATTR_LAST, id.c_str(), myCurrentIsOk);
194  if (myCurrentIsOk && index < 0) {
195  WRITE_ERROR("Negative index of a route alternative (id='" + id + "').");
196  myCurrentIsOk = false;
197  return;
198  }
199  // build the alternative cont
202 }
203 
204 void
206  const std::string& chars) {
207  // process routes only, all other elements do
208  // not have embedded characters
209  if (element != SUMO_TAG_ROUTE) {
210  return;
211  }
212  if (!myAltIsValid) {
213  return;
214  }
215  if (myCurrentRoute != 0) {
216  return;
217  }
218  // check whether the costs and the probability are valid
219  if (myCurrentAlternatives != 0 && !myCurrentIsOk) {
220  return;
221  }
222  // build the list of edges
223  std::vector<const ROEdge*> *list = new std::vector<const ROEdge*>();
225  ROEdge* edge = myNet.getEdge(myVehicleParameter->fromTaz + "-source");
226  if (edge != 0) {
227  list->push_back(edge);
228  } else {
229  WRITE_ERROR("The vehicle '" + myVehicleParameter->id + "' contains the unknown zone '" + myVehicleParameter->fromTaz + "'.");
230  myCurrentIsOk = false;
231  }
232  }
233  StringTokenizer st(chars);
234  while (myCurrentIsOk && st.hasNext()) { // !!! too slow !!!
235  const std::string id = st.next();
236  ROEdge* edge = myNet.getEdge(id);
237  if (edge != 0) {
238  list->push_back(edge);
239  } else {
240  if (!myTryRepair) {
242  WRITE_ERROR("The route '" + rid + "' contains the unknown edge '" + id + "'.");
243  myCurrentIsOk = false;
244  }
245  }
246  }
248  ROEdge* edge = myNet.getEdge(myVehicleParameter->toTaz + "-sink");
249  if (edge != 0) {
250  list->push_back(edge);
251  } else {
252  WRITE_ERROR("The vehicle '" + myVehicleParameter->id + "' contains the unknown zone '" + myVehicleParameter->toTaz + "'.");
253  myCurrentIsOk = false;
254  }
255  }
256  if (myCurrentIsOk) {
257  if (myCurrentAlternatives != 0) {
260  } else {
262  }
263  myColor = 0;
264  }
265  delete list;
266 }
267 
268 
269 void
271  switch (element) {
272  case SUMO_TAG_ROUTE:
273  if (!myAltIsValid) {
274  return;
275  }
276  if (myCurrentRoute != 0 && myCurrentIsOk) {
277  if (myCurrentAlternatives == 0) {
279  myCurrentRoute = 0;
280  }
281  if (myVehicleParameter == 0) {
282  myNextRouteRead = true;
283  }
284  myCurrentRoute = 0;
285  }
286  break;
288  if (!myCurrentIsOk) {
289  return;
290  }
291  if (myVehicleParameter == 0) {
292  myNextRouteRead = true;
293  }
295  myCurrentRoute = 0;
297  break;
298  case SUMO_TAG_VEHICLE:
299  closeVehicle();
300  delete myVehicleParameter;
301  myVehicleParameter = 0;
302  myNextRouteRead = true;
303  break;
305  case SUMO_TAG_VTYPE: {
308  myCurrentVType = 0;
309  }
310  default:
311  break;
312  }
313  if (!myCurrentIsOk) {
314  throw ProcessError();
315  }
316 }
317 
318 
319 bool
321  // get the vehicle id
323  myCurrentIsOk = false;
324  return false;
325  }
326  // get vehicle type
328  // get the route
330  if (route == 0) {
331  route = myNet.getRouteDef("!" + myVehicleParameter->id);
332  }
333  if (route == 0) {
334  WRITE_ERROR("The route of the vehicle '" + myVehicleParameter->id + "' is not known.");
335  myCurrentIsOk = false;
336  return false;
337  }
338  // build the vehicle
339  if (!MsgHandler::getErrorInstance()->wasInformed()) {
340  ROVehicle* veh = new ROVehicle(*myVehicleParameter, route, type);
342  return true;
343  }
344  return false;
345 }
346 
347 
348 
349 /****************************************************************************/
350