EumlInternal.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. using namespace boost::msm::front;
  17. namespace msm = boost::msm;
  18. // entry/exit/action/guard logging functors
  19. #include "logging_functors.h"
  20. namespace // Concrete FSM implementation
  21. {
  22. // events
  23. BOOST_MSM_EUML_EVENT(play)
  24. BOOST_MSM_EUML_EVENT(end_pause)
  25. BOOST_MSM_EUML_EVENT(stop)
  26. BOOST_MSM_EUML_EVENT(pause)
  27. BOOST_MSM_EUML_EVENT(open_close)
  28. BOOST_MSM_EUML_EVENT(internal_event)
  29. // A "complicated" event type that carries some data.
  30. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  31. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  32. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  33. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  34. // Concrete FSM implementation
  35. // The list of FSM states
  36. BOOST_MSM_EUML_STATE(( Empty_Entry,Empty_Exit ),Empty)
  37. BOOST_MSM_EUML_STATE(( Open_Entry,Open_Exit ),Open)
  38. BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
  39. BOOST_MSM_EUML_STATE(( Playing_Entry,Playing_Exit ),Playing)
  40. // state not needing any entry or exit
  41. BOOST_MSM_EUML_STATE((),Paused)
  42. // guard conditions
  43. BOOST_MSM_EUML_ACTION(good_disk_format)
  44. {
  45. template <class FSM,class EVT,class SourceState,class TargetState>
  46. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  47. {
  48. // to test a guard condition, let's say we understand only CDs, not DVD
  49. if (evt.get_attribute(cd_type)!=DISK_CD)
  50. {
  51. std::cout << "wrong disk, sorry" << std::endl;
  52. // just for logging, does not block any transition
  53. return true;
  54. }
  55. std::cout << "good disk" << std::endl;
  56. return true;
  57. }
  58. };
  59. // replaces the old transition table
  60. BOOST_MSM_EUML_TRANSITION_TABLE((
  61. Playing == Stopped + play / start_playback ,
  62. Playing == Paused + end_pause / resume_playback,
  63. // +------------------------------------------------------------------------------+
  64. Empty == Open + open_close / close_drawer,
  65. // +------------------------------------------------------------------------------+
  66. Open == Empty + open_close / open_drawer,
  67. Open == Paused + open_close / stop_and_open,
  68. Open == Stopped + open_close / open_drawer,
  69. Open == Playing + open_close / stop_and_open,
  70. Open + open_close [internal_guard1] / internal_action1,
  71. Open + open_close [internal_guard2] / internal_action2,
  72. Open + internal_event / internal_action ,
  73. // +------------------------------------------------------------------------------+
  74. Paused == Playing + pause / pause_playback,
  75. // +------------------------------------------------------------------------------+
  76. Stopped == Playing + stop / stop_playback,
  77. Stopped == Paused + stop / stop_playback,
  78. Stopped == Empty + cd_detected [good_disk_format&&
  79. (event_(cd_type)==Int_<DISK_CD>())]
  80. / (store_cd_info,process_(play)),
  81. Stopped == Stopped + stop
  82. // +------------------------------------------------------------------------------+
  83. ),transition_table)
  84. // create a state machine "on the fly"
  85. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  86. init_ << Empty, // Init State
  87. no_action, // Entry
  88. no_action, // Exit
  89. attributes_ << no_attributes_, // Attributes
  90. configure_ << no_configure_, // configuration
  91. Log_No_Transition // no_transition handler
  92. ),
  93. player_) //fsm name
  94. // choice of back-end
  95. typedef msm::back::state_machine<player_> player;
  96. //
  97. // Testing utilities.
  98. //
  99. static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
  100. void pstate(player const& p)
  101. {
  102. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  103. }
  104. void test()
  105. {
  106. player p;
  107. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  108. p.start();
  109. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  110. p.process_event(open_close); pstate(p);
  111. std::cout << "sending internal event (not rejected)" << std::endl;
  112. p.process_event(internal_event);
  113. std::cout << "sending open_close event. Conflict with internal transitions (rejecting event)" << std::endl;
  114. p.process_event(open_close); pstate(p);
  115. // will be rejected, wrong disk type
  116. p.process_event(
  117. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  118. p.process_event(
  119. cd_detected("louie, louie",DISK_CD)); pstate(p);
  120. // no need to call play() as the previous event does it in its action method
  121. //p.process_event(play);
  122. // at this point, Play is active
  123. p.process_event(pause); pstate(p);
  124. // go back to Playing
  125. p.process_event(end_pause); pstate(p);
  126. p.process_event(pause); pstate(p);
  127. p.process_event(stop); pstate(p);
  128. // event leading to the same state
  129. // no action method called as none is defined in the transition table
  130. p.process_event(stop); pstate(p);
  131. // test call to no_transition
  132. p.process_event(pause); pstate(p);
  133. }
  134. }
  135. int main()
  136. {
  137. test();
  138. return 0;
  139. }