SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NBTrafficLightLogicCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A container for traffic light definitions and built programs
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 #include <map>
33 #include <string>
34 #include <algorithm>
36 #include <utils/common/ToString.h>
40 #include "NBTrafficLightLogic.h"
42 #include "NBEdgeCont.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // static members
51 // ===========================================================================
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
58 
59 
61  clear();
62 }
63 
64 
65 void
67  // check whether any offsets shall be manipulated by setting
68  // them to half of the duration
69  if (oc.isSet("tls.half-offset")) {
70  std::vector<std::string> ids = oc.getStringVector("tls.half-offset");
71  myHalfOffsetTLS.insert(ids.begin(), ids.end());
72  }
73  // check whether any offsets shall be manipulated by setting
74  // them to a quarter of the duration
75  if (oc.isSet("tls.quarter-offset")) {
76  std::vector<std::string> ids = oc.getStringVector("tls.quarter-offset");
77  myHalfOffsetTLS.insert(ids.begin(), ids.end());
78  }
79 }
80 
81 
82 bool
84  myExtracted.erase(logic);
85  if (myDefinitions.count(logic->getID())) {
86  if (myDefinitions[logic->getID()].count(logic->getProgramID())) {
87  if (forceInsert) {
88  const Program2Def& programs = myDefinitions[logic->getID()];
89  IDSupplier idS("", 0);
90  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
91  idS.avoid(it_prog->first);
92  }
93  logic->setProgramID(idS.getNext());
94  } else {
95  return false;
96  }
97  }
98  } else {
99  myDefinitions[logic->getID()] = Program2Def();
100  }
101  myDefinitions[logic->getID()][logic->getProgramID()] = logic;
102  return true;
103 }
104 
105 
106 bool
108  if (myDefinitions.count(id)) {
109  // delete all programs
110  for (Program2Def::iterator i = myDefinitions[id].begin(); i != myDefinitions[id].end(); i++) {
111  delete i->second;
112  }
113  myDefinitions.erase(id);
114  // also delete any logics that were already computed
115  if (myComputed.count(id)) {
116  for (Program2Logic::iterator i = myComputed[id].begin(); i != myComputed[id].end(); i++) {
117  delete i->second;
118  }
119  myComputed.erase(id);
120  }
121  return true;
122  } else {
123  return false;
124  }
125 }
126 
127 
128 bool
129 NBTrafficLightLogicCont::removeProgram(const std::string id, const std::string programID, bool del) {
130  if (myDefinitions.count(id) && myDefinitions[id].count(programID)) {
131  if (del) {
132  delete myDefinitions[id][programID];
133  }
134  myDefinitions[id].erase(programID);
135  return true;
136  } else {
137  return false;
138  }
139 }
140 
141 
142 void
144  myExtracted.insert(definition);
145  removeProgram(definition->getID(), definition->getProgramID(), false);
146 }
147 
148 
149 std::pair<unsigned int, unsigned int>
151  // clean previous logics
152  Logics logics = getComputed();
153  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
154  delete *it;
155  }
156  myComputed.clear();
157 
158  unsigned int numPrograms = 0;
159  Definitions definitions = getDefinitions();
160  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
161  if (computeSingleLogic(ec, oc, *it)) {
162  numPrograms++;
163  }
164  }
165  return std::pair<unsigned int, unsigned int>((unsigned int)myComputed.size(), numPrograms);
166 }
167 
168 
169 bool
171  const std::string& id = def->getID();
172  const std::string& programID = def->getProgramID();
173  // build program
174  NBTrafficLightLogic* built = def->compute(ec, oc);
175  if (built == 0) {
176  WRITE_WARNING("Could not build program '" + programID + "' for traffic light '" + id + "'");
177  return false;
178  }
179  // compute offset
180  SUMOTime T = built->getDuration();
181  if (myHalfOffsetTLS.count(id)) {
182  built->setOffset((SUMOTime)(T / 2.));
183  }
184  if (myQuarterOffsetTLS.count(id)) {
185  built->setOffset((SUMOTime)(T / 4.));
186  }
187  // and insert the result after computation
188  // make sure we don't leak memory if computeSingleLogic is called externally
189  if (myComputed[id][programID] != 0) {
190  delete myComputed[id][programID];
191  }
192  myComputed[id][programID] = built;
193  return true;
194 }
195 
196 
197 void
199  Definitions definitions = getDefinitions();
200  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
201  delete *it;
202  }
203  myDefinitions.clear();
204  Logics logics = getComputed();
205  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
206  delete *it;
207  }
208  myComputed.clear();
209  for (std::set<NBTrafficLightDefinition*>::iterator it = myExtracted.begin(); it != myExtracted.end(); it++) {
210  delete *it;
211  }
212  myExtracted.clear();
213 }
214 
215 
216 void
218  const EdgeVector& outgoing) {
219  Definitions definitions = getDefinitions();
220  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
221  (*it)->remapRemoved(removed, incoming, outgoing);
222  }
223 }
224 
225 
226 void
228  NBEdge* by, int byLane) {
229  Definitions definitions = getDefinitions();
230  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
231  (*it)->replaceRemoved(removed, removedLane, by, byLane);
232  }
233 }
234 
235 
237 NBTrafficLightLogicCont::getDefinition(const std::string& id, const std::string& programID) const {
238  Id2Defs::const_iterator i = myDefinitions.find(id);
239  if (i != myDefinitions.end()) {
240  Program2Def programs = i->second;
241  Program2Def::const_iterator i2 = programs.find(programID);
242  if (i2 != programs.end()) {
243  return i2->second;
244  }
245  }
246  return 0;
247 }
248 
250 NBTrafficLightLogicCont::getPrograms(const std::string& id) const {
251  Id2Defs::const_iterator it = myDefinitions.find(id);
252  if (it != myDefinitions.end()) {
253  return it->second;
254  } else {
255  return EmptyDefinitions;
256  }
257 }
258 
259 
261 NBTrafficLightLogicCont::getLogic(const std::string& id, const std::string& programID) const {
262  Id2Logics::const_iterator i = myComputed.find(id);
263  if (i != myComputed.end()) {
264  Program2Logic programs = i->second;
265  Program2Logic::const_iterator i2 = programs.find(programID);
266  if (i2 != programs.end()) {
267  return i2->second;
268  }
269  }
270  return 0;
271 }
272 
273 
274 void
276  Definitions definitions = getDefinitions();
277  // set the information about all participants, first
278  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
279  (*it)->setParticipantsInformation();
280  }
281  // clear previous information because tlDefs may have been removed in NETEDIT
283  // insert the information about the tl-controlling
284  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
285  (*it)->setTLControllingInformation(ec);
286  }
287 }
288 
289 
292  Logics result;
293  for (Id2Logics::const_iterator it_id = myComputed.begin(); it_id != myComputed.end(); it_id++) {
294  const Program2Logic& programs = it_id->second;
295  for (Program2Logic::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
296  result.push_back(it_prog->second);
297  }
298  }
299  return result;
300 }
301 
302 
305  Definitions result;
306  for (Id2Defs::const_iterator it_id = myDefinitions.begin(); it_id != myDefinitions.end(); it_id++) {
307  const Program2Def& programs = it_id->second;
308  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
309  result.push_back(it_prog->second);
310  }
311  }
312  return result;
313 }
314 
315 
316 /****************************************************************************/
317