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-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 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 // ===========================================================================
47 
48 // ===========================================================================
49 // member method definitions
50 // ===========================================================================
52  const std::string& subid, int noLinks,
53  SUMOTime offset, TrafficLightType type) :
54  Named(id), myNumLinks(noLinks), mySubID(subid),
55  myOffset(offset),
56  myType(type) {}
57 
59  Named(logic->getID()),
60  myNumLinks(logic->myNumLinks),
61  mySubID(logic->getProgramID()),
62  myOffset(logic->getOffset()),
63  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
64  myType(logic->getType()) {}
65 
66 
68 
69 void
70 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, const std::vector<int>& next, const std::string& name, int index) {
71  addStep(duration, state,
74  next, name, index);
75 }
76 
77 void
78 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, SUMOTime minDur, SUMOTime maxDur, const std::vector<int>& next, const std::string& name, int index) {
79  // check state size
80  if (myNumLinks == 0) {
81  // initialize
82  myNumLinks = (int)state.size();
83  } else if ((int)state.size() != myNumLinks) {
84  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
85  " does not match declared number of links " + toString(myNumLinks));
86  }
87  // check state contents
88  const std::string::size_type illegal = state.find_first_not_of(SUMOXMLDefinitions::ALLOWED_TLS_LINKSTATES);
89  if (std::string::npos != illegal) {
90  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
91  }
92  // interpret index
93  if (index < 0 || index >= (int)myPhases.size()) {
94  // insert at the end
95  index = (int)myPhases.size();
96  }
97  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state, minDur, maxDur, next, name));
98 }
99 
100 
101 void
103  if (index >= (int)myPhases.size()) {
104  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
105  + toString(myPhases.size()) + " phases.");
106  }
107  myPhases.erase(myPhases.begin() + index);
108 }
109 
110 
111 void
113  if (myNumLinks > numLinks) {
114  for (PhaseDefinition& p : myPhases) {
115  p.state = p.state.substr(0, numLinks);
116  }
117  } else {
118  std::string add(numLinks - myNumLinks, (char)fill);
119  for (PhaseDefinition& p : myPhases) {
120  p.state = p.state + add;
121  }
122  }
123  myNumLinks = numLinks;
124 }
125 
126 
127 void
129  myNumLinks = 0;
130  myPhases.clear();
131 }
132 
133 
134 SUMOTime
136  SUMOTime duration = 0;
137  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
138  duration += (*i).duration;
139  }
140  return duration;
141 }
142 
143 
144 void
145 NBTrafficLightLogic::closeBuilding(bool checkVarDurations) {
146  for (int i = 0; i < (int)myPhases.size() - 1;) {
147  if (myPhases[i].state != myPhases[i + 1].state) {
148  ++i;
149  continue;
150  }
151  myPhases[i].duration += myPhases[i + 1].duration;
154  myPhases[i].minDur += myPhases[i + 1].minDur;
155  } else {
156  myPhases[i].minDur = myPhases[i + 1].minDur;
157  }
158  }
161  myPhases[i].maxDur += myPhases[i + 1].maxDur;
162  } else {
163  myPhases[i].maxDur = myPhases[i + 1].maxDur;
164  }
165  }
166  myPhases.erase(myPhases.begin() + i + 1);
167  }
168  // check if actuated lights are defined correctly
169  if (checkVarDurations) {
170  if (myType != TLTYPE_STATIC) {
171  bool found = false;
172  for (auto p : myPhases) {
175  found = true;
176  break;
177  }
178  }
179  if (!found) {
180  WRITE_WARNING("Non-static traffic light '" + getID() + "' does not define variable phase length.");
181  }
182  }
183  }
184 }
185 
186 
187 void
188 NBTrafficLightLogic::setPhaseState(int phaseIndex, int tlIndex, LinkState linkState) {
189  assert(phaseIndex < (int)myPhases.size());
190  std::string& phaseState = myPhases[phaseIndex].state;
191  assert(tlIndex < (int)phaseState.size());
192  phaseState[tlIndex] = (char)linkState;
193 }
194 
195 
196 void
198  assert(phaseIndex < (int)myPhases.size());
199  myPhases[phaseIndex].duration = duration;
200 }
201 
202 void
204  assert(phaseIndex < (int)myPhases.size());
205  myPhases[phaseIndex].minDur = duration;
206 }
207 
208 void
210  assert(phaseIndex < (int)myPhases.size());
211  myPhases[phaseIndex].maxDur = duration;
212 }
213 
214 void
215 NBTrafficLightLogic::setPhaseNext(int phaseIndex, const std::vector<int>& next) {
216  assert(phaseIndex < (int)myPhases.size());
217  myPhases[phaseIndex].next = next;
218 }
219 
220 void
221 NBTrafficLightLogic::setPhaseName(int phaseIndex, const std::string& name) {
222  assert(phaseIndex < (int)myPhases.size());
223  myPhases[phaseIndex].name = name;
224 }
225 
226 /****************************************************************************/
227 
NBTrafficLightLogic::getDuration
SUMOTime getDuration() const
Returns the duration of the complete cycle.
Definition: NBTrafficLightLogic.cpp:135
ToString.h
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
NBTrafficLightLogic::setPhaseName
void setPhaseName(int phaseIndex, const std::string &name)
Definition: NBTrafficLightLogic.cpp:221
Named
Base class for objects which have an id.
Definition: Named.h:57
OptionsCont.h
NBTrafficLightLogic.h
TLTYPE_STATIC
Definition: SUMOXMLDefinitions.h:1193
MsgHandler.h
NBTrafficLightLogic::closeBuilding
void closeBuilding(bool checkVarDurations=true)
closes the building process
Definition: NBTrafficLightLogic.cpp:145
TrafficLightType
TrafficLightType
Definition: SUMOXMLDefinitions.h:1192
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
NBEdgeCont.h
NBTrafficLightLogic::setPhaseDuration
void setPhaseDuration(int phaseIndex, SUMOTime duration)
Modifies the duration for an existing phase (used by NETEDIT)
Definition: NBTrafficLightLogic.cpp:197
NBTrafficLightDefinition.h
NBTrafficLightLogic::PhaseDefinition
The definition of a single phase of the logic.
Definition: NBTrafficLightLogic.h:58
NBTrafficLightLogic::myPhases
PhaseDefinitionVector myPhases
The junction logic's storage for traffic light phase list.
Definition: NBTrafficLightLogic.h:250
NBTrafficLightLogic::setPhaseState
void setPhaseState(int phaseIndex, int tlIndex, LinkState linkState)
Modifies the state for an existing phase (used by NETEDIT)
Definition: NBTrafficLightLogic.cpp:188
NBTrafficLightLogic::addStep
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.
Definition: NBTrafficLightLogic.cpp:70
SUMOXMLDefinitions::ALLOWED_TLS_LINKSTATES
static const std::string ALLOWED_TLS_LINKSTATES
all allowed characters for phase state
Definition: SUMOXMLDefinitions.h:1446
LinkState
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
Definition: SUMOXMLDefinitions.h:1132
NBTrafficLightLogic::setPhaseMinDuration
void setPhaseMinDuration(int phaseIndex, SUMOTime duration)
Definition: NBTrafficLightLogic.cpp:203
NBTrafficLightDefinition::UNSPECIFIED_DURATION
static const SUMOTime UNSPECIFIED_DURATION
Definition: NBTrafficLightDefinition.h:71
NBTrafficLightLogic::setStateLength
void setStateLength(int numLinks, LinkState fill=LINKSTATE_TL_RED)
Definition: NBTrafficLightLogic.cpp:112
OutputDevice.h
ProcessError
Definition: UtilExceptions.h:40
NBTrafficLightLogic::NBTrafficLightLogic
NBTrafficLightLogic(const std::string &id, const std::string &subid, int noLinks, SUMOTime offset=0, TrafficLightType type=TLTYPE_STATIC)
Constructor.
Definition: NBTrafficLightLogic.cpp:51
NBTrafficLightLogic::~NBTrafficLightLogic
~NBTrafficLightLogic()
Destructor.
Definition: NBTrafficLightLogic.cpp:67
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Option.h
NBTrafficLightLogic::deletePhase
void deletePhase(int index)
Definition: NBTrafficLightLogic.cpp:102
InvalidArgument
Definition: UtilExceptions.h:57
config.h
NBTrafficLightLogic::myType
TrafficLightType myType
The algorithm type for the traffic light.
Definition: NBTrafficLightLogic.h:253
NBTrafficLightLogic::resetPhases
void resetPhases()
Definition: NBTrafficLightLogic.cpp:128
StringTokenizer.h
NBTrafficLightLogic
A SUMO-compliant built logic for a traffic light.
Definition: NBTrafficLightLogic.h:52
NBTrafficLightLogic::setPhaseNext
void setPhaseNext(int phaseIndex, const std::vector< int > &next)
Definition: NBTrafficLightLogic.cpp:215
NBTrafficLightLogic::myNumLinks
int myNumLinks
The number of participating links.
Definition: NBTrafficLightLogic.h:238
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
NBEdge.h
NBTrafficLightLogic::setPhaseMaxDuration
void setPhaseMaxDuration(int phaseIndex, SUMOTime duration)
Definition: NBTrafficLightLogic.cpp:209