boost msm
2021-05-30  本文已影响0人  zxlele_c763

sub-msm process events

sub-msm can become state in master msm

  • set sub msm as a state

master MSM define example

 using initial_state = boost::mpl::vector<Init, Working>;

 struct transition_table : mpl::vector<
            //        Start         Event        Next          Action           Guard 
            msmf::Row<Init,         Event1,      SubSm1,         PerformEvent1Action,         ExecutionInformation>,
            msmf::Row<SubSm1,       Abort,       End,            PerformAbortAction,          ExecutionInformation>,
            msmf::Row<Working,      Abort,       msmf::none,     PerformSetWaitFlag,          ExecutionInformation>
            //msmf::Row<State1,       Timeout,     msmf::none,     PerformEvent1Action,       ExecutionInformation>
        >{};

sub- MSM define

 using initial_state = boost::mpl::vector<Init, Working>;

struct transition_table : mpl::vector<
            //        Start        Event        Next          Action           Guard
            
            msmf::Row<Working,      Abort,       msmf::none,     msmf::none,             ExecutionInformation>,
            msmf::Row<Working,      Abort,       msmf::none,     PerformSetWaitFlag,     ExecutionNeedWait>,
            msmf::Row<Init,         Abort,       State1,         PerformAbortAction,     ExecutionInformation>,
            msmf::Row<State1,       Timeout,     msmf::none,     PerformEvent1Action,    ExecutionInformation>
        >{};

input Test codes:

void MsmTest::subMsmEventsTest()
{
    
    MasterMsm::Sm1 sm1;
    sm1.process_event(Event1{});
    struct Abort abort;
    abort.isNgApMsgBack = true;
    sm1.process_event(abort);

    return; 
}

  • common codes:
struct ExecutionInformation
{
    template <class Event, class StateMachine, class SourceState, class TargetState>
    bool operator()(Event const& event, StateMachine&, SourceState& sourceState, TargetState& targetState)
    {
      
        std::cout<<"received message: "<< boost::core::demangle(typeid(event).name()) <<" ." <<std::endl;
        std::cout<<"sourceState is: "<< boost::core::demangle(typeid(sourceState).name()) <<" ." <<std::endl;
        std::cout<<"targetState is: "<<boost::core::demangle(typeid(targetState).name()) <<" ." <<std::endl;

        return true;
    }
};

struct PerformEvent1Action
{

    template <class Event, class StateMachine, class SourceState, class TargetState>
    void operator()(const Event&, StateMachine&, SourceState&, TargetState&)
    {
        std::cout<<" PerformEvent1Action "<<std::endl;
    }
};

struct PerformAbortAction
{

    template <class Event, class StateMachine, class SourceState, class TargetState>
    void operator()(const Event&, StateMachine&, SourceState&, TargetState&)
    {
        std::cout<<" PerformAbortAction "<<std::endl;
    }
};

test logs:

========subMsmEventsTest beging=======
received message: Event1 .
sourceState is: MasterMsm::Sm1_::Init .
targetState is: boost::msm::back::state_machine<(anonymous namespace)::SubSm1_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_> .
 PerformEvent1Action
sub msm :: Init::on_entry()
sub msm ::Working::on_entry()
received message: Abort .
sourceState is: (anonymous namespace)::SubSm1_::Init .
targetState is: (anonymous namespace)::SubSm1_::State1 .
 PerformAbortAction
sub msm ::State1::on_entry()
received message: Abort .
sourceState is: (anonymous namespace)::SubSm1_::Working .
targetState is: (anonymous namespace)::SubSm1_::Working .
event isNgApMsgBack is true.
 PerformSetWaitFlag
received message: Abort .
sourceState is: MasterMsm::Sm1_::Working .
targetState is: MasterMsm::Sm1_::Working .
 PerformSetWaitFlag
MsmTest deconstruct.

sub msm return to master msm

method 1: exit_pseudo_state submsm exit .

  • C++ codes master msm(include defer process)
typedef int activate_deferred_events;  //defer data type

//using ActionSequence = boost::msm::front::ActionSequence_<boost::mpl::vector<Actions...>>;

 using E1F1Exit = SubMsmSubToMaster::SubSm::exit_pt<SubMsmSubToMaster::SubSm::ExitState>;

