SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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.sourceforge.net/
12 // Copyright (C) 2001-2013 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 // ===========================================================================
63  };
64 
66 
67 // ===========================================================================
68 // member method definitions
69 // ===========================================================================
71  const std::string& subid, unsigned int noLinks,
72  SUMOTime offset, TrafficLightType type) :
73  Named(id), myNumLinks(noLinks), mySubID(subid),
74  myOffset(offset),
75  myType(type) {}
76 
78  Named(logic->getID()),
79  myNumLinks(logic->myNumLinks),
80  mySubID(logic->getProgramID()),
81  myOffset(logic->getOffset()),
82  myPhases(logic->myPhases.begin(), logic->myPhases.end()),
83  myType(logic->getType()) {}
84 
85 
87 
88 
89 void
90 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
91  // check state size
92  if (myNumLinks == 0) {
93  // initialize
94  myNumLinks = (unsigned int)state.size();
95  } else if (state.size() != myNumLinks) {
96  throw ProcessError("When adding phase to tlLogic '" + getID() + "': state length of " + toString(state.size()) +
97  " does not match declared number of links " + toString(myNumLinks));
98  }
99  // check state contents
100  const size_t illegal = state.find_first_not_of(ALLOWED_STATES);
101  if (std::string::npos != illegal) {
102  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
103  }
104  // interpret index
105  if (index < 0 || index >= (int)myPhases.size()) {
106  // insert at the end
107  index = (int)myPhases.size();
108  }
109  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state));
110 }
111 
112 
113 void
114 NBTrafficLightLogic::deletePhase(unsigned int index) {
115  if (index >= myPhases.size()) {
116  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
117  + toString(myPhases.size()) + " phases.");
118  }
119  myPhases.erase(myPhases.begin() + index);
120 }
121 
122 
123 void
125  myNumLinks = 0;
126  myPhases.clear();
127 }
128 
129 
130 SUMOTime
132  SUMOTime duration = 0;
133  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
134  duration += (*i).duration;
135  }
136  return duration;
137 }
138 
139 
140 void
142  for (unsigned int i = 0; i < myPhases.size() - 1;) {
143  if (myPhases[i].state != myPhases[i + 1].state) {
144  ++i;
145  continue;
146  }
147  myPhases[i].duration += myPhases[i + 1].duration;
148  myPhases.erase(myPhases.begin() + i + 1);
149  }
150 }
151 
152 
153 void
154 NBTrafficLightLogic::setPhaseState(unsigned int phaseIndex, unsigned int tlIndex, LinkState linkState) {
155  assert(phaseIndex < myPhases.size());
156  std::string& phaseState = myPhases[phaseIndex].state;
157  assert(tlIndex < phaseState.size());
158  phaseState[tlIndex] = (char)linkState;
159 }
160 
161 
162 void
163 NBTrafficLightLogic::setPhaseDuration(unsigned int phaseIndex, SUMOTime duration) {
164  assert(phaseIndex < myPhases.size());
165  myPhases[phaseIndex].duration = duration;
166 }
167 
168 
169 /****************************************************************************/
170