CompositeTutorialEuml.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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(region2_evt)
  30. // A "complicated" event type that carries some data.
  31. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  32. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  33. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  34. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  35. // Concrete FSM implementation
  36. // The list of FSM states
  37. // state not defining any entry or exit
  38. BOOST_MSM_EUML_STATE((),Paused)
  39. BOOST_MSM_EUML_STATE(( Empty_Entry,Empty_Exit ),Empty)
  40. BOOST_MSM_EUML_STATE(( Open_Entry,Open_Exit ),Open)
  41. BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
  42. // Playing is now a state machine itself.
  43. // It has 5 substates
  44. BOOST_MSM_EUML_STATE(( Song1_Entry,Song1_Exit ),Song1)
  45. BOOST_MSM_EUML_STATE(( Song2_Entry,Song2_Exit ),Song2)
  46. BOOST_MSM_EUML_STATE(( Song3_Entry,Song3_Exit ),Song3)
  47. BOOST_MSM_EUML_STATE(( Region2State1_Entry,Region2State1_Exit ),Region2State1)
  48. BOOST_MSM_EUML_STATE(( Region2State2_Entry,Region2State2_Exit ),Region2State2)
  49. // Playing has a transition table
  50. BOOST_MSM_EUML_TRANSITION_TABLE((
  51. // +------------------------------------------------------------------------------+
  52. Song2 == Song1 + next_song / start_next_song,
  53. Song1 == Song2 + previous_song / start_prev_song,
  54. Song3 == Song2 + next_song / start_next_song,
  55. Song2 == Song3 + previous_song / start_prev_song,
  56. Region2State2 == Region2State1 + region2_evt
  57. // +------------------------------------------------------------------------------+
  58. ),playing_transition_table )
  59. BOOST_MSM_EUML_DECLARE_STATE_MACHINE( (playing_transition_table, //STT
  60. init_ << Song1 << Region2State1 // Init State
  61. ),Playing_)
  62. // choice of back-end
  63. typedef msm::back::state_machine<Playing_> Playing_type;
  64. Playing_type const Playing;
  65. // guard conditions
  66. BOOST_MSM_EUML_ACTION(good_disk_format)
  67. {
  68. template <class FSM,class EVT,class SourceState,class TargetState>
  69. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  70. {
  71. // to test a guard condition, let's say we understand only CDs, not DVD
  72. if (evt.get_attribute(cd_type)!=DISK_CD)
  73. {
  74. std::cout << "wrong disk, sorry" << std::endl;
  75. // just for logging, does not block any transition
  76. return true;
  77. }
  78. std::cout << "good disk" << std::endl;
  79. return true;
  80. }
  81. };
  82. // replaces the old transition table
  83. BOOST_MSM_EUML_TRANSITION_TABLE((
  84. Playing == Stopped + play / start_playback ,
  85. Playing == Paused + end_pause / resume_playback,
  86. // +------------------------------------------------------------------------------+
  87. Empty == Open + open_close / close_drawer,
  88. // +------------------------------------------------------------------------------+
  89. Open == Empty + open_close / open_drawer,
  90. Open == Paused + open_close / stop_and_open,
  91. Open == Stopped + open_close / open_drawer,
  92. Open == Playing + open_close / stop_and_open,
  93. // +------------------------------------------------------------------------------+
  94. Paused == Playing + pause / pause_playback,
  95. // +------------------------------------------------------------------------------+
  96. Stopped == Playing + stop / stop_playback,
  97. Stopped == Paused + stop / stop_playback,
  98. Stopped == Empty + cd_detected [good_disk_format &&(event_(cd_type)==Int_<DISK_CD>())]
  99. / (store_cd_info,process_(play)),
  100. Stopped == Stopped + stop
  101. // +------------------------------------------------------------------------------+
  102. ),transition_table)
  103. // create a state machine "on the fly"
  104. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  105. init_ << Empty, // Init State
  106. no_action, // Entry
  107. no_action, // Exit
  108. attributes_ << no_attributes_, // Attributes
  109. configure_ << no_configure_, // configuration
  110. Log_No_Transition // no_transition handler
  111. ),
  112. player_) //fsm name
  113. // choice of back-end
  114. typedef msm::back::state_machine<player_> player;
  115. //
  116. // Testing utilities.
  117. //
  118. static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
  119. void pstate(player const& p)
  120. {
  121. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  122. }
  123. void test()
  124. {
  125. player p;
  126. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  127. p.start();
  128. // note that we write open_close and not open_close(), like usual. Both are possible with eUML, but
  129. // you now have less to type.
  130. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  131. p.process_event(open_close); pstate(p);
  132. p.process_event(open_close); pstate(p);
  133. // will be rejected, wrong disk type
  134. p.process_event(
  135. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  136. p.process_event(
  137. cd_detected("louie, louie",DISK_CD)); pstate(p);
  138. // no need to call play() as the previous event does it in its action method
  139. //p.process_event(play);
  140. // make transition happen inside it. Player has no idea about this event but it's ok.
  141. p.process_event(next_song);pstate(p); //2nd song active
  142. p.process_event(next_song);pstate(p);//3rd song active
  143. p.process_event(previous_song);pstate(p);//2nd song active
  144. // at this point, Play is active
  145. p.process_event(pause); pstate(p);
  146. // go back to Playing
  147. p.process_event(end_pause); pstate(p);
  148. p.process_event(pause); pstate(p);
  149. p.process_event(stop); pstate(p);
  150. // event leading to the same state
  151. // no action method called as none is defined in the transition table
  152. p.process_event(stop); pstate(p);
  153. }
  154. }
  155. int main()
  156. {
  157. test();
  158. return 0;
  159. }