SUMO - Simulation of Urban MObility
MSPhaseDefinition.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
19 // The definition of a single phase of a tls logic
20 /****************************************************************************/
21 #ifndef MSPhaseDefinition_h
22 #define MSPhaseDefinition_h
23 
24 #define TARGET_BIT 0
25 #define TRANSIENT_NOTDECISIONAL_BIT 1
26 #define COMMIT_BIT 2
27 #define UNDEFINED_BIT 3
28 
29 
30 
31 // ===========================================================================
32 // included modules
33 // ===========================================================================
34 #include <config.h>
35 
36 #include <bitset>
37 #include <string>
38 #include <vector>
40 #include <utils/common/SUMOTime.h>
43 
44 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
53 public:
54  /*
55  * @brief The definition of phase types
56  * Phase types are compulsory directives for SOTL policies.
57  * Knowing the phase type a policy can drive complex junction that need higly customized phases.
58  * Leaving the phase type as "undefined" makes SOTL policies to malfunction.
59  * Four bits:
60  * TARGET_BIT 0 -> the phase is a target one
61  * TRANSIENT_NOTDECISIONAL_BIT 1 -> the phase is a transient one or a decisional one
62  * COMMIT_BIT 2 -> the phase is a commit one
63  * UNDEFINED_BIT 3 -> the phase type is undefined
64  */
65  typedef std::bitset<4> PhaseType;
66 
67  typedef std::vector<std::string> LaneIdVector;
68 
69 public:
72 
75 
78 
81 
84 
86  int nextPhase;
87 
88 private:
90  std::string state;
91 
92  /*
93  * The type of this phase
94  */
95  PhaseType phaseType;
96 
97  /*
98  * @brief The lanes-set
99  * This array can be null if this phase is not a target step,
100  * otherwise, a bit is true if the corresponding lane belongs to a
101  * set of input lanes.
102  * SOTL traffic light logics choose the target step according to sensors
103  * belonging to the lane-set.
104  */
105  LaneIdVector targetLaneSet;
106 
107  void init(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, int nextPhase) {
108  this->duration = durationArg;
109  this->state = stateArg;
110  this->minDuration = minDurationArg < 0 ? durationArg : minDurationArg;
111  this->maxDuration = (maxDurationArg < 0 || maxDurationArg < minDurationArg) ? durationArg : maxDurationArg;
112  // assert(this->minDuration <= this->maxDuration); // not ensured by the previous lines
113  this->myLastSwitch = string2time(OptionsCont::getOptions().getString("begin")); // SUMOTime-option
114  //For SOTL phases
115  //this->phaseType = phaseTypeArg;
116  this->nextPhase = nextPhase;
117  }
118 
119  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, int nextPhase, LaneIdVector& targetLaneSetArg) {
120  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhase);
121  //For SOTL target phases
122  this->targetLaneSet = targetLaneSetArg;
123  }
124 
125 
126 public:
134  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, int nextPhase) {
135  //PhaseType phaseType;
136  phaseType = PhaseType();
137  phaseType[UNDEFINED_BIT] = 1;
138  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
139  phaseType[TARGET_BIT] = 0;
140  phaseType[COMMIT_BIT] = 0;
141  init(durationArg, stateArg, durationArg, durationArg, nextPhase);
142  }
143 
144 
152  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg=-1, SUMOTime maxDurationArg=-1, int nextPhase=-1) {
153  //PhaseType phaseType;
154  phaseType = PhaseType();
155  phaseType[UNDEFINED_BIT] = 1;
156  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
157  phaseType[TARGET_BIT] = 0;
158  phaseType[COMMIT_BIT] = 0;
159  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhase);
160  }
161 
162  /*
163  * @brief Constructor for definitions for SOTL target step
164  * In this phase the duration is fixed, because min and max duration are unspecified
165  * @param[in] transient_notdecisional true means this is a transient phase, false is a decisional one
166  * @param[in] commit true means this is a commit phase, i.e. the traffic light logic can jump to a target phase form this phase
167  * @param[in] targetLaneSet identifies the lane-set to be considered when deciding the target phase to jump to
168  * @see MSPhaseDefinition::PhaseType
169  */
170  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, int nextPhase, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) {
171  if (targetLaneSetArg.size() == 0) {
172  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
173  }
174  //PhaseType phaseType;
175  phaseType = PhaseType();
176  phaseType[UNDEFINED_BIT] = 0;
177  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
178  phaseType[TARGET_BIT] = 1;
179  phaseType[COMMIT_BIT] = commit;
180  init(durationArg, durationArg, durationArg, stateArg, nextPhase, targetLaneSetArg);
181  }
182 
183  /*
184  * @brief Constructor for definitions for SOTL non-target step
185  * In this phase the duration is fixed, because min and max duration are unspecified
186  * @param[in] phaseType Indicates the type of the step
187  */
188  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, int nextPhase, bool transient_notdecisional, bool commit) {
189  //PhaseType phaseType;
190  phaseType = PhaseType();
191  phaseType[UNDEFINED_BIT] = 0;
192  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
193  phaseType[TARGET_BIT] = 0;
194  phaseType[COMMIT_BIT] = commit;
195  init(durationArg, stateArg, durationArg, durationArg, nextPhase);
196  }
197 
198 
199  /*
200  * @brief Constructor for definitions for SOTL target step
201  * In this phase the duration is constrained between min and max duration
202  * @param[in] phaseType Indicates the type of the step
203  * @param[in] targetLaneSet If not null, specifies this MSPhaseDefinition is a target step
204  * @see MSPhaseDefinition::PhaseType
205  */
206  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, int nextPhase, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) {
207  if (targetLaneSetArg.size() == 0) {
208  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
209  }
210  //PhaseType phaseType;
211  //phaseType = PhaseType::bitset();
212  phaseType = PhaseType();
213  phaseType[UNDEFINED_BIT] = 0;
214  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
215  phaseType[TARGET_BIT] = 1;
216  phaseType[COMMIT_BIT] = commit;
217  init(durationArg, minDurationArg, maxDurationArg, stateArg, nextPhase, targetLaneSetArg);
218  }
219 
220  /*
221  * @brief Constructor for definitions for SOTL target step
222  * In this phase the duration is constrained between min and max duration
223  * @param[in] phaseType Indicates the type of the step
224  * @see MSPhaseDefinition::PhaseType
225  */
226  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, int nextPhase, bool transient_notdecisional, bool commit) {
227  //PhaseType phaseType;
228  phaseType = PhaseType();
229  phaseType[UNDEFINED_BIT] = 0;
230  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
231  phaseType[TARGET_BIT] = 0;
232  phaseType[COMMIT_BIT] = commit;
233  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhase);
234 
235  }
236 
237 
239  virtual ~MSPhaseDefinition() { }
240 
241 
245  const std::string& getState() const {
246  return state;
247  }
248 
249  void setState(const std::string& _state) {
250  state = _state;
251  }
252 
253  const LaneIdVector& getTargetLaneSet() const {
254  return targetLaneSet;
255  }
256 
257  int getNextPhase() const {
258  return nextPhase;
259  }
260 
268  bool isGreenPhase() const {
269  if (state.find_first_of("gG") == std::string::npos) {
270  return false;
271  }
272  if (state.find_first_of("yY") != std::string::npos) {
273  return false;
274  }
275  return true;
276  }
277 
278 
283  LinkState getSignalState(int pos) const {
284  return (LinkState) state[pos];
285  }
286 
287 
294  bool operator!=(const MSPhaseDefinition& pd) {
295  return state != pd.state;
296  }
297 
298 
299  /*
300  * @return true if the phase type is undefined
301  */
302  bool isUndefined() const {
303  return phaseType[UNDEFINED_BIT];
304  }
305 
306  /*
307  * @return true if this is a target phase
308  */
309  bool isTarget() const {
310  return phaseType[TARGET_BIT];
311  }
312 
313  /*
314  * @return true if this is a transient phase
315  */
316  bool isTransient() const {
317  return phaseType[TRANSIENT_NOTDECISIONAL_BIT];
318  }
319 
320  /*
321  * @return true if this is a decisional phase
322  */
323  bool isDecisional() const {
324  return !phaseType[TRANSIENT_NOTDECISIONAL_BIT];
325  }
326 
327  /*
328  * @return true if this is a commit phase
329  */
330  bool isCommit() const {
331  return phaseType[COMMIT_BIT];
332  }
333 
334 };
335 
336 #endif
337 
338 /****************************************************************************/
339 
const std::string & getState() const
Returns the state within this phase.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:76
long long int SUMOTime
Definition: SUMOTime.h:36
#define COMMIT_BIT
#define UNDEFINED_BIT
int getNextPhase() const
#define TRANSIENT_NOTDECISIONAL_BIT
LaneIdVector targetLaneSet
std::bitset< 4 > PhaseType
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, int nextPhase, bool transient_notdecisional, bool commit, LaneIdVector &targetLaneSetArg)
LinkState getSignalState(int pos) const
Returns the state of the tls signal at the given position.
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, int nextPhase, bool transient_notdecisional, bool commit)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
SUMOTime duration
The duration of the phase.
void setState(const std::string &_state)
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
bool isTransient() const
void init(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, int nextPhase)
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic, in MSLink and GNEInternalLane.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:42
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg=-1, SUMOTime maxDurationArg=-1, int nextPhase=-1)
Constructor In this phase the duration is constrained between min and max duration.
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, int nextPhase)
Constructor.
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, int nextPhase, bool transient_notdecisional, bool commit)
SUMOTime lastDuration
The previous duration of the phase.
bool isUndefined() const
std::string state
The phase definition.
SUMOTime maxDuration
The maximum duration of the phase.
std::vector< std::string > LaneIdVector
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:113
#define TARGET_BIT
bool operator!=(const MSPhaseDefinition &pd)
Comparison operator.
SUMOTime minDuration
The minimum duration of the phase.
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, int nextPhase, bool transient_notdecisional, bool commit, LaneIdVector &targetLaneSetArg)
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
virtual ~MSPhaseDefinition()
Destructor.
const LaneIdVector & getTargetLaneSet() const
int nextPhase
The index of the phase that suceeds this one (or -1)
void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg, int nextPhase, LaneIdVector &targetLaneSetArg)
The definition of a single phase of a tls logic.
bool isDecisional() const