Boost.MSM:通过连接伪状态退出正交区域
Boost.MSM: Exit orthogonal regions via a join pseudo state
我打算使用boost.msm和包含正交区域的复合的概念。我想在退出时同步所有正交区域。换言之:当且仅当所有区域都已达到其最后状态时,我的合成之后的状态将被激活。
UML 2.4"Superstructure"提出了join伪状态(即第15.3.8章)。在boost中,有一个fork,但我找不到它的对应join的任何实现。
boost.msm中是否没有联接伪状态?如何将连接伪状态的概念与boost.msm应用
您可以使用一个计数器,该计数器将在每次进入联接状态时递增。当该计数器等于正交区域的数量时,连接状态之后的状态将被激活。
这可以手动完成,也可以以通用方式完成。下面我实现了一种通用方式,其中通过从模板JoinSM
继承来将连接逻辑添加到子机Sub
。
Sub
有3个正交区域(在这个简单的例子中,每个区域仅由一个状态组成,即Orthogonal1
、Orthogonal2
和Orthogonal3
)。所有这些正交状态都连接到Join
状态,但是在Sub
内没有指定从Join
状态到Exit
状态的直接连接。
该连接在JoinSM
中实现。每次从Sub
达到Join
状态时,Waiting
状态被激活,计数器递增。如果计数器达到正交区域的数量,则触发事件AllJoined
,并激活向Exit
的转换。
由于JoinSM
通过initial_state
的大小查询正交区域的数量,在Sub
中添加或删除区域将自动反映在连接逻辑中。
#include <iostream>
#include <cstdlib>
#include <memory>
#include <cxxabi.h>
template <class T>
std::string demangle()
{
const char* name = typeid(T).name();
int status = -1;
std::unique_ptr<char, void(*)(void*)> res {
abi::__cxa_demangle(name, NULL, NULL, &status),
std::free
};
return (status==0) ? res.get() : name ;
}
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/back/metafunctions.hpp>
#include <boost/mpl/assert.hpp>
using namespace boost::msm;
using namespace boost::msm::front;
template <typename State>
struct BaseState : public boost::msm::front::state<>
{
template <class Event,class FSM> void on_entry(Event const&,FSM& )
{
std::cout << "on_entry: " << demangle<State>() << std::endl;
}
template <class Event,class FSM> void on_exit(Event const&,FSM& )
{
std::cout << "on_exit: " << demangle<State>() << std::endl;
}
};
// EVENTS
struct EnterOrthogonal {};
struct Orthogonal1Finished{};
struct Orthogonal2Finished{};
struct Orthogonal3Finished{};
struct SubSM_ : state_machine_def<SubSM_>
{
struct Started : BaseState<Started>{};
struct Exit : exit_pseudo_state<none> {};
struct Orthogonal1 : BaseState<Orthogonal1>{};
struct Orthogonal2 : BaseState<Orthogonal2>{};
struct Orthogonal3 : BaseState<Orthogonal3>{};
struct Join : BaseState<Join>{};
typedef boost::mpl::vector<Orthogonal1, Orthogonal2, Orthogonal3> initial_state;
struct transition_table : boost::mpl::vector<
Row<Orthogonal1, Orthogonal1Finished, Join, none, none>,
Row<Orthogonal2, Orthogonal2Finished, Join, none, none>,
Row<Orthogonal3, Orthogonal3Finished, Join, none, none>
> {};
};
template <typename SM, typename JoinState = typename SM::Join, typename ExitState = typename SM::Exit>
struct JoinSM : SM
{
struct AllJoined{};
constexpr static int num_regions = boost::mpl::size<typename SM::initial_state>::value;
int count;
template <class Event,class FSM>
void on_entry(Event const& ,FSM&)
{
// reset count
count = 0;
}
struct Waiting : BaseState<Waiting>
{
template <class Event,class FSM>
void on_entry(const Event& e,FSM& f)
{
BaseState<Waiting>::on_entry(e,f);
f.count++;
if (f.count == FSM::num_regions)
{
f.process_event(AllJoined());
}
}
};
typedef boost::mpl::vector<
Row<JoinState, none, Waiting, none, none>,
Row<Waiting, AllJoined, ExitState, none, none>
> additional_transition_table;
typedef boost::mpl::joint_view<
typename SM::transition_table,
additional_transition_table
> transition_table;
};
// inherit from JoinSM to add the joining logic
using Sub = back::state_machine<JoinSM<SubSM_>>;
struct MainSM_ : state_machine_def<MainSM_>
{
struct Started : BaseState<Started>{};
struct AfterJoin : BaseState<AfterJoin>{};
using initial_state = boost::mpl::vector<Started>;
struct transition_table : boost::mpl::vector<
Row<Started, EnterOrthogonal, Sub, none, none>,
Row<Sub::exit_pt<SubSM_::Exit>, none, AfterJoin, none, none>
> {};
};
struct MainSM_;
using Main = back::state_machine<MainSM_>;
int main()
{
Main main;
main.start();
main.process_event(EnterOrthogonal());
main.process_event(Orthogonal3Finished());
main.process_event(Orthogonal1Finished());
main.process_event(Orthogonal2Finished());
}
输出:
on_entry: MainSM_::Started
on_exit: MainSM_::Started
on_entry: SubSM_::Orthogonal1
on_entry: SubSM_::Orthogonal2
on_entry: SubSM_::Orthogonal3
on_exit: SubSM_::Orthogonal3
on_entry: SubSM_::Join
on_exit: SubSM_::Join
on_entry: JoinSM<SubSM_, SubSM_::Join, SubSM_::Exit>::Waiting
on_exit: SubSM_::Orthogonal1
on_entry: SubSM_::Join
on_exit: SubSM_::Join
on_entry: JoinSM<SubSM_, SubSM_::Join, SubSM_::Exit>::Waiting
on_exit: SubSM_::Orthogonal2
on_entry: SubSM_::Join
on_exit: SubSM_::Join
on_entry: JoinSM<SubSM_, SubSM_::Join, SubSM_::Exit>::Waiting
on_exit: JoinSM<SubSM_, SubSM_::Join, SubSM_::Exit>::Waiting
on_exit: JoinSM<SubSM_, SubSM_::Join, SubSM_::Exit>::Waiting
on_exit: JoinSM<SubSM_, SubSM_::Join, SubSM_::Exit>::Waiting
on_entry: MainSM_::AfterJoin
示例:http://coliru.stacked-crooked.com/a/6c060d032bc53573
相关文章:
- 我不断收到 [错误] ID 返回 1 退出状态错误,但看不到问题所在
- 为什么系统函数总是在C++中返回已转移的退出状态?
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 编译问题:在函数"_start"中:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 试图在崇高中奔跑. 错误 已发生: 收集2.exe: 错误: ld 返回 1 退出状态
- collect2:错误:ld 返回 1 个退出状态未定义的引用
- C++ collect2:错误:ld 返回 1 退出状态
- 找不到 -llibmysqld Collect2.exe:错误:ld 返回 1 退出状态
- 每次我要使用外部资源时,我都会得到:collect2。exe:错误 ld 返回 1 退出状态
- 当出现错误ld return 1退出状态时,如何编译程序
- 无法访问 Arduino 结构字段。错误"退出状态 1。xxxx 不命名类型"
- "Extern"对象问题:错误:Id 返回 1 个退出状态
- c++ (C::B) 错误:ld 返回 1 个退出状态
- 在 c++ 中捕获 shell 脚本退出状态
- 对"displayForStudent(int, int const*, double const*, int)"的未定义引用 collect2.exe:错误:ld 返回 1 个退出状态
- 使用类[错误] LD返回1退出状态
- 如何解决退出状态 1 错误
- 错误 (Id) 返回 1 个退出状态
- 退出状态 1 令牌之前的预期主表达式'.'
- 错误:LD返回1个退出状态-CodeBlocks