还有另一种解决方案。在您的情况下,复合状态是更好的选择。见图表。

如何避免将许多转换写入“紧急停止”。
如果发生“停止”事件,则将所有需要转换为“紧急停止”的状态置于“正常”状态。并放置从“正常”到“紧急停止”的过渡。它的触发事件是“停止”。这种方法可以避免将许多转换写入“EmergencyStopped”。即使您要添加“正常”的其他子状态,您也不需要为添加的状态添加转换。这是复合状态方法的好处之一。
如果发生“恢复”事件,如何转换到“空闲”状态。
将“空闲”状态设置为initial_state
。它反映在 UML 状态机图中的初始伪状态。
typedef mpl::vector<Idle> initial_state;
如果转换目标是父状态“正常”,那么转换目标状态是“空闲”状态,因为它被标记为initial_state
。
这两种技术解决了你的问题。
这是完整的代码:
#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/static_assert.hpp>
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
// ----- Events
struct ev1 {};
struct ev2 {};
struct ev3 {};
struct stop {};
struct recover {};
// ----- State machine
struct YourSystem_:msmf::state_machine_def<YourSystem_>
{
struct Normal_:msmf::state_machine_def<Normal_>
{
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "Normal::on_entry()" << std::endl;
}
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "Normal::on_exit()" << std::endl;
}
struct Idle:msmf::state<> {
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "Idle::on_entry()" << std::endl;
}
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "Idle::on_exit()" << std::endl;
}
};
struct Work:msmf::state<> {
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "Work::on_entry()" << std::endl;
}
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "Work::on_exit()" << std::endl;
}
};
struct AllOk:msmf::state<> {
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "AllOk::on_entry()" << std::endl;
}
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "AllOk::on_exit()" << std::endl;
}
};
// Set initial state
typedef mpl::vector<Idle> initial_state;
// Transition table
struct transition_table:mpl::vector<
// Start Event Next Action Guard
msmf::Row < Idle, ev1, Work, msmf::none, msmf::none >,
msmf::Row < Work, ev2, AllOk, msmf::none, msmf::none >,
msmf::Row < AllOk, ev3, Idle, msmf::none, msmf::none >
> {};
};
struct EmergencyStopped:msmf::state<>
{
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "EmergencyStopped::on_entry()" << std::endl;
}
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "EmergencyStopped::on_exit()" << std::endl;
}
};
typedef msm::back::state_machine<Normal_> Normal;
// Set initial state
typedef Normal initial_state;
// Transition table
struct transition_table:mpl::vector<
// Start Event Next Action Guard
msmf::Row < Normal, stop, EmergencyStopped, msmf::none, msmf::none >,
msmf::Row < EmergencyStopped, recover, Normal, msmf::none, msmf::none >
> {};
};
// Pick a back-end
typedef msm::back::state_machine<YourSystem_> Ys;
int main()
{
Ys ys;
ys.start();
std::cout << "> Send ev1()" << std::endl;
ys.process_event(ev1());
std::cout << "> Send ev2()" << std::endl;
ys.process_event(ev2());
std::cout << "> Send stop()" << std::endl;
ys.process_event(stop());
std::cout << "> Send recover()" << std::endl;
ys.process_event(recover());
}
并运行演示https://wandbox.org/permlink/uBm6jTvG0YL3gSgl