SimpleInternalEuml.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 <iostream>
  11. #include <boost/msm/back/state_machine.hpp>
  12. #include <boost/msm/front/euml/euml.hpp>
  13. #ifndef BOOST_MSM_NONSTANDALONE_TEST
  14. #define BOOST_TEST_MODULE MyTest
  15. #endif
  16. #include <boost/test/unit_test.hpp>
  17. using namespace std;
  18. using namespace boost::msm::front::euml;
  19. namespace msm = boost::msm;
  20. namespace
  21. {
  22. // A "complicated" event type that carries some data.
  23. enum DiskTypeEnum
  24. {
  25. DISK_CD=0,
  26. DISK_DVD=1
  27. };
  28. // events
  29. BOOST_MSM_EUML_EVENT(play)
  30. BOOST_MSM_EUML_EVENT(end_pause)
  31. BOOST_MSM_EUML_EVENT(stop)
  32. BOOST_MSM_EUML_EVENT(pause)
  33. BOOST_MSM_EUML_EVENT(open_close)
  34. BOOST_MSM_EUML_EVENT(internal_evt)
  35. BOOST_MSM_EUML_EVENT(to_ignore)
  36. // A "complicated" event type that carries some data.
  37. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  38. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  39. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  40. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  41. //states
  42. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,entry_counter)
  43. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,exit_counter)
  44. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Open)
  45. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Stopped)
  46. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Playing)
  47. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Paused)
  48. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,empty_internal_guard_counter)
  49. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,empty_internal_action_counter)
  50. BOOST_MSM_EUML_ACTION(internal_guard_fct)
  51. {
  52. template <class FSM,class EVT,class SourceState,class TargetState>
  53. bool operator()(EVT const&, FSM& ,SourceState& src,TargetState& )
  54. {
  55. ++src.get_attribute(empty_internal_guard_counter);
  56. return false;
  57. }
  58. };
  59. BOOST_MSM_EUML_DECLARE_STATE((++state_(entry_counter),++state_(exit_counter),
  60. attributes_ << entry_counter << exit_counter
  61. << empty_internal_guard_counter << empty_internal_action_counter),Empty_def)
  62. // derive to be able to add an internal transition table
  63. struct Empty_impl : public Empty_def
  64. {
  65. Empty_impl(){}
  66. BOOST_MSM_EUML_DECLARE_INTERNAL_TRANSITION_TABLE((
  67. internal_evt [internal_guard_fct] / ++source_(empty_internal_action_counter)
  68. ))
  69. };
  70. // declare an instance for the stt as we are manually declaring a state
  71. Empty_impl const Empty;
  72. //fsm
  73. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,start_playback_counter)
  74. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,can_close_drawer_counter)
  75. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,internal_action_counter)
  76. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,internal_guard_counter)
  77. BOOST_MSM_EUML_ACTION(No_Transition)
  78. {
  79. template <class FSM,class Event>
  80. void operator()(Event const&,FSM&,int)
  81. {
  82. BOOST_FAIL("no_transition called!");
  83. }
  84. };
  85. BOOST_MSM_EUML_ACTION(good_disk_format)
  86. {
  87. template <class FSM,class EVT,class SourceState,class TargetState>
  88. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  89. {
  90. if (evt.get_attribute(cd_type)!=DISK_CD)
  91. {
  92. return false;
  93. }
  94. return true;
  95. }
  96. };
  97. BOOST_MSM_EUML_ACTION(internal_guard)
  98. {
  99. template <class FSM,class EVT,class SourceState,class TargetState>
  100. bool operator()(EVT const&,FSM& fsm,SourceState& ,TargetState& )
  101. {
  102. ++fsm.get_attribute(internal_guard_counter);
  103. return false;
  104. }
  105. };
  106. BOOST_MSM_EUML_ACTION(internal_guard2)
  107. {
  108. template <class FSM,class EVT,class SourceState,class TargetState>
  109. bool operator()(EVT const&,FSM& fsm,SourceState& ,TargetState& )
  110. {
  111. ++fsm.get_attribute(internal_guard_counter);
  112. return true;
  113. }
  114. };
  115. BOOST_MSM_EUML_TRANSITION_TABLE((
  116. Playing == Stopped + play / ++fsm_(start_playback_counter) ,
  117. Playing == Paused + end_pause ,
  118. // +------------------------------------------------------------------------------+
  119. Empty == Open + open_close / ++fsm_(can_close_drawer_counter),
  120. Empty + to_ignore ,
  121. Empty + internal_evt [internal_guard2] / ++fsm_(internal_action_counter) ,
  122. Empty + cd_detected [internal_guard] ,
  123. // +------------------------------------------------------------------------------+
  124. Open == Empty + open_close ,
  125. Open == Paused + open_close ,
  126. Open == Stopped + open_close ,
  127. Open == Playing + open_close ,
  128. // +------------------------------------------------------------------------------+
  129. Paused == Playing + pause ,
  130. // +------------------------------------------------------------------------------+
  131. Stopped == Playing + stop ,
  132. Stopped == Paused + stop ,
  133. Stopped == Empty + cd_detected [good_disk_format] ,
  134. Stopped == Stopped + stop
  135. // +------------------------------------------------------------------------------+
  136. ),transition_table)
  137. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  138. init_ << Empty, // Init State
  139. no_action, // Entry
  140. no_action, // Exit
  141. attributes_ << start_playback_counter << can_close_drawer_counter
  142. << internal_action_counter << internal_guard_counter, // Attributes
  143. configure_ << no_configure_, // configuration
  144. No_Transition // no_transition handler
  145. ),
  146. player_) //fsm name
  147. typedef msm::back::state_machine<player_> player;
  148. // static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
  149. BOOST_AUTO_TEST_CASE( my_test )
  150. {
  151. player p;
  152. p.start();
  153. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(entry_counter) == 1,
  154. "Empty entry not called correctly");
  155. // internal events
  156. p.process_event(to_ignore);
  157. p.process_event(internal_evt);
  158. BOOST_CHECK_MESSAGE(p.get_attribute(internal_action_counter) == 1,"Internal action not called correctly");
  159. BOOST_CHECK_MESSAGE(p.get_attribute(internal_guard_counter) == 1,"Internal guard not called correctly");
  160. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(empty_internal_action_counter) == 0,
  161. "Empty internal action not called correctly");
  162. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(empty_internal_guard_counter) == 1,
  163. "Empty internal guard not called correctly");
  164. p.process_event(open_close());
  165. BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Open should be active"); //Open
  166. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(exit_counter) == 1,
  167. "Empty exit not called correctly");
  168. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(entry_counter) == 1,
  169. "Open entry not called correctly");
  170. p.process_event(open_close());
  171. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Empty should be active"); //Empty
  172. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Open)&>().get_attribute(exit_counter) == 1,
  173. "Open exit not called correctly");
  174. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(entry_counter) == 2,
  175. "Empty entry not called correctly");
  176. BOOST_CHECK_MESSAGE(p.get_attribute(can_close_drawer_counter) == 1,"guard not called correctly");
  177. p.process_event(
  178. cd_detected("louie, louie",DISK_DVD));
  179. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Empty should be active"); //Empty
  180. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Open)&>().get_attribute(exit_counter) == 1,
  181. "Open exit not called correctly");
  182. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(entry_counter) == 2,
  183. "Empty entry not called correctly");
  184. p.process_event(
  185. cd_detected("louie, louie",DISK_CD));
  186. p.process_event(play);
  187. BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
  188. BOOST_CHECK_MESSAGE(p.get_state<Empty_impl&>().get_attribute(exit_counter) == 2,
  189. "Empty exit not called correctly");
  190. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 1,
  191. "Stopped entry not called correctly");
  192. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(exit_counter) == 1,
  193. "Stopped exit not called correctly");
  194. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(entry_counter) == 1,
  195. "Playing entry not called correctly");
  196. BOOST_CHECK_MESSAGE(p.get_attribute(start_playback_counter) == 1,"action not called correctly");
  197. p.process_event(pause());
  198. BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Paused should be active"); //Paused
  199. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(exit_counter) == 1,
  200. "Playing exit not called correctly");
  201. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(entry_counter) == 1,
  202. "Paused entry not called correctly");
  203. // go back to Playing
  204. p.process_event(end_pause());
  205. BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
  206. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(exit_counter) == 1,
  207. "Paused exit not called correctly");
  208. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(entry_counter) == 2,
  209. "Playing entry not called correctly");
  210. p.process_event(pause());
  211. BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Paused should be active"); //Paused
  212. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(exit_counter) == 2,
  213. "Playing exit not called correctly");
  214. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(entry_counter) == 2,
  215. "Paused entry not called correctly");
  216. p.process_event(stop());
  217. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  218. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(exit_counter) == 2,
  219. "Paused exit not called correctly");
  220. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 2,
  221. "Stopped entry not called correctly");
  222. p.process_event(stop());
  223. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  224. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(exit_counter) == 2,
  225. "Stopped exit not called correctly");
  226. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 3,
  227. "Stopped entry not called correctly");
  228. }
  229. }