OrthogonalDeferredEuml.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. // Copyright 2010 Christophe Henry
  2. // henry UNDERSCORE christophe AT hotmail DOT com
  3. // This is an extended version of the state machine available in the boost::mpl library
  4. // Distributed under the same license as the original.
  5. // Copyright for the original version:
  6. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  7. // under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #include <vector>
  11. #include <iostream>
  12. #include <boost/msm/back/state_machine.hpp>
  13. #include <boost/msm/front/euml/euml.hpp>
  14. using namespace std;
  15. using namespace boost::msm::front::euml;
  16. namespace msm = boost::msm;
  17. // entry/exit/action/guard logging functors
  18. #include "logging_functors.h"
  19. namespace // Concrete FSM implementation
  20. {
  21. // events
  22. BOOST_MSM_EUML_EVENT(play)
  23. BOOST_MSM_EUML_EVENT(end_pause)
  24. BOOST_MSM_EUML_EVENT(stop)
  25. BOOST_MSM_EUML_EVENT(pause)
  26. BOOST_MSM_EUML_EVENT(open_close)
  27. BOOST_MSM_EUML_EVENT(next_song)
  28. BOOST_MSM_EUML_EVENT(previous_song)
  29. BOOST_MSM_EUML_EVENT(end_error)
  30. BOOST_MSM_EUML_EVENT(error_found)
  31. // A "complicated" event type that carries some data.
  32. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  33. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  34. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  35. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  36. // Flags. Allow information about a property of the current state
  37. BOOST_MSM_EUML_FLAG(PlayingPaused)
  38. BOOST_MSM_EUML_FLAG(CDLoaded)
  39. BOOST_MSM_EUML_FLAG(FirstSongPlaying)
  40. // Concrete FSM implementation
  41. // The list of FSM states
  42. BOOST_MSM_EUML_STATE(( Empty_Entry,
  43. Empty_Exit,
  44. attributes_ << no_attributes_,
  45. configure_ << play // defer play
  46. ),
  47. Empty)
  48. BOOST_MSM_EUML_STATE(( Open_Entry,
  49. Open_Exit,
  50. attributes_ << no_attributes_,
  51. configure_<< CDLoaded << play // defer play, flag state with CDLoaded
  52. ),
  53. Open)
  54. BOOST_MSM_EUML_STATE(( Stopped_Entry,
  55. Stopped_Exit,
  56. attributes_ << no_attributes_,
  57. configure_<< CDLoaded // flag state with CDLoaded
  58. ),
  59. Stopped)
  60. // state not defining any entry or exit
  61. BOOST_MSM_EUML_STATE(( no_action,
  62. no_action,
  63. attributes_ << no_attributes_,
  64. configure_<< PlayingPaused << CDLoaded // flag state with CDLoaded and PlayingPaused
  65. ),
  66. Paused)
  67. BOOST_MSM_EUML_STATE(( AllOk_Entry,AllOk_Exit ),AllOk)
  68. // a terminate state
  69. //BOOST_MSM_EUML_TERMINATE_STATE(( ErrorMode_Entry,ErrorMode_Exit ),ErrorMode)
  70. // or as an interrupt state
  71. BOOST_MSM_EUML_INTERRUPT_STATE(( end_error,ErrorMode_Entry,ErrorMode_Exit ),ErrorMode)
  72. // Playing is now a state machine itself.
  73. // It has 3 substates
  74. BOOST_MSM_EUML_STATE(( Song1_Entry,
  75. Song1_Exit,
  76. attributes_ << no_attributes_,
  77. configure_<< FirstSongPlaying ),Song1)
  78. BOOST_MSM_EUML_STATE(( Song2_Entry,Song2_Exit ),Song2)
  79. BOOST_MSM_EUML_STATE(( Song3_Entry,Song3_Exit ),Song3)
  80. // Playing has a transition table
  81. BOOST_MSM_EUML_TRANSITION_TABLE((
  82. // +------------------------------------------------------------------------------+
  83. Song2 == Song1 + next_song / start_next_song,
  84. Song1 == Song2 + previous_song / start_prev_song,
  85. Song3 == Song2 + next_song / start_next_song,
  86. Song2 == Song3 + previous_song / start_prev_song
  87. // +------------------------------------------------------------------------------+
  88. ),playing_transition_table )
  89. BOOST_MSM_EUML_DECLARE_STATE_MACHINE( (playing_transition_table, //STT
  90. init_ << Song1, // Init State
  91. no_action, // entry
  92. no_action, // exit
  93. attributes_ << no_attributes_, //attributes
  94. configure_<< PlayingPaused << CDLoaded //flags
  95. ),Playing_)
  96. // choice of back-end
  97. typedef msm::back::state_machine<Playing_> Playing_type;
  98. Playing_type const Playing;
  99. // guard conditions
  100. BOOST_MSM_EUML_ACTION(good_disk_format)
  101. {
  102. template <class FSM,class EVT,class SourceState,class TargetState>
  103. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  104. {
  105. // to test a guard condition, let's say we understand only CDs, not DVD
  106. if (evt.get_attribute(cd_type)!=DISK_CD)
  107. {
  108. std::cout << "wrong disk, sorry" << std::endl;
  109. // just for logging, does not block any transition
  110. return true;
  111. }
  112. std::cout << "good disk" << std::endl;
  113. return true;
  114. }
  115. };
  116. // replaces the old transition table
  117. BOOST_MSM_EUML_TRANSITION_TABLE((
  118. Playing == Stopped + play / start_playback ,
  119. Playing == Paused + end_pause / resume_playback,
  120. // +------------------------------------------------------------------------------+
  121. Empty == Open + open_close / close_drawer,
  122. // +------------------------------------------------------------------------------+
  123. Open == Empty + open_close / open_drawer,
  124. Open == Paused + open_close / stop_and_open,
  125. Open == Stopped + open_close / open_drawer,
  126. Open == Playing + open_close / stop_and_open,
  127. // +------------------------------------------------------------------------------+
  128. Paused == Playing + pause / pause_playback,
  129. // +------------------------------------------------------------------------------+
  130. Stopped == Playing + stop / stop_playback,
  131. Stopped == Paused + stop / stop_playback,
  132. Stopped == Empty + cd_detected [good_disk_format&&
  133. (event_(cd_type)==Int_<DISK_CD>())]
  134. / (store_cd_info,process_(play)),
  135. Stopped == Stopped + stop,
  136. ErrorMode == AllOk + error_found / report_error,
  137. AllOk == ErrorMode+ end_error / report_end_error
  138. // +------------------------------------------------------------------------------+
  139. ),transition_table)
  140. // create a state machine "on the fly"
  141. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  142. init_ << Empty<< AllOk, // Init State
  143. no_action, // Entry
  144. no_action, // Exit
  145. attributes_ << no_attributes_, // Attributes
  146. configure_ << no_configure_, // configuration
  147. Log_No_Transition // no_transition handler
  148. ),
  149. player_) //fsm name
  150. // choice of back-end
  151. typedef msm::back::state_machine<player_> player;
  152. //
  153. // Testing utilities.
  154. //
  155. static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing","AllOk","ErrorMode" };
  156. void pstate(player const& p)
  157. {
  158. // we have now several active states, which we show
  159. for (unsigned int i=0;i<player::nr_regions::value;++i)
  160. {
  161. std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
  162. }
  163. }
  164. void test()
  165. {
  166. player p;
  167. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  168. p.start();
  169. // tests some flags
  170. std::cout << "CDLoaded active:" << std::boolalpha
  171. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(CDLoaded)>() << std::endl; //=> false (no CD yet)
  172. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  173. p.process_event(open_close); pstate(p);
  174. p.process_event(open_close); pstate(p);
  175. // will be rejected, wrong disk type
  176. p.process_event(
  177. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  178. p.process_event(
  179. cd_detected("louie, louie",DISK_CD)); pstate(p);
  180. // no need to call play as the previous event does it in its action method
  181. //p.process_event(play);
  182. // at this point, Play is active
  183. std::cout << "PlayingPaused active:" << std::boolalpha
  184. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(PlayingPaused)>() << std::endl;//=> true
  185. std::cout << "FirstSong active:" << std::boolalpha
  186. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(FirstSongPlaying)>() << std::endl;//=> true
  187. // make transition happen inside it. Player has no idea about this event but it's ok.
  188. p.process_event(next_song);pstate(p); //2nd song active
  189. p.process_event(next_song);pstate(p);//3rd song active
  190. p.process_event(previous_song);pstate(p);//2nd song active
  191. std::cout << "FirstSong active:" << std::boolalpha
  192. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(FirstSongPlaying)>() << std::endl;//=> false
  193. std::cout << "PlayingPaused active:" << std::boolalpha
  194. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(PlayingPaused)>() << std::endl;//=> true
  195. // at this point, Play is active
  196. p.process_event(pause); pstate(p);
  197. std::cout << "PlayingPaused active:" << std::boolalpha
  198. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(PlayingPaused)>() << std::endl;//=> true
  199. // go back to Playing
  200. p.process_event(end_pause); pstate(p);
  201. p.process_event(pause); pstate(p);
  202. p.process_event(stop); pstate(p);
  203. std::cout << "PlayingPaused active:" << std::boolalpha
  204. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(PlayingPaused)>() << std::endl;//=> false
  205. std::cout << "CDLoaded active:" << std::boolalpha
  206. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(CDLoaded)>() << std::endl;//=> true
  207. // by default, the flags are OR'ed but you can also use AND. Then the flag must be present in
  208. // all of the active states
  209. std::cout << "CDLoaded active with AND:" << std::boolalpha
  210. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(CDLoaded),player::Flag_AND>() << std::endl;//=> false
  211. // event leading to the same state
  212. // no action method called as none is defined in the transition table
  213. p.process_event(stop); pstate(p);
  214. // event leading to a terminal/interrupt state
  215. p.process_event(error_found); pstate(p);
  216. // try generating more events
  217. std::cout << "Trying to generate another event" << std::endl; // will not work, fsm is terminated or interrupted
  218. p.process_event(play);pstate(p);
  219. std::cout << "Trying to end the error" << std::endl; // will work only if ErrorMode is interrupt state
  220. p.process_event(end_error);pstate(p);
  221. std::cout << "Trying to generate another event" << std::endl; // will work only if ErrorMode is interrupt state
  222. p.process_event(play);pstate(p);
  223. }
  224. }
  225. int main()
  226. {
  227. test();
  228. return 0;
  229. }