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