0

我有一个带有 states 的状态机AB并且C. C直接处理事件e,whileABdo not,但我想回退到事件的默认处理程序(Samek 称之为“终极钩子”模式) (当在 states和e中找不到处理程序时将调用该处理程序)。但是,使用 Boost MSM,我无法在状态机级别实现此处理程序,而必须引入包含,和的附加容器状态。ABSABC

有没有办法直接在状态机级别本身实现事件处理程序,而不需要这个容器状态?

4

1 回答 1

1

您可以使用no_transition处理程序来做到这一点。

假设事件 E 定义如下:

struct E {
    int val = 42;
};

以下是事件 E 的 no_transition 处理程序示例:

template <typename Fsm> 
void no_transition(E const& e, Fsm&, int state) {
    std::cout << "no transition at state " << state << " event e.val" << e.val << std::endl;
}

但是,由于元编程,Boost.MSM 需要其他类型的 no_transition 处理程序。

因此,您需要为其他事件定义 no_transition 处理程序,如下所示:

template <typename Fsm, typename Event> 
void no_transition(Event const&, Fsm& ,int) {
    std::cout << "shouldn't be called but need to define to compile" << std::endl;
}

您可能认为您可以执行以下操作:

template <typename Fsm, typename Event> 
void no_transition(Event const&, Fsm& ,int) {
    std::cout << "no transition at state " << state << " event e.val" << e.val << std::endl;
}

它不起作用,因为 no_transition 处理程序不仅实例化 E ,还实例化其他没有成员变量 的事件val

这是基于您的问题的整个工作代码的示例:

#include <iostream>
#include <boost/msm/back/state_machine.hpp>

#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>

namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;

// ----- Events
struct E {
    int val = 42;
};
struct AtoB {};
struct BtoC {};

// ----- State machine
struct Sm1_:msmf::state_machine_def<Sm1_>
{
    // States
    struct A:msmf::state<> {
        template <class Event,class Fsm>
        void on_entry(Event const&, Fsm&) {
            std::cout << "A::on_entry()" << std::endl;
        }
    };
    struct B:msmf::state<> {
        template <class Event,class Fsm>
        void on_entry(Event const&, Fsm&) {
            std::cout << "B::on_entry()" << std::endl;
        }
    };
    struct C:msmf::state<> {
        template <class Event,class Fsm>
        void on_entry(Event const&, Fsm&) {
            std::cout << "C::on_entry()" << std::endl;
        }
    };

    // Set initial state
    using initial_state = A;

    // Transition table
    struct transition_table:mpl::vector<
        //          Start   Event   Next  Action      Guard
        msmf::Row < A,      AtoB,   B,    msmf::none, msmf::none >,
        msmf::Row < B,      BtoC,   C,    msmf::none, msmf::none >,
        msmf::Row < C,      E,      A,    msmf::none, msmf::none >
    > {};

    template <typename Fsm> 
    void no_transition(E const& e, Fsm&, int state) {
        std::cout << "no transition at state " << state << " event e.val" << e.val << std::endl;
    }

    template <typename Fsm, typename Event> 
    void no_transition(Event const&, Fsm& ,int) {
        std::cout << "shouldn't be called but need to define to compile" << std::endl;
    }
};

// Pick a back-end
using Sm1 =  msm::back::state_machine<Sm1_>;

int main() {
    Sm1 sm1;
    sm1.start(); 
    std::cout << "> Send Event E" << std::endl;
    sm1.process_event(E());
    std::cout << "> Send Event AtoB" << std::endl;
    sm1.process_event(AtoB());
    std::cout << "> Send Event E" << std::endl;
    sm1.process_event(E());
    std::cout << "> Send Event BtoC" << std::endl;
    sm1.process_event(BtoC());
    std::cout << "> Send Event E" << std::endl;
    sm1.process_event(E());
}

现场演示:https ://wandbox.org/permlink/NWvuaetwFAm5Nm23

于 2019-11-20T23:42:51.033 回答