Files
pip/libs/main/state_machine/pistatemachine_state.cpp

207 lines
4.6 KiB
C++

/*
PIP - Platform Independent Primitives
State machine
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "pistatemachine_state.h"
#include "pistatemachine_transition.h"
PIStateBase::~PIStateBase() {
piDeleteAll(children);
piDeleteAll(transitions);
}
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;
propagateRoot(root);
}
void PIStateBase::addStates(PIVector<PIStateBase *> s) {
children << s;
for (auto * i: s)
i->parent_state = this;
propagateRoot(root);
}
void PIStateBase::setInitialState(PIStateBase * s) {
if (children.contains(s))
initial_state = s;
else
initial_state = nullptr;
}
PITransitionBase * PIStateBase::addTransition(PIStateBase * target, int event_id) {
PITransitionBase * ret = new PITransitionBase(this, target, event_id);
ret->root = root;
transitions << ret;
return ret;
}
void PIStateBase::print(PIString prefix) {
PICout(PICoutManipulators::AddNewLine) << prefix << "[" << (isActive() ? "*" : " ") << "] " << getName() << " active "
<< activeChildren();
prefix += " ";
for (auto * c: children)
c->print(prefix);
}
bool PIStateBase::start() {
if (isAtomic()) {
setActive(true);
return true;
} else {
if (!isParallel()) {
if (initial_state) {
setActive(true);
return initial_state->start();
} else
piCout << "error:" << getName() << "no initial state!";
} else {
setActive(true);
for (auto * s: children) {
if (!s->start()) return false;
}
return true;
}
}
return false;
}
void PIStateBase::setActive(bool yes) {
bool prev_active = is_active;
is_active = yes;
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();
}
}
void PIStateBase::setActiveRecursive(bool yes) {
if (yes) setActive(true);
for (auto * c: children)
c->setActiveRecursive(yes);
if (!yes) setActive(false);
}
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::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);
}
}
void PIStateBase::childActived(PIStateBase * s) {
if (!isParallel()) {
active_state = s;
if (active_state->isFinal()) onFinish();
} else
active_state = nullptr;
}
void PIStateBase::propagateRoot(PIStateMachine * r) {
if (is_root)
root = reinterpret_cast<PIStateMachine *>(this);
else
root = r;
for (auto * t: transitions)
t->root = root;
for (auto * c: children)
c->propagateRoot(root);
}
void PIStateBase::gatherActiveStates(PIVector<PIStateBase *> & output) {
for (auto * c: children)
c->gatherActiveStates(output);
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);
}
void PIStateBase::gatherPathToMachine(PIVector<PIStateBase *> & output) {
if (isStateMachine()) return;
output << this;
if (parent()) parent()->gatherPathToMachine(output);
}
PIVector<PIStateBase *> PIStateBase::pathToMachine() {
PIVector<PIStateBase *> ret;
gatherPathToMachine(ret);
return ret;
}