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 
85 private:
87  std::string state;
88 
89  /*
90  * The type of this phase
91  */
92  PhaseType phaseType;
93 
94  /*
95  * @brief The lanes-set
96  * This array can be null if this phase is not a target step,
97  * otherwise, a bit is true if the corresponding lane belongs to a
98  * set of input lanes.
99  * SOTL traffic light logics choose the target step according to sensors
100  * belonging to the lane-set.
101  */
102  LaneIdVector targetLaneSet;
103 
104  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg) {
105  this->duration = durationArg;
106  this->minDuration = minDurationArg < 0 ? durationArg : minDurationArg;
107  this->maxDuration = (maxDurationArg < 0 || maxDurationArg < minDurationArg) ? durationArg : maxDurationArg;
108  // assert(this->minDuration <= this->maxDuration); // not ensured by the previous lines
109  this->state = stateArg;
110  this->myLastSwitch = string2time(OptionsCont::getOptions().getString("begin")); // SUMOTime-option
111  //For SOTL phases
112  //this->phaseType = phaseTypeArg;
113  }
114 
115  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, LaneIdVector& targetLaneSetArg) {
116  init(durationArg, minDurationArg, maxDurationArg, stateArg);
117  //For SOTL target phases
118  this->targetLaneSet = targetLaneSetArg;
119  }
120 
121 
122 public:
130  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg) {
131  //PhaseType phaseType;
132  phaseType = PhaseType();
133  phaseType[UNDEFINED_BIT] = 1;
134  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
135  phaseType[TARGET_BIT] = 0;
136  phaseType[COMMIT_BIT] = 0;
137  init(durationArg, durationArg, durationArg, stateArg);
138  }
139 
140 
148  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg) {
149  //PhaseType phaseType;
150  phaseType = PhaseType();
151  phaseType[UNDEFINED_BIT] = 1;
152  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
153  phaseType[TARGET_BIT] = 0;
154  phaseType[COMMIT_BIT] = 0;
155  init(durationArg, minDurationArg, maxDurationArg, stateArg);
156  }
157 
158  /*
159  * @brief Constructor for definitions for SOTL target step
160  * In this phase the duration is fixed, because min and max duration are unspecified
161  * @param[in] transient_notdecisional true means this is a transient phase, false is a decisional one
162  * @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
163  * @param[in] targetLaneSet identifies the lane-set to be considered when deciding the target phase to jump to
164  * @see MSPhaseDefinition::PhaseType
165  */
166  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) {
167  if (targetLaneSetArg.size() == 0) {
168  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
169  }
170  //PhaseType phaseType;
171  phaseType = PhaseType();
172  phaseType[UNDEFINED_BIT] = 0;
173  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
174  phaseType[TARGET_BIT] = 1;
175  phaseType[COMMIT_BIT] = commit;
176  init(durationArg, durationArg, durationArg, stateArg, targetLaneSetArg);
177  }
178 
179  /*
180  * @brief Constructor for definitions for SOTL non-target step
181  * In this phase the duration is fixed, because min and max duration are unspecified
182  * @param[in] phaseType Indicates the type of the step
183  */
184  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, bool transient_notdecisional, bool commit) {
185  //PhaseType phaseType;
186  phaseType = PhaseType();
187  phaseType[UNDEFINED_BIT] = 0;
188  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
189  phaseType[TARGET_BIT] = 0;
190  phaseType[COMMIT_BIT] = commit;
191  init(durationArg, durationArg, durationArg, stateArg);
192  }
193 
194 
195  /*
196  * @brief Constructor for definitions for SOTL target step
197  * In this phase the duration is constrained between min and max duration
198  * @param[in] phaseType Indicates the type of the step
199  * @param[in] targetLaneSet If not null, specifies this MSPhaseDefinition is a target step
200  * @see MSPhaseDefinition::PhaseType
201  */
202  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) {
203  if (targetLaneSetArg.size() == 0) {
204  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
205  }
206  //PhaseType phaseType;
207  //phaseType = PhaseType::bitset();
208  phaseType = PhaseType();
209  phaseType[UNDEFINED_BIT] = 0;
210  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
211  phaseType[TARGET_BIT] = 1;
212  phaseType[COMMIT_BIT] = commit;
213  init(durationArg, minDurationArg, maxDurationArg, stateArg, targetLaneSetArg);
214  }
215 
216  /*
217  * @brief Constructor for definitions for SOTL target step
218  * In this phase the duration is constrained between min and max duration
219  * @param[in] phaseType Indicates the type of the step
220  * @see MSPhaseDefinition::PhaseType
221  */
222  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, bool transient_notdecisional, bool commit) {
223  //PhaseType phaseType;
224  phaseType = PhaseType();
225  phaseType[UNDEFINED_BIT] = 0;
226  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
227  phaseType[TARGET_BIT] = 0;
228  phaseType[COMMIT_BIT] = commit;
229  init(durationArg, minDurationArg, maxDurationArg, stateArg);
230 
231  }
232 
233 
235  virtual ~MSPhaseDefinition() { }
236 
237 
241  const std::string& getState() const {
242  return state;
243  }
244 
245  void setState(const std::string& _state) {
246  state = _state;
247  }
248 
249  const LaneIdVector& getTargetLaneSet() const {
250  return targetLaneSet;
251  }
252 
260  bool isGreenPhase() const {
261  if (state.find_first_of("gG") == std::string::npos) {
262  return false;
263  }
264  if (state.find_first_of("yY") != std::string::npos) {
265  return false;
266  }
267  return true;
268  }
269 
270 
275  LinkState getSignalState(int pos) const {
276  return (LinkState) state[pos];
277  }
278 
279 
286  bool operator!=(const MSPhaseDefinition& pd) {
287  return state != pd.state;
288  }
289 
290 
291  /*
292  * @return true if the phase type is undefined
293  */
294  bool isUndefined() const {
295  return phaseType[UNDEFINED_BIT];
296  }
297 
298  /*
299  * @return true if this is a target phase
300  */
301  bool isTarget() const {
302  return phaseType[TARGET_BIT];
303  }
304 
305  /*
306  * @return true if this is a transient phase
307  */
308  bool isTransient() const {
309  return phaseType[TRANSIENT_NOTDECISIONAL_BIT];
310  }
311 
312  /*
313  * @return true if this is a decisional phase
314  */
315  bool isDecisional() const {
316  return !phaseType[TRANSIENT_NOTDECISIONAL_BIT];
317  }
318 
319  /*
320  * @return true if this is a commit phase
321  */
322  bool isCommit() const {
323  return phaseType[COMMIT_BIT];
324  }
325 
326 };
327 
328 #endif
329 
330 /****************************************************************************/
331 
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
long long int SUMOTime
Definition: SUMOTime.h:36
#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:58
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)
void setState(const std::string &_state)
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:42
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: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, 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
The definition of a single phase of a tls logic.
bool isDecisional() const