BOOST :: MSM检查警戒状态下的状态转换
问题描述:
我有一个通用警戒条件,我想在某些情况下有条件地禁止转换。下面是我想要做的一些C++/Pseudocode。BOOST :: MSM检查警戒状态下的状态转换
bool operator()(Event const& evt, FSM & fsm, SourceState& src, TargetState& tgt)
{
bool transition = (current_state != next_state);
bool transitionAllowed = (x | y | z); //some custom condition
return (transition && transitionAllowed);
}
我想知道,如果目标状态是从源状态不同,并基于该决定我们是否能够允许基于其他参数的转变。到目前为止,我还没有找到很多成功的文档。
答
看来你的问题包含两部分。 一个是如何检查当前状态和下一个状态。另一个是如何检查自定义条件。
为了检查当前状态和下一个状态,可以使用std::is_same
元函数。
这里是一个防护件的一个示例:
struct Guard1 {
template <class Event, class Fsm, class Source, class Target>
bool operator()(Event const&, Fsm& fsm, Source& src, Target&) const {
bool transition = !std::is_same<Source, Target>::value;
bool transitionAllowed = x() || fsm.y || src.z;
return transition && transitionAllowed;
}
};
这里是转换表:
// Transition table
struct transition_table:mpl::vector<
// Start Event Next Action Guard
// source and target is the same
msmf::Row < State1, Event1, State1, msmf::none, Guard1 >,
// source and target is different
msmf::Row < State1, Event2, State2, msmf::none, Guard1 >
> {};
引起事件1的过渡具有相同的源和目标的状态。两者都是State1。 Event2引起的转换具有不同的来源和目标状态。源状态是State1,目标状态是State2。
前一种情况std::is_same<Source, Target>::value
返回true,后一种情况返回false。 变量transition
是结果的唠叨。
您可以将此用作返回值的一部分。
为了评估自定义条件,您需要从某些来源获取评估值。 我写了三个来源的例子。
-
x()
是全局函数。当然,你可以像这样使用全局变量。 -
y
是状态机的成员变量。 -
z
是源状态State1的成员变量。
下面是完整的代码:
#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/static_assert.hpp>
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
// You can test changing the value
bool const example_value = true;
struct Event1 {};
struct Event2 {};
// example of a condition
bool x() { return example_value; }
struct Sm_:msmf::state_machine_def<Sm_>
{
// States
struct State1:msmf::state<> {
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "State1::on_entry()" << std::endl;
}
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "State1::on_exit()" << std::endl;
}
bool z = example_value; // example of a condition
};
struct State2:msmf::state<> {
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) {
std::cout << "State2::on_entry()" << std::endl;
}
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "State2::on_exit()" << std::endl;
}
int property;
};
// Set initial state
typedef State1 initial_state;
// Guards
struct Guard1 {
template <class Event, class Fsm, class Source, class Target>
bool operator()(Event const&, Fsm& fsm, Source& src, Target&) const {
bool transition = !std::is_same<Source, Target>::value;
bool transitionAllowed = x() || fsm.y || src.z;
return transition && transitionAllowed;
}
};
// Transition table
struct transition_table:mpl::vector<
// Start Event Next Action Guard
// source and target is the same
msmf::Row < State1, Event1, State1, msmf::none, Guard1 >,
// source and target is different
msmf::Row < State1, Event2, State2, msmf::none, Guard1 >
> {};
bool y = example_value; // example of a condition
};
// Pick a back-end
typedef msm::back::state_machine<Sm_> Sm;
int main() {
Sm sm;
sm.start();
std::cout << "> Send Event1()" << std::endl;
sm.process_event(Event1());
std::cout << "> Send Event2()" << std::endl;
sm.process_event(Event2());
}
您可以更改自定义条件的值。
// You can test changing the value
bool const example_value = true;
如果将example_value设置为false,则不满足自定义条件。
如果不满足自定义条件,则Event1
和Event2
都不会进行转换。 如果自定义条件得到满足,Event1
不会进行转换,因为源和目标状态相同。 Event2
进行了转换。
这里是正在运行的演示。
壳体example_value = true
https://wandbox.org/permlink/6qHcW9e6JX4QXAuH
壳体example_value = false
这是纯溶液。我找到了一种方法来做到这一点,通过使用onEntry事件中的'stateNum'来填充源和目标状态。 (即在实际状态的onEntry中,stateNum = fsm.current_state(),其中当前状态分别指向每个状态的唯一状态标识符) 虽然我喜欢你的解决方案!这看起来比我更优雅一些:-) – Edwin