boost msm submachine current_state用于访问子状态

时间:2016-07-28 09:11:48

标签: state boost-msm

任何想法如何,使用boost msm 1_60,我可以得到一个子机的current_state?考虑下面的代码,描述一个外部状态机,它允许在两个不同的交通信号灯之间进行选择(例如标准的红色,黄色,绿色和另一个交替的两个黄灯):

class SMBigMom : public msmf::state_machine_def<SMBigMom>
{
public:
SMBigMom() {};

using initial_state = SMSelectorState;

class SMLightBase : public msmf::state_machine_def<SMLightBase>
{
public:
    SMLightBase() {};

    using initial_state = BaseState;
    struct transition_table : mpl::vector<> {};
};
using SMBaseBackend = msm::back::state_machine<SMLightBase>;

class SMCommonRYG : public SMLightBase
{
public:
    SMCommonRYG() = default;
    ~SMCommonRYG() {};

    using initial_state = Red; // init state

    struct transition_table : mpl::vector<
        //         Start, Event, Target, Action, Guard
        msmf::Row< Red, evNext, RedYellow, msmf::none, msmf::none >,
        msmf::Row< RedYellow, evNext, Green, msmf::none, msmf::none >,
        msmf::Row< Green, evNext, Yellow, msmf::none, msmf::none >,
        msmf::Row< Yellow, evNext, Red, msmf::none, msmf::none >
    > {};
};
using SMCommonRYGBackend = msm::back::state_machine<SMCommonRYG>;

class SMYellowAlternate : public SMLightBase
{
public:
    SMYellowAlternate() = default;
    ~SMYellowAlternate() {};

    using initial_state = Yellow; // init state

    struct transition_table : mpl::vector<
        //         Start, Event, Target, Action, Guard
        msmf::Row< Yellow, evNext, Yellow2, msmf::none, msmf::none >,
        msmf::Row< Yellow2, evNext, Yellow, msmf::none, msmf::none >
    > {};
};
using SMYellowAlternateBackend = msm::back::state_machine<SMYellowAlternate>;

struct transition_table : mpl::vector<
    msmf::Row< SMSelectorState, evSelectCommonRYG, SMCommonRYGBackend, msmf::none, msmf::none >,
    msmf::Row< SMSelectorState, evSelectYellowAlternate, SMYellowAlternateBackend, msmf::none, msmf::none >
> {};

};

using SMBackend = msm::back::state_machine<SMBigMom>;

现在,我可以通过

跳到RYG
SMBackend oSM. oSM.process_event(evSelectCommonRYG());

但是如何获得RYG冲锋枪的当前状态?

oSM.current_state()[0]

仅返回1(因为这是外部状态机BigMom的状态......)...

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我同时想到了,解决方案相当简单。只是,使用仿函数前端,添加一个动作(ROW中的第4列)函数,这个函数调用如下:

struct submachineAction
{
  toNext() {};
  ~toNext() {};
  template<class TFsm, class TEvent, class TStateIn, class TStateOut>
  void operator() (const TEvent& p_rEV, const TFsm& p_rFSM, TStateIn&   p_rStateIn, TStateOut& p_rStateOut)
  { 
    std::cout << "Substate: " << p_rFSM.current_state()[0] << std::endl;
  }
};

如果你有几台“主”机器正在运行,并且需要知道哪一台叫做submachineAction,你可以定义一个带有附加标识符的基本前端类(string,int,你可以命名),并派生出来。该类的子机器。然后,在此线程之后,您可以设置标识符,可以通过上述仿函数中的p_rFSM访问该标识符:How to Pass data to the current boost meta state machine(MSM) substate

希望这可以帮助别人。