msm_adaptor.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2010 Gordon Woodhull
  2. // modified from MSMv2.10/libs/msm/doc/HTML/examples/SimpleTutorial.cpp
  3. // for the purpose of showing how to run mpl_graph algorithms on MSMs
  4. // and distributed same license as source
  5. // Copyright 2008 Christophe Henry
  6. // henry UNDERSCORE christophe AT hotmail DOT com
  7. // This is an extended version of the state machine available in the boost::mpl library
  8. // Distributed under the same license as the original.
  9. // Copyright for the original version:
  10. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  11. // under the Boost Software License, Version 1.0. (See accompanying
  12. // file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #include <iostream>
  15. // back-end
  16. #include <boost/msm/back/state_machine.hpp>
  17. //front-end
  18. #include <boost/msm/front/state_machine_def.hpp>
  19. // mpl_graph graph implementation and depth first search
  20. #include <boost/msm/mpl_graph/incidence_list_graph.hpp>
  21. #include <boost/msm/mpl_graph/depth_first_search.hpp>
  22. #include <boost/msm/mpl_graph/breadth_first_search.hpp>
  23. namespace msm = boost::msm;
  24. namespace mpl = boost::mpl;
  25. namespace mpl_graph = boost::msm::mpl_graph;
  26. namespace
  27. {
  28. // events
  29. struct play {};
  30. struct end_pause {};
  31. struct stop {};
  32. struct pause {};
  33. struct open_close {};
  34. // A "complicated" event type that carries some data.
  35. enum DiskTypeEnum
  36. {
  37. DISK_CD=0,
  38. DISK_DVD=1
  39. };
  40. struct cd_detected
  41. {
  42. cd_detected(std::string name, DiskTypeEnum diskType)
  43. : name(name),
  44. disc_type(diskType)
  45. {}
  46. std::string name;
  47. DiskTypeEnum disc_type;
  48. };
  49. // front-end: define the FSM structure
  50. struct player_ : public msm::front::state_machine_def<player_>
  51. {
  52. // The list of FSM states
  53. struct Empty : public msm::front::state<>
  54. {
  55. // every (optional) entry/exit methods get the event passed.
  56. template <class Event,class FSM>
  57. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  58. template <class Event,class FSM>
  59. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  60. };
  61. struct Open : public msm::front::state<>
  62. {
  63. template <class Event,class FSM>
  64. void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
  65. template <class Event,class FSM>
  66. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  67. };
  68. // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
  69. struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
  70. {
  71. template <class Event,class FSM>
  72. void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
  73. template <class Event,class FSM>
  74. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  75. void set_sm_ptr(player_* pl)
  76. {
  77. m_player=pl;
  78. }
  79. player_* m_player;
  80. };
  81. struct Playing : public msm::front::state<>
  82. {
  83. template <class Event,class FSM>
  84. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  85. template <class Event,class FSM>
  86. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  87. };
  88. // state not defining any entry or exit
  89. struct Paused : public msm::front::state<>
  90. {
  91. };
  92. // the initial state of the player SM. Must be defined
  93. typedef Empty initial_state;
  94. // transition actions
  95. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  96. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  97. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  98. void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
  99. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  100. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  101. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  102. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  103. void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
  104. // guard conditions
  105. bool good_disk_format(cd_detected const& evt)
  106. {
  107. // to test a guard condition, let's say we understand only CDs, not DVD
  108. if (evt.disc_type != DISK_CD)
  109. {
  110. std::cout << "wrong disk, sorry" << std::endl;
  111. return false;
  112. }
  113. return true;
  114. }
  115. // used to show a transition conflict. This guard will simply deactivate one transition and thus
  116. // solve the conflict
  117. bool auto_start(cd_detected const&)
  118. {
  119. return false;
  120. }
  121. typedef player_ p; // makes transition table cleaner
  122. // Transition table for player
  123. struct transition_table : mpl::vector<
  124. // Start Event Next Action Guard
  125. // +---------+-------------+---------+---------------------+----------------------+
  126. a_row < Stopped , play , Playing , &p::start_playback >,
  127. a_row < Stopped , open_close , Open , &p::open_drawer >,
  128. _row < Stopped , stop , Stopped >,
  129. // +---------+-------------+---------+---------------------+----------------------+
  130. a_row < Open , open_close , Empty , &p::close_drawer >,
  131. // +---------+-------------+---------+---------------------+----------------------+
  132. a_row < Empty , open_close , Open , &p::open_drawer >,
  133. row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
  134. row < Empty , cd_detected , Playing , &p::store_cd_info ,&p::auto_start >,
  135. // +---------+-------------+---------+---------------------+----------------------+
  136. a_row < Playing , stop , Stopped , &p::stop_playback >,
  137. a_row < Playing , pause , Paused , &p::pause_playback >,
  138. a_row < Playing , open_close , Open , &p::stop_and_open >,
  139. // +---------+-------------+---------+---------------------+----------------------+
  140. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  141. a_row < Paused , stop , Stopped , &p::stop_playback >,
  142. a_row < Paused , open_close , Open , &p::stop_and_open >
  143. // +---------+-------------+---------+---------------------+----------------------+
  144. > {};
  145. // Replaces the default no-transition response.
  146. template <class FSM,class Event>
  147. void no_transition(Event const& e, FSM&,int state)
  148. {
  149. std::cout << "no transition from state " << state
  150. << " on event " << typeid(e).name() << std::endl;
  151. }
  152. // transition table is already an incidence list;
  153. // select Edge, Source, Target = pair<Start,Event>, Start, Next
  154. // making Start part of Edge is necessary because Edge tags have to be unique
  155. template<typename Row>
  156. struct row_to_incidence :
  157. mpl::vector<mpl::pair<typename Row::Target, typename Row::Evt>, typename Row::Source, typename Row::Target> {};
  158. typedef mpl::fold<player_::transition_table,
  159. mpl::vector<>,
  160. mpl::push_back<mpl::_1, row_to_incidence<mpl::_2> > >::type
  161. transition_incidence_list;
  162. typedef mpl_graph::incidence_list_graph<transition_incidence_list>
  163. transition_graph;
  164. struct preordering_dfs_visitor : mpl_graph::dfs_default_visitor_operations {
  165. template<typename Node, typename Graph, typename State>
  166. struct discover_vertex :
  167. mpl::push_back<State, Node>
  168. {};
  169. };
  170. typedef mpl::first<mpl_graph::
  171. depth_first_search<transition_graph,
  172. preordering_dfs_visitor,
  173. mpl::vector<>,
  174. player_::initial_state>::type>::type
  175. dfs_from_initial_state;
  176. BOOST_MPL_ASSERT(( mpl::equal<dfs_from_initial_state,
  177. mpl::vector<Empty,Open,Stopped,Playing,Paused> > ));
  178. struct preordering_bfs_visitor : mpl_graph::bfs_default_visitor_operations {
  179. template<typename Node, typename Graph, typename State>
  180. struct discover_vertex :
  181. mpl::push_back<State, Node>
  182. {};
  183. };
  184. typedef mpl::first<mpl_graph::
  185. breadth_first_search<transition_graph,
  186. preordering_bfs_visitor,
  187. mpl::vector<>,
  188. player_::initial_state>::type>::type
  189. bfs_from_initial_state;
  190. // yawn, BFS happens to produce the same result as DFS for this example
  191. BOOST_MPL_ASSERT(( mpl::equal<bfs_from_initial_state,
  192. mpl::vector<Empty,Open,Stopped,Playing,Paused> > ));
  193. };
  194. // Pick a back-end
  195. typedef msm::back::state_machine<player_> player;
  196. //
  197. // Testing utilities.
  198. //
  199. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  200. void pstate(player const& p)
  201. {
  202. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  203. }
  204. void test()
  205. {
  206. player p;
  207. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  208. p.start();
  209. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  210. p.process_event(open_close()); pstate(p);
  211. p.process_event(open_close()); pstate(p);
  212. // will be rejected, wrong disk type
  213. p.process_event(
  214. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  215. p.process_event(
  216. cd_detected("louie, louie",DISK_CD)); pstate(p);
  217. p.process_event(play());
  218. // at this point, Play is active
  219. p.process_event(pause()); pstate(p);
  220. // go back to Playing
  221. p.process_event(end_pause()); pstate(p);
  222. p.process_event(pause()); pstate(p);
  223. p.process_event(stop()); pstate(p);
  224. // event leading to the same state
  225. // no action method called as it is not present in the transition table
  226. p.process_event(stop()); pstate(p);
  227. }
  228. }
  229. int main()
  230. {
  231. test();
  232. return 0;
  233. }