1

我的主 SM 中有两个子 SM。我希望能够从主 SM 跳到其中一个,也可以从一个子 SM 跳到另一个 SM。但我不能。我能够从主 SM 跳到子 SM,从一个子 SM 跳到另一个,但是当我在子 SM 之间添加“相互”转换时,编译失败,大约 10 个错误抱怨不同的事情。我想这是因为编译器进入了递归旋转。

我想我可以在主 SM 中添加一个虚拟状态,并匿名转换到目标子 SM。但随后我会放弃触发转换的真实事件,我不想要那个(它包含数据)。

这是一些测试代码,其中有问题的行被注释掉了

#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 {

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

    struct EvGotoSub1 {};
    struct EvGotoSub2 {};

    struct MainSM_;
    using Main = back::state_machine<MainSM_>;    
    struct Sub1SM_;
    using Sub1 = back::state_machine<Sub1SM_>;
    struct Sub2SM_;
    using Sub2 = back::state_machine<Sub2SM_>;

    struct Sub1SM_ : state_machine_def<Sub1SM_> {
        struct Started : state<> { };
        using initial_state = mpl::vector<Started>;
        struct transition_table:mpl::vector<
        Row<Started, EvGotoSub2, Sub2, none, none>
        > {};
    };

    struct Sub2SM_ : state_machine_def<Sub2SM_> {
        struct Started : state<> { };
        using initial_state = mpl::vector<Started>;
        struct transition_table:mpl::vector<
        // Uncomment line below to break things
        //Row<Started, EvGotoSub1, Sub1, none, none>
        > {};
    };

    struct MainSM_ : state_machine_def<MainSM_> {
        struct Started : state<> { };
        using initial_state = mpl::vector<Started>;
        struct transition_table:mpl::vector<
        Row<Started, EvGotoSub1, Sub1, none, none>,
        Row<Started, EvGotoSub2, Sub2, none, none>
        > {};
    };
}

int main() {

    Main main;
    main.start();
    main.process_event(EvGotoSub1());
    main.process_event(EvGotoSub2());
    main.process_event(EvGotoSub1());
}
4

1 回答 1

1

我假设您不想嵌套Sub2在里面Sub1,反之亦然,但是两者都是Main没有任何嵌套的子机。您可以使用伪退出状态从一台子机退出并转到另一台。

这些退出状态只是转发传入的事件,然后您可以通过在转换表中定义额外的转换将其传递给其他子机Main

#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 {

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

    struct EvGotoSub1 { EvGotoSub1(int d1):d1(d1){} int d1;};
    struct EvGotoSub2 { EvGotoSub2(int d2):d2(d2){} int d2;};

    struct MainSM_;
    using Main = back::state_machine<MainSM_>;    
    struct Sub1SM_;
    using Sub1 = back::state_machine<Sub1SM_>;
    struct Sub2SM_;
    using Sub2 = back::state_machine<Sub2SM_>;

    struct Sub1SM_ : state_machine_def<Sub1SM_> {
        struct Started : state<> {  template <class Event,class Fsm> void on_entry(const Event& e, Fsm&) const { std::cout << "SUB2SM_ Started::on_entry(): d1="<<e.d1 << std::endl; } };
        struct Exit : exit_pseudo_state<EvGotoSub2> {};
        using initial_state = mpl::vector<Started>;
        struct transition_table:mpl::vector<
         Row<Started, EvGotoSub2, Exit, none, none>
        > {};
    };

    struct Sub2SM_ : state_machine_def<Sub2SM_> {
        struct Started : state<> {  template <class Event,class Fsm> void on_entry(const Event& e, Fsm&) const { std::cout << "SUB2SM_ Started::on_entry(): d2="<<e.d2 << std::endl; } };
        struct Exit : exit_pseudo_state<EvGotoSub1> {};
        using initial_state = mpl::vector<Started>;
        struct transition_table:mpl::vector<
        Row<Started, EvGotoSub1, Exit, none, none>
        > {};
    };

    struct MainSM_ : state_machine_def<MainSM_> {
        struct Started : state<> { };
        using initial_state = mpl::vector<Started>;
        struct transition_table:mpl::vector<
        Row<Started, EvGotoSub1, Sub1, none, none>,
        Row<Started, EvGotoSub2, Sub2, none, none>,
        Row<Sub2::exit_pt<Sub2SM_::Exit>, EvGotoSub1, Sub1, none, none>,
        Row<Sub1::exit_pt<Sub1SM_::Exit>, EvGotoSub2, Sub2, none, none>
        > {};
    };
}


int main() {

    Main main;
    main.start();
    main.process_event(EvGotoSub1(0));
    main.process_event(EvGotoSub2(1));
    main.process_event(EvGotoSub1(2));
}

现场示例:http ://coliru.stacked-crooked.com/a/d491442b38a24e82

如您所见,事件并没有丢失,而是被转发了。

可以在http://redboltz.wikidot.com/exit-point-pseudo-state找到有关伪退出状态的良好资源。

于 2015-06-18T13:51:43.143 回答