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-2016 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  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)
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
long long int SUMOTime
Definition: SUMOTime.h:43
const std::string & getState() const
Returns the state within this phase.
bool isTarget() const
#define COMMIT_BIT
#define UNDEFINED_BIT
#define TRANSIENT_NOTDECISIONAL_BIT
bool isCommit() const
LaneIdVector targetLaneSet
std::bitset< 4 > PhaseType
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg)
Constructor.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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.
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)
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
LinkState getSignalState(int pos) const
Returns the state of the tls signal at the given position.
SUMOTime lastDuration
The previous duration of the phase.
std::string state
The phase definition.
void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg)
const LaneIdVector & getTargetLaneSet() const
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:89
#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)
virtual ~MSPhaseDefinition()
Destructor.
bool isTransient() const
The definition of a single phase of a tls logic.
bool isDecisional() const
bool isUndefined() const