state machine, parallel seems to work, final state and info about active atomic states
This commit is contained in:
@@ -28,6 +28,23 @@ PIStateBase::~PIStateBase() {
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIStateBase *> PIStateBase::activeChildren() const {
|
||||
if (!isParallel()) {
|
||||
if (active_state) return {active_state};
|
||||
return {};
|
||||
}
|
||||
if (isActive()) return children;
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
PIVector<PIStateBase *> PIStateBase::activeAtomics() const {
|
||||
PIVector<PIStateBase *> ret;
|
||||
gatherActiveAtomicStates(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::addState(PIStateBase * s) {
|
||||
children << s;
|
||||
s->parent_state = this;
|
||||
@@ -60,7 +77,8 @@ PITransitionBase * PIStateBase::addTransition(PIStateBase * target, int event_id
|
||||
|
||||
|
||||
void PIStateBase::print(PIString prefix) {
|
||||
PICout(PICoutManipulators::AddNewLine) << prefix << "[" << (isActive() ? "*" : " ") << "] " << getName();
|
||||
PICout(PICoutManipulators::AddNewLine) << prefix << "[" << (isActive() ? "*" : " ") << "] " << getName() << " active "
|
||||
<< activeChildren();
|
||||
prefix += " ";
|
||||
for (auto * c: children)
|
||||
c->print(prefix);
|
||||
@@ -72,11 +90,19 @@ bool PIStateBase::start() {
|
||||
setActive(true);
|
||||
return true;
|
||||
} else {
|
||||
if (initial_state) {
|
||||
if (!isParallel()) {
|
||||
if (initial_state) {
|
||||
setActive(true);
|
||||
return initial_state->start();
|
||||
} else
|
||||
piCout << "error:" << getName() << "no initial state!";
|
||||
} else {
|
||||
setActive(true);
|
||||
return initial_state->start();
|
||||
} else
|
||||
piCout << "error:" << getName() << "no initial state!";
|
||||
for (auto * s: children) {
|
||||
if (!s->start()) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -85,8 +111,16 @@ bool PIStateBase::start() {
|
||||
void PIStateBase::setActive(bool yes) {
|
||||
bool prev_active = is_active;
|
||||
is_active = yes;
|
||||
if (!prev_active && is_active) onEnter();
|
||||
if (prev_active && !is_active) onExit();
|
||||
if (parent_state && yes) parent_state->childActived(this);
|
||||
if (!prev_active && is_active) {
|
||||
onEnter();
|
||||
// if (isParallel()) setChildrenActive(true);
|
||||
}
|
||||
if (prev_active && !is_active) {
|
||||
active_state = nullptr;
|
||||
if (isParallel()) setChildrenActiveRecursive(false);
|
||||
onExit();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -98,26 +132,37 @@ void PIStateBase::setActiveRecursive(bool yes) {
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::setChildActiveRecursive(bool yes) {
|
||||
void PIStateBase::setChildrenActive(bool yes) {
|
||||
for (auto * c: children)
|
||||
c->setActive(yes);
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::setChildrenActiveRecursive(bool yes) {
|
||||
for (auto * c: children)
|
||||
c->setActiveRecursive(yes);
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::setChildActive(PIStateBase * s) {
|
||||
if (isAtomic()) return;
|
||||
if (!s) s = initial_state;
|
||||
if (!s) {
|
||||
piCout << "error:" << getName() << "no initial state!";
|
||||
return;
|
||||
void PIStateBase::activeChild(PIStateBase * c) {
|
||||
setActive(true);
|
||||
if (isParallel())
|
||||
for (auto * s: children) {
|
||||
if (c != s) s->start();
|
||||
}
|
||||
else {
|
||||
if (!c) c = initial_state;
|
||||
if (c) c->setActive(true);
|
||||
}
|
||||
for (auto * c: children)
|
||||
c->setActive(false);
|
||||
if (active_state) {
|
||||
if (active_state != s) active_state->setActive(false);
|
||||
}
|
||||
active_state = s;
|
||||
if (active_state) active_state->setActive(true);
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::childActived(PIStateBase * s) {
|
||||
if (!isParallel()) {
|
||||
active_state = s;
|
||||
if (active_state->isFinal()) onFinish();
|
||||
} else
|
||||
active_state = nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -136,7 +181,14 @@ void PIStateBase::propagateRoot(PIStateMachine * r) {
|
||||
void PIStateBase::gatherActiveStates(PIVector<PIStateBase *> & output) {
|
||||
for (auto * c: children)
|
||||
c->gatherActiveStates(output);
|
||||
if (is_active) output << this;
|
||||
if (isActive()) output << this;
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::gatherActiveAtomicStates(PIVector<PIStateBase *> & output) const {
|
||||
for (auto * c: children)
|
||||
c->gatherActiveAtomicStates(output);
|
||||
if (isActive() && isAtomic()) output << const_cast<PIStateBase *>(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user