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 /****************************************************************************/
18 // The definition of a single phase of a tls logic
19 /****************************************************************************/
20 #ifndef MSPhaseDefinition_h
21 #define MSPhaseDefinition_h
22 
23 #define TARGET_BIT 0
24 #define TRANSIENT_NOTDECISIONAL_BIT 1
25 #define COMMIT_BIT 2
26 #define UNDEFINED_BIT 3
27 
28 
29 
30 // ===========================================================================
31 // included modules
32 // ===========================================================================
33 #include <config.h>
34 
35 #include <bitset>
36 #include <string>
37 #include <vector>
39 #include <utils/common/SUMOTime.h>
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
52 public:
53  /*
54  * @brief The definition of phase types
55  * Phase types are compulsory directives for SOTL policies.
56  * Knowing the phase type a policy can drive complex junction that need higly customized phases.
57  * Leaving the phase type as "undefined" makes SOTL policies to malfunction.
58  * Four bits:
59  * TARGET_BIT 0 -> the phase is a target one
60  * TRANSIENT_NOTDECISIONAL_BIT 1 -> the phase is a transient one or a decisional one
61  * COMMIT_BIT 2 -> the phase is a commit one
62  * UNDEFINED_BIT 3 -> the phase type is undefined
63  */
64  typedef std::bitset<4> PhaseType;
65 
66  typedef std::vector<std::string> LaneIdVector;
67 
68 public:
71 
74 
77 
80 
83 
85  std::vector<int> nextPhases;
86 
88  std::string name;
89 
90 private:
92  std::string state;
93 
94  /*
95  * The type of this phase
96  */
98 
99  /*
100  * @brief The lanes-set
101  * This array can be null if this phase is not a target step,
102  * otherwise, a bit is true if the corresponding lane belongs to a
103  * set of input lanes.
104  * SOTL traffic light logics choose the target step according to sensors
105  * belonging to the lane-set.
106  */
108 
109  void init(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg,
110  const std::vector<int> nextPhasesArg, const std::string& nameArg) {
111  this->duration = durationArg;
112  this->state = stateArg;
113  this->minDuration = minDurationArg < 0 ? durationArg : minDurationArg;
114  this->maxDuration = (maxDurationArg < 0 || maxDurationArg < minDurationArg) ? durationArg : maxDurationArg;
115  // assert(this->minDuration <= this->maxDuration); // not ensured by the previous lines
116  this->myLastSwitch = string2time(OptionsCont::getOptions().getString("begin")); // SUMOTime-option
117  //For SOTL phases
118  //this->phaseType = phaseTypeArg;
119  this->nextPhases = nextPhasesArg;
120  this->name = nameArg;
121  }
122 
123  void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string& stateArg,
124  const std::vector<int>& nextPhasesArg, const std::string& nameArg, LaneIdVector* targetLaneSetArg) {
125  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhasesArg, nameArg);
126  //For SOTL target phases
127  if (targetLaneSetArg != nullptr) {
128  this->targetLaneSet = *targetLaneSetArg;
129  }
130  }
131 
132 
133 public:
141  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, const std::vector<int>& nextPhases, const std::string& name = "") {
142  //PhaseType phaseType;
143  phaseType = PhaseType();
146  phaseType[TARGET_BIT] = 0;
147  phaseType[COMMIT_BIT] = 0;
148  init(durationArg, stateArg, durationArg, durationArg, nextPhases, name);
149  }
150 
151 
159  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg = -1, SUMOTime maxDurationArg = -1,
160  const std::vector<int>& nextPhases = std::vector<int>(), const std::string& name = "") {
161  //PhaseType phaseType;
162  phaseType = PhaseType();
165  phaseType[TARGET_BIT] = 0;
166  phaseType[COMMIT_BIT] = 0;
167  init(durationArg, stateArg, minDurationArg, maxDurationArg, nextPhases, name);
168  }
169 
170  /*
171  * @brief Constructor for definitions for SOTL target step
172  * In this phase the duration is constrained between min and max duration
173  * @param[in] phaseType Indicates the type of the step
174  * @param[in] targetLaneSet If not null, specifies this MSPhaseDefinition is a target step
175  * @see MSPhaseDefinition::PhaseType
176  */
177  MSPhaseDefinition(SUMOTime durationArg, const std::string& stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg,
178  const std::vector<int>& nextPhases, const std::string& name, bool transient_notdecisional, bool commit, LaneIdVector* targetLaneSetArg = nullptr) {
179  if (targetLaneSetArg != nullptr && targetLaneSetArg->size() == 0) {
180  MsgHandler::getErrorInstance()->inform("MSPhaseDefinition::MSPhaseDefinition -> targetLaneSetArg cannot be empty for a target phase");
181  }
182  //PhaseType phaseType;
183  //phaseType = PhaseType::bitset();
184  phaseType = PhaseType();
186  phaseType[TRANSIENT_NOTDECISIONAL_BIT] = transient_notdecisional;
187  phaseType[TARGET_BIT] = targetLaneSetArg == nullptr ? 0 : 1;
188  phaseType[COMMIT_BIT] = commit;
189  init(durationArg, minDurationArg, maxDurationArg, stateArg, nextPhases, name, targetLaneSetArg);
190  }
191 
193  virtual ~MSPhaseDefinition() { }
194 
195 
199  const std::string& getState() const {
200  return state;
201  }
202 
203  void setState(const std::string& _state) {
204  state = _state;
205  }
206 
208  return targetLaneSet;
209  }
210 
211  const std::vector<int>& getNextPhases() const {
212  return nextPhases;
213  }
214 
215  const std::string& getName() const {
216  return name;
217  }
218 
219  void setName(const std::string& _name) {
220  name = _name;
221  }
222 
230  bool isGreenPhase() const {
231  if (state.find_first_of("gG") == std::string::npos) {
232  return false;
233  }
234  if (state.find_first_of("yY") != std::string::npos) {
235  return false;
236  }
237  return true;
238  }
239 
240 
245  LinkState getSignalState(int pos) const {
246  return (LinkState) state[pos];
247  }
248 
249 
256  bool operator!=(const MSPhaseDefinition& pd) {
257  return state != pd.state;
258  }
259 
260 
261  /*
262  * @return true if the phase type is undefined
263  */
264  bool isUndefined() const {
265  return phaseType[UNDEFINED_BIT];
266  }
267 
268  /*
269  * @return true if this is a target phase
270  */
271  bool isTarget() const {
272  return phaseType[TARGET_BIT];
273  }
274 
275  /*
276  * @return true if this is a transient phase
277  */
278  bool isTransient() const {
280  }
281 
282  /*
283  * @return true if this is a decisional phase
284  */
285  bool isDecisional() const {
287  }
288 
289  /*
290  * @return true if this is a commit phase
291  */
292  bool isCommit() const {
293  return phaseType[COMMIT_BIT];
294  }
295 
296 };
297 
298 #endif
299 
300 /****************************************************************************/
301 
MSPhaseDefinition::init
void init(SUMOTime durationArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::string &stateArg, const std::vector< int > &nextPhasesArg, const std::string &nameArg, LaneIdVector *targetLaneSetArg)
Definition: MSPhaseDefinition.h:123
TRANSIENT_NOTDECISIONAL_BIT
#define TRANSIENT_NOTDECISIONAL_BIT
Definition: MSPhaseDefinition.h:24
MSPhaseDefinition::myLastSwitch
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
Definition: MSPhaseDefinition.h:82
SUMOTime.h
MSPhaseDefinition::state
std::string state
The phase definition.
Definition: MSPhaseDefinition.h:92
MSPhaseDefinition::MSPhaseDefinition
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.
Definition: MSPhaseDefinition.h:159
MSPhaseDefinition::isTransient
bool isTransient() const
Definition: MSPhaseDefinition.h:278
MSPhaseDefinition::~MSPhaseDefinition
virtual ~MSPhaseDefinition()
Destructor.
Definition: MSPhaseDefinition.h:193
MSPhaseDefinition::lastDuration
SUMOTime lastDuration
The previous duration of the phase.
Definition: MSPhaseDefinition.h:73
OptionsCont.h
MSPhaseDefinition::nextPhases
std::vector< int > nextPhases
The index of the phase that suceeds this one (or -1)
Definition: MSPhaseDefinition.h:85
MsgHandler.h
MsgHandler::inform
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:118
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
MSPhaseDefinition::PhaseType
std::bitset< 4 > PhaseType
Definition: MSPhaseDefinition.h:64
MSPhaseDefinition::LaneIdVector
std::vector< std::string > LaneIdVector
Definition: MSPhaseDefinition.h:66
MSPhaseDefinition::getNextPhases
const std::vector< int > & getNextPhases() const
Definition: MSPhaseDefinition.h:211
MSPhaseDefinition::maxDuration
SUMOTime maxDuration
The maximum duration of the phase.
Definition: MSPhaseDefinition.h:79
MSPhaseDefinition::setState
void setState(const std::string &_state)
Definition: MSPhaseDefinition.h:203
LinkState
LinkState
The right-of-way state of a link between two lanes used when constructing a NBTrafficLightLogic,...
Definition: SUMOXMLDefinitions.h:1137
MSPhaseDefinition::isDecisional
bool isDecisional() const
Definition: MSPhaseDefinition.h:285
MSPhaseDefinition::duration
SUMOTime duration
The duration of the phase.
Definition: MSPhaseDefinition.h:70
UNDEFINED_BIT
#define UNDEFINED_BIT
Definition: MSPhaseDefinition.h:26
MSPhaseDefinition::getState
const std::string & getState() const
Returns the state within this phase.
Definition: MSPhaseDefinition.h:199
MSPhaseDefinition::isCommit
bool isCommit() const
Definition: MSPhaseDefinition.h:292
MSPhaseDefinition::isGreenPhase
bool isGreenPhase() const
Returns whether this phase is a pure "green" phase.
Definition: MSPhaseDefinition.h:230
MSPhaseDefinition::MSPhaseDefinition
MSPhaseDefinition(SUMOTime durationArg, const std::string &stateArg, const std::vector< int > &nextPhases, const std::string &name="")
Constructor.
Definition: MSPhaseDefinition.h:141
MSPhaseDefinition::getTargetLaneSet
const LaneIdVector & getTargetLaneSet() const
Definition: MSPhaseDefinition.h:207
MSPhaseDefinition::getSignalState
LinkState getSignalState(int pos) const
Returns the state of the tls signal at the given position.
Definition: MSPhaseDefinition.h:245
string2time
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:44
MSPhaseDefinition::phaseType
PhaseType phaseType
Definition: MSPhaseDefinition.h:97
MSPhaseDefinition::targetLaneSet
LaneIdVector targetLaneSet
Definition: MSPhaseDefinition.h:107
MSPhaseDefinition::isUndefined
bool isUndefined() const
Definition: MSPhaseDefinition.h:264
TARGET_BIT
#define TARGET_BIT
Definition: MSPhaseDefinition.h:23
MSPhaseDefinition::MSPhaseDefinition
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)
Definition: MSPhaseDefinition.h:177
MSPhaseDefinition::name
std::string name
Optional name or description for the current phase.
Definition: MSPhaseDefinition.h:88
COMMIT_BIT
#define COMMIT_BIT
Definition: MSPhaseDefinition.h:25
config.h
MSPhaseDefinition::setName
void setName(const std::string &_name)
Definition: MSPhaseDefinition.h:219
MSPhaseDefinition::init
void init(SUMOTime durationArg, const std::string &stateArg, SUMOTime minDurationArg, SUMOTime maxDurationArg, const std::vector< int > nextPhasesArg, const std::string &nameArg)
Definition: MSPhaseDefinition.h:109
MSPhaseDefinition::operator!=
bool operator!=(const MSPhaseDefinition &pd)
Comparison operator.
Definition: MSPhaseDefinition.h:256
MSPhaseDefinition::isTarget
bool isTarget() const
Definition: MSPhaseDefinition.h:271
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:51
MsgHandler::getErrorInstance
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:81
SUMOXMLDefinitions.h
MSPhaseDefinition::minDuration
SUMOTime minDuration
The minimum duration of the phase.
Definition: MSPhaseDefinition.h:76
MSPhaseDefinition::getName
const std::string & getName() const
Definition: MSPhaseDefinition.h:215