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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
22 // The definition of a single phase of a tls logic
23 /****************************************************************************/
24 #pragma once
25 
26 #define TARGET_BIT 0
27 #define TRANSIENT_NOTDECISIONAL_BIT 1
28 #define COMMIT_BIT 2
29 #define UNDEFINED_BIT 3
30 #include <config.h>
31 
32 #include <bitset>
33 #include <string>
34 #include <vector>
36 #include <utils/common/SUMOTime.h>
39 
40 
41 // ===========================================================================
42 // class definitions
43 // ===========================================================================
49 public:
50  /*
51  * @brief The definition of phase types
52  * Phase types are compulsory directives for SOTL policies.
53  * Knowing the phase type a policy can drive complex junction that need higly customized phases.
54  * Leaving the phase type as "undefined" makes SOTL policies to malfunction.
55  * Four bits:
56  * TARGET_BIT 0 -> the phase is a target one
57  * TRANSIENT_NOTDECISIONAL_BIT 1 -> the phase is a transient one or a decisional one
58  * COMMIT_BIT 2 -> the phase is a commit one
59  * UNDEFINED_BIT 3 -> the phase type is undefined
60  */
61  typedef std::bitset<4> PhaseType;
62 
63  typedef std::vector<std::string> LaneIdVector;
64 
65 public:
68 
71 
74 
77 
80 
82  std::vector<int> nextPhases;
83 
85  std::string name;
86 
87 private:
89  std::string state;
90 
91  /*
92  * The type of this phase
93  */
95 
96  /*
97  * @brief The lanes-set
98  * This array can be null if this phase is not a target step,
99  * otherwise, a bit is true if the corresponding lane belongs to a
100  * set of input lanes.
101  * SOTL traffic light logics choose the target step according to sensors
102  * belonging to the lane-set.
103  */
105 
106  void init(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg,
107  const std::vector<int> nextPhasesArg, const std::string& nameArg) {
108  this->duration = durationArg;
109  this->state = stateArg;
110  this->minDuration = minDurationArg < 0 ? durationArg : minDurationArg;
111  this->maxDuration = (maxDurationArg < 0 || maxDurationArg < minDurationArg) ? durationArg : maxDurationArg;
112  // assert(this->minDuration <= this->maxDuration); // not ensured by the previous lines
113  this->myLastSwitch = string2time(OptionsCont::getOptions().getString("begin")); // SUMOTime-option
114  //For SOTL phases
115  //this->phaseType = phaseTypeArg;
116  this->nextPhases = nextPhasesArg;
117  this->name = nameArg;
118  }
119 
120  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg,
121  const std::vector<int>& nextPhasesArg, const std::string& nameArg, LaneIdVector* targetLaneSetArg) {
122  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhasesArg, nameArg);
123  //For SOTL target phases
124  if (targetLaneSetArg != nullptr) {
125  this->targetLaneSet = *targetLaneSetArg;
126  }
127  }
128 
129 
130 public:
138  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, const std::vector<int>& nextPhases, const std::string& name = "") {
139  //PhaseType phaseType;
140  phaseType = PhaseType();
143  phaseType[TARGET_BIT] = 0;
144  phaseType[COMMIT_BIT] = 0;
145  init(durationArg, stateArg, durationArg, durationArg, nextPhases, name);
146  }
147 
148 
156  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg = -1, SUMOTime maxDurationArg = -1,
157  const std::vector<int>& nextPhases = std::vector<int>(), const std::string& name = "") {
158  //PhaseType phaseType;
159  phaseType = PhaseType();
162  phaseType[TARGET_BIT] = 0;
163  phaseType[COMMIT_BIT] = 0;
164  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhases, name);
165  }
166 
167  /*
168  * @brief Constructor for definitions for SOTL target step
169  * In this phase the duration is constrained between min and max duration
170  * @param[in] phaseType Indicates the type of the step
171  * @param[in] targetLaneSet If not null, specifies this MSPhaseDefinition is a target step
172  * @see MSPhaseDefinition::PhaseType
173  */
174  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg,
175  const std::vector<int>& nextPhases, const std::string& name, bool transient_notdecisional, bool commit, LaneIdVector* targetLaneSetArg = nullptr) {
176  if (targetLaneSetArg != nullptr && targetLaneSetArg->size() == 0) {
177  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
178  }
179  //PhaseType phaseType;
180  //phaseType = PhaseType::bitset();
181  phaseType = PhaseType();
183  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
184  phaseType[TARGET_BIT] = targetLaneSetArg == nullptr ? 0 : 1;
185  phaseType[COMMIT_BIT] = commit;
186  init(durationArg, minDurationArg, maxDurationArg, stateArg, nextPhases, name, targetLaneSetArg);
187  }
188 
190  virtual ~MSPhaseDefinition() { }
191 
192 
196  const std::string& getState() const {
197  return state;
198  }
199 
200  void setState(const std::string& _state) {
201  state = _state;
202  }
203 
205  return targetLaneSet;
206  }
207 
208  const std::vector<int>& getNextPhases() const {
209  return nextPhases;
210  }
211 
212  const std::string& getName() const {
213  return name;
214  }
215 
216  void setName(const std::string& _name) {
217  name = _name;
218  }
219 
227  bool isGreenPhase() const {
228  if (state.find_first_of("gG") == std::string::npos) {
229  return false;
230  }
231  if (state.find_first_of("yY") != std::string::npos) {
232  return false;
233  }
234  return true;
235  }
236 
237 
242  LinkState getSignalState(int pos) const {
243  return (LinkState) state[pos];
244  }
245 
246 
253  bool operator!=(const MSPhaseDefinition& pd) {
254  return state != pd.state;
255  }
256 
257 
258  /*
259  * @return true if the phase type is undefined
260  */
261  bool isUndefined() const {
262  return phaseType[UNDEFINED_BIT];
263  }
264 
265  /*
266  * @return true if this is a target phase
267  */
268  bool isTarget() const {
269  return phaseType[TARGET_BIT];
270  }
271 
272  /*
273  * @return true if this is a transient phase
274  */
275  bool isTransient() const {
277  }
278 
279  /*
280  * @return true if this is a decisional phase
281  */
282  bool isDecisional() const {
284  }
285 
286  /*
287  * @return true if this is a commit phase
288  */
289  bool isCommit() const {
290  return phaseType[COMMIT_BIT];
291  }
292 
293 };
#define UNDEFINED_BIT
#define TARGET_BIT
#define TRANSIENT_NOTDECISIONAL_BIT
#define COMMIT_BIT
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
long long int SUMOTime
Definition: SUMOTime.h:31
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
The definition of a single phase of a tls logic.
const std::string & getState() const
Returns the state within this phase.
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)
std::string name
Optional name or description for the current phase.
SUMOTime lastDuration
The previous duration of the phase.
bool isTransient() const
bool isUndefined() const
SUMOTime maxDuration
The maximum duration of the phase.
LinkState getSignalState(int pos) const
Returns the state of the tls signal at the given position.
std::bitset< 4 > PhaseType
std::vector< std::string > LaneIdVector
void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg, const std::vector< int > &nextPhasesArg, const std::string &nameArg, LaneIdVector *targetLaneSetArg)
virtual ~MSPhaseDefinition()
Destructor.
const std::vector< int > & getNextPhases() const
SUMOTime minDuration
The minimum duration of the phase.
LaneIdVector targetLaneSet
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
std::string state
The phase definition.
SUMOTime duration
The duration of the phase.
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
const LaneIdVector & getTargetLaneSet() const
const std::string & getName() const
bool isDecisional() const
std::vector< int > nextPhases
The index of the phase that suceeds this one (or -1)
void init(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::vector< int > nextPhasesArg, const std::string &nameArg)
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 setState(const std::string &_state)
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, const std::vector< int > &nextPhases, const std::string &name="")
Constructor.
void setName(const std::string &_name)
bool operator!=(const MSPhaseDefinition &pd)
Comparison operator.
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:117
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58