c++ - BOOST::MSM 检查保护条件下的状态转换

标签 c++ boost state-machine boost-msm

我有一个通用的保护条件,我想在某些情况下有条件地阻止转换。下面是我想做的一些 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 >
> {};

由 Event1 引起的转换具有相同的源状态和目标状态。两者都是State1。 Event2 引起的转换具有不同的源状态和目标状态。源状态为State1,目标状态为State2。

前例std::is_same<Source, Target>::value返回真,后一种情况返回假。 变量 transition是结果的 nagation。

您可以将其用作返回值的一部分。

为了评估自定义条件,您需要从某些来源获取评估值。 我写了三个来源的例子。

  • 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,则不满足自定义条件。

如果自定义条件不满足,Event1Event2不要过渡。 如果满足自定义条件,Event1不会进行转换,因为源状态和目标状态相同。 Event2进行转换。

这是正在运行的演示。

案例example_value = true

https://wandbox.org/permlink/6qHcW9e6JX4QXAuH

案例example_value = false

https://wandbox.org/permlink/HxaGpAr90YLEc5l8

关于c++ - BOOST::MSM 检查保护条件下的状态转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43399765/

相关文章:

c++ - 运行时和编译时的数组声明

c++ - Boost BGL BFS 找到从源到目标的所有唯一路径

java - 如何编写 lwuit 单选按钮代码

c - 定义C中用于存储的枚举的数据类型

c++ - 在将字符串转换为 ptime 时使用 %f 标志 boost ptime input_facet 错误

uml - 状态机和状态图是同一个意思吗?

c++ - undefined symbol std::__throw_bad_function_call

c++ - 测试给定 id 的任何进程目前是否正在 Linux 上运行的简单方法是什么?

c++ - 如何将文本文件复制到另一个文件?

c++ - boost::gregorian::date 如何在内部存储日期?