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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // The definition of a single phase of a tls logic
22 /****************************************************************************/
23 #ifndef MSPhaseDefinition_h
24 #define MSPhaseDefinition_h
25 
26 #define TARGET_BIT 0
27 #define TRANSIENT_NOTDECISIONAL_BIT 1
28 #define COMMIT_BIT 2
29 #define UNDEFINED_BIT 3
30 
31 
32 
33 // ===========================================================================
34 // included modules
35 // ===========================================================================
36 #ifdef _MSC_VER
37 #include <windows_config.h>
38 #else
39 #include <config.h>
40 #endif
41 
42 #include <bitset>
43 #include <string>
44 #include <vector>
46 #include <utils/common/SUMOTime.h>
49 
50 
51 // ===========================================================================
52 // class definitions
53 // ===========================================================================
59 public:
60  /*
61  * @brief The definition of phase types
62  * Phase types are compulsory directives for SOTL policies.
63  * Knowing the phase type a policy can drive complex junction that need higly customized phases.
64  * Leaving the phase type as "undefined" makes SOTL policies to malfunction.
65  * Four bits:
66  * TARGET_BIT 0 -> the phase is a target one
67  * TRANSIENT_NOTDECISIONAL_BIT 1 -> the phase is a transient one or a decisional one
68  * COMMIT_BIT 2 -> the phase is a commit one
69  * UNDEFINED_BIT 3 -> the phase type is undefined
70  */
71  typedef std::bitset<4> PhaseType;
72 
73  typedef std::vector<std::string> LaneIdVector;
74 
75 public:
78 
81 
84 
87 
90 
91 private:
93  std::string state;
94 
95  /*
96  * The type of this phase
97  */
98  PhaseType phaseType;
99 
100  /*
101  * @brief The lanes-set
102  * This array can be null if this phase is not a target step,
103  * otherwise, a bit is true if the corresponding lane belongs to a
104  * set of input lanes.
105  * SOTL traffic light logics choose the target step according to sensors
106  * belonging to the lane-set.
107  */
108  LaneIdVector targetLaneSet;
109 
110  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg) {
111  this->duration = durationArg;
112  this->minDuration = minDurationArg < 0 ? durationArg : minDurationArg;
113  this->maxDuration = (maxDurationArg < 0 || maxDurationArg < minDurationArg) ? durationArg : maxDurationArg;
114  // assert(this->minDuration <= this->maxDuration); // not ensured by the previous lines
115  this->state = stateArg;
116  this->myLastSwitch = string2time(OptionsCont::getOptions().getString("begin")); // SUMOTime-option
117  //For SOTL phases
118  //this->phaseType = phaseTypeArg;
119  }
120 
121  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, LaneIdVector& targetLaneSetArg) {
122  init(durationArg, minDurationArg, maxDurationArg, stateArg);
123  //For SOTL target phases
124  this->targetLaneSet = targetLaneSetArg;
125  }
126 
127 
128 public:
136  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg) {
137  //PhaseType phaseType;
138  phaseType = PhaseType();
139  phaseType[UNDEFINED_BIT] = 1;
140  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
141  phaseType[TARGET_BIT] = 0;
142  phaseType[COMMIT_BIT] = 0;
143  init(durationArg, durationArg, durationArg, stateArg);
144  }
145 
146 
154  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg) throw() {
155  //PhaseType phaseType;
156  phaseType = PhaseType();
157  phaseType[UNDEFINED_BIT] = 1;
158  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
159  phaseType[TARGET_BIT] = 0;
160  phaseType[COMMIT_BIT] = 0;
161  init(durationArg, minDurationArg, maxDurationArg, stateArg);
162  }
163 
164  /*
165  * @brief Constructor for definitions for SOTL target step
166  * In this phase the duration is fixed, because min and max duration are unspecified
167  * @param[in] transient_notdecisional true means this is a transient phase, false is a decisional one
168  * @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
169  * @param[in] targetLaneSet identifies the lane-set to be considered when deciding the target phase to jump to
170  * @see MSPhaseDefinition::PhaseType
171  */
172  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) throw() {
173  if (targetLaneSetArg.size() == 0) {
174  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
175  }
176  //PhaseType phaseType;
177  phaseType = PhaseType();
178  phaseType[UNDEFINED_BIT] = 0;
179  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
180  phaseType[TARGET_BIT] = 1;
181  phaseType[COMMIT_BIT] = commit;
182  init(durationArg, durationArg, durationArg, stateArg, targetLaneSetArg);
183  }
184 
185  /*
186  * @brief Constructor for definitions for SOTL non-target step
187  * In this phase the duration is fixed, because min and max duration are unspecified
188  * @param[in] phaseType Indicates the type of the step
189  */
190  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, bool transient_notdecisional, bool commit) throw() {
191  //PhaseType phaseType;
192  phaseType = PhaseType();
193  phaseType[UNDEFINED_BIT] = 0;
194  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
195  phaseType[TARGET_BIT] = 0;
196  phaseType[COMMIT_BIT] = commit;
197  init(durationArg, durationArg, durationArg, stateArg);
198  }
199 
200 
201  /*
202  * @brief Constructor for definitions for SOTL target step
203  * In this phase the duration is constrained between min and max duration
204  * @param[in] phaseType Indicates the type of the step
205  * @param[in] targetLaneSet If not null, specifies this MSPhaseDefinition is a target step
206  * @see MSPhaseDefinition::PhaseType
207  */
208  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, bool transient_notdecisional, bool commit, LaneIdVector& targetLaneSetArg) throw() {
209  if (targetLaneSetArg.size() == 0) {
210  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
211  }
212  //PhaseType phaseType;
213  //phaseType = PhaseType::bitset();
214  phaseType = PhaseType();
215  phaseType[UNDEFINED_BIT] = 0;
216  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
217  phaseType[TARGET_BIT] = 1;
218  phaseType[COMMIT_BIT] = commit;
219  init(durationArg, minDurationArg, maxDurationArg, stateArg, targetLaneSetArg);
220  }
221 
222  /*
223  * @brief Constructor for definitions for SOTL target step
224  * In this phase the duration is constrained between min and max duration
225  * @param[in] phaseType Indicates the type of the step
226  * @see MSPhaseDefinition::PhaseType
227  */
228  MSPhaseDefinition(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg, bool transient_notdecisional, bool commit) throw() {
229  //PhaseType phaseType;
230  phaseType = PhaseType();
231  phaseType[UNDEFINED_BIT] = 0;
232  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
233  phaseType[TARGET_BIT] = 0;
234  phaseType[COMMIT_BIT] = commit;
235  init(durationArg, minDurationArg, maxDurationArg, stateArg);
236 
237  }
238 
239 
241  virtual ~MSPhaseDefinition() { }
242 
243 
247  const std::string& getState() const {
248  return state;
249  }
250 
251  const LaneIdVector& getTargetLaneSet() const throw() {
252  return targetLaneSet;
253  }
254 
262  bool isGreenPhase() const {
263  if (state.find_first_of("gG") == std::string::npos) {
264  return false;
265  }
266  if (state.find_first_of("yY") != std::string::npos) {
267  return false;
268  }
269  return true;
270  }
271 
272 
277  LinkState getSignalState(int pos) const {
278  return (LinkState) state[pos];
279  }
280 
281 
288  bool operator!=(const MSPhaseDefinition& pd) {
289  return state != pd.state;
290  }
291 
292 
293  /*
294  * @return true if the phase type is undefined
295  */
296  bool isUndefined() const throw() {
297  return phaseType[UNDEFINED_BIT];
298  }
299 
300  /*
301  * @return true if this is a target phase
302  */
303  bool isTarget() const throw() {
304  return phaseType[TARGET_BIT];
305  }
306 
307  /*
308  * @return true if this is a transient phase
309  */
310  bool isTransient() const throw() {
311  return phaseType[TRANSIENT_NOTDECISIONAL_BIT];
312  }
313 
314  /*
315  * @return true if this is a decisional phase
316  */
317  bool isDecisional() const throw() {
318  return !phaseType[TRANSIENT_NOTDECISIONAL_BIT];
319  }
320 
321  /*
322  * @return true if this is a commit phase
323  */
324  bool isCommit() const throw() {
325  return phaseType[COMMIT_BIT];
326  }
327 
328 };
329 
330 #endif
331 
332 /****************************************************************************/
333 
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:75
#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:64
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:46
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:84
#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:51
const LaneIdVector & getTargetLaneSet() const
The definition of a single phase of a tls logic.
bool isDecisional() const