Eclipse 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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A container for traffic light definitions and built programs
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 #include <map>
25 #include <string>
26 #include <algorithm>
28 #include <utils/common/ToString.h>
32 #include "NBTrafficLightLogic.h"
34 #include "NBOwnTLDef.h"
35 #include "NBEdgeCont.h"
36 #include "NBNodeCont.h"
37 
38 
39 // ===========================================================================
40 // static members
41 // ===========================================================================
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48 
49 
51  clear();
52 }
53 
54 
55 void
57  // check whether any offsets shall be manipulated by setting
58  // them to half of the duration
59  if (oc.isSet("tls.half-offset")) {
60  std::vector<std::string> ids = oc.getStringVector("tls.half-offset");
61  myHalfOffsetTLS.insert(ids.begin(), ids.end());
62  }
63  // check whether any offsets shall be manipulated by setting
64  // them to a quarter of the duration
65  if (oc.isSet("tls.quarter-offset")) {
66  std::vector<std::string> ids = oc.getStringVector("tls.quarter-offset");
67  myHalfOffsetTLS.insert(ids.begin(), ids.end());
68  }
69 }
70 
71 
72 bool
74  myExtracted.erase(logic);
75  if (myDefinitions.count(logic->getID())) {
76  if (myDefinitions[logic->getID()].count(logic->getProgramID())) {
77  if (forceInsert) {
78  const Program2Def& programs = myDefinitions[logic->getID()];
79  IDSupplier idS("", 0);
80  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
81  idS.avoid(it_prog->first);
82  }
83  logic->setProgramID(idS.getNext());
84  } else {
85  return false;
86  }
87  }
88  } else {
89  myDefinitions[logic->getID()] = Program2Def();
90  }
91  myDefinitions[logic->getID()][logic->getProgramID()] = logic;
92  return true;
93 }
94 
95 
96 bool
97 NBTrafficLightLogicCont::removeFully(const std::string id) {
98  if (myDefinitions.count(id)) {
99  // delete all programs
100  for (Program2Def::iterator i = myDefinitions[id].begin(); i != myDefinitions[id].end(); i++) {
101  delete i->second;
102  }
103  myDefinitions.erase(id);
104  // also delete any logics that were already computed
105  if (myComputed.count(id)) {
106  for (Program2Logic::iterator i = myComputed[id].begin(); i != myComputed[id].end(); i++) {
107  delete i->second;
108  }
109  myComputed.erase(id);
110  }
111  return true;
112  } else {
113  return false;
114  }
115 }
116 
117 
118 bool
119 NBTrafficLightLogicCont::removeProgram(const std::string id, const std::string programID, bool del) {
120  if (myDefinitions.count(id) && myDefinitions[id].count(programID)) {
121  if (del) {
122  delete myDefinitions[id][programID];
123  }
124  myDefinitions[id].erase(programID);
125  return true;
126  } else {
127  return false;
128  }
129 }
130 
131 
132 void
134  myExtracted.insert(definition);
135  removeProgram(definition->getID(), definition->getProgramID(), false);
136 }
137 
138 
139 std::pair<int, int>
141  // clean previous logics
142  Logics logics = getComputed();
143  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
144  delete *it;
145  }
146  myComputed.clear();
147 
148  int numPrograms = 0;
149  Definitions definitions = getDefinitions();
150  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
151  if (computeSingleLogic(oc, *it)) {
152  numPrograms++;
153  }
154  }
155  return std::pair<int, int>((int)myComputed.size(), numPrograms);
156 }
157 
158 
159 bool
161  if (def->getNodes().size() == 0) {
162  return false;
163  }
164  const std::string& id = def->getID();
165  const std::string& programID = def->getProgramID();
166  // build program
167  NBTrafficLightLogic* built = def->compute(oc);
168  if (built == nullptr) {
169  WRITE_WARNING("Could not build program '" + programID + "' for traffic light '" + id + "'");
170  return false;
171  }
172  // compute offset
173  SUMOTime T = built->getDuration();
174  if (myHalfOffsetTLS.count(id)) {
175  built->setOffset((SUMOTime)(T / 2.));
176  }
177  if (myQuarterOffsetTLS.count(id)) {
178  built->setOffset((SUMOTime)(T / 4.));
179  }
180  // and insert the result after computation
181  // make sure we don't leak memory if computeSingleLogic is called externally
182  if (myComputed[id][programID] != nullptr) {
183  delete myComputed[id][programID];
184  }
185  myComputed[id][programID] = built;
186  return true;
187 }
188 
189 
190 void
192  Definitions definitions = getDefinitions();
193  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
194  delete *it;
195  }
196  myDefinitions.clear();
197  Logics logics = getComputed();
198  for (Logics::iterator it = logics.begin(); it != logics.end(); it++) {
199  delete *it;
200  }
201  myComputed.clear();
202  for (std::set<NBTrafficLightDefinition*>::iterator it = myExtracted.begin(); it != myExtracted.end(); it++) {
203  delete *it;
204  }
205  myExtracted.clear();
206 }
207 
208 
209 void
211  const EdgeVector& outgoing) {
212  Definitions definitions = getDefinitions();
213  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
214  (*it)->remapRemoved(removed, incoming, outgoing);
215  }
216 }
217 
218 
219 void
221  NBEdge* by, int byLane) {
222  Definitions definitions = getDefinitions();
223  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
224  (*it)->replaceRemoved(removed, removedLane, by, byLane);
225  }
226 }
227 
228 
230 NBTrafficLightLogicCont::getDefinition(const std::string& id, const std::string& programID) const {
231  Id2Defs::const_iterator i = myDefinitions.find(id);
232  if (i != myDefinitions.end()) {
233  Program2Def programs = i->second;
234  Program2Def::const_iterator i2 = programs.find(programID);
235  if (i2 != programs.end()) {
236  return i2->second;
237  }
238  }
239  return nullptr;
240 }
241 
243 NBTrafficLightLogicCont::getPrograms(const std::string& id) const {
244  Id2Defs::const_iterator it = myDefinitions.find(id);
245  if (it != myDefinitions.end()) {
246  return it->second;
247  } else {
248  return EmptyDefinitions;
249  }
250 }
251 
252 
254 NBTrafficLightLogicCont::getLogic(const std::string& id, const std::string& programID) const {
255  Id2Logics::const_iterator i = myComputed.find(id);
256  if (i != myComputed.end()) {
257  Program2Logic programs = i->second;
258  Program2Logic::const_iterator i2 = programs.find(programID);
259  if (i2 != programs.end()) {
260  return i2->second;
261  }
262  }
263  return nullptr;
264 }
265 
266 
267 void
269  Definitions definitions = getDefinitions();
270  // set the information about all participants, first
271  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
272  (*it)->setParticipantsInformation();
273  }
274  // clear previous information because tlDefs may have been removed in NETEDIT
276  // insert the information about the tl-controlling
277  for (Definitions::iterator it = definitions.begin(); it != definitions.end(); it++) {
278  (*it)->setTLControllingInformation();
279  }
280  // handle rail signals which are not instantiated as normal definitions
281  for (std::map<std::string, NBNode*>::const_iterator it = nc.begin(); it != nc.end(); it ++) {
282  NBNode* n = it->second;
284  NBOwnTLDef dummy(n->getID(), n, 0, TLTYPE_STATIC);
287  n->setCrossingTLIndices(dummy.getID(), (int)dummy.getControlledLinks().size());
288  n->removeTrafficLight(&dummy);
289  }
290  }
291 }
292 
293 
296  Logics result;
297  for (Id2Logics::const_iterator it_id = myComputed.begin(); it_id != myComputed.end(); it_id++) {
298  const Program2Logic& programs = it_id->second;
299  for (Program2Logic::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
300  result.push_back(it_prog->second);
301  }
302  }
303  return result;
304 }
305 
306 
309  Definitions result;
310  for (Id2Defs::const_iterator it_id = myDefinitions.begin(); it_id != myDefinitions.end(); it_id++) {
311  const Program2Def& programs = it_id->second;
312  for (Program2Def::const_iterator it_prog = programs.begin(); it_prog != programs.end(); it_prog++) {
313  result.push_back(it_prog->second);
314  }
315  }
316  return result;
317 }
318 
319 
320 /****************************************************************************/
321 
NBTrafficLightLogicCont::Program2Logic
std::map< std::string, NBTrafficLightLogic * > Program2Logic
Definition of internal the container types.
Definition: NBTrafficLightLogicCont.h:215
NBTrafficLightLogic::getDuration
SUMOTime getDuration() const
Returns the duration of the complete cycle.
Definition: NBTrafficLightLogic.cpp:134
OptionsCont::isSet
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
Definition: OptionsCont.cpp:135
ToString.h
NBTrafficLightLogicCont::myComputed
Id2Logics myComputed
The container for previously computed tl-logics.
Definition: NBTrafficLightLogicCont.h:222
NBTrafficLightLogicCont::EmptyDefinitions
static const Program2Def EmptyDefinitions
Definition: NBTrafficLightLogicCont.h:236
NBTrafficLightDefinition::compute
NBTrafficLightLogic * compute(OptionsCont &oc)
Computes the traffic light logic.
Definition: NBTrafficLightDefinition.cpp:106
NBTrafficLightLogicCont::getPrograms
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
Definition: NBTrafficLightLogicCont.cpp:243
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
NBTrafficLightLogicCont::removeProgram
bool removeProgram(const std::string id, const std::string programID, bool del=true)
Removes a program of a logic definition from the dictionary.
Definition: NBTrafficLightLogicCont.cpp:119
NBEdgeCont
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:60
NBNodeCont::end
std::map< std::string, NBNode * >::const_iterator end() const
Returns the pointer to the end of the stored nodes.
Definition: NBNodeCont.h:120
OptionsCont.h
NBTrafficLightLogic.h
TLTYPE_STATIC
@ TLTYPE_STATIC
Definition: SUMOXMLDefinitions.h:1198
MsgHandler.h
EdgeVector
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
NBEdgeCont.h
NBNodeCont::begin
std::map< std::string, NBNode * >::const_iterator begin() const
Returns the pointer to the begin of the stored nodes.
Definition: NBNodeCont.h:115
IDSupplier
Definition: IDSupplier.h:37
NBOwnTLDef
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:46
NBNode::getType
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:272
NBTrafficLightLogicCont::Definitions
std::vector< NBTrafficLightDefinition * > Definitions
Returns a list of all definitions (convenience for easier iteration)
Definition: NBTrafficLightLogicCont.h:210
NBTrafficLightLogicCont::myExtracted
std::set< NBTrafficLightDefinition * > myExtracted
The container for extracted definitions.
Definition: NBTrafficLightLogicCont.h:228
NBTrafficLightLogic::setOffset
void setOffset(SUMOTime offset)
Sets the offset of this tls.
Definition: NBTrafficLightLogic.h:190
NBTrafficLightDefinition::setProgramID
void setProgramID(const std::string &programID)
Sets the programID.
Definition: NBTrafficLightDefinition.h:316
NBNodeCont
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:59
NBTrafficLightLogicCont::myQuarterOffsetTLS
std::set< std::string > myQuarterOffsetTLS
List of tls which shall have an offset of T/2.
Definition: NBTrafficLightLogicCont.h:234
NBEdge
The representation of a single edge during network building.
Definition: NBEdge.h:91
OptionsCont::getStringVector
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
Definition: OptionsCont.cpp:235
IDSupplier::avoid
void avoid(const std::string &id)
make sure that the given id is never supplied
Definition: IDSupplier.cpp:59
NBTrafficLightLogicCont::getDefinition
NBTrafficLightDefinition * getDefinition(const std::string &id, const std::string &programID) const
Returns the named definition.
Definition: NBTrafficLightLogicCont.cpp:230
NODETYPE_RAIL_SIGNAL
@ NODETYPE_RAIL_SIGNAL
Definition: SUMOXMLDefinitions.h:1059
NBTrafficLightLogicCont::applyOptions
void applyOptions(OptionsCont &oc)
Initialises the storage by applying given options.
Definition: NBTrafficLightLogicCont.cpp:56
NBTrafficLightLogicCont::extract
void extract(NBTrafficLightDefinition *definition)
Extracts a traffic light definition from myDefinitions but keeps it in myExtracted for eventual * del...
Definition: NBTrafficLightLogicCont.cpp:133
NBTrafficLightLogicCont::Program2Def
std::map< std::string, NBTrafficLightDefinition * > Program2Def
Definition: NBTrafficLightLogicCont.h:217
NBNode::setCrossingTLIndices
bool setCrossingTLIndices(const std::string &tlID, int startIndex)
Definition: NBNode.cpp:3124
NBTrafficLightDefinition::getControlledLinks
const NBConnectionVector & getControlledLinks() const
returns the controlled links (depends on previous call to collectLinks)
Definition: NBTrafficLightDefinition.h:294
NBNode::removeTrafficLight
void removeTrafficLight(NBTrafficLightDefinition *tlDef)
Removes the given traffic light from this node.
Definition: NBNode.cpp:371
NBTrafficLightLogicCont::myHalfOffsetTLS
std::set< std::string > myHalfOffsetTLS
List of tls which shall have an offset of T/2.
Definition: NBTrafficLightLogicCont.h:231
NBTrafficLightLogicCont::remapRemoved
void remapRemoved(NBEdge *removed, const EdgeVector &incoming, const EdgeVector &outgoing)
Replaces occurences of the removed edge in incoming/outgoing edges of all definitions.
Definition: NBTrafficLightLogicCont.cpp:210
OutputDevice.h
NBTrafficLightDefinition::getNodes
const std::vector< NBNode * > & getNodes() const
Returns the list of controlled nodes.
Definition: NBTrafficLightDefinition.h:172
OptionsCont
A storage for options typed value containers)
Definition: OptionsCont.h:89
IDSupplier.h
NBTrafficLightLogicCont::myDefinitions
Id2Defs myDefinitions
The container for tl-ids to their definitions.
Definition: NBTrafficLightLogicCont.h:225
NODETYPE_RAIL_CROSSING
@ NODETYPE_RAIL_CROSSING
Definition: SUMOXMLDefinitions.h:1060
NBTrafficLightLogicCont::NBTrafficLightLogicCont
NBTrafficLightLogicCont()
Constructor.
Definition: NBTrafficLightLogicCont.cpp:47
NBTrafficLightLogicCont::getComputed
std::vector< NBTrafficLightLogic * > getComputed() const
Returns a list of all computed logics.
Definition: NBTrafficLightLogicCont.cpp:295
NBNodeCont.h
NBTrafficLightLogicCont::Logics
std::vector< NBTrafficLightLogic * > Logics
Definition: NBTrafficLightLogicCont.h:219
NBTrafficLightLogicCont::removeFully
bool removeFully(const std::string id)
Removes a logic definition (and all programs) from the dictionary.
Definition: NBTrafficLightLogicCont.cpp:97
NBTrafficLightDefinition::setParticipantsInformation
virtual void setParticipantsInformation()
Builds the list of participating nodes/edges/links.
Definition: NBTrafficLightDefinition.cpp:157
NBTrafficLightDefinition::getProgramID
const std::string & getProgramID() const
Returns the ProgramID.
Definition: NBTrafficLightDefinition.h:308
IDSupplier::getNext
std::string getNext()
Returns the next id.
Definition: IDSupplier.cpp:51
config.h
NBTrafficLightLogicCont::computeSingleLogic
bool computeSingleLogic(OptionsCont &oc, NBTrafficLightDefinition *def)
Computes a specific traffic light logic (using by NETEDIT)
Definition: NBTrafficLightLogicCont.cpp:160
NBTrafficLightLogicCont::setTLControllingInformation
void setTLControllingInformation(const NBEdgeCont &ec, const NBNodeCont &nc)
Informs the edges about being controlled by a tls.
Definition: NBTrafficLightLogicCont.cpp:268
NBTrafficLightLogic
A SUMO-compliant built logic for a traffic light.
Definition: NBTrafficLightLogic.h:51
NBNode
Represents a single node (junction) during network building.
Definition: NBNode.h:67
NBTrafficLightLogicCont::~NBTrafficLightLogicCont
~NBTrafficLightLogicCont()
Destructor.
Definition: NBTrafficLightLogicCont.cpp:50
NBOwnTLDef.h
NBTrafficLightLogicCont.h
NBOwnTLDef::setTLControllingInformation
void setTLControllingInformation() const
Informs edges about being controlled by a tls.
Definition: NBOwnTLDef.cpp:735
NBTrafficLightLogicCont::getLogic
NBTrafficLightLogic * getLogic(const std::string &id, const std::string &programID) const
Returns the computed logic for the given name.
Definition: NBTrafficLightLogicCont.cpp:254
NBTrafficLightLogicCont::clear
void clear()
Destroys all stored definitions and logics.
Definition: NBTrafficLightLogicCont.cpp:191
NBTrafficLightLogicCont::getDefinitions
Definitions getDefinitions() const
Definition: NBTrafficLightLogicCont.cpp:308
NBTrafficLightLogicCont::computeLogics
std::pair< int, int > computeLogics(OptionsCont &oc)
Computes the traffic light logics using the stored definitions and stores the results.
Definition: NBTrafficLightLogicCont.cpp:140
NBTrafficLightLogicCont::insert
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
Definition: NBTrafficLightLogicCont.cpp:73
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
NBTrafficLightLogicCont::replaceRemoved
void replaceRemoved(NBEdge *removed, int removedLane, NBEdge *by, int byLane)
Replaces occurences of the removed edge/lane in all definitions by the given edge.
Definition: NBTrafficLightLogicCont.cpp:220
NBTrafficLightDefinition
The base class for traffic light logic definitions.
Definition: NBTrafficLightDefinition.h:67
NBEdgeCont::clearControllingTLInformation
void clearControllingTLInformation() const
Clears information about controlling traffic lights for all connenections of all edges.
Definition: NBEdgeCont.cpp:766