SUMO - Simulation of Urban MObility
NIXMLTrafficLightsHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2011-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // Importer for traffic lights stored in XML
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
33 #include <iostream>
34 #include <xercesc/sax/HandlerBase.hpp>
35 #include <xercesc/sax/AttributeList.hpp>
36 #include <xercesc/sax/SAXParseException.hpp>
37 #include <xercesc/sax/SAXException.hpp>
41 #include <utils/common/ToString.h>
46 #include <netbuild/NBEdge.h>
47 #include <netbuild/NBEdgeCont.h>
48 #include <netbuild/NBNode.h>
49 #include <netbuild/NBOwnTLDef.h>
52 #include "NIImporter_SUMO.h"
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
60  NBTrafficLightLogicCont& tlCont, NBEdgeCont& ec) :
61  SUMOSAXHandler("xml-tllogics"),
62  myTLLCont(tlCont),
63  myEdgeCont(ec),
64  myCurrentTL(0),
65  myResetPhases(false) {
66 }
67 
68 
70 
71 
72 void
74  int element, const SUMOSAXAttributes& attrs) {
75  switch (element) {
76  case SUMO_TAG_TLLOGIC:
78  break;
79  case SUMO_TAG_PHASE:
80  if (myCurrentTL != 0) {
81  if (myResetPhases) {
83  myResetPhases = false;
84  }
87  }
88  break;
90  addTlConnection(attrs);
91  break;
92  case SUMO_TAG_DELETE:
93  removeTlConnection(attrs);
94  break;
95  case SUMO_TAG_PARAM:
96  if (myCurrentTL != 0) {
97  bool ok = true;
98  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, ok);
99  // circumventing empty string test
100  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
101  myCurrentTL->setParameter(key, val);
102  }
103  default:
104  break;
105  }
106 }
107 
108 
109 void
111  switch (element) {
112  case SUMO_TAG_TLLOGIC:
113  if (!myCurrentTL) {
114  WRITE_ERROR("Unmatched closing tag for tlLogic.");
115  } else {
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<double>(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 three 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  // 3) the tll.xml is loaded to define new programs for a defined traffic light
150  // there should be a definition with the same id but different programID
151  const std::map<std::string, NBTrafficLightDefinition*>& programs = myTLLCont.getPrograms(id);
152  if (programs.size() == 0) {
153  WRITE_ERROR("Cannot load traffic light program for unknown id '" + id + "', programID '" + programID + "'.");
154  return 0;
155  }
156  const std::string existingProgram = programs.begin()->first; // arbitrary for our purpose
157  NBLoadedSUMOTLDef* loadedDef = dynamic_cast<NBLoadedSUMOTLDef*>(myTLLCont.getDefinition(id, programID));
158  if (loadedDef == 0) {
159  NBLoadedSUMOTLDef* oldDef = dynamic_cast<NBLoadedSUMOTLDef*>(myTLLCont.getDefinition(id, existingProgram));
160  if (oldDef == 0) {
161  // case 2
164  if (newDef == 0) {
165  // the default program may have already been replaced with a loaded program
166  newDef = dynamic_cast<NBLoadedSUMOTLDef*>(myTLLCont.getDefinition(
168  if (newDef == 0) {
169  WRITE_ERROR("Cannot load traffic light program for unknown id '" + id + "', programID '" + programID + "'.");
170  return 0;
171  }
172  }
173  assert(newDef != 0);
174  loadedDef = new NBLoadedSUMOTLDef(id, programID, offset, type);
175  // copy nodes and controlled inner edges
176  std::vector<NBNode*> nodes = newDef->getNodes();
177  for (std::vector<NBNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) {
178  loadedDef->addNode(*it);
179  }
180  loadedDef->addControlledInnerEdges(newDef->getControlledInnerEdges());
182  // replace default Program
183  std::vector<NBNode*> nodes = newDef->getNodes();
184  for (std::vector<NBNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) {
185  (*it)->removeTrafficLight(newDef);
186  }
188  }
189  myTLLCont.insert(loadedDef);
190  } else {
191  // case 3
192  NBTrafficLightLogic* oldLogic = oldDef->getLogic();
193  NBTrafficLightLogic* newLogic = new NBTrafficLightLogic(id, programID,
194  oldLogic->getNumLinks(), offset, type);
195  loadedDef = new NBLoadedSUMOTLDef(oldDef, newLogic);
196  // copy nodes
197  std::vector<NBNode*> nodes = oldDef->getNodes();
198  for (std::vector<NBNode*>::iterator it = nodes.begin(); it != nodes.end(); it++) {
199  loadedDef->addNode(*it);
200  }
201  myTLLCont.insert(loadedDef);
202  }
203  } else {
204  // case 1
205  loadedDef->setOffset(offset);
206  loadedDef->setType(type);
207  }
208  if (ok) {
209  myResetPhases = true;
210  return loadedDef;
211  } else {
212  return 0;
213  }
214 }
215 
216 
217 void
219  bool ok = true;
220  // parse identifying attributes
221  NBEdge* from = retrieveEdge(attrs, SUMO_ATTR_FROM, ok);
222  NBEdge* to = retrieveEdge(attrs, SUMO_ATTR_TO, ok);
223  if (!ok) {
224  return;
225  }
226  int fromLane = retrieveLaneIndex(attrs, SUMO_ATTR_FROM_LANE, from, ok);
227  int toLane = retrieveLaneIndex(attrs, SUMO_ATTR_TO_LANE, to, ok);
228  if (!ok) {
229  return;
230  }
231  // retrieve connection
232  const std::vector<NBEdge::Connection>& connections = from->getConnections();
233  std::vector<NBEdge::Connection>::const_iterator con_it;
234  con_it = find_if(connections.begin(), connections.end(),
235  NBEdge::connections_finder(fromLane, to, toLane));
236  if (con_it == connections.end()) {
237  WRITE_ERROR("Connection from=" + from->getID() + " to=" + to->getID() +
238  " fromLane=" + toString(fromLane) + " toLane=" + toString(toLane) + " not found");
239  return;
240  }
241  NBEdge::Connection c = *con_it;
242  // read other attributes
243  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, "");
244  if (tlID == "") {
245  // we are updating an existing tl-controlled connection
246  tlID = c.tlID;
247  assert(tlID != "");
248  }
249  int tlIndex = attrs.getOpt<int>(SUMO_ATTR_TLLINKINDEX, 0, ok, -1);
250  if (tlIndex == -1) {
251  // we are updating an existing tl-controlled connection
252  tlIndex = c.tlLinkNo;
253  }
254 
255  // register the connection with all definitions
256  const std::map<std::string, NBTrafficLightDefinition*>& programs = myTLLCont.getPrograms(tlID);
257  if (programs.size() > 0) {
258  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
259  for (it = programs.begin(); it != programs.end(); it++) {
260  NBLoadedSUMOTLDef* tlDef = dynamic_cast<NBLoadedSUMOTLDef*>(it->second);
261  if (tlDef) {
262  tlDef->addConnection(from, c.toEdge, c.fromLane, c.toLane, tlIndex, false);
263  } else {
264  throw ProcessError("Corrupt traffic light definition '"
265  + tlID + "' (program '" + it->first + "')");
266  }
267  }
268  } else {
269  SumoXMLNodeType type = from->getToNode()->getType();
270  if (type != NODETYPE_RAIL_CROSSING && type != NODETYPE_RAIL_SIGNAL) {
271  WRITE_ERROR("The traffic light '" + tlID + "' is not known.");
272  }
273  }
274 }
275 
276 
277 void
279  bool ok = true;
280  std::string tlID = attrs.get<std::string>(SUMO_ATTR_TLID, 0, ok);
281  // does the traffic light still exist?
282  const std::map<std::string, NBTrafficLightDefinition*>& programs = myTLLCont.getPrograms(tlID);
283  if (programs.size() > 0) {
284  // parse identifying attributes
285  NBEdge* from = retrieveEdge(attrs, SUMO_ATTR_FROM, ok);
286  NBEdge* to = retrieveEdge(attrs, SUMO_ATTR_TO, ok);
287  if (!ok) {
288  return;
289  }
290  int fromLane = retrieveLaneIndex(attrs, SUMO_ATTR_FROM_LANE, from, ok);
291  int toLane = retrieveLaneIndex(attrs, SUMO_ATTR_TO_LANE, to, ok);
292  if (!ok) {
293  return;
294  }
295  int tlIndex = attrs.get<int>(SUMO_ATTR_TLLINKINDEX, 0, ok);
296 
297  NBConnection conn(from, fromLane, to, toLane, tlIndex);
298  // remove the connection from all definitions
299  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
300  for (it = programs.begin(); it != programs.end(); it++) {
301  NBLoadedSUMOTLDef* tlDef = dynamic_cast<NBLoadedSUMOTLDef*>(it->second);
302  if (tlDef) {
303  tlDef->removeConnection(conn, false);
304  } else {
305  throw ProcessError("Corrupt traffic light definition '"
306  + tlID + "' (program '" + it->first + "')");
307  }
308  }
309  }
310 }
311 
312 
313 NBEdge*
315  const SUMOSAXAttributes& attrs, SumoXMLAttr attr, bool& ok) {
316  std::string edgeID = attrs.get<std::string>(attr, 0, ok);
317  NBEdge* edge = myEdgeCont.retrieve(edgeID, true);
318  if (edge == 0) {
319  WRITE_ERROR("Unknown edge '" + edgeID + "' given in connection.");
320  ok = false;
321  }
322  return edge;
323 }
324 
325 
326 int
328  const SUMOSAXAttributes& attrs, SumoXMLAttr attr, NBEdge* edge, bool& ok) {
329  int laneIndex = attrs.get<int>(attr, 0, ok);
330  if (edge->getNumLanes() <= laneIndex) {
331  WRITE_ERROR("Invalid lane index '" + toString(laneIndex) + "' for edge '" + edge->getID() + "'.");
332  ok = false;
333  }
334  return laneIndex;
335 }
336 
337 
338 /****************************************************************************/
339 
int tlLinkNo
The index of this connection within the controlling traffic light.
Definition: NBEdge.h:195
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
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:161
int toLane
The lane the connections yields in.
Definition: NBEdge.h:189
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:186
void setOffset(SUMOTime offset)
Sets the offset of this tls.
int getNumLinks()
Returns the number of participating links.
A loaded (complete) traffic light logic.
NBEdgeCont & myEdgeCont
The edge container for retrieving edges.
A container for traffic light definitions and built programs.
A SUMO-compliant built logic for a traffic light.
NBEdge * retrieveEdge(const SUMOSAXAttributes &attrs, SumoXMLAttr attr, bool &ok)
parses and edge id an returns an existing edge
connectio between two lanes
The representation of a single edge during network building.
Definition: NBEdge.h:70
void phasesLoaded()
mark phases as load
The base class for traffic light logic definitions.
link,node: the traffic light id responsible for this link
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
#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:64
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
NBTrafficLightLogicCont & myTLLCont
The traffic light container to fill.
int retrieveLaneIndex(const SUMOSAXAttributes &attrs, SumoXMLAttr attr, NBEdge *edge, bool &ok)
parses a lane index and verifies its correctness
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
NBTrafficLightDefinition * getDefinition(const std::string &id, const std::string &programID) const
Returns the named definition.
Encapsulated SAX-Attributes.
std::string tlID
The id of the traffic light that controls this connection.
Definition: NBEdge.h:192
void addTlConnection(const SUMOSAXAttributes &attrs)
reads and adds tl-controlled connection
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:412
int fromLane
The lane the connections starts at.
Definition: NBEdge.h:183
parameter associated to a certain key
void addConnection(NBEdge *from, NBEdge *to, int fromLane, int toLane, int linkIndex, bool reconstruct=true)
Adds a connection and immediately informs the edges.
T get(const std::string &str) const
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
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.
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
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.
bool myResetPhases
whether phases of a previously loaded traffic light must be reset
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
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.
void myEndElement(int element)
Called when a closing tag occurs.
const std::vector< Connection > & getConnections() const
Returns the connections.
Definition: NBEdge.h:845
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:250
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:266
NIXMLTrafficLightsHandler(NBTrafficLightLogicCont &tlCont, NBEdgeCont &ec)
Constructor.
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
link: the index of the link within the traffic light
long long int SUMOTime
Definition: TraCIDefs.h:51
void setType(TrafficLightType type)
Sets the algorithm type of this tls.
a traffic light logic
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:53
delete certain element
std::vector< std::string > getControlledInnerEdges() const
Retrieve the ids of edges explicitly controlled by the tls.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:433
a single phase description
TrafficLightType