Eclipse 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-2019 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  std::vector<int> nextPhases;
87 
89  std::string name;
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, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg,
111  const std::vector<int> nextPhasesArg, const std::string& nameArg) {
112  this->duration = durationArg;
113  this->state = stateArg;
114  this->minDuration = minDurationArg < 0 ? durationArg : minDurationArg;
115  this->maxDuration = (maxDurationArg < 0 || maxDurationArg < minDurationArg) ? durationArg : maxDurationArg;
116  // assert(this->minDuration <= this->maxDuration); // not ensured by the previous lines
117  this->myLastSwitch = string2time(OptionsCont::getOptions().getString("begin")); // SUMOTime-option
118  //For SOTL phases
119  //this->phaseType = phaseTypeArg;
120  this->nextPhases = nextPhasesArg;
121  this->name = nameArg;
122  }
123 
124  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg,
125  const std::vector<int>& nextPhasesArg, const std::string& nameArg, LaneIdVector* targetLaneSetArg) {
126  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhasesArg, nameArg);
127  //For SOTL target phases
128  if (targetLaneSetArg != nullptr) {
129  this->targetLaneSet = *targetLaneSetArg;
130  }
131  }
132 
133 
134 public:
142  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, const std::vector<int>& nextPhases, const std::string& name = "") {
143  //PhaseType phaseType;
144  phaseType = PhaseType();
145  phaseType[UNDEFINED_BIT] = 1;
146  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
147  phaseType[TARGET_BIT] = 0;
148  phaseType[COMMIT_BIT] = 0;
149  init(durationArg, stateArg, durationArg, durationArg, nextPhases, name);
150  }
151 
152 
160  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg = -1, SUMOTime maxDurationArg = -1,
161  const std::vector<int>& nextPhases = std::vector<int>(), const std::string& name = "") {
162  //PhaseType phaseType;
163  phaseType = PhaseType();
164  phaseType[UNDEFINED_BIT] = 1;
165  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = 0;
166  phaseType[TARGET_BIT] = 0;
167  phaseType[COMMIT_BIT] = 0;
168  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhases, name);
169  }
170 
171  /*
172  * @brief Constructor for definitions for SOTL target step
173  * In this phase the duration is constrained between min and max duration
174  * @param[in] phaseType Indicates the type of the step
175  * @param[in] targetLaneSet If not null, specifies this MSPhaseDefinition is a target step
176  * @see MSPhaseDefinition::PhaseType
177  */
178  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg,
179  const std::vector<int>& nextPhases, const std::string& name, bool transient_notdecisional, bool commit, LaneIdVector* targetLaneSetArg = nullptr) {
180  if (targetLaneSetArg != nullptr && targetLaneSetArg->size() == 0) {
181  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
182  }
183  //PhaseType phaseType;
184  //phaseType = PhaseType::bitset();
185  phaseType = PhaseType();
186  phaseType[UNDEFINED_BIT] = 0;
187  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
188  phaseType[TARGET_BIT] = targetLaneSetArg == nullptr ? 0 : 1;
189  phaseType[COMMIT_BIT] = commit;
190  init(durationArg, minDurationArg, maxDurationArg, stateArg, nextPhases, name, targetLaneSetArg);
191  }
192 
194  virtual ~MSPhaseDefinition() { }
195 
196 
200  const std::string& getState() const {
201  return state;
202  }
203 
204  void setState(const std::string& _state) {
205  state = _state;
206  }
207 
208  const LaneIdVector& getTargetLaneSet() const {
209  return targetLaneSet;
210  }
211 
212  const std::vector<int>& getNextPhases() const {
213  return nextPhases;
214  }
215 
216  const std::string& getName() const {
217  return name;
218  }
219 
220  void setName(const std::string& _name) {
221  name = _name;
222  }
223 
231  bool isGreenPhase() const {
232  if (state.find_first_of("gG") == std::string::npos) {
233  return false;
234  }
235  if (state.find_first_of("yY") != std::string::npos) {
236  return false;
237  }
238  return true;
239  }
240 
241 
246  LinkState getSignalState(int pos) const {
247  return (LinkState) state[pos];
248  }
249 
250 
257  bool operator!=(const MSPhaseDefinition& pd) {
258  return state != pd.state;
259  }
260 
261 
262  /*
263  * @return true if the phase type is undefined
264  */
265  bool isUndefined() const {
266  return phaseType[UNDEFINED_BIT];
267  }
268 
269  /*
270  * @return true if this is a target phase
271  */
272  bool isTarget() const {
273  return phaseType[TARGET_BIT];
274  }
275 
276  /*
277  * @return true if this is a transient phase
278  */
279  bool isTransient() const {
280  return phaseType[TRANSIENT_NOTDECISIONAL_BIT];
281  }
282 
283  /*
284  * @return true if this is a decisional phase
285  */
286  bool isDecisional() const {
287  return !phaseType[TRANSIENT_NOTDECISIONAL_BIT];
288  }
289 
290  /*
291  * @return true if this is a commit phase
292  */
293  bool isCommit() const {
294  return phaseType[COMMIT_BIT];
295  }
296 
297 };
298 
299 #endif
300 
301 /****************************************************************************/
302 
std::vector< int > nextPhases
The index of the phase that suceeds this one (or -1)
const std::string & getState() const
Returns the state within this phase.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
long long int SUMOTime
Definition: SUMOTime.h:35
#define COMMIT_BIT
#define UNDEFINED_BIT
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::vector< int > &nextPhases, const std::string &name, bool transient_notdecisional, bool commit, LaneIdVector *targetLaneSetArg=nullptr)
#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.
void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg, const std::vector< int > &nextPhasesArg, const std::string &nameArg, LaneIdVector *targetLaneSetArg)
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, const std::vector< int > &nextPhases, const std::string &name="")
Constructor.
void init(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::vector< int > nextPhasesArg, const std::string &nameArg)
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.
std::string name
Optional name or description for the current 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
SUMOTime lastDuration
The previous duration of the phase.
bool isUndefined() const
const std::vector< int > & getNextPhases() const
std::string state
The phase definition.
SUMOTime maxDuration
The maximum duration of the phase.
std::vector< std::string > LaneIdVector
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg=-1, SUMOTime maxDurationArg=-1, const std::vector< int > &nextPhases=std::vector< int >(), const std::string &name="")
Constructor In this phase the duration is constrained between min and max duration.
void setName(const std::string &_name)
#define TARGET_BIT
bool operator!=(const MSPhaseDefinition &pd)
Comparison operator.
SUMOTime minDuration
The minimum duration of the phase.
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
virtual ~MSPhaseDefinition()
Destructor.
const LaneIdVector & getTargetLaneSet() const
const std::string & getName() const
The definition of a single phase of a tls logic.
bool isDecisional() const