我在 boost::msm 中实现了描述的状态机作为最小示例。代码可在此处查看:https ://wandbox.org/permlink/tIhZao8YGhJvPMfF
如下代码所示,我想从“someAction”调用 event2,当从顶级状态机“MainMachine”对 event4 调用 someAction 时,它可以正常工作。但是,当从 Submachine 内部调用 event3 时,只会触发 submachine 的无转换响应(或者如果 submachine 中没有提供转换响应,则运行时断言失败)。我本来希望 event2 被引用到顶级状态机的转换表,但这似乎不起作用,因为 someAction 只获得对 SubMachine FSM 的引用。
在从 Submachine 调用的动作内部的顶级状态机的正交区域中触发事件的最佳方法是什么?
struct someAction
{
template<class EVT, class FSM, class SourceState, class TargetState>
void operator()(EVT const& evt,
FSM& fsm,
SourceState& src,
TargetState& tgt) const
{
fsm.process_event(event2());
std::cout << "Called from: " << typeid(fsm).name() << " / " << &fsm << std::endl;
}
};
#include "fsm.h"
#include <iostream>
int
main()
{
MainMachine p;
std::cout << "--> Start STM" << std::endl;
p.start();
std::cout << "--> Event 1" << std::endl;
p.process_event(event1());
std::cout << "--> Event 3" << std::endl;
p.process_event(event3());
std::cout << "--> Event 4" << std::endl;
p.process_event(event4());
std::cout << "--> Stop STM" << std::endl;
p.stop();
return 0;
}
输出:
Start
--> Start STM
entering: MainMachine
entering: State1
entering: State3
--> Event 1
leaving: State1
entering: SubMachine
entering: SubState1
--> Event 3
leaving: SubState1
Called from: N5boost3msm4back13state_machineIN12MainMachine_11SubMachine1ENS_9parameter5void_ES6_S6_S6_EE / 0x7ffd823f1138
entering: SubState2
no transition from state 1 on event 6event2
--> Event 4
leaving: SubState2
leaving: SubMachine
Called from: N5boost3msm4back13state_machineI12MainMachine_NS_9parameter5void_ES5_S5_S5_EE / 0x7ffd823f10e0
entering: State1
leaving: State3
entering: State4
--> Stop STM
leaving: State1
leaving: State4
leaving: MainMachine
0
Finish