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 
37 #include <utils/common/ToString.h>
41 #include "RORouteDef.h"
42 #include "RONet.h"
43 #include "ROVehicle.h"
44 #include "RORoute.h"
45 #include "RORDLoader_SUMOBase.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 bool tryRepair,
57  const bool withTaz, const std::string& file)
58  : ROTypedXMLRoutesLoader(net, begin, end, file),
59  myVehicleParameter(0), myColor(0), myCurrentIsOk(true),
60  myAltIsValid(true), myCurrentAlternatives(0), myTryRepair(tryRepair),
61  myWithTaz(withTaz), myCurrentRoute(0), myCurrentVType(0) {
62 }
63 
64 
66  // clean up (on failure)
67  delete myCurrentAlternatives;
68  delete myCurrentRoute;
69  delete myVehicleParameter;
70  delete myColor;
71 }
72 
73 
74 void
76  const SUMOSAXAttributes& attrs) {
77  switch (element) {
78  case SUMO_TAG_ROUTE:
79  startRoute(attrs);
80  break;
81  case SUMO_TAG_VEHICLE:
82  // try to parse the vehicle definition
83  delete myVehicleParameter;
86  if (myVehicleParameter != 0) {
88  }
90  break;
91  case SUMO_TAG_VTYPE:
93  break;
95  myAltIsValid = true;
96  startAlternative(attrs);
97  if (!myCurrentIsOk) {
98  myAltIsValid = false;
99  }
100  break;
101  default:
102  break;
103  }
104  // parse embedded vtype information
105  if (myCurrentVType != 0 && element != SUMO_TAG_VTYPE) {
107  return;
108  }
109  if (!myCurrentIsOk) {
110  throw ProcessError();
111  }
112 }
113 
114 
115 void
117  delete myColor;
118  myColor = 0;
119  if (!myAltIsValid) {
120  return;
121  }
122  if (myCurrentAlternatives == 0) {
123  myCurrentIsOk = true;
124  if (myVehicleParameter != 0) {
125  if (attrs.hasAttribute(SUMO_ATTR_ID)) {
126  WRITE_ERROR("Internal routes do not have an id (vehicle '" + myVehicleParameter->id + "').");
127  myCurrentIsOk = false;
128  return;
129  }
131  } else {
133  }
134  } else {
135  // parse route alternative...
138  if (myCurrentIsOk && myCost < 0 && myCost != -1) {
139  WRITE_ERROR("Invalid cost in alternative for route '" + myCurrentAlternatives->getID() + "' (" + toString<SUMOReal>(myCost) + ").");
140  myCurrentIsOk = false;
141  return;
142  }
143  if (myCurrentIsOk && myProbability < 0) {
144  WRITE_ERROR("Invalid probability in alternative for route '" + myCurrentAlternatives->getID() + "' (" + toString<SUMOReal>(myProbability) + ").");
145  myCurrentIsOk = false;
146  return;
147  }
148  }
149  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
151  }
152  std::string edges = attrs.getStringReporting(SUMO_ATTR_EDGES, myCurrentRouteName.c_str(), myCurrentIsOk);
153  if (myCurrentIsOk) {
154  parseRoute(edges);
155  }
156 }
157 
158 
159 void
161  // try to get the id
162  myCurrentIsOk = true;
163  std::string id;
164  if (myVehicleParameter != 0) {
165  id = myVehicleParameter->id;
166  if (id == "") {
167  WRITE_ERROR("Missing 'id' of a routeDistribution.");
168  myCurrentIsOk = false;
169  return;
170  }
171  id = "!" + id;
172  } else {
174  if (!myCurrentIsOk) {
175  return;
176  }
177  }
178  // try to get the index of the last element
179  int index = attrs.getIntReporting(SUMO_ATTR_LAST, id.c_str(), myCurrentIsOk);
180  if (myCurrentIsOk && index < 0) {
181  WRITE_ERROR("Negative index of a route alternative (id='" + id + "').");
182  myCurrentIsOk = false;
183  return;
184  }
185  // build the alternative cont
186  myCurrentAlternatives = new RORouteDef(id, index, false);
187 }
188 
189 
190 void
191 RORDLoader_SUMOBase::parseRoute(const std::string& chars) {
192  if (!myAltIsValid) {
193  return;
194  }
195  if (myCurrentRoute != 0) {
196  return;
197  }
198  // check whether the costs and the probability are valid
199  if (myCurrentAlternatives != 0 && !myCurrentIsOk) {
200  return;
201  }
202  // build the list of edges
203  std::vector<const ROEdge*>* list = new std::vector<const ROEdge*>();
205  ROEdge* edge = myNet.getEdge(myVehicleParameter->fromTaz + "-source");
206  if (edge != 0) {
207  list->push_back(edge);
208  } else {
209  WRITE_ERROR("The vehicle '" + myVehicleParameter->id + "' contains the unknown zone '" + myVehicleParameter->fromTaz + "'.");
210  myCurrentIsOk = false;
211  }
212  }
213  if (chars[0] == BinaryFormatter::BF_ROUTE) {
214  std::istringstream in(chars, std::ios::binary);
215  char c;
216  in >> c;
218  FileHelpers::readEdgeVector(in, *list, rid);
219  } else {
220  StringTokenizer st(chars);
221  while (myCurrentIsOk && st.hasNext()) { // !!! too slow !!!
222  const std::string id = st.next();
223  ROEdge* edge = myNet.getEdge(id);
224  if (edge != 0) {
225  list->push_back(edge);
226  } else {
227  if (!myTryRepair) {
229  WRITE_ERROR("The route '" + rid + "' contains the unknown edge '" + id + "'.");
230  myCurrentIsOk = false;
231  }
232  }
233  }
234  }
236  ROEdge* edge = myNet.getEdge(myVehicleParameter->toTaz + "-sink");
237  if (edge != 0) {
238  list->push_back(edge);
239  } else {
240  WRITE_ERROR("The vehicle '" + myVehicleParameter->id + "' contains the unknown zone '" + myVehicleParameter->toTaz + "'.");
241  myCurrentIsOk = false;
242  }
243  }
244  if (myCurrentIsOk) {
245  if (myCurrentAlternatives != 0) {
248  } else {
251  }
252  myColor = 0;
253  }
254  delete list;
255 }
256 
257 
258 void
260  switch (element) {
261  case SUMO_TAG_ROUTE:
262  if (!myAltIsValid) {
263  return;
264  }
265  if (myCurrentRoute != 0 && myCurrentIsOk) {
266  if (myCurrentAlternatives == 0) {
268  myCurrentRoute = 0;
269  }
270  if (myVehicleParameter == 0) {
271  myNextRouteRead = true;
272  }
273  myCurrentRoute = 0;
274  }
275  break;
277  if (!myCurrentIsOk) {
278  return;
279  }
280  if (myVehicleParameter == 0) {
281  myNextRouteRead = true;
282  }
284  myCurrentRoute = 0;
286  break;
287  case SUMO_TAG_VEHICLE:
288  closeVehicle();
289  delete myVehicleParameter;
290  myVehicleParameter = 0;
291  myNextRouteRead = true;
292  break;
293  case SUMO_TAG_VTYPE: {
296  myCurrentVType = 0;
297  }
298  default:
299  break;
300  }
301  if (!myCurrentIsOk) {
302  throw ProcessError();
303  }
304 }
305 
306 
307 bool
309  // get the vehicle id
311  myCurrentIsOk = false;
312  return false;
313  }
314  // get vehicle type
316  // get the route
318  if (route == 0) {
319  route = myNet.getRouteDef("!" + myVehicleParameter->id);
320  } else {
321  route = route->copy("!" + myVehicleParameter->id);
322  }
323  if (route == 0) {
324  WRITE_ERROR("The route of the vehicle '" + myVehicleParameter->id + "' is not known.");
325  myCurrentIsOk = false;
326  return false;
327  }
328  // build the vehicle
329  if (!MsgHandler::getErrorInstance()->wasInformed()) {
330  ROVehicle* veh = new ROVehicle(*myVehicleParameter, route, type);
332  return true;
333  }
334  return false;
335 }
336 
337 
338 
339 /****************************************************************************/
340