first state machine code, exclusive already works
need to make final, parallel, timeouts
This commit is contained in:
154
libs/main/state_machine/pistatemachine_state.cpp
Normal file
154
libs/main/state_machine/pistatemachine_state.cpp
Normal file
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
prefix += " ";
|
||||
for (auto * c: children)
|
||||
c->print(prefix);
|
||||
}
|
||||
|
||||
|
||||
bool PIStateBase::start() {
|
||||
if (isAtomic()) {
|
||||
setActive(true);
|
||||
return true;
|
||||
} else {
|
||||
if (initial_state) {
|
||||
setActive(true);
|
||||
return initial_state->start();
|
||||
} else
|
||||
piCout << "error:" << getName() << "no initial state!";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::setActiveRecursive(bool yes) {
|
||||
if (yes) setActive(true);
|
||||
for (auto * c: children)
|
||||
c->setActiveRecursive(yes);
|
||||
if (!yes) setActive(false);
|
||||
}
|
||||
|
||||
|
||||
void PIStateBase::setChildActiveRecursive(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;
|
||||
}
|
||||
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::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 (is_active) output << 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;
|
||||
}
|
||||
Reference in New Issue
Block a user