// Copyright 2010 Christophe Henry // henry UNDERSCORE christophe AT hotmail DOT com // This is an extended version of the state machine available in the boost::mpl library // Distributed under the same license as the original. // Copyright for the original version: // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed // under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // This file includes some changes just to create a kind of test case. // It seems to me that there is a kind of bug because events are not precessed // in any transition table (state machine or submachine) neither by their no_transition // functions when the event is trying to be processed in an action (functor) in a submachine. // // The ActionErrorFound functor has been created and added in some places of the two // transition tables of the tutorial. // // The test() function has been also reduced to the minimum. // // by Albert Gil // #include // back-end #include //front-end #include #include namespace msm = boost::msm; namespace mpl = boost::mpl; using namespace boost::msm::front; namespace { // events struct play {}; struct end_pause {}; struct stop {}; struct pause {}; struct open_close {}; struct NextSong {}; struct PreviousSong {}; struct error_found {}; struct end_error {}; // Event not included in any transition table. // It is well processed by no_transition except if you try to // process it in an action of a submachine (bug?) // // by Albert Gil struct to_be_ignored {}; // Flags. Allow information about a property of the current state struct PlayingPaused{}; struct CDLoaded {}; struct FirstSongPlaying {}; // A "complicated" event type that carries some data. struct cd_detected { cd_detected(std::string name) : name(name) {} std::string name; }; // This action functor will be used in // - the Playing submachine when NextSong event is received in the last song (Song3) // - the Player machine when play event is received while Playing // // by Albert Gil struct ActionErrorFound { template void operator()(EVT const& evt,FSM& fsm,SourceState& src,TargetState& tgt) { std::cout << "ActionErrorFound: processing two events: to_be_ignored and error_found" << std::endl; // The two events are processed normally if "fsm" is the main state machine (no_transition+ AllOk->ErrorMode). // But this any of them are correctly processed if "fsm" is a submachine. // So, if it is a bug, it seems to be not related to orthogonal regions, but to the process of events // in submachines actions, when this events are not in the submacihne transition table... isn't it? fsm.process_event(to_be_ignored()); fsm.process_event(error_found()); //fsm.process_event(PreviousSong()); // this always works!! } }; // front-end: define the FSM structure struct player_ : public msm::front::state_machine_def { // we want deferred events and no state requires deferred events (only the fsm in the // transition table), so the fsm does. typedef int activate_deferred_events; // The list of FSM states struct Empty : public msm::front::state<> { // every (optional) entry/exit methods get the event passed. template void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;} }; struct Open : public msm::front::state<> { typedef mpl::vector1 flag_list; template void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;} }; struct Stopped : public msm::front::state<> { // when stopped, the CD is loaded typedef mpl::vector1 flag_list; template void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;} }; // the player state machine contains a state which is himself a state machine // as you see, no need to declare it anywhere so Playing can be developed separately // by another team in another module. For simplicity I just declare it inside player struct Playing_ : public msm::front::state_machine_def { // when playing, the CD is loaded and we are in either pause or playing (duh) typedef mpl::vector2 flag_list; template void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;} // The list of FSM states struct Song1 : public msm::front::state<> { typedef mpl::vector1 flag_list; template void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;} }; struct Song2 : public msm::front::state<> { template void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;} }; struct Song3 : public msm::front::state<> { template void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;} }; // the initial state. Must be defined typedef Song1 initial_state; // transition actions void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; } void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; } void playing_error(NextSong const&) { std::cout << "Playing::playing_error\n"; } // guard conditions typedef Playing_ pl; // makes transition table cleaner // Transition table for Playing struct transition_table : mpl::vector< // Start Event Next Action Guard // +---------+-------------+---------+---------------------+----------------------+ a_row < Song1 , NextSong , Song2 , &pl::start_next_song >, a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >, a_row < Song2 , NextSong , Song3 , &pl::start_next_song >, a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >, Row < Song3 , NextSong , none , ActionErrorFound > // by Albert Gil // +---------+-------------+---------+---------------------+----------------------+ > {}; // Replaces the default no-transition response. template void no_transition(Event const& e, FSM&,int state) { std::cout << "no transition from state " << state << " on event " << typeid(e).name() << " in the Playing submachine" << std::endl; } }; // back-end typedef msm::back::state_machine Playing; // state not defining any entry or exit struct Paused : public msm::front::state<> { typedef mpl::vector2 flag_list; }; struct AllOk : public msm::front::state<> { template void on_entry(Event const&,FSM& ) {std::cout << "starting: AllOk" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "finishing: AllOk" << std::endl;} }; // this state is also made terminal so that all the events are blocked struct ErrorMode : //public msm::front::terminate_state<> // ErrorMode terminates the state machine public msm::front::interrupt_state // ErroMode just interrupts. Will resume if // the event end_error is generated { template void on_entry(Event const&,FSM& ) {std::cout << "starting: ErrorMode" << std::endl;} template void on_exit(Event const&,FSM& ) {std::cout << "finishing: ErrorMode" << std::endl;} }; // the initial state of the player SM. Must be defined typedef mpl::vector initial_state; // transition actions void start_playback(play const&) { std::cout << "player::start_playback\n"; } void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; } void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; } void store_cd_info(cd_detected const& cd) {std::cout << "player::store_cd_info\n";} void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; } void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; } void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; } void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; } void stopped_again(stop const&){std::cout << "player::stopped_again\n";} void report_error(error_found const&) {std::cout << "player::report_error\n";} void report_end_error(end_error const&) {std::cout << "player::report_end_error\n";} // guard conditions typedef player_ p; // makes transition table cleaner // Transition table for player struct transition_table : mpl::vector< // Start Event Next Action Guard // +---------+-------------+---------+---------------------+----------------------+ a_row < Stopped , play , Playing , &p::start_playback >, a_row < Stopped , open_close , Open , &p::open_drawer >, a_row < Stopped , stop , Stopped , &p::stopped_again >, // +---------+-------------+---------+---------------------+----------------------+ a_row < Open , open_close , Empty , &p::close_drawer >, Row < Open , play , none , Defer , none >, // +---------+-------------+---------+---------------------+----------------------+ a_row < Empty , open_close , Open , &p::open_drawer >, a_row < Empty , cd_detected , Stopped , &p::store_cd_info >, Row < Empty , play , none , Defer , none >, // +---------+-------------+---------+---------------------+----------------------+ a_row < Playing , stop , Stopped , &p::stop_playback >, a_row < Playing , pause , Paused , &p::pause_playback >, a_row < Playing , open_close , Open , &p::stop_and_open >, Row < Playing , play , none , ActionErrorFound , none >, //by Albert Gil // +---------+-------------+---------+---------------------+----------------------+ a_row < Paused , end_pause , Playing , &p::resume_playback >, a_row < Paused , stop , Stopped , &p::stop_playback >, a_row < Paused , open_close , Open , &p::stop_and_open >, // +---------+-------------+---------+---------------------+----------------------+ a_row < AllOk , error_found ,ErrorMode, &p::report_error >, a_row // +---------+-------------+---------+---------------------+----------------------+ > {}; // Replaces the default no-transition response. template void no_transition(Event const& e, FSM&,int state) { std::cout << "no transition from state " << state << " on event " << typeid(e).name() << " in the player state machine" << std::endl; } }; // Pick a back-end typedef msm::back::state_machine player; // // Testing utilities. // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" }; void pstate(player const& p) { // we have now several active states, which we show for (unsigned int i=0;i " << state_names[p.current_state()[i]] << std::endl; } } void test() { player p; // needed to start the highest-level SM. This will call on_entry and mark the start of the SM p.start(); // test deferred event // deferred in Empty and Open, will be handled only after event cd_detected p.process_event(play()); // tests some flags std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active() << std::endl; //=> false (no CD yet) // go to Open, call on_exit on Empty, then action, then on_entry on Open p.process_event(open_close()); pstate(p); p.process_event(open_close()); pstate(p); p.process_event(cd_detected("louie, louie")); // at this point, Play is active (was deferred) std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active() << std::endl;//=> true std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active() << std::endl;//=> true // make transition happen inside it. Player has no idea about this event but it's ok. p.process_event(NextSong());pstate(p); //2nd song active p.process_event(NextSong());pstate(p);//3rd song active // just to check that some events are passed to the no_transition function if they are processed here // if they are processed in an action of a submachine the are "ignored" (bug?) std::cout << "Processing events directly to the main state machine: " << std::endl; p.process_event(to_be_ignored()); pstate(p); // -> works: no_transition of the main state machine and submachine executed p.process_event(error_found()); pstate(p); // -> works (the original way, not from an action) std::cout << "Restoring the AllOk state: " << std::endl; p.process_event(end_error()); pstate(p); std::cout << "Processing a no_transition and an error in an action of the global state machine: " << std::endl; p.process_event(play()); pstate(p); // -> also works std::cout << "Restoring the AllOk state: " << std::endl; p.process_event(end_error()); pstate(p); std::cout << "TRYING to force a no_transition and an error in an action of a submachine: " << std::endl; p.process_event(NextSong()); pstate(p); // -> not work!! nothing happen!! // -> the no_transition functions are NOT executed! // -> still in AllOk std::cout << "has been no_transition executed?" << std::endl; std::cout << "are we in the ErrorMode?" << std::endl; } } int main() { test(); return 0; }