SUMO - Simulation of Urban MObility
NBTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A SUMO-compliant built logic for a traffic light
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <vector>
34 #include <bitset>
35 #include <utility>
36 #include <string>
37 #include <sstream>
38 #include <cassert>
39 #include "NBEdge.h"
40 #include "NBEdgeCont.h"
41 #include "NBTrafficLightLogic.h"
43 #include <utils/options/Option.h>
44 #include <utils/common/ToString.h>
47 
48 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // static members
55 // ===========================================================================
58  LINKSTATE_STOP, // used for NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED
65  };
66 
68 
69 // ===========================================================================
70 // member method definitions
71 // ===========================================================================
73  const std::string& subid, unsigned int noLinks,
74  SUMOTime offset, TrafficLightType type) :
75  Named(id), myNumLinks(noLinks), mySubID(subid),
76  myOffset(offset),
77  myType(type) {}
78 
80  Named(logic->getID()),
81  myNumLinks(logic->myNumLinks),
82  mySubID(logic->getProgramID()),
83  myOffset(logic->getOffset()),
84  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
85  myType(logic->getType()) {}
86 
87 
89 
90 
91 void
92 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
93  // check state size
94  if (myNumLinks == 0) {
95  // initialize
96  myNumLinks = (unsigned int)state.size();
97  } else if (state.size() != myNumLinks) {
98  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
99  " does not match declared number of links " + toString(myNumLinks));
100  }
101  // check state contents
102  const size_t illegal = state.find_first_not_of(ALLOWED_STATES);
103  if (std::string::npos != illegal) {
104  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
105  }
106  // interpret index
107  if (index < 0 || index >= (int)myPhases.size()) {
108  // insert at the end
109  index = (int)myPhases.size();
110  }
111  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state));
112 }
113 
114 
115 void
116 NBTrafficLightLogic::deletePhase(unsigned int index) {
117  if (index >= myPhases.size()) {
118  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
119  + toString(myPhases.size()) + " phases.");
120  }
121  myPhases.erase(myPhases.begin() + index);
122 }
123 
124 
125 void
127  myNumLinks = 0;
128  myPhases.clear();
129 }
130 
131 
132 SUMOTime
134  SUMOTime duration = 0;
135  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
136  duration += (*i).duration;
137  }
138  return duration;
139 }
140 
141 
142 void
144  for (unsigned int i = 0; i < myPhases.size() - 1;) {
145  if (myPhases[i].state != myPhases[i + 1].state) {
146  ++i;
147  continue;
148  }
149  myPhases[i].duration += myPhases[i + 1].duration;
150  myPhases.erase(myPhases.begin() + i + 1);
151  }
152 }
153 
154 
155 void
156 NBTrafficLightLogic::setPhaseState(unsigned int phaseIndex, unsigned int tlIndex, LinkState linkState) {
157  assert(phaseIndex < myPhases.size());
158  std::string& phaseState = myPhases[phaseIndex].state;
159  assert(tlIndex < phaseState.size());
160  phaseState[tlIndex] = (char)linkState;
161 }
162 
163 
164 void
165 NBTrafficLightLogic::setPhaseDuration(unsigned int phaseIndex, SUMOTime duration) {
166  assert(phaseIndex < myPhases.size());
167  myPhases[phaseIndex].duration = duration;
168 }
169 
170 
171 /****************************************************************************/
172 
The link has green light, may pass.
static const std::string ALLOWED_STATES
TrafficLightType getType() const
get the algorithm type (static etc..)
long long int SUMOTime
Definition: SUMOTime.h:43
const std::string & getProgramID() const
Returns the ProgramID.
void closeBuilding()
closes the building process
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.
The definition of a single phase of the logic.
The link is controlled by a tls which is off, not blinking, may pass.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
SUMOTime myOffset
The tls program&#39;s offset.
void setPhaseDuration(unsigned int phaseIndex, SUMOTime duration)
Modifies the duration for an existing phase (used by NETEDIT)
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
void setPhaseState(unsigned int phaseIndex, unsigned int tlIndex, LinkState linkState)
Modifies the state for an existing phase (used by NETEDIT)
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
SUMOTime getDuration() const
Returns the duration of the complete cycle.
Base class for objects which have an id.
Definition: Named.h:45
SUMOTime getOffset() const
Returns the offset of first switch.
unsigned int myNumLinks
The number of participating links.
void deletePhase(unsigned int index)
The link has yellow light, may pass.
The link is controlled by a tls which is off and blinks, has to brake.
const std::string mySubID
The tls program&#39;s subid.
~NBTrafficLightLogic()
Destructor.
The link has red light (must brake)
NBTrafficLightLogic(const std::string &id, const std::string &subid, unsigned int noLinks, SUMOTime offset=0, TrafficLightType type=TLTYPE_STATIC)
Constructor.
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)