SUMO - Simulation of Urban MObility
MSPhaseDefinition.h
Go to the documentation of this file.
1 /****************************************************************************/
11 // The definition of a single phase of a tls logic
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 #ifndef MSPhaseDefinition_h
25 #define MSPhaseDefinition_h
26 
27 #define TARGET_BIT 0
28 #define TRANSIENT_NOTDECISIONAL_BIT 1
29 #define COMMIT_BIT 2
30 #define UNDEFINED_BIT 3
31 
32 
33 
34 // ===========================================================================
35 // included modules
36 // ===========================================================================
37 #ifdef _MSC_VER
38 #include <windows_config.h>
39 #else
40 #include <config.h>
41 #endif
42 
43 #include <bitset>
44 #include <string>
45 #include <vector>
47 #include <utils/common/SUMOTime.h>
50 
51 
52 // ===========================================================================
53 // class definitions
54 // ===========================================================================
60 public:
61  /*
62  * @brief The definition of phase types
63  * Phase types are compulsory directives for SOTL policies.
64  * Knowing the phase type a policy can drive complex junction that need higly customized phases.
65  * Leaving the phase type as "undefined" makes SOTL policies to malfunction.
66  * Four bits:
67  * TARGET_BIT 0 -> the phase is a target one
68  * TRANSIENT_NOTDECISIONAL_BIT 1 -> the phase is a transient one or a decisional one
69  * COMMIT_BIT 2 -> the phase is a commit one
70  * UNDEFINED_BIT 3 -> the phase type is undefined
71  */
72  typedef std::bitset<4> PhaseType;
73 
74  typedef std::vector<std::string> LaneIdVector;
75 
76 public:
79 
82 
85 
88 
91 
92 private:
94  std::string state;
95 
96  /*
97  * The type of this phase
98  */
99  PhaseType phaseType;
100 
101  /*
102  * @brief The lanes-set
103  * This array can be null if this phase is not a target step,
104  * otherwise, a bit is true if the corresponding lane belongs to a
105  * set of input lanes.
106  * SOTL traffic light logics choose the target step according to sensors
107  * belonging to the lane-set.
108  */
109  LaneIdVector targetLaneSet;
110 
111  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg) {
112  this->duration = durationArg;
113  this->minDuration = minDurationArg < 0 ? durationArg : minDurationArg;
114  this->maxDuration = (maxDurationArg < 0 || maxDurationArg < minDurationArg) ? durationArg : maxDurationArg;
115  // assert(this->minDuration <= this->maxDuration); // not ensured by the previous lines
116  this->state = stateArg;
117  this->myLastSwitch = string2time(OptionsCont::getOptions().getString("begin")); // SUMOTime-option
118  //For SOTL phases
119  //this->phaseType = phaseTypeArg;
120  }
121 
122  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, LaneIdVector& targetLaneSetArg) {
123  init(durationArg, minDurationArg, maxDurationArg, stateArg);
124  //For SOTL target phases
125  this->targetLaneSet = targetLaneSetArg;
126  }
127 
128 
129 public:
137  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg) {
138  //PhaseType phaseType;
139  phaseType = PhaseType();
140  phaseType[UNDEFINED_BIT] = 1;
141  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
142  phaseType[TARGET_BIT] = 0;
143  phaseType[COMMIT_BIT] = 0;
144  init(durationArg, durationArg, durationArg, stateArg);
145  }
146 
147 
155  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg) throw() {
156  //PhaseType phaseType;
157  phaseType = PhaseType();
158  phaseType[UNDEFINED_BIT] = 1;
159  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
160  phaseType[TARGET_BIT] = 0;
161  phaseType[COMMIT_BIT] = 0;
162  init(durationArg, minDurationArg, maxDurationArg, stateArg);
163  }
164 
165  /*
166  * @brief Constructor for definitions for SOTL target step
167  * In this phase the duration is fixed, because min and max duration are unspecified
168  * @param[in] transient_notdecisional true means this is a transient phase, false is a decisional one
169  * @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
170  * @param[in] targetLaneSet identifies the lane-set to be considered when deciding the target phase to jump to
171  * @see MSPhaseDefinition::PhaseType
172  */
173  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) throw() {
174  if (targetLaneSetArg.size() == 0) {
175  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
176  }
177  //PhaseType phaseType;
178  phaseType = PhaseType();
179  phaseType[UNDEFINED_BIT] = 0;
180  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
181  phaseType[TARGET_BIT] = 1;
182  phaseType[COMMIT_BIT] = commit;
183  init(durationArg, durationArg, durationArg, stateArg, targetLaneSetArg);
184  }
185 
186  /*
187  * @brief Constructor for definitions for SOTL non-target step
188  * In this phase the duration is fixed, because min and max duration are unspecified
189  * @param[in] phaseType Indicates the type of the step
190  */
191  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, bool transient_notdecisional, bool commit) throw() {
192  //PhaseType phaseType;
193  phaseType = PhaseType();
194  phaseType[UNDEFINED_BIT] = 0;
195  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
196  phaseType[TARGET_BIT] = 0;
197  phaseType[COMMIT_BIT] = commit;
198  init(durationArg, durationArg, durationArg, stateArg);
199  }
200 
201 
202  /*
203  * @brief Constructor for definitions for SOTL target step
204  * In this phase the duration is constrained between min and max duration
205  * @param[in] phaseType Indicates the type of the step
206  * @param[in] targetLaneSet If not null, specifies this MSPhaseDefinition is a target step
207  * @see MSPhaseDefinition::PhaseType
208  */
209  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) throw() {
210  if (targetLaneSetArg.size() == 0) {
211  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
212  }
213  //PhaseType phaseType;
214  //phaseType = PhaseType::bitset();
215  phaseType = PhaseType();
216  phaseType[UNDEFINED_BIT] = 0;
217  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
218  phaseType[TARGET_BIT] = 1;
219  phaseType[COMMIT_BIT] = commit;
220  init(durationArg, minDurationArg, maxDurationArg, stateArg, targetLaneSetArg);
221  }
222 
223  /*
224  * @brief Constructor for definitions for SOTL target step
225  * In this phase the duration is constrained between min and max duration
226  * @param[in] phaseType Indicates the type of the step
227  * @see MSPhaseDefinition::PhaseType
228  */
229  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, bool transient_notdecisional, bool commit) throw() {
230  //PhaseType phaseType;
231  phaseType = PhaseType();
232  phaseType[UNDEFINED_BIT] = 0;
233  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
234  phaseType[TARGET_BIT] = 0;
235  phaseType[COMMIT_BIT] = commit;
236  init(durationArg, minDurationArg, maxDurationArg, stateArg);
237 
238  }
239 
240 
242  virtual ~MSPhaseDefinition() { }
243 
244 
248  const std::string& getState() const {
249  return state;
250  }
251 
252  const LaneIdVector& getTargetLaneSet() const throw() {
253  return targetLaneSet;
254  }
255 
263  bool isGreenPhase() const {
264  if (state.find_first_of("gG") == std::string::npos) {
265  return false;
266  }
267  if (state.find_first_of("yY") != std::string::npos) {
268  return false;
269  }
270  return true;
271  }
272 
273 
278  LinkState getSignalState(int pos) const {
279  return (LinkState) state[pos];
280  }
281 
282 
289  bool operator!=(const MSPhaseDefinition& pd) {
290  return state != pd.state;
291  }
292 
293 
294  /*
295  * @return true if the phase type is undefined
296  */
297  bool isUndefined() const throw() {
298  return phaseType[UNDEFINED_BIT];
299  }
300 
301  /*
302  * @return true if this is a target phase
303  */
304  bool isTarget() const throw() {
305  return phaseType[TARGET_BIT];
306  }
307 
308  /*
309  * @return true if this is a transient phase
310  */
311  bool isTransient() const throw() {
312  return phaseType[TRANSIENT_NOTDECISIONAL_BIT];
313  }
314 
315  /*
316  * @return true if this is a decisional phase
317  */
318  bool isDecisional() const throw() {
319  return !phaseType[TRANSIENT_NOTDECISIONAL_BIT];
320  }
321 
322  /*
323  * @return true if this is a commit phase
324  */
325  bool isCommit() const throw() {
326  return phaseType[COMMIT_BIT];
327  }
328 
329 };
330 
331 #endif
332 
333 /****************************************************************************/
334 
MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg, bool transient_notdecisional, bool commit, LaneIdVector &targetLaneSetArg)
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
#define COMMIT_BIT
#define UNDEFINED_BIT
#define TRANSIENT_NOTDECISIONAL_BIT
LaneIdVector targetLaneSet
std::bitset< 4 > PhaseType
LinkState getSignalState(int pos) const
Returns the state of the tls signal at the given position.
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg)
Constructor.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:65
MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg)
Constructor In this phase the duration is constrained between min and max duration.
SUMOTime duration
The duration of the phase.
void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg, LaneIdVector &targetLaneSetArg)
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
bool isTransient() const
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:47
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, bool transient_notdecisional, bool commit)
SUMOTime lastDuration
The previous duration of the phase.
bool isUndefined() const
std::string state
The phase definition.
void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg)
SUMOTime maxDuration
The maximum duration of the phase.
std::vector< std::string > LaneIdVector
MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg, bool transient_notdecisional, bool commit)
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:85
#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, bool transient_notdecisional, bool commit, LaneIdVector &targetLaneSetArg)
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
virtual ~MSPhaseDefinition()
Destructor.
long long int SUMOTime
Definition: TraCIDefs.h:52
const LaneIdVector & getTargetLaneSet() const
The definition of a single phase of a tls logic.
bool isDecisional() const