SUMO - Simulation of Urban MObility
NIXMLTrafficLightsHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Importer for traffic lights stored in XML
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2011-2014 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 <string>
34 #include <iostream>
35 #include <xercesc/sax/HandlerBase.hpp>
36 #include <xercesc/sax/AttributeList.hpp>
37 #include <xercesc/sax/SAXParseException.hpp>
38 #include <xercesc/sax/SAXException.hpp>
42 #include <utils/common/ToString.h>
47 #include <netbuild/NBEdge.h>
48 #include <netbuild/NBEdgeCont.h>
49 #include <netbuild/NBNode.h>
50 #include <netbuild/NBOwnTLDef.h>
53 #include "NIImporter_SUMO.h"
55 
56 #ifdef CHECK_MEMORY_LEAKS
57 #include <foreign/nvwa/debug_new.h>
58 #endif // CHECK_MEMORY_LEAKS
59 
60 
61 // ===========================================================================
62 // method definitions
63 // ===========================================================================
65  NBTrafficLightLogicCont& tlCont, NBEdgeCont& ec) :
66  SUMOSAXHandler("xml-tllogics"),
67  myTLLCont(tlCont),
68  myEdgeCont(ec),
69  myCurrentTL(0),
70  myResetPhases(false)
71 {}
72 
73 
75 
76 
77 void
79  int element, const SUMOSAXAttributes& attrs) {
80  switch (element) {
81  case SUMO_TAG_TLLOGIC:
83  break;
84  case SUMO_TAG_PHASE:
85  if (myCurrentTL != 0) {
86  if (myResetPhases) {
88  myResetPhases = false;
89  }
91  }
92  break;
94  addTlConnection(attrs);
95  break;
96  case SUMO_TAG_DELETE:
97  removeTlConnection(attrs);
98  break;
99  default:
100  break;
101  }
102 }
103 
104 
105 void
107  switch (element) {
108  case SUMO_TAG_TLLOGIC:
109  if (!myCurrentTL) {
110  WRITE_ERROR("Unmatched closing tag for tlLogic.");
111  } else {
112  if (!myTLLCont.insert(myCurrentTL)) {
113  WRITE_MESSAGE("Updating program '" + myCurrentTL->getProgramID() +
114  "' for traffic light '" + myCurrentTL->getID() + "'");
115  }
116  myCurrentTL = 0;
117  }
118  break;
119  default:
120  break;
121  }
122 }
123 
124 
127  if (currentTL) {
128  WRITE_ERROR("Definition of tlLogic '" + currentTL->getID() + "' was not finished.");
129  return 0;
130  }
131  bool ok = true;
132  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
133  std::string programID = attrs.getOpt<std::string>(SUMO_ATTR_PROGRAMID, id.c_str(), ok, "<unknown>");
134  SUMOTime offset = attrs.hasAttribute(SUMO_ATTR_OFFSET) ? TIME2STEPS(attrs.get<SUMOReal>(SUMO_ATTR_OFFSET, id.c_str(), ok)) : 0;
135  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, 0, ok,
136  OptionsCont::getOptions().getString("tls.default-type"));
137  TrafficLightType type;
138  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
140  } else {
141  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for tlLogic '" + id + "'.");
142  return 0;
143  }
144  // there are two scenarios to consider
145  // 1) the tll.xml is loaded to update traffic lights defined in a net.xml:
146  // simply retrieve the loaded definitions and update them
147  // 2) the tll.xml is loaded to define new traffic lights
148  // nod.xml will have triggered building of NBOwnTLDef. Replace it with NBLoadedSUMOTLDef
149  NBLoadedSUMOTLDef* loadedDef = dynamic_cast<NBLoadedSUMOTLDef*>(myTLLCont.getDefinition(id, programID));
150  if (loadedDef == 0) {
151  // case 2
154  if (newDef == 0) {
155  // the default program may have already been replaced with a loaded program
156  newDef = dynamic_cast<NBLoadedSUMOTLDef*>(myTLLCont.getDefinition(
158  if (newDef == 0) {
159  WRITE_ERROR("Cannot load traffic light program for unknown id '" + id + "', programID '" + programID + "'.");
160  return 0;
161  }
162  }
163  assert(newDef != 0);
164  loadedDef = new NBLoadedSUMOTLDef(id, programID, offset, type);
165  // copy nodes and controlled inner edges
166  std::vector<NBNode*> nodes = newDef->getNodes();
167  for (std::vector<NBNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) {
168  loadedDef->addNode(*it);
169  }
170  loadedDef->addControlledInnerEdges(newDef->getControlledInnerEdges());
172  // replace default Program
173  std::vector<NBNode*> nodes = newDef->getNodes();
174  for (std::vector<NBNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) {
175  (*it)->removeTrafficLight(newDef);
176  }
178  }
179  myTLLCont.insert(loadedDef);
180  }
181  if (ok) {
182  myResetPhases = true;
183  return loadedDef;
184  } else {
185  return 0;
186  }
187 }
188 
189 
190 void
192  bool ok = true;
193  // parse identifying attributes
194  NBEdge* from = retrieveEdge(attrs, SUMO_ATTR_FROM, ok);
195  NBEdge* to = retrieveEdge(attrs, SUMO_ATTR_TO, ok);
196  if (!ok) {
197  return;
198  }
199  int fromLane = retrieveLaneIndex(attrs, SUMO_ATTR_FROM_LANE, from, ok);
200  int toLane = retrieveLaneIndex(attrs, SUMO_ATTR_TO_LANE, to, ok);
201  if (!ok) {
202  return;
203  }
204  // retrieve connection
205  const std::vector<NBEdge::Connection>& connections = from->getConnections();
206  std::vector<NBEdge::Connection>::const_iterator con_it;
207  con_it = find_if(connections.begin(), connections.end(),
208  NBEdge::connections_finder(fromLane, to, toLane));
209  if (con_it == connections.end()) {
210  WRITE_ERROR("Connection from=" + from->getID() + " to=" + to->getID() +
211  " fromLane=" + toString(fromLane) + " toLane=" + toString(toLane) + " not found");
212  return;
213  }
214  NBEdge::Connection c = *con_it;
215  // read other attributes
216  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, "");
217  if (tlID == "") {
218  // we are updating an existing tl-controlled connection
219  tlID = c.tlID;
220  assert(tlID != "");
221  }
222  int tlIndex = attrs.getOpt<int>(SUMO_ATTR_TLLINKINDEX, 0, ok, -1);
223  if (tlIndex == -1) {
224  // we are updating an existing tl-controlled connection
225  tlIndex = c.tlLinkNo;
226  }
227 
228  // register the connection with all definitions
229  const std::map<std::string, NBTrafficLightDefinition*>& programs = myTLLCont.getPrograms(tlID);
230  if (programs.size() > 0) {
231  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
232  for (it = programs.begin(); it != programs.end(); it++) {
233  NBLoadedSUMOTLDef* tlDef = dynamic_cast<NBLoadedSUMOTLDef*>(it->second);
234  if (tlDef) {
235  tlDef->addConnection(from, c.toEdge, c.fromLane, c.toLane, tlIndex);
236  } else {
237  throw ProcessError("Corrupt traffic light definition '"
238  + tlID + "' (program '" + it->first + "')");
239  }
240  }
241  } else {
242  WRITE_ERROR("The traffic light '" + tlID + "' is not known.");
243  }
244 }
245 
246 
247 void
249  bool ok = true;
250  std::string tlID = attrs.get<std::string>(SUMO_ATTR_TLID, 0, ok);
251  // does the traffic light still exist?
252  const std::map<std::string, NBTrafficLightDefinition*>& programs = myTLLCont.getPrograms(tlID);
253  if (programs.size() > 0) {
254  // parse identifying attributes
255  NBEdge* from = retrieveEdge(attrs, SUMO_ATTR_FROM, ok);
256  NBEdge* to = retrieveEdge(attrs, SUMO_ATTR_TO, ok);
257  if (!ok) {
258  return;
259  }
260  int fromLane = retrieveLaneIndex(attrs, SUMO_ATTR_FROM_LANE, from, ok);
261  int toLane = retrieveLaneIndex(attrs, SUMO_ATTR_TO_LANE, to, ok);
262  if (!ok) {
263  return;
264  }
265  int tlIndex = attrs.get<int>(SUMO_ATTR_TLLINKINDEX, 0, ok);
266 
267  NBConnection conn(from, fromLane, to, toLane, tlIndex);
268  // remove the connection from all definitions
269  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
270  for (it = programs.begin(); it != programs.end(); it++) {
271  NBLoadedSUMOTLDef* tlDef = dynamic_cast<NBLoadedSUMOTLDef*>(it->second);
272  if (tlDef) {
273  tlDef->removeConnection(conn, false);
274  } else {
275  throw ProcessError("Corrupt traffic light definition '"
276  + tlID + "' (program '" + it->first + "')");
277  }
278  }
279  }
280 }
281 
282 
283 NBEdge*
285  const SUMOSAXAttributes& attrs, SumoXMLAttr attr, bool& ok) {
286  std::string edgeID = attrs.get<std::string>(attr, 0, ok);
287  NBEdge* edge = myEdgeCont.retrieve(edgeID, true);
288  if (edge == 0) {
289  WRITE_ERROR("Unknown edge '" + edgeID + "' given in connection.");
290  ok = false;
291  }
292  return edge;
293 }
294 
295 
296 int
298  const SUMOSAXAttributes& attrs, SumoXMLAttr attr, NBEdge* edge, bool& ok) {
299  int laneIndex = attrs.get<int>(attr, 0, ok);
300  if (edge->getNumLanes() <= (size_t) laneIndex) {
301  WRITE_ERROR("Invalid lane index '" + toString(laneIndex) + "' for edge '" + edge->getID() + "'.");
302  ok = false;
303  }
304  return laneIndex;
305 }
306 
307 
308 /****************************************************************************/
309 
void removeConnection(const NBConnection &conn, bool reconstruct=true)
removes the given connection from the traffic light if recontruct=true, reconstructs the logic and in...
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
A structure which describes a connection between edges or lanes.
Definition: NBEdge.h:148
int toLane
The lane the connections yields in.
Definition: NBEdge.h:166
static void addPhase(const SUMOSAXAttributes &attrs, NBLoadedSUMOTLDef *currentTL)
adds a phase to the traffic lights logic currently build
bool removeProgram(const std::string id, const std::string programID, bool del=true)
Removes a program of a logic definition from the dictionary.
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
NBEdge * toEdge
The edge the connections yields in.
Definition: NBEdge.h:164
A loaded (complete) traffic light logic.
NBEdgeCont & myEdgeCont
The edge container for retrieving edges.
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
A container for traffic light definitions and built programs.
NBEdge * retrieveEdge(const SUMOSAXAttributes &attrs, SumoXMLAttr attr, bool &ok)
parses and edge id an returns an existing edge
const std::string & getProgramID() const
Returns the ProgramID.
The representation of a single edge during network building.
Definition: NBEdge.h:71
The base class for traffic light logic definitions.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
NBLoadedSUMOTLDef * initTrafficLightLogic(const SUMOSAXAttributes &attrs, NBLoadedSUMOTLDef *currentTL)
SAX-handler base for SUMO-files.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
NBTrafficLightDefinition * getDefinition(const std::string &id, const std::string &programID) const
Returns the named definition.
NBTrafficLightLogicCont & myTLLCont
The traffic light container to fill.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
const std::string & getID() const
Returns the id.
Definition: Named.h:60
int retrieveLaneIndex(const SUMOSAXAttributes &attrs, SumoXMLAttr attr, NBEdge *edge, bool &ok)
parses a lane index and verifies its correctness
Encapsulated SAX-Attributes.
std::string tlID
The id of the traffic light that controls this connection.
Definition: NBEdge.h:168
void addTlConnection(const SUMOSAXAttributes &attrs)
reads and adds tl-controlled connection
static StringBijection< TrafficLightType > TrafficLightTypes
int fromLane
The lane the connections starts at.
Definition: NBEdge.h:162
unsigned int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:344
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
NBLoadedSUMOTLDef * myCurrentTL
The currently parsed traffic light.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
void removeTlConnection(const SUMOSAXAttributes &attrs)
reads and removes tl-controlled connection
NBTrafficLightLogic * getLogic()
Returns the internal logic.
static const std::string DefaultProgramID
void addControlledInnerEdges(const std::vector< std::string > &edges)
Adds the given ids into the list of inner edges controlled by the tls.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
bool myResetPhases
whether phases of a previously loaded traffic light must be reset
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
void myEndElement(int element)
Called when a closing tag occurs.
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:245
unsigned int tlLinkNo
The index of this connection within the controlling traffic light.
Definition: NBEdge.h:170
T get(const std::string &str) const
NIXMLTrafficLightsHandler(NBTrafficLightLogicCont &tlCont, NBEdgeCont &ec)
Constructor.
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
#define SUMOReal
Definition: config.h:215
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.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
const std::vector< Connection > & getConnections() const
Returns the connections.
Definition: NBEdge.h:747
void addConnection(NBEdge *from, NBEdge *to, int fromLane, int toLane, int linkIndex)
Adds a connection and immediately informs the edges.
TrafficLightType