using initial_state = boost::mpl::vector<Init, State2, Working>;


      struct transition_table : mpl::vector<
            //        Start            Event             Next              Action                        Guard
                                                        
            msmf::Row<Init,           Event1,            State1,           PerformEvent1Action,           ExecutionInformation>,
            msmf::Row<State1,         msmf::none,        SubMsmSubToMaster::SubSm, 
                                                                           PerformNoneEventAction,       ExecutionInformation>,
            msmf::Row<State2,         UeContextReleaseCommand,  msmf::none,        msmf::Defer,          ExecutionInformation>,
            msmf::Row<State2,         Timeout,                  msmf::none,        msmf::Defer,          ExecutionInformation>,
            
            //E1F1Exit
            msmf::Row<E1F1Exit,       msmf::none,       HoCompletion,     msmf::none,                    ExecutionInformation>,
            
            //complete:
            msmf::Row<HoCompletion,    UeContextReleaseCommand,           HoExit,            msmf::none,  ExecutionInformation>,
            msmf::Row<HoCompletion,    Timeout,                           HoExit,            msmf::none,  ExecutionInformation>,

            msmf::Row<Working,         Abort,            msmf::none,            msmf::none,               ExecutionInformation>      
        >{};



  • sub msm C++ codes(msmf::exit_pseudo_state)
        struct ExitState : msmf::exit_pseudo_state<msmf::none>
        {   
            // Entry action
            template <class Event,class Fsm>
            void on_entry(Event const&, Fsm&) {
                std::cout << "SubSm::ExitState::on_entry()" << std::endl;
            }
            // Exit action
            template <class Event,class Fsm>
            void on_exit(Event const&, Fsm&) {
                std::cout << "SubSm::ExitState::on_exit()" << std::endl;
            }
        };
  

   struct transition_table : mpl::vector<
            //        Start        Event        Next          Action                  Guard 
            //msmf::Row<Init,        Or_<Event1, Abort>,      State1,       msmf::none,             ExecutionInformation>,
            msmf::Row<Init,        Event1,      State1,       msmf::none,             ExecutionInformation>,
            msmf::Row<State1,      Abort,       ExitState,    PerformAbortAction,     ExecutionInformation>
            //msmf::Row<End,         Timeout,     msmf::none,   PerformEvent1Action,    ExecutionInformation>
        >{};

  • test codes
void MsmTest::subMsmTransferToMasterMsmTest()
{
    std::cout<<std::endl<<"=======subMsmTransferToMasterMsmTest beging======="<<std::endl;
    SubMsmTransferMasterMsm::Sm1 sm1;
    sm1.start();

    sm1.process_event(Event1{});
    sm1.process_event(UeContextReleaseCommand{});
    sm1.process_event(Timeout{});
    sm1.process_event(Event1{});
    sm1.process_event(Abort{});

    return; 
}
  • test logs ::HoCompletion::on_entry()
start.
MsmTest construct.
=======subMsmTransferToMasterMsmTest beging=======
Init::on_entry()
State2::on_entry()
Working::on_entry()
received message: Event1 .
sourceState is: SubMsmTransferMasterMsm::Sm1_::Init .
targetState is: SubMsmTransferMasterMsm::Sm1_::State1 .
 PerformEvent1Action
State1::on_entry()
received message: boost::msm::front::none .
sourceState is: SubMsmTransferMasterMsm::Sm1_::State1 .
targetState is: boost::msm::back::state_machine<SubMsmSubToMaster::Sm1_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_> .
State1::on_exit()
 PerformNoneEventAction
SubSm::Init::on_entry()
received message: UeContextReleaseCommand .
sourceState is: SubMsmTransferMasterMsm::Sm1_::State2 .
targetState is: SubMsmTransferMasterMsm::Sm1_::State2 .
received message: Timeout .
sourceState is: SubMsmTransferMasterMsm::Sm1_::State2 .
targetState is: SubMsmTransferMasterMsm::Sm1_::State2 .
received message: Event1 .
sourceState is: SubMsmSubToMaster::Sm1_::Init .
targetState is: SubMsmSubToMaster::Sm1_::State1 .
SubSm::State1::on_entry()
received message: UeContextReleaseCommand .
sourceState is: SubMsmTransferMasterMsm::Sm1_::State2 .
targetState is: SubMsmTransferMasterMsm::Sm1_::State2 .
received message: Timeout .
sourceState is: SubMsmTransferMasterMsm::Sm1_::State2 .
targetState is: SubMsmTransferMasterMsm::Sm1_::State2 .
received message: Abort .
sourceState is: SubMsmSubToMaster::Sm1_::State1 .
targetState is: boost::msm::back::state_machine<SubMsmSubToMaster::Sm1_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_>::exit_pt<SubMsmSubToMaster::Sm1_::ExitState> .
SubSm::State1::on_exit()
 PerformAbortAction
SubSm::ExitState::on_entry()
received message: Abort .
sourceState is: SubMsmTransferMasterMsm::Sm1_::Working .
targetState is: SubMsmTransferMasterMsm::Sm1_::Working .
received message: boost::msm::front::none .
sourceState is: boost::msm::back::state_machine<SubMsmSubToMaster::Sm1_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_, boost::parameter::void_> .
targetState is: SubMsmTransferMasterMsm::Sm1_::HoCompletion .
SubSm::ExitState::on_exit()
HoCompletion::on_entry()    // exit from sub msm to master msm
received message: UeContextReleaseCommand .
sourceState is: SubMsmTransferMasterMsm::Sm1_::HoCompletion .
targetState is: SubMsmTransferMasterMsm::Sm1_::HoExit .
HoCompletion::on_exit()
HoExit::on_entry()
received message: UeContextReleaseCommand .
sourceState is: SubMsmTransferMasterMsm::Sm1_::State2 .
targetState is: SubMsmTransferMasterMsm::Sm1_::State2 .
received message: Timeout .
sourceState is: SubMsmTransferMasterMsm::Sm1_::State2 .
targetState is: SubMsmTransferMasterMsm::Sm1_::State2 .
MsmTest deconstruct.

上一篇下一篇

猜你喜欢

热点阅读