Eclipse SUMO - Simulation of Urban MObility
NBTrafficLightLogic.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-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // A SUMO-compliant built logic for a traffic light
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <vector>
25 #include <bitset>
26 #include <utility>
27 #include <string>
28 #include <sstream>
29 #include <cassert>
30 #include "NBEdge.h"
31 #include "NBEdgeCont.h"
32 #include "NBTrafficLightLogic.h"
35 #include <utils/options/Option.h>
36 #include <utils/common/ToString.h>
40 
41 
42 // ===========================================================================
43 // static members
44 // ===========================================================================
45 
46 // ===========================================================================
47 // member method definitions
48 // ===========================================================================
50  const std::string& subid, int noLinks,
51  SUMOTime offset, TrafficLightType type) :
52  Named(id), myNumLinks(noLinks), mySubID(subid),
53  myOffset(offset),
54  myType(type) {}
55 
57  Named(logic->getID()),
58  myNumLinks(logic->myNumLinks),
59  mySubID(logic->getProgramID()),
60  myOffset(logic->getOffset()),
61  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
62  myType(logic->getType()) {}
63 
64 
66 
67 void
68 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, const std::vector<int>& next, const std::string& name, int index) {
69  addStep(duration, state,
72  next, name, index);
73 }
74 
75 void
76 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, SUMOTime minDur, SUMOTime maxDur, const std::vector<int>& next, const std::string& name, int index) {
77  // check state size
78  if (myNumLinks == 0) {
79  // initialize
80  myNumLinks = (int)state.size();
81  } else if ((int)state.size() != myNumLinks) {
82  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
83  " does not match declared number of links " + toString(myNumLinks));
84  }
85  // check state contents
86  const std::string::size_type illegal = state.find_first_not_of(SUMOXMLDefinitions::ALLOWED_TLS_LINKSTATES);
87  if (std::string::npos != illegal) {
88  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
89  }
90  // interpret index
91  if (index < 0 || index >= (int)myPhases.size()) {
92  // insert at the end
93  index = (int)myPhases.size();
94  }
95  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state, minDur, maxDur, next, name));
96 }
97 
98 
99 void
101  if (index >= (int)myPhases.size()) {
102  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
103  + toString(myPhases.size()) + " phases.");
104  }
105  myPhases.erase(myPhases.begin() + index);
106 }
107 
108 
109 void
111  if (myNumLinks > numLinks) {
112  for (PhaseDefinition& p : myPhases) {
113  p.state = p.state.substr(0, numLinks);
114  }
115  } else {
116  std::string add(numLinks - myNumLinks, (char)fill);
117  for (PhaseDefinition& p : myPhases) {
118  p.state = p.state + add;
119  }
120  }
121  myNumLinks = numLinks;
122 }
123 
124 void
126  assert(index >= 0);
127  assert(index < myNumLinks);
128  for (PhaseDefinition& p : myPhases) {
129  p.state.erase(index, 1);
130  }
131  myNumLinks--;
132 }
133 
134 
135 void
137  myNumLinks = 0;
138  myPhases.clear();
139 }
140 
141 
142 SUMOTime
144  SUMOTime duration = 0;
145  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
146  duration += (*i).duration;
147  }
148  return duration;
149 }
150 
151 
152 void
153 NBTrafficLightLogic::closeBuilding(bool checkVarDurations) {
154  for (int i = 0; i < (int)myPhases.size() - 1;) {
155  if (myPhases[i].state != myPhases[i + 1].state || myPhases[i].next.size() > 0 || myPhases[i + 1].next.size() > 0 || myPhases[i].name != myPhases[i + 1].name) {
156  ++i;
157  continue;
158  }
159  myPhases[i].duration += myPhases[i + 1].duration;
162  myPhases[i].minDur += myPhases[i + 1].minDur;
163  } else {
164  myPhases[i].minDur = myPhases[i + 1].minDur;
165  }
166  }
169  myPhases[i].maxDur += myPhases[i + 1].maxDur;
170  } else {
171  myPhases[i].maxDur = myPhases[i + 1].maxDur;
172  }
173  }
174  myPhases.erase(myPhases.begin() + i + 1);
175  }
176  // check if actuated lights are defined correctly
177  if (checkVarDurations) {
179  bool found = false;
180  for (auto p : myPhases) {
183  found = true;
184  break;
185  }
186  }
187  if (!found) {
188  WRITE_WARNING("Non-static traffic light '" + getID() + "' does not define variable phase length.");
189  }
190  }
191  }
192 }
193 
194 
195 void
196 NBTrafficLightLogic::setPhaseState(int phaseIndex, int tlIndex, LinkState linkState) {
197  assert(phaseIndex < (int)myPhases.size());
198  std::string& phaseState = myPhases[phaseIndex].state;
199  assert(tlIndex < (int)phaseState.size());
200  phaseState[tlIndex] = (char)linkState;
201 }
202 
203 
204 void
206  assert(phaseIndex < (int)myPhases.size());
207  myPhases[phaseIndex].duration = duration;
208 }
209 
210 void
212  assert(phaseIndex < (int)myPhases.size());
213  myPhases[phaseIndex].minDur = duration;
214 }
215 
216 void
218  assert(phaseIndex < (int)myPhases.size());
219  myPhases[phaseIndex].maxDur = duration;
220 }
221 
222 void
223 NBTrafficLightLogic::setPhaseNext(int phaseIndex, const std::vector<int>& next) {
224  assert(phaseIndex < (int)myPhases.size());
225  myPhases[phaseIndex].next = next;
226 }
227 
228 void
229 NBTrafficLightLogic::setPhaseName(int phaseIndex, const std::string& name) {
230  assert(phaseIndex < (int)myPhases.size());
231  myPhases[phaseIndex].name = name;
232 }
233 
234 
235 /****************************************************************************/
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:280
long long int SUMOTime
Definition: SUMOTime.h:32
TrafficLightType
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
static const SUMOTime UNSPECIFIED_DURATION
The definition of a single phase of the logic.
A SUMO-compliant built logic for a traffic light.
SUMOTime getDuration() const
Returns the duration of the complete cycle.
void deleteStateIndex(int index)
remove the index from all phase states
int myNumLinks
The number of participating links.
void setPhaseMinDuration(int phaseIndex, SUMOTime duration)
NBTrafficLightLogic(const std::string &id, const std::string &subid, int noLinks, SUMOTime offset=0, TrafficLightType type=TrafficLightType::STATIC)
Constructor.
void closeBuilding(bool checkVarDurations=true)
closes the building process
PhaseDefinitionVector myPhases
The junction logic's storage for traffic light phase list.
void setPhaseName(int phaseIndex, const std::string &name)
void setPhaseDuration(int phaseIndex, SUMOTime duration)
Modifies the duration for an existing phase (used by NETEDIT)
void setPhaseState(int phaseIndex, int tlIndex, LinkState linkState)
Modifies the state for an existing phase (used by NETEDIT)
void setPhaseMaxDuration(int phaseIndex, SUMOTime duration)
void addStep(SUMOTime duration, const std::string &state, const std::vector< int > &next=std::vector< int >(), const std::string &name="", int index=-1)
Adds a phase to the logic.
void setPhaseNext(int phaseIndex, const std::vector< int > &next)
~NBTrafficLightLogic()
Destructor.
void setStateLength(int numLinks, LinkState fill=LINKSTATE_TL_RED)
TrafficLightType myType
The algorithm type for the traffic light.
Base class for objects which have an id.
Definition: Named.h:54
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static const std::string ALLOWED_TLS_LINKSTATES
all allowed characters for phase state