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 
std::string myCurrentName
The name of the edge/node that is currently processed.
Definition: RONetHandler.h:169
ROAbstractEdgeBuilder & myEdgeBuilder
The object used to build of edges of the desired type.
Definition: RONetHandler.h:178
A single lane the router may use.
Definition: ROLane.h:51
void parseDistrict(const SUMOSAXAttributes &attrs)
void addNode(RONode *node)
Definition: RONet.cpp:91
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:101
void parseJunction(const SUMOSAXAttributes &attrs)
Parses a junction&#39;s position.
virtual void addLane(ROLane *lane)
Adds a lane to the edge while loading.
Definition: ROEdge.cpp:86
virtual void parseLane(const SUMOSAXAttributes &attrs)
Parses and builds a lane.
SAX-handler base for SUMO-files.
Interface for building instances of router-edges.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
An edge where vehicles are inserted at (no vehicle may come from back)
Definition: ROEdge.h:79
RONetHandler(RONet &net, ROAbstractEdgeBuilder &eb)
Constructor.
void setType(EdgeType type)
Sets the type of te edge.
Definition: ROEdge.cpp:312
the edges of a route
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
An edge representing a whole district.
Definition: ROEdge.h:77
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers. ...
void setPosition(const Position &p)
Sets the position of the node.
Definition: RONode.cpp:51
virtual ~RONetHandler()
Destructor.
const SVCPermissions SVCFreeForAll
A basic edge for routing applications.
Definition: ROEdge.h:67
virtual std::vector< std::string > getStringVector(int attr) const =0
Tries to read given attribute assuming it is a string vector.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
virtual ROEdge * buildEdge(const std::string &name, RONode *from, RONode *to, const int priority)=0
Builds an edge with the given name.
The router&#39;s network representation.
Definition: RONet.h:65
void setRestrictionFound()
Definition: RONet.cpp:400
A normal edge.
Definition: ROEdge.h:75
RONode * getNode(const std::string &id) const
Retrieves an node from the network.
Definition: RONet.h:123
The abstract direction of a link.
virtual bool addEdge(ROEdge *edge)
Definition: RONet.cpp:80
RONet & myNet
The net to store the information into.
Definition: RONetHandler.h:166
void parseConnection(const SUMOSAXAttributes &attrs)
#define SUMOReal
Definition: config.h:221
Base class for nodes used by the router.
Definition: RONode.h:46
An edge where vehicles disappear (no vehicle may leave this edge)
Definition: ROEdge.h:81
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.
bool myProcess
An indicator whether the next edge shall be read (internal edges are not read by now) ...
Definition: RONetHandler.h:175
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual void addFollower(ROEdge *s, std::string dir="")
Adds information about a connected edge.
Definition: ROEdge.cpp:100
ROEdge * myCurrentEdge
The currently built edge.
Definition: RONetHandler.h:172
void parseDistrictEdge(const SUMOSAXAttributes &attrs, bool isSource)
void parseEdge(const SUMOSAXAttributes &attrs)
Parses and builds an edge.