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-2017 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 
49 // ===========================================================================
50 // static members
51 // ===========================================================================
54  LINKSTATE_STOP, // used for NODETYPE_TRAFFIC_LIGHT_RIGHT_ON_RED
61  };
62 
64 
65 // ===========================================================================
66 // member method definitions
67 // ===========================================================================
69  const std::string& subid, int noLinks,
70  SUMOTime offset, TrafficLightType type) :
71  Named(id), myNumLinks(noLinks), mySubID(subid),
72  myOffset(offset),
73  myType(type) {}
74 
76  Named(logic->getID()),
77  myNumLinks(logic->myNumLinks),
78  mySubID(logic->getProgramID()),
79  myOffset(logic->getOffset()),
80  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
81  myType(logic->getType()) {}
82 
83 
85 
86 
87 void
88 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
89  // check state size
90  if (myNumLinks == 0) {
91  // initialize
92  myNumLinks = (int)state.size();
93  } else if ((int)state.size() != myNumLinks) {
94  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
95  " does not match declared number of links " + toString(myNumLinks));
96  }
97  // check state contents
98  const std::string::size_type illegal = state.find_first_not_of(ALLOWED_STATES);
99  if (std::string::npos != illegal) {
100  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
101  }
102  // interpret index
103  if (index < 0 || index >= (int)myPhases.size()) {
104  // insert at the end
105  index = (int)myPhases.size();
106  }
107  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state));
108 }
109 
110 
111 void
113  if (index >= (int)myPhases.size()) {
114  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
115  + toString(myPhases.size()) + " phases.");
116  }
117  myPhases.erase(myPhases.begin() + index);
118 }
119 
120 
121 void
123  myNumLinks = 0;
124  myPhases.clear();
125 }
126 
127 
128 SUMOTime
130  SUMOTime duration = 0;
131  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
132  duration += (*i).duration;
133  }
134  return duration;
135 }
136 
137 
138 void
140  for (int i = 0; i < (int)myPhases.size() - 1;) {
141  if (myPhases[i].state != myPhases[i + 1].state) {
142  ++i;
143  continue;
144  }
145  myPhases[i].duration += myPhases[i + 1].duration;
146  myPhases.erase(myPhases.begin() + i + 1);
147  }
148 }
149 
150 
151 void
152 NBTrafficLightLogic::setPhaseState(int phaseIndex, int tlIndex, LinkState linkState) {
153  assert(phaseIndex < (int)myPhases.size());
154  std::string& phaseState = myPhases[phaseIndex].state;
155  assert(tlIndex < (int)phaseState.size());
156  phaseState[tlIndex] = (char)linkState;
157 }
158 
159 
160 void
162  assert(phaseIndex < (int)myPhases.size());
163  myPhases[phaseIndex].duration = duration;
164 }
165 
166 
167 /****************************************************************************/
168 
The link has green light, may pass.
static const std::string ALLOWED_STATES
SUMOTime getOffset() const
Returns the offset of first switch.
void closeBuilding()
closes the building process
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.
const std::string & getProgramID() const
Returns the ProgramID.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
The definition of a single phase of the logic.
The link is controlled by a tls which is off, not blinking, may pass.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
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:46
SUMOTime getDuration() const
Returns the duration of the complete cycle.
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)
long long int SUMOTime
Definition: TraCIDefs.h:52
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)