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),
53  myTLLogic(0) {
54  myTLLogic = new NBTrafficLightLogic(id, programID, 0);
55  myTLLogic->setOffset(offset);
56 }
57 
58 
60  NBTrafficLightDefinition(def->getID(), def->getProgramID()),
61  myTLLogic(new NBTrafficLightLogic(logic)),
62  myOriginalNodes(def->getNodes().begin(), def->getNodes().end()) {
64 }
65 
66 
68  delete myTLLogic;
69 }
70 
71 
73 NBLoadedSUMOTLDef::myCompute(const NBEdgeCont& ec, unsigned int brakingTime) {
74  // @todo what to do with those parameters?
75  UNUSED_PARAMETER(ec);
76  UNUSED_PARAMETER(brakingTime);
78  return new NBTrafficLightLogic(myTLLogic);
79 }
80 
81 
82 void
83 NBLoadedSUMOTLDef::addConnection(NBEdge* from, NBEdge* to, int fromLane, int toLane, int linkIndex) {
84  assert(myTLLogic->getNumLinks() > 0); // logic should be loaded by now
85  if (linkIndex >= (int)myTLLogic->getNumLinks()) {
86  WRITE_ERROR("Invalid linkIndex " + toString(linkIndex) + " for traffic light '" + getID() +
87  "' with " + toString(myTLLogic->getNumLinks()) + " links.");
88  return;
89  }
90  NBConnection conn(from, fromLane, to, toLane, linkIndex);
91  // avoid duplicates
92  remove_if(myControlledLinks.begin(), myControlledLinks.end(), connection_equal(conn));
93  myControlledLinks.push_back(conn);
94  addNode(from->getToNode());
95  addNode(to->getFromNode());
96  myOriginalNodes.insert(from->getToNode());
97  myOriginalNodes.insert(to->getFromNode());
98  // added connections are definitely controlled. make sure none are removed because they lie within the tl
99  myControlledInnerEdges.insert(from->getID());
100  // set this information now so that it can be used while loading diffs
101  from->setControllingTLInformation(conn, getID());
102 }
103 
104 
105 void
108 }
109 
110 
111 void
113  // if nodes have been removed our links may have been invalidated as well
114  // since no logic will be built anyway there is no reason to inform any edges
115  if (amInvalid()) {
116  return;
117  }
118  // set the information about the link's positions within the tl into the
119  // edges the links are starting at, respectively
120  for (NBConnectionVector::const_iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
121  const NBConnection& c = *it;
122  assert(c.getTLIndex() < myTLLogic->getNumLinks());
123  NBEdge* edge = c.getFrom();
125  }
126 }
127 
128 
129 void
131 
132 
133 void
135 
136 
137 void
138 NBLoadedSUMOTLDef::addPhase(SUMOTime duration, const std::string& state) {
139  myTLLogic->addStep(duration, state);
140 }
141 
142 
143 bool
145  if (myControlledLinks.size() == 0) {
146  return true;
147  }
148  // make sure that myControlledNodes are the original nodes
149  if (myControlledNodes.size() != myOriginalNodes.size()) {
150  return true;
151  }
152  for (std::vector<NBNode*>::const_iterator i = myControlledNodes.begin(); i != myControlledNodes.end(); i++) {
153  if (myOriginalNodes.count(*i) != 1) {
154  return true;
155  }
156  }
157  return false;
158 }
159 
160 
161 void
162 NBLoadedSUMOTLDef::removeConnection(const NBConnection& conn, bool reconstruct) {
163  NBConnectionVector::iterator it = find(myControlledLinks.begin(), myControlledLinks.end(), conn);
164  if (it == myControlledLinks.end()) {
165  throw ProcessError("Attempt to remove nonexistant connection");
166  }
167  const int removed = conn.getTLIndex();
168  // remove the connection
169  myControlledLinks.erase(it);
170  if (reconstruct) {
171  // updating the edge is only needed for immediate use in NETEDIT.
172  // It may conflict with loading diffs
173  conn.getFrom()->setControllingTLInformation(conn, "");
174  // shift link numbers down so there is no gap
175  for (NBConnectionVector::iterator it = myControlledLinks.begin(); it != myControlledLinks.end(); it++) {
176  NBConnection& c = *it;
177  if (c.getTLIndex() > removed) {
178  c.setTLIndex(c.getTLIndex() - 1);
179  }
180  }
181  // update controlling information with new link numbers
183  // rebuild the logic
184  const std::vector<NBTrafficLightLogic::PhaseDefinition> phases = myTLLogic->getPhases();
186  newLogic->setOffset(myTLLogic->getOffset());
187  for (std::vector<NBTrafficLightLogic::PhaseDefinition>::const_iterator it = phases.begin(); it != phases.end(); it++) {
188  std::string newState = it->state;
189  newState.erase(newState.begin() + removed);
190  newLogic->addStep(it->duration, newState);
191  }
192  delete myTLLogic;
193  myTLLogic = newLogic;
194  }
195 }
196 
197 /****************************************************************************/
198