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-2012 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  Named(id), myNumLinks(noLinks), mySubID(subid),
73  myOffset(0) {}
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 
82 
84 
85 
86 void
87 NBTrafficLightLogic::addStep(SUMOTime duration, const std::string& state, int index) {
88  // check state size
89  if (myNumLinks == 0) {
90  // initialize
91  myNumLinks = (unsigned int)state.size();
92  } else if (state.size() != myNumLinks) {
93  throw ProcessError("When adding phase: state length of " + toString(state.size()) +
94  " does not match declared number of links " + toString(myNumLinks));
95  }
96  // check state contents
97  const size_t illegal = state.find_first_not_of(ALLOWED_STATES);
98  if (std::string::npos != illegal) {
99  throw ProcessError("When adding phase: illegal character '" + toString(state[illegal]) + "' in state");
100  }
101  // interpret index
102  if (index < 0 || index >= (int)myPhases.size()) {
103  // insert at the end
104  index = (int)myPhases.size();
105  }
106  myPhases.insert(myPhases.begin() + index, PhaseDefinition(duration, state));
107 }
108 
109 
110 void
111 NBTrafficLightLogic::deletePhase(unsigned int index) {
112  if (index >= myPhases.size()) {
113  throw InvalidArgument("Index " + toString(index) + " out of range for logic with "
114  + toString(myPhases.size()) + " phases.");
115  }
116  myPhases.erase(myPhases.begin() + index);
117 }
118 
119 
120 void
122  myNumLinks = 0;
123  myPhases.clear();
124 }
125 
126 
127 SUMOTime
129  SUMOTime duration = 0;
130  for (PhaseDefinitionVector::const_iterator i = myPhases.begin(); i != myPhases.end(); ++i) {
131  duration += (*i).duration;
132  }
133  return duration;
134 }
135 
136 
137 void
139  for (unsigned int i = 0; i < myPhases.size() - 1;) {
140  if (myPhases[i].state != myPhases[i + 1].state) {
141  ++i;
142  continue;
143  }
144  myPhases[i].duration += myPhases[i + 1].duration;
145  myPhases.erase(myPhases.begin() + i + 1);
146  }
147 }
148 
149 
150 void
151 NBTrafficLightLogic::setPhaseState(unsigned int phaseIndex, int tlIndex, LinkState linkState) {
152  assert(phaseIndex < myPhases.size());
153  std::string& phaseState = myPhases[phaseIndex].state;
154  assert(tlIndex < phaseState.size());
155  phaseState[tlIndex] = (char)linkState;
156 }
157 
158 
159 void
160 NBTrafficLightLogic::setPhaseDuration(unsigned int phaseIndex, SUMOTime duration) {
161  assert(phaseIndex < myPhases.size());
162  myPhases[phaseIndex].duration = duration;
163 }
164 
165 
166 /****************************************************************************/
167