提升状态图:返回上一个状态

时间:2017-05-31 16:06:34

标签: c++ boost boost-statechart

我有一台状态机,如果我进入某个特定状态,有时我需要定期转换到另一个状态,有时我需要返回到先前状态。

例如,具有状态ABC,假设转换S将状态A移动到C并且从B移动到C.我需要转换T在S发生在状态A时将C移动到A,当它发生在状态时移动到C到B. B.

在下面的代码中,转换S发生在状态B,因此我希望转换T返回到状态B(而目前,它返回到状态A)。

#include <boost/mpl/list.hpp>

#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/transition.hpp>

// states
struct A;
struct B;
struct C;
struct D;

// events
struct S : boost::statechart::event<S> {};
struct T : boost::statechart::event<T> {};
struct U : boost::statechart::event<U> {};

// fsm
struct FSM : boost::statechart::state_machine<FSM, B> {};

// fully defined states/transitions
struct A : boost::statechart::simple_state<A, FSM> {
    typedef boost::statechart::transition<S, C> reactions;

    A() { std::cout << "entered A" << std::endl; }
};

struct B : boost::statechart::simple_state<B, FSM> {
    typedef boost::statechart::transition<S, C> reactions;

    B() { std::cout << "entered B" << std::endl; }
};

struct C : boost::statechart::simple_state<C, FSM> {
    typedef boost::mpl::list<
                boost::statechart::transition<T, A>,
                boost::statechart::transition<T, B>,
                boost::statechart::transition<U, D> > reactions;

    C() { std::cout << "entered C" << std::endl; }
};

struct D : boost::statechart::simple_state<D, FSM> {
    D() { std::cout << "entered D" << std::endl; } 
};

int main() {
    FSM fsm;

    fsm.initiate();

    fsm.process_event(S());
    fsm.process_event(T());
    fsm.process_event(S());
    fsm.process_event(U());

    return 0;
}

上面的代码返回:

entered B
entered C
entered A
entered C
entered D

我希望看到:

entered B
entered C
entered B
entered C
entered D

使用Boost :: Statechart有没有干净的方法呢?

1 个答案:

答案 0 :(得分:1)

通过为状态创建枚举映射,在最外层的上下文(顶级fsm)中存储先前的状态,然后对T事件使用自定义反应,我找到了一个好的方法:< / p>

#include <boost/mpl/list.hpp>

#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/transition.hpp>
#include <boost/statechart/custom_reaction.hpp>

// states
struct A;
struct B;
struct C;
struct D;

// state enum mapping
enum class state_mapping {
    A = 0,
    B,
    C,
    D
};

// events
struct S : boost::statechart::event<S> {};
struct T : boost::statechart::event<T> {};
struct U : boost::statechart::event<U> {};

// fsm
struct FSM : boost::statechart::state_machine<FSM, B> {
    state_mapping previous_state = state_mapping::B;
};

// fully defined states/transitions
struct A : boost::statechart::simple_state<A, FSM> {
    typedef boost::statechart::transition<S, C> reactions;

    A() { std::cout << "entered A" << std::endl; }
    virtual ~A() { outermost_context().previous_state = state_mapping::A; }
};

struct B : boost::statechart::simple_state<B, FSM> {
    typedef boost::statechart::transition<S, C> reactions;

    B() { std::cout << "entered B" << std::endl; }
    virtual ~B() { outermost_context().previous_state = state_mapping::B; }
};

struct C : boost::statechart::simple_state<C, FSM> {
    typedef boost::mpl::list<
                boost::statechart::custom_reaction<T>,
                boost::statechart::transition<U, D> > reactions;

    C() { std::cout << "entered C" << std::endl; }

    boost::statechart::result react(const T&) {

        switch(outermost_context().previous_state) {

        case state_mapping::A:
            return transit<A>();
        case state_mapping::B:
            return  transit<B>();
        default:
            return discard_event();

        }

    }
};

struct D : boost::statechart::simple_state<D, FSM> {
    D() { std::cout << "entered D" << std::endl; }
};

int main() {
    FSM fsm;

    fsm.initiate();

    fsm.process_event(S());
    fsm.process_event(T());
    fsm.process_event(S());
    fsm.process_event(U());

    return 0;
}

如果有人有更好的建议,我会全力以赴,否则我会在一两天内接受这个。