OrthogonalDeferredEuml2.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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_ << no_configure_
  46. ),
  47. Empty)
  48. BOOST_MSM_EUML_STATE(( Open_Entry,
  49. Open_Exit,
  50. attributes_ << no_attributes_,
  51. configure_<< CDLoaded // 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. // we now defer using the defer_ function. This will need deferred_events as config (see below)
  123. Empty + play / defer_ ,
  124. // +------------------------------------------------------------------------------+
  125. Open == Empty + open_close / open_drawer ,
  126. Open == Paused + open_close / stop_and_open ,
  127. Open == Stopped + open_close / open_drawer ,
  128. Open == Playing + open_close / stop_and_open ,
  129. // we now defer using the defer_ function. This will need deferred_events as config (see below)
  130. Open + play / defer_ ,
  131. // +------------------------------------------------------------------------------+
  132. Paused == Playing + pause / pause_playback ,
  133. // +------------------------------------------------------------------------------+
  134. Stopped == Playing + stop / stop_playback ,
  135. Stopped == Paused + stop / stop_playback ,
  136. Stopped == Empty + cd_detected [good_disk_format&&
  137. (event_(cd_type)==Int_<DISK_CD>())]
  138. / (store_cd_info,process_(play)),
  139. Stopped == Stopped + stop ,
  140. ErrorMode == AllOk + error_found / report_error ,
  141. AllOk == ErrorMode+ end_error / report_end_error
  142. // +------------------------------------------------------------------------------+
  143. ),transition_table)
  144. // create a state machine "on the fly"
  145. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  146. init_ << Empty << AllOk, // Init State
  147. no_action, // Entry
  148. no_action, // Exit
  149. attributes_ << no_attributes_, // Attributes
  150. configure_ << deferred_events, // configuration
  151. Log_No_Transition // no_transition handler
  152. ),
  153. player_) //fsm name
  154. // choice of back-end
  155. typedef msm::back::state_machine<player_> player;
  156. //
  157. // Testing utilities.
  158. //
  159. static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing","AllOk","ErrorMode" };
  160. void pstate(player const& p)
  161. {
  162. // we have now several active states, which we show
  163. for (unsigned int i=0;i<player::nr_regions::value;++i)
  164. {
  165. std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
  166. }
  167. }
  168. void test()
  169. {
  170. player p;
  171. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  172. p.start();
  173. // tests some flags
  174. std::cout << "CDLoaded active:" << std::boolalpha
  175. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(CDLoaded)>() << std::endl; //=> false (no CD yet)
  176. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  177. p.process_event(open_close); pstate(p);
  178. p.process_event(open_close); pstate(p);
  179. // will be rejected, wrong disk type
  180. p.process_event(
  181. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  182. p.process_event(
  183. cd_detected("louie, louie",DISK_CD)); pstate(p);
  184. // no need to call play as the previous event does it in its action method
  185. //p.process_event(play);
  186. // at this point, Play is active
  187. p.process_event(pause); pstate(p);
  188. std::cout << "PlayingPaused active:" << std::boolalpha
  189. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(PlayingPaused)>() << std::endl;//=> true
  190. // go back to Playing
  191. p.process_event(end_pause); pstate(p);
  192. p.process_event(pause); pstate(p);
  193. p.process_event(stop); pstate(p);
  194. std::cout << "PlayingPaused active:" << std::boolalpha
  195. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(PlayingPaused)>() << std::endl;//=> false
  196. std::cout << "CDLoaded active:" << std::boolalpha
  197. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(CDLoaded)>() << std::endl;//=> true
  198. // by default, the flags are OR'ed but you can also use AND. Then the flag must be present in
  199. // all of the active states
  200. std::cout << "CDLoaded active with AND:" << std::boolalpha
  201. << p.is_flag_active<BOOST_MSM_EUML_FLAG_NAME(CDLoaded),player::Flag_AND>() << std::endl;//=> false
  202. // event leading to the same state
  203. // no action method called as none is defined in the transition table
  204. p.process_event(stop); pstate(p);
  205. // event leading to a terminal/interrupt state
  206. p.process_event(error_found); pstate(p);
  207. // try generating more events
  208. std::cout << "Trying to generate another event" << std::endl; // will not work, fsm is terminated or interrupted
  209. p.process_event(play);pstate(p);
  210. std::cout << "Trying to end the error" << std::endl; // will work only if ErrorMode is interrupt state
  211. p.process_event(end_error);pstate(p);
  212. std::cout << "Trying to generate another event" << std::endl; // will work only if ErrorMode is interrupt state
  213. p.process_event(play);pstate(p);
  214. }
  215. }
  216. int main()
  217. {
  218. test();
  219. return 0;
  220. }