SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 // ===========================================================================
64  };
65 
67 
68 // ===========================================================================
69 // member method definitions
70 // ===========================================================================
72  const std::string& subid, unsigned int noLinks,
73  SUMOTime offset, TrafficLightType type) :
74  Named(id), myNumLinks(noLinks), mySubID(subid),
75  myOffset(offset),
76  myType(type) {}
77 
79  Named(logic->getID()),
80  myNumLinks(logic->myNumLinks),
81  mySubID(logic->getProgramID()),
82  myOffset(logic->getOffset()),
83  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
84  myType(logic->getType()) {}
85 
86 
88 
89 
90 void
91 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
92  // check state size
93  if (myNumLinks == 0) {
94  // initialize
95  myNumLinks = (unsigned int)state.size();
96  } else if (state.size() != myNumLinks) {
97  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
98  " does not match declared number of links " + toString(myNumLinks));
99  }
100  // check state contents
101  const size_t illegal = state.find_first_not_of(ALLOWED_STATES);
102  if (std::string::npos != illegal) {
103  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
104  }
105  // interpret index
106  if (index < 0 || index >= (int)myPhases.size()) {
107  // insert at the end
108  index = (int)myPhases.size();
109  }
110  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state));
111 }
112 
113 
114 void
115 NBTrafficLightLogic::deletePhase(unsigned int index) {
116  if (index >= myPhases.size()) {
117  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
118  + toString(myPhases.size()) + " phases.");
119  }
120  myPhases.erase(myPhases.begin() + index);
121 }
122 
123 
124 void
126  myNumLinks = 0;
127  myPhases.clear();
128 }
129 
130 
131 SUMOTime
133  SUMOTime duration = 0;
134  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
135  duration += (*i).duration;
136  }
137  return duration;
138 }
139 
140 
141 void
143  for (unsigned int i = 0; i < myPhases.size() - 1;) {
144  if (myPhases[i].state != myPhases[i + 1].state) {
145  ++i;
146  continue;
147  }
148  myPhases[i].duration += myPhases[i + 1].duration;
149  myPhases.erase(myPhases.begin() + i + 1);
150  }
151 }
152 
153 
154 void
155 NBTrafficLightLogic::setPhaseState(unsigned int phaseIndex, unsigned int tlIndex, LinkState linkState) {
156  assert(phaseIndex < myPhases.size());
157  std::string& phaseState = myPhases[phaseIndex].state;
158  assert(tlIndex < phaseState.size());
159  phaseState[tlIndex] = (char)linkState;
160 }
161 
162 
163 void
164 NBTrafficLightLogic::setPhaseDuration(unsigned int phaseIndex, SUMOTime duration) {
165  assert(phaseIndex < myPhases.size());
166  myPhases[phaseIndex].duration = duration;
167 }
168 
169 
170 /****************************************************************************/
171 
The link has green light, may pass.
static const std::string ALLOWED_STATES
void closeBuilding()
closes the building process
A SUMO-compliant built logic for a traffic light.
The link has green light, has to brake.
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:60
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
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.
~NBTrafficLightLogic()
Destructor.
The link has red light (must brake)
int SUMOTime
Definition: SUMOTime.h:43
NBTrafficLightLogic(const std::string &id, const std::string &subid, unsigned int noLinks, SUMOTime offset=0, TrafficLightType type=TLTYPE_STATIC)
Constructor.
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's storage for traffic light phase list.
TrafficLightType
static const char allowedStatesInitializer[]
all allowed characters for phase state (see SUMOXMLDefinitions)