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