SUMO - Simulation of Urban MObility
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.dlr.de/
12 // Copyright (C) 2001-2016 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 "NBOwnTLDef.h"
43 #include "NBEdgeCont.h"
44 #include "NBNodeCont.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // static members
53 // ===========================================================================
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
60 
61 
63  clear();
64 }
65 
66 
67 void
69  // check whether any offsets shall be manipulated by setting
70  // them to half of the duration
71  if (oc.isSet("tls.half-offset")) {
72  std::vector<std::string> ids = oc.getStringVector("tls.half-offset");
73  myHalfOffsetTLS.insert(ids.begin(), ids.end());
74  }
75  // check whether any offsets shall be manipulated by setting
76  // them to a quarter of the duration
77  if (oc.isSet("tls.quarter-offset")) {
78  std::vector<std::string> ids = oc.getStringVector("tls.quarter-offset");
79  myHalfOffsetTLS.insert(ids.begin(), ids.end());
80  }
81 }
82 
83 
84 bool
86  myExtracted.erase(logic);
87  if (myDefinitions.count(logic->getID())) {
88  if (myDefinitions[logic->getID()].count(logic->getProgramID())) {
89  if (forceInsert) {
90  const Program2Def& programs = myDefinitions[logic->getID()];
91  IDSupplier idS("", 0);
92  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
93  idS.avoid(it_prog->first);
94  }
95  logic->setProgramID(idS.getNext());
96  } else {
97  return false;
98  }
99  }
100  } else {
101  myDefinitions[logic->getID()] = Program2Def();
102  }
103  myDefinitions[logic->getID()][logic->getProgramID()] = logic;
104  return true;
105 }
106 
107 
108 bool
110  if (myDefinitions.count(id)) {
111  // delete all programs
112  for (Program2Def::iterator i = myDefinitions[id].begin(); i != myDefinitions[id].end(); i++) {
113  delete i->second;
114  }
115  myDefinitions.erase(id);
116  // also delete any logics that were already computed
117  if (myComputed.count(id)) {
118  for (Program2Logic::iterator i = myComputed[id].begin(); i != myComputed[id].end(); i++) {
119  delete i->second;
120  }
121  myComputed.erase(id);
122  }
123  return true;
124  } else {
125  return false;
126  }
127 }
128 
129 
130 bool
131 NBTrafficLightLogicCont::removeProgram(const std::string id, const std::string programID, bool del) {
132  if (myDefinitions.count(id) && myDefinitions[id].count(programID)) {
133  if (del) {
134  delete myDefinitions[id][programID];
135  }
136  myDefinitions[id].erase(programID);
137  return true;
138  } else {
139  return false;
140  }
141 }
142 
143 
144 void
146  myExtracted.insert(definition);
147  removeProgram(definition->getID(), definition->getProgramID(), false);
148 }
149 
150 
151 std::pair<unsigned int, unsigned int>
153  // clean previous logics
154  Logics logics = getComputed();
155  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
156  delete *it;
157  }
158  myComputed.clear();
159 
160  unsigned int numPrograms = 0;
161  Definitions definitions = getDefinitions();
162  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
163  if (computeSingleLogic(oc, *it)) {
164  numPrograms++;
165  }
166  }
167  return std::pair<unsigned int, unsigned int>((unsigned int)myComputed.size(), numPrograms);
168 }
169 
170 
171 bool
173  const std::string& id = def->getID();
174  const std::string& programID = def->getProgramID();
175  // build program
176  NBTrafficLightLogic* built = def->compute(oc);
177  if (built == 0) {
178  WRITE_WARNING("Could not build program '" + programID + "' for traffic light '" + id + "'");
179  return false;
180  }
181  // compute offset
182  SUMOTime T = built->getDuration();
183  if (myHalfOffsetTLS.count(id)) {
184  built->setOffset((SUMOTime)(T / 2.));
185  }
186  if (myQuarterOffsetTLS.count(id)) {
187  built->setOffset((SUMOTime)(T / 4.));
188  }
189  // and insert the result after computation
190  // make sure we don't leak memory if computeSingleLogic is called externally
191  if (myComputed[id][programID] != 0) {
192  delete myComputed[id][programID];
193  }
194  myComputed[id][programID] = built;
195  return true;
196 }
197 
198 
199 void
201  Definitions definitions = getDefinitions();
202  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
203  delete *it;
204  }
205  myDefinitions.clear();
206  Logics logics = getComputed();
207  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
208  delete *it;
209  }
210  myComputed.clear();
211  for (std::set<NBTrafficLightDefinition*>::iterator it = myExtracted.begin(); it != myExtracted.end(); it++) {
212  delete *it;
213  }
214  myExtracted.clear();
215 }
216 
217 
218 void
220  const EdgeVector& outgoing) {
221  Definitions definitions = getDefinitions();
222  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
223  (*it)->remapRemoved(removed, incoming, outgoing);
224  }
225 }
226 
227 
228 void
230  NBEdge* by, int byLane) {
231  Definitions definitions = getDefinitions();
232  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
233  (*it)->replaceRemoved(removed, removedLane, by, byLane);
234  }
235 }
236 
237 
239 NBTrafficLightLogicCont::getDefinition(const std::string& id, const std::string& programID) const {
240  Id2Defs::const_iterator i = myDefinitions.find(id);
241  if (i != myDefinitions.end()) {
242  Program2Def programs = i->second;
243  Program2Def::const_iterator i2 = programs.find(programID);
244  if (i2 != programs.end()) {
245  return i2->second;
246  }
247  }
248  return 0;
249 }
250 
252 NBTrafficLightLogicCont::getPrograms(const std::string& id) const {
253  Id2Defs::const_iterator it = myDefinitions.find(id);
254  if (it != myDefinitions.end()) {
255  return it->second;
256  } else {
257  return EmptyDefinitions;
258  }
259 }
260 
261 
263 NBTrafficLightLogicCont::getLogic(const std::string& id, const std::string& programID) const {
264  Id2Logics::const_iterator i = myComputed.find(id);
265  if (i != myComputed.end()) {
266  Program2Logic programs = i->second;
267  Program2Logic::const_iterator i2 = programs.find(programID);
268  if (i2 != programs.end()) {
269  return i2->second;
270  }
271  }
272  return 0;
273 }
274 
275 
276 void
278  Definitions definitions = getDefinitions();
279  // set the information about all participants, first
280  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
281  (*it)->setParticipantsInformation();
282  }
283  // clear previous information because tlDefs may have been removed in NETEDIT
285  // insert the information about the tl-controlling
286  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
287  (*it)->setTLControllingInformation();
288  }
289  // handle rail signals which are not instantiated as normal definitions
290  for (std::map<std::string, NBNode*>::const_iterator it = nc.begin(); it != nc.end(); it ++) {
291  NBNode* n = it->second;
293  NBOwnTLDef dummy(n->getID(), n, 0, TLTYPE_STATIC);
295  dummy.setTLControllingInformation();
296  n->removeTrafficLight(&dummy);
297  }
298  }
299 }
300 
301 
304  Logics result;
305  for (Id2Logics::const_iterator it_id = myComputed.begin(); it_id != myComputed.end(); it_id++) {
306  const Program2Logic& programs = it_id->second;
307  for (Program2Logic::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
308  result.push_back(it_prog->second);
309  }
310  }
311  return result;
312 }
313 
314 
317  Definitions result;
318  for (Id2Defs::const_iterator it_id = myDefinitions.begin(); it_id != myDefinitions.end(); it_id++) {
319  const Program2Def& programs = it_id->second;
320  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
321  result.push_back(it_prog->second);
322  }
323  }
324  return result;
325 }
326 
327 
328 /****************************************************************************/
329 
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) ...
long long int SUMOTime
Definition: SUMOTime.h:43
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:70
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
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::pair< unsigned int, unsigned int > computeLogics(OptionsCont &oc)
Computes the traffic light logics using the stored definitions and stores the results.
std::vector< NBTrafficLightDefinition * > Definitions
void clearControllingTLInformation() const
Clears information about controlling traffic lights for all connenections of all edges.
Definition: NBEdgeCont.cpp:577
Definitions getDefinitions() const
Returns a list of all definitions (convenience for easier iteration)
void removeTrafficLight(NBTrafficLightDefinition *tlDef)
Removes the given traffic light from this node.
Definition: NBNode.cpp:322
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:200
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:65
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.
std::set< std::string > myHalfOffsetTLS
List of tls which shall have an offset of T/2.
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:265
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.
void setProgramID(const std::string &programID)
Sets the programID.
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:135
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.
bool computeSingleLogic(OptionsCont &oc, NBTrafficLightDefinition *def)
Computes a specific traffic light logic (using by NETEDIT)
std::vector< NBEdge * > EdgeVector
Definition: NBCont.h:41
A storage for options typed value containers)
Definition: OptionsCont.h:108
Represents a single node (junction) during network building.
Definition: NBNode.h:74
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.
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:64
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces occurences of the removed edge/lane in all definitions by the given edge.
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
Definition: NBOwnTLDef.cpp:527
void setTLControllingInformation(const NBEdgeCont &ec, const NBNodeCont &nc)
Informs the edges about being controlled by a tls.
Id2Logics myComputed
The container for previously computed tl-logics.
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:127
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.