SimpleTutorialEuml.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // A "complicated" event type that carries some data.
  28. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  29. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  30. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  31. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  32. // Concrete FSM implementation
  33. // The list of FSM states
  34. // state not needing any entry or exit
  35. BOOST_MSM_EUML_STATE((),Paused)
  36. // it is also possible to define a state which you can implement normally
  37. // just make it a state, as usual, and also a grammar terminal, euml_state
  38. struct Empty_impl : public msm::front::state<> , public euml_state<Empty_impl>
  39. {
  40. // this allows us to add some functions
  41. void activate_empty() {std::cout << "switching to Empty " << std::endl;}
  42. // standard entry behavior
  43. template <class Event,class FSM>
  44. void on_entry(Event const& evt,FSM& fsm)
  45. {
  46. std::cout << "entering: Empty" << std::endl;
  47. }
  48. template <class Event,class FSM>
  49. void on_exit(Event const& evt,FSM& fsm)
  50. {
  51. std::cout << "leaving: Empty" << std::endl;
  52. }
  53. };
  54. //instance for use in the transition table
  55. Empty_impl const Empty;
  56. // create a functor and a eUML function for the activate_empty method from Entry
  57. BOOST_MSM_EUML_METHOD(ActivateEmpty_ , activate_empty , activate_empty_ , void , void )
  58. // define more states
  59. BOOST_MSM_EUML_STATE(( Open_Entry,Open_Exit ),Open)
  60. BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
  61. BOOST_MSM_EUML_STATE(( Playing_Entry,Playing_Exit ),Playing)
  62. // guard conditions
  63. BOOST_MSM_EUML_ACTION(good_disk_format)
  64. {
  65. template <class FSM,class EVT,class SourceState,class TargetState>
  66. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  67. {
  68. // to test a guard condition, let's say we understand only CDs, not DVD
  69. if (evt.get_attribute(cd_type)!=DISK_CD)
  70. {
  71. std::cout << "wrong disk, sorry" << std::endl;
  72. // just for logging, does not block any transition
  73. return true;
  74. }
  75. std::cout << "good disk" << std::endl;
  76. return true;
  77. }
  78. };
  79. // it is also possible to use a plain functor, with default-constructor in the transition table
  80. struct start_play
  81. {
  82. template <class FSM,class EVT,class SourceState,class TargetState>
  83. void operator()(EVT const& ,FSM&,SourceState& ,TargetState& )
  84. {
  85. cout << "player::start_play" << endl;
  86. }
  87. };
  88. // replaces the old transition table
  89. BOOST_MSM_EUML_TRANSITION_TABLE((
  90. Playing == Stopped + play / start_play() ,
  91. Playing == Paused + end_pause / resume_playback,
  92. // +------------------------------------------------------------------------------+
  93. Empty == Open + open_close / (close_drawer,activate_empty_(target_)),
  94. // +------------------------------------------------------------------------------+
  95. Open == Empty + open_close / open_drawer,
  96. Open == Paused + open_close / stop_and_open,
  97. Open == Stopped + open_close / open_drawer,
  98. Open == Playing + open_close / stop_and_open,
  99. // +------------------------------------------------------------------------------+
  100. Paused == Playing + pause / pause_playback,
  101. // +------------------------------------------------------------------------------+
  102. Stopped == Playing + stop / stop_playback,
  103. Stopped == Paused + stop / stop_playback,
  104. Stopped == Empty + cd_detected [good_disk_format &&
  105. (event_(cd_type)==Int_<DISK_CD>())]
  106. / (store_cd_info,process_(play)),
  107. Stopped == Stopped + stop
  108. // +------------------------------------------------------------------------------+
  109. ),transition_table)
  110. // create a state machine "on the fly"
  111. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  112. init_ << Empty, // Init State
  113. no_action, // Entry
  114. no_action, // Exit
  115. attributes_ << no_attributes_, // Attributes
  116. configure_ << no_configure_, // configuration
  117. Log_No_Transition // no_transition handler
  118. ),
  119. player_) //fsm name
  120. // or simply, if no no_transition handler needed:
  121. //BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  122. // Empty // Init State
  123. // ),player_)
  124. // choice of back-end
  125. typedef msm::back::state_machine<player_> player;
  126. //
  127. // Testing utilities.
  128. //
  129. static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
  130. void pstate(player const& p)
  131. {
  132. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  133. }
  134. void test()
  135. {
  136. player p;
  137. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  138. p.start();
  139. // note that we write open_close and not open_close(), like usual. Both are possible with eUML, but
  140. // you now have less to type.
  141. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  142. p.process_event(open_close); pstate(p);
  143. p.process_event(open_close); pstate(p);
  144. // will be rejected, wrong disk type
  145. p.process_event(
  146. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  147. p.process_event(
  148. cd_detected("louie, louie",DISK_CD)); pstate(p);
  149. // no need to call play as the previous event does it in its action method
  150. //p.process_event(play);
  151. // at this point, Play is active
  152. p.process_event(pause); pstate(p);
  153. // go back to Playing
  154. p.process_event(end_pause); pstate(p);
  155. p.process_event(pause); pstate(p);
  156. p.process_event(stop); pstate(p);
  157. // event leading to the same state
  158. // no action method called as none is defined in the transition table
  159. p.process_event(stop); pstate(p);
  160. // test call to no_transition
  161. p.process_event(pause); pstate(p);
  162. }
  163. }
  164. int main()
  165. {
  166. test();
  167. return 0;
  168. }