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-2018 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 SUMO-compliant built logic for a traffic light
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <vector>
27 #include <bitset>
28 #include <utility>
29 #include <string>
30 #include <sstream>
31 #include <cassert>
32 #include "NBEdge.h"
33 #include "NBEdgeCont.h"
34 #include "NBTrafficLightLogic.h"
37 #include <utils/options/Option.h>
38 #include <utils/common/ToString.h>
42 
43 
44 // ===========================================================================
45 // static members
46 // ===========================================================================
49  LINKSTATE_STOP, // used for NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED
56  };
57 
59 
60 // ===========================================================================
61 // member method definitions
62 // ===========================================================================
64  const std::string& subid, int noLinks,
65  SUMOTime offset, TrafficLightType type) :
66  Named(id), myNumLinks(noLinks), mySubID(subid),
67  myOffset(offset),
68  myType(type) {}
69 
71  Named(logic->getID()),
72  myNumLinks(logic->myNumLinks),
73  mySubID(logic->getProgramID()),
74  myOffset(logic->getOffset()),
75  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
76  myType(logic->getType()) {}
77 
78 
80 
81 void
82 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
83  addStep(duration, state,
86  index);
87 }
88 
89 void
90 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, SUMOTime minDur, SUMOTime maxDur, int index) {
91  // check state size
92  if (myNumLinks == 0) {
93  // initialize
94  myNumLinks = (int)state.size();
95  } else if ((int)state.size() != myNumLinks) {
96  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
97  " does not match declared number of links " + toString(myNumLinks));
98  }
99  // check state contents
100  const std::string::size_type illegal = state.find_first_not_of(ALLOWED_STATES);
101  if (std::string::npos != illegal) {
102  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
103  }
104  // interpret index
105  if (index < 0 || index >= (int)myPhases.size()) {
106  // insert at the end
107  index = (int)myPhases.size();
108  }
109  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state, minDur, maxDur));
110 }
111 
112 
113 void
115  if (index >= (int)myPhases.size()) {
116  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
117  + toString(myPhases.size()) + " phases.");
118  }
119  myPhases.erase(myPhases.begin() + index);
120 }
121 
122 
123 void
125  if (myNumLinks > numLinks) {
126  for (PhaseDefinition& p : myPhases) {
127  p.state = p.state.substr(0, numLinks);
128  }
129  } else {
130  std::string add(numLinks - myNumLinks, (char)fill);
131  for (PhaseDefinition& p : myPhases) {
132  p.state = p.state + add;
133  }
134  }
135  myNumLinks = numLinks;
136 }
137 
138 
139 void
141  myNumLinks = 0;
142  myPhases.clear();
143 }
144 
145 
146 SUMOTime
148  SUMOTime duration = 0;
149  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
150  duration += (*i).duration;
151  }
152  return duration;
153 }
154 
155 
156 void
157 NBTrafficLightLogic::closeBuilding(bool checkVarDurations) {
158  for (int i = 0; i < (int)myPhases.size() - 1;) {
159  if (myPhases[i].state != myPhases[i + 1].state) {
160  ++i;
161  continue;
162  }
163  myPhases[i].duration += myPhases[i + 1].duration;
166  myPhases[i].minDur += myPhases[i + 1].minDur;
167  } else {
168  myPhases[i].minDur = myPhases[i + 1].minDur;
169  }
170  }
173  myPhases[i].maxDur += myPhases[i + 1].maxDur;
174  } else {
175  myPhases[i].maxDur = myPhases[i + 1].maxDur;
176  }
177  }
178  myPhases.erase(myPhases.begin() + i + 1);
179  }
180  // check if actuated lights are defined correctly
181  if (checkVarDurations) {
182  if (myType != TLTYPE_STATIC) {
183  bool found = false;
184  for (auto p : myPhases) {
187  found = true;
188  break;
189  }
190  }
191  if (!found) {
192  WRITE_WARNING("Non-static traffic light '" + getID() + "' does not define variable phase length.");
193  }
194  }
195  }
196 }
197 
198 
199 void
200 NBTrafficLightLogic::setPhaseState(int phaseIndex, int tlIndex, LinkState linkState) {
201  assert(phaseIndex < (int)myPhases.size());
202  std::string& phaseState = myPhases[phaseIndex].state;
203  assert(tlIndex < (int)phaseState.size());
204  phaseState[tlIndex] = (char)linkState;
205 }
206 
207 
208 void
210  assert(phaseIndex < (int)myPhases.size());
211  myPhases[phaseIndex].duration = duration;
212 }
213 
214 void
216  assert(phaseIndex < (int)myPhases.size());
217  myPhases[phaseIndex].minDur = duration;
218 }
219 
220 void
222  assert(phaseIndex < (int)myPhases.size());
223  myPhases[phaseIndex].maxDur = duration;
224 }
225 
226 
227 /****************************************************************************/
228 
The link has green light, may pass.
static const std::string ALLOWED_STATES
long long int SUMOTime
Definition: SUMOTime.h:36
SUMOTime getOffset() const
Returns the offset of first switch.
void setPhaseMaxDuration(int phaseIndex, SUMOTime duration)
void setPhaseState(int phaseIndex, int tlIndex, LinkState linkState)
Modifies the state for an existing phase (used by NETEDIT)
A SUMO-compliant built logic for a traffic light.
The link has green light, has to brake.
This is an uncontrolled, minor link, has to stop.
int myNumLinks
The number of participating links.
NBTrafficLightLogic(const std::string &id, const std::string &subid, int noLinks, SUMOTime offset=0, TrafficLightType type=TLTYPE_STATIC)
Constructor.
void setPhaseMinDuration(int phaseIndex, SUMOTime duration)
static const SUMOTime UNSPECIFIED_DURATION
const std::string & getProgramID() const
Returns the ProgramID.
const std::string & getID() const
Returns the id.
Definition: Named.h:69
The definition of a single phase of the logic.
The link is controlled by a tls which is off, not blinking, may pass.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:242
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
SUMOTime myOffset
The tls program&#39;s offset.
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
Base class for objects which have an id.
Definition: Named.h:49
SUMOTime getDuration() const
Returns the duration of the complete cycle.
void closeBuilding(bool checkVarDurations=true)
closes the building process
The link has yellow light, may pass.
The link is controlled by a tls which is off and blinks, has to brake.
TrafficLightType getType() const
get the algorithm type (static etc..)
const std::string mySubID
The tls program&#39;s subid.
~NBTrafficLightLogic()
Destructor.
The link has red light (must brake)
void setPhaseDuration(int phaseIndex, SUMOTime duration)
Modifies the duration for an existing phase (used by NETEDIT)
void setStateLength(int numLinks, LinkState fill=LINKSTATE_TL_RED)
TrafficLightType myType
The algorithm type for the traffic light.
The link has yellow light, has to brake anyway.
void addStep(SUMOTime duration, const std::string &state, int index=-1)
Adds a phase to the logic.
The link has red light (must brake) but indicates upcoming green.
PhaseDefinitionVector myPhases
The junction logic&#39;s storage for traffic light phase list.
TrafficLightType
static const char allowedStatesInitializer[]
all allowed characters for phase state (see SUMOXMLDefinitions)