first state machine code, exclusive already works
need to make final, parallel, timeouts
This commit is contained in:
81
libs/main/state_machine/pistatemachine_transition.cpp
Normal file
81
libs/main/state_machine/pistatemachine_transition.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
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_transition.h"
|
||||
|
||||
#include "pistatemachine_state.h"
|
||||
|
||||
|
||||
PITransitionBase::PITransitionBase(PIStateBase * source, PIStateBase * target, int event_id) {
|
||||
target_state = target;
|
||||
source_state = source;
|
||||
eventID = event_id;
|
||||
}
|
||||
|
||||
|
||||
PITransitionBase::~PITransitionBase() {
|
||||
piDeleteSafety(guard);
|
||||
}
|
||||
|
||||
|
||||
PITransitionBase * PITransitionBase::addAction(std::function<void()> a) {
|
||||
action = a;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
void PITransitionBase::makeAction() {
|
||||
if (action) action();
|
||||
}
|
||||
|
||||
|
||||
void PITransitionBase::trigger() {
|
||||
if (!target_state || (source_state == target_state)) {
|
||||
makeAction();
|
||||
return;
|
||||
}
|
||||
// source_state->setActiveRecursive(false);
|
||||
auto source_path = source_state->pathToMachine();
|
||||
auto target_path = target_state->pathToMachine();
|
||||
auto source_target_path = target_path;
|
||||
source_target_path.reverse();
|
||||
// piCout << "source_path" << source_path;
|
||||
// piCout << "target_path" << target_path;
|
||||
ssize_t common_count = 0;
|
||||
for (common_count = 0; common_count < piMini(source_path.size_s(), target_path.size_s()); ++common_count) {
|
||||
// piCout << "check" << source_path[source_path.size_s() - common_count - 1] << target_path[target_path.size_s() - common_count -
|
||||
// 1];
|
||||
if (source_path[source_path.size_s() - common_count - 1] != target_path[target_path.size_s() - common_count - 1]) {
|
||||
// piCout << "diff" << common_count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// piCout << "common" << common_count;
|
||||
source_path.remove(source_path.size_s() - common_count, common_count);
|
||||
target_path.remove(target_path.size_s() - common_count, common_count);
|
||||
// piCout << "source_path" << source_path;
|
||||
// piCout << "target_path" << target_path;
|
||||
source_state->setChildActiveRecursive(false);
|
||||
for (auto * s: source_path)
|
||||
s->setActive(false);
|
||||
makeAction();
|
||||
for (auto * s: source_target_path)
|
||||
s->setActive(true);
|
||||
if (target_state->isCompound()) target_state->start();
|
||||
}
|
||||
Reference in New Issue
Block a user