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-sim.org/
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 
std::map< std::string, NBTrafficLightLogic * > Program2Logic
Definition of internal the container types.
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
void setTLControllingInformation(const NBEdgeCont &ec)
Informs the edges about being controlled by a tls.
bool removeProgram(const std::string id, const std::string programID, bool del=true)
Removes a program of a logic definition from the dictionary.
static const Program2Def EmptyDefinitions
A SUMO-compliant built logic for a traffic light.
const std::string & getProgramID() const
Returns the ProgramID.
The representation of a single edge during network building.
Definition: NBEdge.h:71
void avoid(const std::string &id)
make sure that the given id is never supplied
Definition: IDSupplier.cpp:71
The base class for traffic light logic definitions.
void setOffset(SUMOTime offset)
Sets the offset of this tls.
std::vector< NBTrafficLightDefinition * > Definitions
void clearControllingTLInformation() const
Clears information about controlling traffic lights for all connenections of all edges.
Definition: NBEdgeCont.cpp:558
Definitions getDefinitions() const
Returns a list of all definitions (convenience for easier iteration)
void extract(NBTrafficLightDefinition *definition)
Extracts a traffic light definition from myDefinitions but keeps it in myExtracted for eventual * del...
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
std::set< NBTrafficLightDefinition * > myExtracted
The container for extracted definitions.
NBTrafficLightDefinition * getDefinition(const std::string &id, const std::string &programID) const
Returns the named definition.
std::set< std::string > myQuarterOffsetTLS
List of tls which shall have an offset of T/2.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
std::map< std::string, NBTrafficLightDefinition * > Program2Def
std::string getNext()
Returns the next id.
Definition: IDSupplier.cpp:63
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
NBTrafficLightLogic * compute(const NBEdgeCont &ec, OptionsCont &oc)
Computes the traffic light logic.
std::set< std::string > myHalfOffsetTLS
List of tls which shall have an offset of T/2.
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
std::vector< NBTrafficLightLogic * > getComputed() const
Returns a list of all computed logics.
std::pair< unsigned int, unsigned int > computeLogics(NBEdgeCont &ec, OptionsCont &oc)
Computes the traffic light logics using the stored definitions and stores the results.
void setProgramID(const std::string &programID)
Sets the programID.
SUMOTime getDuration() const
Returns the duration of the complete cycle.
std::vector< NBTrafficLightLogic * > Logics
Id2Defs myDefinitions
The container for tl-ids to their definitions.
NBTrafficLightLogic * getLogic(const std::string &id, const std::string &programID) const
Returns the computed logic for the given name.
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:38
A storage for options typed value containers)
Definition: OptionsCont.h:108
bool computeSingleLogic(NBEdgeCont &ec, OptionsCont &oc, NBTrafficLightDefinition *def)
Computes a specific traffic light logic (using by NETEDIT)
bool removeFully(const std::string id)
Removes a logic definition (and all programs) from the dictionary.
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
void clear()
Destroys all stored definitions and logics.
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces occurences of the removed edge/lane in all definitions by the given edge.
Id2Logics myComputed
The container for previously computed tl-logics.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.