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-sim.org/
11 // Copyright (C) 2001-2013 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,
52  SUMOTime offset, TrafficLightType type) :
53  NBTrafficLightDefinition(id, programID, offset, type),
54  myTLLogic(0) {
55  myTLLogic = new NBTrafficLightLogic(id, programID, 0, offset, type);
56 }
57 
58 
60  NBTrafficLightDefinition(def->getID(), def->getProgramID(), def->getOffset(), def->getType()),
61  myTLLogic(new NBTrafficLightLogic(logic)),
62  myOriginalNodes(def->getNodes().begin(), def->getNodes().end()) {
63  assert(def->getOffset() == logic->getOffset());
64  assert(def->getType() == logic->getType());
66 }
67 
68 
70  delete myTLLogic;
71 }
72 
73 
75 NBLoadedSUMOTLDef::myCompute(const NBEdgeCont& ec, unsigned int brakingTime) {
76  // @todo what to do with those parameters?
77  UNUSED_PARAMETER(ec);
78  UNUSED_PARAMETER(brakingTime);
80  return new NBTrafficLightLogic(myTLLogic);
81 }
82 
83 
84 void
85 NBLoadedSUMOTLDef::addConnection(NBEdge* from, NBEdge* to, int fromLane, int toLane, int linkIndex) {
86  assert(myTLLogic->getNumLinks() > 0); // logic should be loaded by now
87  if (linkIndex >= (int)myTLLogic->getNumLinks()) {
88  WRITE_ERROR("Invalid linkIndex " + toString(linkIndex) + " for traffic light '" + getID() +
89  "' with " + toString(myTLLogic->getNumLinks()) + " links.");
90  return;
91  }
92  NBConnection conn(from, fromLane, to, toLane, linkIndex);
93  // avoid duplicates
94  remove_if(myControlledLinks.begin(), myControlledLinks.end(), connection_equal(conn));
95  myControlledLinks.push_back(conn);
96  addNode(from->getToNode());
97  addNode(to->getFromNode());
98  myOriginalNodes.insert(from->getToNode());
99  myOriginalNodes.insert(to->getFromNode());
100  // added connections are definitely controlled. make sure none are removed because they lie within the tl
101  myControlledInnerEdges.insert(from->getID());
102  // set this information now so that it can be used while loading diffs
103  from->setControllingTLInformation(conn, getID());
104 }
105 
106 
107 void
110 }
111 
112 
113 void
115  // if nodes have been removed our links may have been invalidated as well
116  // since no logic will be built anyway there is no reason to inform any edges
117  if (amInvalid()) {
118  return;
119  }
120  // set the information about the link's positions within the tl into the
121  // edges the links are starting at, respectively
122  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
123  const NBConnection& c = *it;
124  assert(c.getTLIndex() < (int)myTLLogic->getNumLinks());
125  NBEdge* edge = c.getFrom();
127  }
128 }
129 
130 
131 void
133 
134 
135 void
137 
138 
139 void
140 NBLoadedSUMOTLDef::addPhase(SUMOTime duration, const std::string& state) {
141  myTLLogic->addStep(duration, state);
142 }
143 
144 
145 bool
147  if (myControlledLinks.size() == 0) {
148  return true;
149  }
150  // make sure that myControlledNodes are the original nodes
151  if (myControlledNodes.size() != myOriginalNodes.size()) {
152  return true;
153  }
154  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
155  if (myOriginalNodes.count(*i) != 1) {
156  return true;
157  }
158  }
159  return false;
160 }
161 
162 
163 void
164 NBLoadedSUMOTLDef::removeConnection(const NBConnection& conn, bool reconstruct) {
165  NBConnectionVector::iterator it = myControlledLinks.begin();
166  // find the connection but ignore its TLIndex since it might have been
167  // invalidated by an earlier removal
168  for (; it != myControlledLinks.end(); ++it) {
169  if (it->getFrom() == conn.getFrom() &&
170  it->getTo() == conn.getTo() &&
171  it->getFromLane() == conn.getFromLane() &&
172  it->getToLane() == conn.getToLane()) {
173  break;
174  }
175  }
176  if (it == myControlledLinks.end()) {
177  // a traffic light doesn't always controll all connections at a junction
178  // especially when using the option --tls.join
179  return;
180  }
181  const int removed = it->getTLIndex();
182  // remove the connection
183  myControlledLinks.erase(it);
184  if (reconstruct) {
185  // updating the edge is only needed for immediate use in NETEDIT.
186  // It may conflict with loading diffs
187  conn.getFrom()->setControllingTLInformation(conn, "");
188  // shift link numbers down so there is no gap
189  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
190  NBConnection& c = *it;
191  if (c.getTLIndex() > removed) {
192  c.setTLIndex(c.getTLIndex() - 1);
193  }
194  }
195  // update controlling information with new link numbers
197  // rebuild the logic
198  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
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 void
220  if (myControlledLinks.size() == 0) {
221  // maybe we only loaded a different program for a default traffic light.
222  // Try to build links now.
223  myOriginalNodes.insert(myControlledNodes.begin(), myControlledNodes.end());
224  collectAllLinks();
225  }
226 }
227 /****************************************************************************/
228