推迟子状态机的事件

时间:2018-12-21 14:33:05

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

在我的项目中,有一个使用boost元状态机实现的状态机。这个主状态机有一个状态(为简单起见,我们称其为SubMachineEntry)代表一个子状态机的入口点:

namespace msmf = boost::msm::front;
namespace msmb = boost::msm::back;
...
msmf::Row < SomeState, enterSub, SubMachineEntry, msmf::none, msmf::none >
...
using SubMachine = msmb::state_machine<SubMachine_>;

此子状态机执行一些处理,然后回到外部状态机进行状态

using SubMachineFinished = SubMachine::exit_pt<...>;

,代表子机的退出状态。我的问题是,在子状态机退出之前,可能会调度某个事件(toProcessInOuter),并且应该在子状态机退出后在外部状态机中对其进行处理。将该事件在子状态机中的所有状态中推迟都行不通,它不会传播到SubMachineFinished之后的状态。有没有一种方法可以使用boost MSM的某种机制来实现事件的转发,而无需使用某些自定义解决方法?

1 个答案:

答案 0 :(得分:1)

不幸的是,子机上的延迟事件无法在MSM的父状态机上求值。因此,您需要在父状态下编写延迟转换。

从内部状态机到外部状态机评估事件。如果要延迟事件,则可以在父状态下编写延迟过渡。 我写了一个演示它的例子。

enter image description here

请参阅附图。假设Event2是您要推迟的事件。您需要将延迟转换写入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>

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

// ----- Events
struct Event1 {};
struct Event2 {};

// ----- State machine
struct OuterSm_:msmf::state_machine_def<OuterSm_> {
    struct State1_:msmf::state_machine_def<State1_> {
        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;
        }

        struct SubState1:msmf::state<> {
            template <class Event,class Fsm>
            void on_entry(Event const&, Fsm&) const {
                std::cout << "SubState1::on_entry()" << std::endl;
            }
            template <class Event,class Fsm>
            void on_exit(Event const&, Fsm&) const {
                std::cout << "SubState1::on_exit()" << std::endl;
            }
        };
        template <class Fsm,class Event> 
        void no_transition(Event const& e, Fsm& ,int state) {
            std::cout << "No handled event in InnerSm " << typeid(e).name() << " on State " << state << std::endl;
        }
        struct Exit1:msmf::exit_pseudo_state<msmf::none> {};
        // Set initial state
        typedef mpl::vector<SubState1> initial_state;
        // Transition table
        struct transition_table:mpl::vector<
            //          Start      Event   Next        Action       Guard
            msmf::Row < SubState1, Event1, Exit1,      msmf::none,  msmf::none >
            > {};
    };
    struct State2:msmf::state<> {
        template <class Event,class Fsm>
        void on_entry(Event const&, Fsm&) const {
            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;
        }
    };
    template <class Fsm,class Event> 
    void no_transition(Event const& e, Fsm& ,int state) {
        std::cout << "No handled event in OuterSm " << typeid(e).name() << " on State " << state << std::endl;
    }

    typedef msm::back::state_machine<State1_> State1;
    // Actions
    struct Action {
        template <class Event, class Fsm, class SourceState, class TargetState>
        void operator()(Event const&, Fsm&, SourceState&, TargetState&) const {
            std::cout << "Action()" << std::endl;
        }
    };
    // enable deferred events
    typedef int activate_deferred_events;
    // Set initial state
    typedef State1 initial_state;
    // Transition table
    struct transition_table:mpl::vector<
        //          Start             Event       Next        Action       Guard
        msmf::Row < State1::exit_pt
                    <State1_::Exit1>, msmf::none, State2,     msmf::none,  msmf::none >,
        msmf::Row < State1,           Event2,     msmf::none, msmf::Defer, msmf::none >,
        msmf::Row < State2,           Event2,     msmf::none, Action,      msmf::none >
    > {};
};

// Pick a back-end
typedef msm::back::state_machine<OuterSm_> Osm;


int main() {
    Osm osm;
    osm.start(); 

    std::cout << "> Send Event2()" << std::endl;
    osm.process_event(Event2());
    std::cout << "> Send Event1()" << std::endl;
    osm.process_event(Event1());
    return 0;
}

正在运行的演示:https://wandbox.org/permlink/WQixcoGGQwAWou34