TestInternal.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/mpl/vector/vector30.hpp"
  12. // back-end
  13. #include <boost/msm/back/state_machine.hpp>
  14. //front-end
  15. #include <boost/msm/front/state_machine_def.hpp>
  16. #include <boost/msm/front/functor_row.hpp>
  17. #include <boost/msm/front/internal_row.hpp>
  18. namespace msm = boost::msm;
  19. namespace mpl = boost::mpl;
  20. using namespace msm::front;
  21. namespace
  22. {
  23. // events
  24. struct play {};
  25. struct end_pause {};
  26. struct stop {};
  27. struct pause {};
  28. struct internal_event {};
  29. struct open_close {};
  30. struct NextSong {};
  31. struct PreviousSong {};
  32. struct to_ignore {};
  33. // A "complicated" event type that carries some data.
  34. enum DiskTypeEnum
  35. {
  36. DISK_CD=0,
  37. DISK_DVD=1
  38. };
  39. struct cd_detected
  40. {
  41. cd_detected(std::string name, DiskTypeEnum diskType)
  42. : name(name),
  43. disc_type(diskType)
  44. {}
  45. std::string name;
  46. DiskTypeEnum disc_type;
  47. };
  48. // front-end: define the FSM structure
  49. struct player_ : public msm::front::state_machine_def<player_>
  50. {
  51. // transition actions
  52. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  53. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  54. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  55. void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
  56. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  57. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  58. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  59. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  60. void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
  61. // guard conditions
  62. bool good_disk_format(cd_detected const& evt)
  63. {
  64. // to test a guard condition, let's say we understand only CDs, not DVD
  65. if (evt.disc_type != DISK_CD)
  66. {
  67. std::cout << "wrong disk, sorry" << std::endl;
  68. return false;
  69. }
  70. return true;
  71. }
  72. // transitions internal to Empty
  73. void internal_action(cd_detected const&){ std::cout << "Empty::internal action\n"; }
  74. bool internal_guard(cd_detected const&)
  75. {
  76. std::cout << "Empty::internal guard\n";
  77. return false;
  78. }
  79. void internal_action(internal_event const&){ std::cout << "Playing::internal action\n"; }
  80. bool internal_guard(internal_event const&)
  81. {
  82. std::cout << "Playing::internal guard\n";
  83. return false;
  84. }
  85. // The list of FSM states
  86. struct Empty : public msm::front::state<>
  87. {
  88. // every (optional) entry/exit methods get the event passed.
  89. template <class Event,class FSM>
  90. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  91. template <class Event,class FSM>
  92. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  93. struct internal_guard_fct
  94. {
  95. template <class EVT,class FSM,class SourceState,class TargetState>
  96. bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
  97. {
  98. std::cout << "Empty::internal guard functor\n";
  99. return false;
  100. }
  101. };
  102. struct internal_action_fct
  103. {
  104. template <class EVT,class FSM,class SourceState,class TargetState>
  105. void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& )
  106. {
  107. std::cout << "Empty::internal action functor" << std::endl;
  108. }
  109. };
  110. void internal_action(to_ignore const&) { std::cout << "Empty::(almost)ignoring event\n"; }
  111. // Transition table for Empty
  112. struct internal_transition_table : mpl::vector<
  113. // Start Event Next Action Guard
  114. Row < Empty , cd_detected , none , internal_action_fct ,internal_guard_fct >,
  115. Internal < cd_detected , internal_action_fct ,internal_guard_fct >,
  116. a_internal< to_ignore , Empty,&Empty::internal_action >
  117. // +---------+-------------+----------+------------------------+----------------------+
  118. > {};
  119. };
  120. struct Open : public msm::front::state<>
  121. {
  122. template <class Event,class FSM>
  123. void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
  124. template <class Event,class FSM>
  125. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  126. };
  127. struct Stopped : public msm::front::state<>
  128. {
  129. template <class Event,class FSM>
  130. void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
  131. template <class Event,class FSM>
  132. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  133. };
  134. struct Playing_ : public msm::front::state_machine_def<Playing_>
  135. {
  136. // when playing, the CD is loaded and we are in either pause or playing (duh)
  137. template <class Event,class FSM>
  138. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  139. template <class Event,class FSM>
  140. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  141. // The list of FSM states
  142. struct Song1 : public msm::front::state<>
  143. {
  144. template <class Event,class FSM>
  145. void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
  146. template <class Event,class FSM>
  147. void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
  148. };
  149. struct Song2 : public msm::front::state<>
  150. {
  151. template <class Event,class FSM>
  152. void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
  153. template <class Event,class FSM>
  154. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
  155. };
  156. struct Song3 : public msm::front::state<>
  157. {
  158. template <class Event,class FSM>
  159. void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
  160. template <class Event,class FSM>
  161. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
  162. };
  163. // the initial state. Must be defined
  164. typedef Song1 initial_state;
  165. // transition actions
  166. void start_next_song(NextSong const&){std::cout << "Playing: start_next_song" << std::endl;}
  167. void start_prev_song(PreviousSong const&){std::cout << "Playing: start_prev_song" << std::endl;}
  168. // guard conditions
  169. struct playing_internal_guard
  170. {
  171. template <class EVT,class FSM,class SourceState,class TargetState>
  172. bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
  173. {
  174. std::cout << "Playing::internal guard fct\n";
  175. return true;
  176. }
  177. };
  178. struct playing_false_guard
  179. {
  180. template <class EVT,class FSM,class SourceState,class TargetState>
  181. bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
  182. {
  183. std::cout << "Playing::false guard\n";
  184. return false;
  185. }
  186. };
  187. struct playing_internal_fct
  188. {
  189. template <class EVT,class FSM,class SourceState,class TargetState>
  190. void operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& )
  191. {
  192. std::cout << "Playing::internal fct\n";
  193. }
  194. };
  195. typedef Playing_ pl; // makes transition table cleaner
  196. // Transition table for Playing
  197. struct transition_table : mpl::vector4<
  198. // Start Event Next Action Guard
  199. // +---------+---------------+---------+---------------------+----------------------+
  200. a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
  201. a_row < Song2 , PreviousSong , Song1 , &pl::start_prev_song >,
  202. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  203. a_row < Song3 , PreviousSong , Song2 , &pl::start_prev_song >
  204. // +---------+-------------+---------+---------------------+----------------------+
  205. > {};
  206. // Internal transition table for Playing
  207. // +---------+----------------+---------+---------------------+-----------------------+
  208. struct internal_transition_table : mpl::vector<
  209. // normal internal transition
  210. // Start Event Next Action Guard
  211. Internal < internal_event , playing_internal_fct,playing_internal_guard >,
  212. // conflict between internal and the external defined above
  213. Internal < PreviousSong , playing_internal_fct,playing_false_guard >,
  214. internal < internal_event , player_,&player_::internal_action,
  215. player_,&player_::internal_guard >
  216. // +---------+----------------+---------+---------------------+-----------------------+
  217. > {};
  218. // Replaces the default no-transition response.
  219. template <class FSM,class Event>
  220. void no_transition(Event const& e, FSM&,int state)
  221. {
  222. std::cout << "no transition from state " << state
  223. << " on event " << typeid(e).name() << std::endl;
  224. }
  225. };
  226. // back-end
  227. typedef msm::back::state_machine<Playing_> Playing;
  228. // state not defining any entry or exit
  229. struct Paused : public msm::front::state<>
  230. {
  231. };
  232. // the initial state of the player SM. Must be defined
  233. typedef Empty initial_state;
  234. typedef player_ p; // makes transition table cleaner
  235. // Transition table for player
  236. struct transition_table : mpl::vector<
  237. // Start Event Next Action Guard
  238. // +---------+-------------+---------+---------------------+----------------------+
  239. a_row < Stopped , play , Playing , &p::start_playback >,
  240. a_row < Stopped , open_close , Open , &p::open_drawer >,
  241. _row < Stopped , stop , Stopped >,
  242. // +---------+-------------+---------+---------------------+----------------------+
  243. a_row < Open , open_close , Empty , &p::close_drawer >,
  244. // +---------+-------------+---------+---------------------+----------------------+
  245. a_row < Empty , open_close , Open , &p::open_drawer >,
  246. // conflict between a normal and 2 internal transitions (irow/g_irow)
  247. // + a state-defined internals defined 2 ways (see Empty)
  248. row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
  249. irow < Empty , cd_detected , &p::internal_action ,&p::internal_guard >,
  250. g_irow < Empty , cd_detected ,&p::internal_guard >,
  251. // +---------+-------------+---------+---------------------+----------------------+
  252. a_row < Playing , stop , Stopped , &p::stop_playback >,
  253. a_row < Playing , pause , Paused , &p::pause_playback >,
  254. a_row < Playing , open_close , Open , &p::stop_and_open >,
  255. // +---------+-------------+---------+---------------------+----------------------+
  256. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  257. a_row < Paused , stop , Stopped , &p::stop_playback >,
  258. a_row < Paused , open_close , Open , &p::stop_and_open >
  259. // +---------+-------------+---------+---------------------+----------------------+
  260. > {};
  261. // Replaces the default no-transition response.
  262. template <class FSM,class Event>
  263. void no_transition(Event const& e, FSM&,int state)
  264. {
  265. std::cout << "no transition from state " << state
  266. << " on event " << typeid(e).name() << std::endl;
  267. }
  268. };
  269. // Pick a back-end
  270. typedef msm::back::state_machine<player_> player;
  271. //
  272. // Testing utilities.
  273. //
  274. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  275. void pstate(player const& p)
  276. {
  277. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  278. }
  279. void test()
  280. {
  281. player p;
  282. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  283. p.start();
  284. // this event will be ignored and not call no_transition
  285. p.process_event(to_ignore());
  286. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  287. p.process_event(open_close()); pstate(p);
  288. p.process_event(open_close()); pstate(p);
  289. // will be rejected, wrong disk type
  290. p.process_event(
  291. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  292. p.process_event(
  293. cd_detected("louie, louie",DISK_CD)); pstate(p);
  294. p.process_event(play());
  295. p.process_event(NextSong());
  296. std::cout << "sending an internal event" << std::endl;
  297. p.process_event(internal_event());
  298. std::cout << "conflict between the internal and normal transition. Internal is tried last" << std::endl;
  299. p.process_event(PreviousSong());
  300. // at this point, Play is active
  301. p.process_event(pause()); pstate(p);
  302. // go back to Playing
  303. p.process_event(end_pause()); pstate(p);
  304. p.process_event(pause()); pstate(p);
  305. p.process_event(stop()); pstate(p);
  306. // event leading to the same state
  307. // no action method called as it is not present in the transition table
  308. p.process_event(stop()); pstate(p);
  309. }
  310. }
  311. int main()
  312. {
  313. test();
  314. return 0;
  315. }