SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RONetHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // The handler for SUMO-Networks
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>
39 #include <utils/common/ToString.h>
42 #include "ROEdge.h"
43 #include "ROLane.h"
44 #include "RONode.h"
45 #include "RONet.h"
46 #include "RONetHandler.h"
47 #include "ROAbstractEdgeBuilder.h"
48 
49 #ifdef CHECK_MEMORY_LEAKS
50 #include <foreign/nvwa/debug_new.h>
51 #endif // CHECK_MEMORY_LEAKS
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
59  : SUMOSAXHandler("sumo-network"),
60  myNet(net), myCurrentName(),
61  myCurrentEdge(0), myEdgeBuilder(eb) {}
62 
63 
65 
66 
67 void
69  const SUMOSAXAttributes& attrs) {
70  switch (element) {
71  case SUMO_TAG_EDGE:
72  // in the first step, we do need the name to allocate the edge
73  // in the second, we need it to know to which edge we have to add
74  // the following edges to
75  parseEdge(attrs);
76  break;
77  case SUMO_TAG_LANE:
78  if (myProcess) {
79  parseLane(attrs);
80  }
81  break;
82  case SUMO_TAG_JUNCTION:
83  parseJunction(attrs);
84  break;
86  parseConnection(attrs);
87  break;
88  case SUMO_TAG_TAZ:
89  parseDistrict(attrs);
90  break;
91  case SUMO_TAG_TAZSOURCE:
92  parseDistrictEdge(attrs, true);
93  break;
94  case SUMO_TAG_TAZSINK:
95  parseDistrictEdge(attrs, false);
96  break;
97  default:
98  break;
99  }
100 }
101 
102 
103 void
105  // get the id, report an error if not given or empty...
106  bool ok = true;
107  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
108  if (!ok) {
109  throw ProcessError();
110  }
111  // get the edge
112  myCurrentEdge = 0;
113  if (myCurrentName[0] == ':') {
114  // this is an internal edge - we will not use it
115  // !!! recheck this; internal edges may be of importance during the dua
116  return;
117  }
118  const std::string from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
119  const std::string to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
120  const std::string type = attrs.hasAttribute(SUMO_ATTR_FUNCTION) ? attrs.get<std::string>(SUMO_ATTR_FUNCTION, myCurrentName.c_str(), ok) : "";
121  const int priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
122  if (!ok) {
123  return;
124  }
125  RONode* fromNode = myNet.getNode(from);
126  if (fromNode == 0) {
127  fromNode = new RONode(from);
128  myNet.addNode(fromNode);
129  }
130  RONode* toNode = myNet.getNode(to);
131  if (toNode == 0) {
132  toNode = new RONode(to);
133  myNet.addNode(toNode);
134  }
135  // build the edge
136  myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority);
137  if (myNet.addEdge(myCurrentEdge)) {
138  // get the type of the edge
139  myProcess = true;
140  if (type == "" || type == "normal" || type == "connector") {
142  } else if (type == "source") {
144  } else if (type == "sink") {
146  } else if (type == "internal") {
147  myProcess = false;
148  } else {
149  WRITE_ERROR("Edge '" + myCurrentName + "' has an unknown type.");
150  return;
151  }
152  } else {
153  myCurrentEdge = 0;
154  }
155 }
156 
157 
158 void
160  if (myCurrentEdge == 0) {
161  // was an internal edge to skip or an error occured
162  return;
163  }
164  bool ok = true;
165  // get the id, report an error if not given or empty...
166  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
167  if (!ok) {
168  return;
169  }
170  // get the speed
171  SUMOReal maxSpeed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, id.c_str(), ok);
172  SUMOReal length = attrs.get<SUMOReal>(SUMO_ATTR_LENGTH, id.c_str(), ok);
173  std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
174  std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
175  if (!ok) {
176  return;
177  }
178  // get the length
179  // get the vehicle classes
180  SVCPermissions permissions = parseVehicleClasses(allow, disallow);
181  if (permissions != SVCFreeForAll) {
183  }
184  // add when both values are valid
185  if (maxSpeed > 0 && length > 0 && id.length() > 0) {
186  myCurrentEdge->addLane(new ROLane(id, length, maxSpeed, permissions));
187  }
188 }
189 
190 
191 void
193  bool ok = true;
194  // get the id, report an error if not given or empty...
195  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
196  if (!ok) {
197  return;
198  }
199  // get the position of the node
200  SUMOReal x = attrs.get<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok);
201  SUMOReal y = attrs.get<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok);
202  if (ok) {
203  RONode* n = myNet.getNode(id);
204  if (n == 0) {
205  n = new RONode(id);
206  myNet.addNode(n);
207  }
208  n->setPosition(Position(x, y));
209  } else {
210  throw ProcessError();
211  }
212 }
213 
214 
215 void
217  bool ok = true;
218  std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, 0, ok);
219  std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, 0, ok);
220  std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, 0, ok);
221  if (fromID[0] == ':') { // skip inner lane connections
222  return;
223  }
224  ROEdge* from = myNet.getEdge(fromID);
225  ROEdge* to = myNet.getEdge(toID);
226  if (from == 0) {
227  throw ProcessError("unknown from-edge '" + fromID + "' in connection");
228  }
229  if (to == 0) {
230  throw ProcessError("unknown to-edge '" + toID + "' in connection");
231  }
232  from->addFollower(to, dir);
233 }
234 
235 
236 void
238  myCurrentEdge = 0;
239  bool ok = true;
240  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
241  if (!ok) {
242  return;
243  }
244  ROEdge* sink = myEdgeBuilder.buildEdge(myCurrentName + "-sink", 0, 0, 0);
246  myNet.addEdge(sink);
247  ROEdge* source = myEdgeBuilder.buildEdge(myCurrentName + "-source", 0, 0, 0);
248  source->setType(ROEdge::ET_DISTRICT);
249  myNet.addEdge(source);
250  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
251  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
252  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
253  ROEdge* edge = myNet.getEdge(*i);
254  // check whether the edge exists
255  if (edge == 0) {
256  throw ProcessError("The edge '" + *i + "' within district '" + myCurrentName + "' is not known.");
257  }
258  source->addFollower(edge);
259  edge->addFollower(sink);
260  }
261  }
262 }
263 
264 
265 void
267  bool ok = true;
268  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
269  ROEdge* succ = myNet.getEdge(id);
270  if (succ != 0) {
271  // connect edge
272  if (isSource) {
273  myNet.getEdge(myCurrentName + "-source")->addFollower(succ);
274  } else {
275  succ->addFollower(myNet.getEdge(myCurrentName + "-sink"));
276  }
277  } else {
278  WRITE_ERROR("At district '" + myCurrentName + "': succeeding edge '" + id + "' does not exist.");
279  }
280 }
281 
282 
283 
284 /****************************************************************************/
285