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 
void removeConnection(const NBConnection &conn, bool reconstruct=true)
removes the given connection from the traffic light if recontruct=true, reconstructs the logic and in...
bool setControllingTLInformation(const NBConnection &c, const std::string &tlID)
Returns if the link could be set as to be controlled.
Definition: NBEdge.cpp:1650
TrafficLightType getType() const
get the algorithm type (static etc..)
TrafficLightType myType
The algorithm type for the traffic light.
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
NBLoadedSUMOTLDef(const std::string &id, const std::string &programID, SUMOTime offset, TrafficLightType type)
Constructor.
int getTLIndex() const
Definition: NBConnection.h:98
void collectAllLinks()
helper method for use in NBOwnTLDef and NBLoadedSUMOTLDef
void setOffset(SUMOTime offset)
Sets the offset of this tls.
void closeBuilding()
closes the building process
A SUMO-compliant built logic for a traffic light.
TrafficLightType getType() const
get the algorithm type (static etc..)
const std::string & getProgramID() const
Returns the ProgramID.
The representation of a single edge during network building.
Definition: NBEdge.h:71
class for identifying connections
int getFromLane() const
returns the from-lane
The base class for traffic light logic definitions.
void setOffset(SUMOTime offset)
Sets the offset of this tls.
SUMOTime myOffset
The offset in the program.
NBEdge * getFrom() const
returns the from-edge (start of the connection)
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:36
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces a removed edge/lane.
void setTLIndex(int tlIndex)
Definition: NBConnection.h:103
SUMOTime getOffset()
Returns the offset.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
const std::vector< PhaseDefinition > & getPhases() const
Returns the phases.
std::set< std::string > myControlledInnerEdges
Set of inner edges that shall be controlled, though.
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
std::set< NBNode * > myOriginalNodes
The original nodes for which the loaded logic is valid.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
NBTrafficLightLogic * myCompute(const NBEdgeCont &ec, unsigned int brakingTime)
Computes the traffic light logic finally in dependence to the type.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:51
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
SUMOTime getOffset() const
Returns the offset of first switch.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
NBNode * getToNode() const
Returns the destination node of the edge.
Definition: NBEdge.h:362
unsigned int getNumLinks()
Returns the number of participating links.
void addPhase(SUMOTime duration, const std::string &state)
Adds a phase to the logic the new phase is inserted at the end of the list of already added phases...
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:38
int getToLane() const
returns the to-lane
void collectLinks()
Collects the links participating in this traffic light (only if not previously loaded) ...
NBEdge * getTo() const
returns the to-edge (end of the connection)
~NBLoadedSUMOTLDef()
Destructor.
std::vector< NBNode * > myControlledNodes
The container with participating nodes.
void addStep(SUMOTime duration, const std::string &state, int index=-1)
Adds a phase to the logic.
NBConnectionVector myControlledLinks
The list of controlled links.
NBTrafficLightLogic * myTLLogic
phases are added directly to myTLLogic which is then returned in myCompute()
void addConnection(NBEdge *from, NBEdge *to, int fromLane, int toLane, int linkIndex)
Adds a connection and immediately informs the edges.
TrafficLightType
NBNode * getFromNode() const
Returns the origin node of the edge.
Definition: NBEdge.h:354