提升::MSM 检查保护条件下的状态转换
BOOST::MSM Checking for state transitions in a guard condition
我有一个通用的保护条件,我想在某些情况下有条件地防止转换。下面是我想做的有点C++/伪代码。
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 引起的转换具有相同的源状态和目标状态。两者都是状态 1。事件 2 引起的转换具有不同的源状态和目标状态。源状态为状态 1,目标状态为状态 2。
前一种情况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
https://wandbox.org/permlink/HxaGpAr90YLEc5l8
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 启动状态机无法在启动时处理内部转换
- 提升::MSM 检查保护条件下的状态转换
- Windows编程:为什么我们要将lParam转换为CREATESTRUCT来获取应用程序状态
- 将Java状态机示例转换为C++,陷入最后一道障碍
- 将无状态C++函数由内而外转换为有状态函数
- 状态机:使用转换矩阵而不是嵌套的开关案例C++
- 将状态的输入转换为缩写
- Boost.MPL 转换与状态
- 布局中的 Qt 状态机转换
- 转换为调用私有构造函数的std::线程状态
- qstatemmachine获取导致状态转换的事件
- Boost状态图通过转换传递参数