SUMO - Simulation of Urban MObility
NBTrafficLightLogicCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A container for traffic light definitions and built programs
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 #include <map>
32 #include <string>
33 #include <algorithm>
35 #include <utils/common/ToString.h>
39 #include "NBTrafficLightLogic.h"
41 #include "NBOwnTLDef.h"
42 #include "NBEdgeCont.h"
43 #include "NBNodeCont.h"
44 
45 
46 // ===========================================================================
47 // static members
48 // ===========================================================================
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55 
56 
58  clear();
59 }
60 
61 
62 void
64  // check whether any offsets shall be manipulated by setting
65  // them to half of the duration
66  if (oc.isSet("tls.half-offset")) {
67  std::vector<std::string> ids = oc.getStringVector("tls.half-offset");
68  myHalfOffsetTLS.insert(ids.begin(), ids.end());
69  }
70  // check whether any offsets shall be manipulated by setting
71  // them to a quarter of the duration
72  if (oc.isSet("tls.quarter-offset")) {
73  std::vector<std::string> ids = oc.getStringVector("tls.quarter-offset");
74  myHalfOffsetTLS.insert(ids.begin(), ids.end());
75  }
76 }
77 
78 
79 bool
81  myExtracted.erase(logic);
82  if (myDefinitions.count(logic->getID())) {
83  if (myDefinitions[logic->getID()].count(logic->getProgramID())) {
84  if (forceInsert) {
85  const Program2Def& programs = myDefinitions[logic->getID()];
86  IDSupplier idS("", 0);
87  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
88  idS.avoid(it_prog->first);
89  }
90  logic->setProgramID(idS.getNext());
91  } else {
92  return false;
93  }
94  }
95  } else {
96  myDefinitions[logic->getID()] = Program2Def();
97  }
98  myDefinitions[logic->getID()][logic->getProgramID()] = logic;
99  return true;
100 }
101 
102 
103 bool
105  if (myDefinitions.count(id)) {
106  // delete all programs
107  for (Program2Def::iterator i = myDefinitions[id].begin(); i != myDefinitions[id].end(); i++) {
108  delete i->second;
109  }
110  myDefinitions.erase(id);
111  // also delete any logics that were already computed
112  if (myComputed.count(id)) {
113  for (Program2Logic::iterator i = myComputed[id].begin(); i != myComputed[id].end(); i++) {
114  delete i->second;
115  }
116  myComputed.erase(id);
117  }
118  return true;
119  } else {
120  return false;
121  }
122 }
123 
124 
125 bool
126 NBTrafficLightLogicCont::removeProgram(const std::string id, const std::string programID, bool del) {
127  if (myDefinitions.count(id) && myDefinitions[id].count(programID)) {
128  if (del) {
129  delete myDefinitions[id][programID];
130  }
131  myDefinitions[id].erase(programID);
132  return true;
133  } else {
134  return false;
135  }
136 }
137 
138 
139 void
141  myExtracted.insert(definition);
142  removeProgram(definition->getID(), definition->getProgramID(), false);
143 }
144 
145 
146 std::pair<int, int>
148  // clean previous logics
149  Logics logics = getComputed();
150  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
151  delete *it;
152  }
153  myComputed.clear();
154 
155  int numPrograms = 0;
156  Definitions definitions = getDefinitions();
157  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
158  if (computeSingleLogic(oc, *it)) {
159  numPrograms++;
160  }
161  }
162  return std::pair<int, int>((int)myComputed.size(), numPrograms);
163 }
164 
165 
166 bool
168  if (def->getNodes().size() == 0) {
169  return false;
170  }
171  const std::string& id = def->getID();
172  const std::string& programID = def->getProgramID();
173  // build program
174  NBTrafficLightLogic* built = def->compute(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();
286  }
287  // handle rail signals which are not instantiated as normal definitions
288  for (std::map<std::string, NBNode*>::const_iterator it = nc.begin(); it != nc.end(); it ++) {
289  NBNode* n = it->second;
291  NBOwnTLDef dummy(n->getID(), n, 0, TLTYPE_STATIC);
293  dummy.setTLControllingInformation();
294  n->setCrossingTLIndices(dummy.getID(), (int)dummy.getControlledLinks().size());
295  n->removeTrafficLight(&dummy);
296  }
297  }
298 }
299 
300 
303  Logics result;
304  for (Id2Logics::const_iterator it_id = myComputed.begin(); it_id != myComputed.end(); it_id++) {
305  const Program2Logic& programs = it_id->second;
306  for (Program2Logic::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
307  result.push_back(it_prog->second);
308  }
309  }
310  return result;
311 }
312 
313 
316  Definitions result;
317  for (Id2Defs::const_iterator it_id = myDefinitions.begin(); it_id != myDefinitions.end(); it_id++) {
318  const Program2Def& programs = it_id->second;
319  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
320  result.push_back(it_prog->second);
321  }
322  }
323  return result;
324 }
325 
326 
327 /****************************************************************************/
328 
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
std::map< std::string, NBTrafficLightLogic * > Program2Logic
Definition of internal the container types.
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
bool removeProgram(const std::string id, const std::string programID, bool del=true)
Removes a program of a logic definition from the dictionary.
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:117
static const Program2Def EmptyDefinitions
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:122
A SUMO-compliant built logic for a traffic light.
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:66
The base class for traffic light logic definitions.
void setOffset(SUMOTime offset)
Sets the offset of this tls.
std::vector< NBTrafficLightDefinition * > Definitions
const std::string & getID() const
Returns the id.
Definition: Named.h:65
void removeTrafficLight(NBTrafficLightDefinition *tlDef)
Removes the given traffic light from this node.
Definition: NBNode.cpp:331
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:199
std::set< NBTrafficLightDefinition * > myExtracted
The container for extracted definitions.
std::set< std::string > myQuarterOffsetTLS
List of tls which shall have an offset of T/2.
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
std::map< std::string, NBTrafficLightDefinition * > Program2Def
std::string getNext()
Returns the next id.
Definition: IDSupplier.cpp:58
NBTrafficLightDefinition * getDefinition(const std::string &id, const std::string &programID) const
Returns the named definition.
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
void setCrossingTLIndices(const std::string &tlID, int startIndex)
set tl indices of this nodes crossing starting at the given index
Definition: NBNode.cpp:2671
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.
void clearControllingTLInformation() const
Clears information about controlling traffic lights for all connenections of all edges.
Definition: NBEdgeCont.cpp:608
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
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 setProgramID(const std::string &programID)
Sets the programID.
const std::string & getProgramID() const
Returns the ProgramID.
std::vector< NBTrafficLightLogic * > Logics
SUMOTime getDuration() const
Returns the duration of the complete cycle.
Id2Defs myDefinitions
The container for tl-ids to their definitions.
std::vector< NBTrafficLightLogic * > getComputed() const
Returns a list of all computed logics.
bool computeSingleLogic(OptionsCont &oc, NBTrafficLightDefinition *def)
Computes a specific traffic light logic (using by NETEDIT)
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:40
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
A storage for options typed value containers)
Definition: OptionsCont.h:98
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:266
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.
long long int SUMOTime
Definition: TraCIDefs.h:51
void clear()
Destroys all stored definitions and logics.
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:66
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:53
NBTrafficLightLogic * getLogic(const std::string &id, const std::string &programID) const
Returns the computed logic for the given name.
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::pair< int, int > computeLogics(OptionsCont &oc)
Computes the traffic light logics using the stored definitions and stores the results.
Definitions getDefinitions() const
Returns a list of all definitions (convenience for easier iteration)