有限自动状态机

这篇博客介绍了一个使用Boost库实现的有限自动状态机(FSM)示例,展示了如何定义状态、事件、转换动作和转换表。示例中,FSM有两个状态:Locked和Unlocked,事件包括coin和push。通过处理这些事件,FSM可以在不同状态之间切换,并打印相应的状态变化信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>


namespace msm = boost::msm;
namespace mpl = boost::mpl;
using namespace msm::front;


// events
struct coin {};
struct push {};


// front-end: define the FSM structure 
struct autodoor_ : public msm::front::state_machine_def<autodoor_>
{
//
struct Locked : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& )
{
std::cout << "entering: Locked" << std::endl;
}


template <class Event,class FSM>
void on_exit(Event const&,FSM& )
{
std::cout << "leaving: Locked" << std::endl;
}
};


//
struct Unlocked : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& )
{
std::cout << "entering: Unlocked" << std::endl;
}


template <class Event,class FSM>
void on_exit(Event const&,FSM& )
{
std::cout << "leaving: Unlocked" << std::endl;
}
};       


// the initial state of the player SM. Must be defined
typedef Locked initial_state;


// transition actions
void locked_coin(coin const&)
{
std::cout << "locked::coin\n" << std::endl;
}


void locked_push(push const &)
{
std::cout << "Locked::push\n" << std::endl;
}


void unlocked_coin(coin const &)
{
std::cout << "Unlocked::coin\n" << std::endl;
}


void unlocked_push(push const &)
{
std::cout << "Unlocked::push\n" << std::endl;
}


// Transition table for player
struct transition_table : mpl::vector<
//    Start     Event         Next      Action               Guard
//  +---------+-------------+---------+---------------------+----------------------+
a_row < Locked  , coin        , Unlocked , &autodoor_::locked_coin                  >,
a_row < Locked  , push        , Locked   , &autodoor_::locked_push                  >,
//  +---------+-------------+---------+---------------------+----------------------+
a_row < Unlocked, coin        , Unlocked , &autodoor_::unlocked_coin                >,
a_row < Unlocked, push        , Locked   , &autodoor_::unlocked_push                >
//  +---------+-------------+---------+---------------------+----------------------+
> {};
};


// Pick a back-end
typedef msm::back::state_machine<autodoor_> autodoor;


//
// Testing utilities.
//
static char const* const state_names[] = { "Locked", "Unlocked"};
void pstate(autodoor const& d)
{
std::cout << " -> " << state_names[d.current_state()[0]] << std::endl;
}


void test()
{        
autodoor d;
// needed to start the highest-level SM. This will call on_entry and mark the start of the SM
d.start(); 


d.process_event(push());
pstate(d);


d.process_event(coin());
pstate(d);


d.process_event(push());
pstate(d);


d.process_event(coin());
pstate(d);
}


int main()
{
    test();
    return 0;
}

有限自动状态机
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值