SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NBLoadedSUMOTLDef.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // A complete traffic light logic loaded from a sumo-net. (opted to reimplement
8 // since NBLoadedTLDef is quite vissim specific)
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
11 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <vector>
32 #include <set>
33 #include <cassert>
34 #include <iterator>
36 #include <utils/common/ToString.h>
38 #include "NBTrafficLightLogic.h"
40 #include "NBLoadedSUMOTLDef.h"
41 #include "NBNode.h"
42 
43 #ifdef CHECK_MEMORY_LEAKS
44 #include <foreign/nvwa/debug_new.h>
45 #endif // CHECK_MEMORY_LEAKS
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 
51 NBLoadedSUMOTLDef::NBLoadedSUMOTLDef(const std::string& id, const std::string& programID, SUMOTime offset) :
52  NBTrafficLightDefinition(id, programID, offset),
53  myTLLogic(0) {
54  myTLLogic = new NBTrafficLightLogic(id, programID, 0, offset);
55 }
56 
57 
59  NBTrafficLightDefinition(def->getID(), def->getProgramID(), def->getOffset()),
60  myTLLogic(new NBTrafficLightLogic(logic)),
61  myOriginalNodes(def->getNodes().begin(), def->getNodes().end())
62 {
63  assert(def->getOffset() == logic->getOffset());
65 }
66 
67 
69  delete myTLLogic;
70 }
71 
72 
74 NBLoadedSUMOTLDef::myCompute(const NBEdgeCont& ec, unsigned int brakingTime) {
75  // @todo what to do with those parameters?
76  UNUSED_PARAMETER(ec);
77  UNUSED_PARAMETER(brakingTime);
79  return new NBTrafficLightLogic(myTLLogic);
80 }
81 
82 
83 void
84 NBLoadedSUMOTLDef::addConnection(NBEdge* from, NBEdge* to, int fromLane, int toLane, int linkIndex) {
85  assert(myTLLogic->getNumLinks() > 0); // logic should be loaded by now
86  if (linkIndex >= (int)myTLLogic->getNumLinks()) {
87  WRITE_ERROR("Invalid linkIndex " + toString(linkIndex) + " for traffic light '" + getID() +
88  "' with " + toString(myTLLogic->getNumLinks()) + " links.");
89  return;
90  }
91  NBConnection conn(from, fromLane, to, toLane, linkIndex);
92  // avoid duplicates
93  remove_if(myControlledLinks.begin(), myControlledLinks.end(), connection_equal(conn));
94  myControlledLinks.push_back(conn);
95  addNode(from->getToNode());
96  addNode(to->getFromNode());
97  myOriginalNodes.insert(from->getToNode());
98  myOriginalNodes.insert(to->getFromNode());
99  // added connections are definitely controlled. make sure none are removed because they lie within the tl
100  myControlledInnerEdges.insert(from->getID());
101  // set this information now so that it can be used while loading diffs
102  from->setControllingTLInformation(conn, getID());
103 }
104 
105 
106 void
109 }
110 
111 
112 void
114  // if nodes have been removed our links may have been invalidated as well
115  // since no logic will be built anyway there is no reason to inform any edges
116  if (amInvalid()) {
117  return;
118  }
119  // set the information about the link's positions within the tl into the
120  // edges the links are starting at, respectively
121  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
122  const NBConnection& c = *it;
123  assert(c.getTLIndex() < (int)myTLLogic->getNumLinks());
124  NBEdge* edge = c.getFrom();
126  }
127 }
128 
129 
130 void
132 
133 
134 void
136 
137 
138 void
139 NBLoadedSUMOTLDef::addPhase(SUMOTime duration, const std::string& state) {
140  myTLLogic->addStep(duration, state);
141 }
142 
143 
144 bool
146  if (myControlledLinks.size() == 0) {
147  return true;
148  }
149  // make sure that myControlledNodes are the original nodes
150  if (myControlledNodes.size() != myOriginalNodes.size()) {
151  return true;
152  }
153  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
154  if (myOriginalNodes.count(*i) != 1) {
155  return true;
156  }
157  }
158  return false;
159 }
160 
161 
162 void
163 NBLoadedSUMOTLDef::removeConnection(const NBConnection& conn, bool reconstruct) {
164  NBConnectionVector::iterator it = myControlledLinks.begin();
165  // find the connection but ignore its TLIndex since it might have been
166  // invalidated by an earlier removal
167  for (; it != myControlledLinks.end(); ++it) {
168  if (it->getFrom() == conn.getFrom() &&
169  it->getTo() == conn.getTo() &&
170  it->getFromLane() == conn.getFromLane() &&
171  it->getToLane() == conn.getToLane()) {
172  break;
173  }
174  }
175  if (it == myControlledLinks.end()) {
176  // a traffic light doesn't always controll all connections at a junction
177  // especially when using the option --tls.join
178  return;
179  }
180  const int removed = it->getTLIndex();
181  // remove the connection
182  myControlledLinks.erase(it);
183  if (reconstruct) {
184  // updating the edge is only needed for immediate use in NETEDIT.
185  // It may conflict with loading diffs
186  conn.getFrom()->setControllingTLInformation(conn, "");
187  // shift link numbers down so there is no gap
188  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
189  NBConnection& c = *it;
190  if (c.getTLIndex() > removed) {
191  c.setTLIndex(c.getTLIndex() - 1);
192  }
193  }
194  // update controlling information with new link numbers
196  // rebuild the logic
197  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
199  newLogic->setOffset(myTLLogic->getOffset());
200  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
201  std::string newState = it->state;
202  newState.erase(newState.begin() + removed);
203  newLogic->addStep(it->duration, newState);
204  }
205  delete myTLLogic;
206  myTLLogic = newLogic;
207  }
208 }
209 
210 
211 void
213  myOffset = offset;
214  myTLLogic->setOffset(offset);
215 }
216 
217 /****************************************************************************/
218