15.04.2014 - Version 0.3.8_beta, last version of 0.3.8 branch. Too much added and fixed...
This commit is contained in:
@@ -78,7 +78,7 @@ class PIP_EXPORT PIStateMachine: public PIObject
|
||||
PIOBJECT(PIStateMachine)
|
||||
public:
|
||||
//! Constructs an empty state machine
|
||||
PIStateMachine() {resetConditions();}
|
||||
PIStateMachine(void * _parent = 0) {if (_parent == 0) parent_ = this; else parent_ = _parent; resetConditions();}
|
||||
~PIStateMachine() {;}
|
||||
|
||||
//! %Condition is a pair (string, number)
|
||||
@@ -87,13 +87,14 @@ public:
|
||||
//! %Rule of transition between states of machine
|
||||
struct Rule {
|
||||
//! Constuctor
|
||||
Rule() {;}
|
||||
Rule() {handler = 0;}
|
||||
//! Constuctor
|
||||
Rule(Type f, Type t, const PIStringList & c = PIStringList(), Handler h = 0, bool rac = false) {
|
||||
Rule(Type f, Type t, const PIStringList & c = PIStringList(), Handler h = 0, bool at = false, bool rac = false) {
|
||||
from = f;
|
||||
to = t;
|
||||
for (int i = 0; i < c.size_s(); ++i)
|
||||
conditions << Condition(c[i], 1);
|
||||
autoTransition = at;
|
||||
resetAllConditions = rac;
|
||||
handler = h;
|
||||
}
|
||||
@@ -103,6 +104,8 @@ public:
|
||||
Type to;
|
||||
//! %Conditions of transition
|
||||
PIVector<Condition> conditions;
|
||||
//! Automatic transition
|
||||
bool autoTransition;
|
||||
//! Reset or not all performed conditions of machine on transition
|
||||
bool resetAllConditions;
|
||||
//! Pointer to function executed on transition
|
||||
@@ -116,7 +119,7 @@ public:
|
||||
//! %State of machine
|
||||
struct State {
|
||||
//! Constuctor
|
||||
State() {;}
|
||||
State() {handler = 0;}
|
||||
//! Constuctor
|
||||
State(Type v, const PIString & n = "", Handler h = 0) {value = v; name = n; handler = h;}
|
||||
//! %State value
|
||||
@@ -128,10 +131,12 @@ public:
|
||||
bool operator ==(const State & other) const {return value == other.value;}
|
||||
bool operator !=(const State & other) const {return value != other.value;}
|
||||
};
|
||||
|
||||
|
||||
void * parent() const {return parent_;}
|
||||
void setParent(void * parent) {parent_ = parent;}
|
||||
|
||||
//! Add state of machine
|
||||
void addState(Type value, const PIString & name = "", Handler handler = 0) {if (states_.contain(State(value, name))) return; states_ << State(value, name, handler);}
|
||||
void addState(Type value, const PIString & name = "", Handler handler = 0) {if (states_.contains(State(value, name))) return; states_ << State(value, name, handler);}
|
||||
|
||||
//! States count
|
||||
int statesCount() const {return states_.size_s();}
|
||||
@@ -141,16 +146,16 @@ public:
|
||||
|
||||
|
||||
//! Add rule of transition
|
||||
void addRule(Type from, Type to, const PIString & condition, Handler handler = 0, bool resetAllConditions = false) {if (rules_.contain(Rule(from, to))) return; rules_ << Rule(from, to, PIStringList(condition), handler, resetAllConditions);}
|
||||
void addRule(Type from, Type to, const PIString & condition, Handler handler = 0, bool autoTransition = false, bool resetAllConditions = false) {if (rules_.contains(Rule(from, to))) return; rules_ << Rule(from, to, PIStringList(condition), handler, autoTransition, resetAllConditions);}
|
||||
|
||||
//! Add rule of transition
|
||||
void addRule(Type from, Type to, Handler handler, bool resetAllConditions = false) {if (rules_.contain(Rule(from, to))) return; rules_ << Rule(from, to, PIStringList(), handler, resetAllConditions);}
|
||||
void addRule(Type from, Type to, Handler handler, bool autoTransition = false, bool resetAllConditions = false) {if (rules_.contains(Rule(from, to))) return; rules_ << Rule(from, to, PIStringList(), handler, autoTransition, resetAllConditions);}
|
||||
|
||||
//! Add rule of transition
|
||||
void addRule(Type from, Type to, const PIStringList & conditions = PIStringList(), Handler handler = 0, bool resetAllConditions = false) {if (rules_.contain(Rule(from, to))) return; rules_ << Rule(from, to, conditions, handler, resetAllConditions);}
|
||||
void addRule(Type from, Type to, const PIStringList & conditions = PIStringList(), Handler handler = 0, bool autoTransition = false, bool resetAllConditions = false) {if (rules_.contains(Rule(from, to))) return; rules_ << Rule(from, to, conditions, handler, autoTransition, resetAllConditions);}
|
||||
|
||||
//! Add rule of transition
|
||||
void addRule(const Rule & rule) {if (rules_.contain(rule)) return; rules_ << rule;}
|
||||
void addRule(const Rule & rule) {if (rules_.contains(rule)) return; rules_ << rule;}
|
||||
|
||||
//! Rules count
|
||||
int rulesCount() const {return rules_.size_s();}
|
||||
@@ -180,7 +185,7 @@ public:
|
||||
if ((r.from != state_.value) || (r.to != to)) continue;
|
||||
if (!checkConditions(r)) continue;
|
||||
State ts = findState(to);
|
||||
if (r.handler != 0) r.handler(this);
|
||||
if (r.handler != 0 && parent_ != 0) r.handler(parent_);
|
||||
transition(state_, ts);
|
||||
state_ = ts;
|
||||
resetConditions(r);
|
||||
@@ -255,9 +260,18 @@ public:
|
||||
|
||||
EVENT_HANDLER(void, tick) {tick(0, 0);}
|
||||
EVENT_HANDLER2(void, tick, void * , data, int, delim) {
|
||||
if (switch_to != state_.value) switchToState(switch_to);
|
||||
execution(state_);
|
||||
if (state_.handler != 0) state_.handler(this);
|
||||
if (state_.handler != 0 && parent_ != 0) state_.handler(parent_);
|
||||
if (switch_to != state_.value) switchToState(switch_to);
|
||||
else {
|
||||
piForeachC (Rule & r, rules_) {
|
||||
if (!r.autoTransition || r.from != state_.value) continue;
|
||||
if (checkConditions(r)) {
|
||||
switchToState(r.to);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -310,6 +324,7 @@ private:
|
||||
PIVector<Rule> rules_;
|
||||
State init_, state_;
|
||||
Type switch_to;
|
||||
void * parent_;
|
||||
PIVector<Condition> cond;
|
||||
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user