SM-2Arg.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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 <string>
  12. #include "boost/mpl/vector/vector30.hpp"
  13. #include <boost/msm/back/state_machine.hpp>
  14. #include <boost/msm/front/state_machine_def.hpp>
  15. #include <boost/msm/back/tools.hpp>
  16. using namespace std;
  17. namespace msm = boost::msm;
  18. namespace // Concrete FSM implementation
  19. {
  20. // events
  21. struct play {};
  22. struct end_pause {};
  23. struct stop {};
  24. struct pause {};
  25. struct open_close {};
  26. struct NextSong {};
  27. struct PreviousSong {};
  28. struct ThreeSec {};
  29. struct TenSec {};
  30. struct go_sleep {};
  31. struct error_found {};
  32. struct end_error {};
  33. // Flags. Allow information about a property of the current state
  34. struct PlayingPaused{};
  35. struct CDLoaded {};
  36. struct FirstSongPlaying {};
  37. // A "complicated" event type that carries some data.
  38. struct cd_detected
  39. {
  40. cd_detected(std::string name)
  41. : name(name)
  42. {}
  43. std::string name;
  44. };
  45. // an easy visitor
  46. struct SomeVisitor
  47. {
  48. template <class T>
  49. void visit_state(T* astate,int i)
  50. {
  51. std::cout << "visiting state:" << typeid(*astate).name()
  52. << " with data:" << i << std::endl;
  53. }
  54. };
  55. // overwrite of the base state (not default)
  56. struct my_visitable_state
  57. {
  58. // signature of the accept function
  59. typedef msm::back::args<void,SomeVisitor&,int> accept_sig;
  60. // we also want polymorphic states
  61. virtual ~my_visitable_state() {}
  62. // default implementation for states who do not need to be visited
  63. void accept(SomeVisitor&,int) const {}
  64. };
  65. // Concrete FSM implementation
  66. struct player_ : public msm::front::state_machine_def<player_,my_visitable_state>
  67. {
  68. template <class Event,class FSM>
  69. void on_entry(Event const&,FSM& ) {std::cout << "starting: player" << std::endl;}
  70. // The list of FSM states
  71. struct Empty : public msm::front::state<my_visitable_state>
  72. {
  73. typedef mpl::vector<play> deferred_events;
  74. // every (optional) entry/exit methods get the event packed as boost::any. Not useful very often.
  75. template <class Event,class FSM>
  76. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  77. template <class Event,class FSM>
  78. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  79. void accept(SomeVisitor& vis,int i) const
  80. {
  81. vis.visit_state(this,i);
  82. }
  83. };
  84. struct Open : public msm::front::state<my_visitable_state>
  85. {
  86. typedef mpl::vector1<CDLoaded> flag_list;
  87. typedef mpl::vector<play> deferred_events;
  88. template <class Event,class FSM>
  89. void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;}
  90. template <class Event,class FSM>
  91. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  92. void accept(SomeVisitor& vis,int i) const
  93. {
  94. vis.visit_state(this,i);
  95. }
  96. };
  97. // a state needing a pointer to the containing state machine
  98. // and using for this the non-default policy
  99. // if policy used, set_sm_ptr is needed
  100. struct Stopped : public msm::front::state<my_visitable_state>
  101. {
  102. // when stopped, the CD is loaded
  103. typedef mpl::vector1<CDLoaded> flag_list;
  104. template <class Event,class FSM>
  105. void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;}
  106. template <class Event,class FSM>
  107. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  108. };
  109. // the player state machine contains a state which is himself a state machine
  110. // it demonstrates Shallow History: if the state gets activated with end_pause
  111. // then it will remember the last active state and reactivate it
  112. // also possible: AlwaysHistory, the last active state will always be reactivated
  113. // or NoHistory, always restart from the initial state
  114. struct Playing_ : public msm::front::state_machine_def<Playing_,my_visitable_state >
  115. {
  116. // when playing, the CD is loaded and we are in either pause or playing (duh)
  117. typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
  118. template <class Event,class FSM>
  119. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  120. template <class Event,class FSM>
  121. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  122. void accept(SomeVisitor& vis,int i) const
  123. {
  124. // note that visiting will recursively visit sub-states
  125. vis.visit_state(this,i);
  126. }
  127. // The list of FSM states
  128. // the Playing state machine contains a state which is himself a state machine
  129. // so we have a SM containing a SM containing a SM
  130. struct Song1_ : public msm::front::state_machine_def<Song1_,my_visitable_state>
  131. {
  132. typedef mpl::vector1<FirstSongPlaying> flag_list;
  133. template <class Event,class FSM>
  134. void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
  135. template <class Event,class FSM>
  136. void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
  137. void accept(SomeVisitor& vis,int i) const
  138. {
  139. vis.visit_state(this,i);
  140. }
  141. struct LightOn : public msm::front::state<my_visitable_state>
  142. {
  143. template <class Event,class FSM>
  144. void on_entry(Event const&,FSM& ) {std::cout << "starting: LightOn" << std::endl;}
  145. template <class Event,class FSM>
  146. void on_exit(Event const&,FSM& ) {std::cout << "finishing: LightOn" << std::endl;}
  147. };
  148. struct LightOff : public msm::front::state<my_visitable_state>
  149. {
  150. template <class Event,class FSM>
  151. void on_entry(Event const&,FSM& ) {std::cout << "starting: LightOff" << std::endl;}
  152. template <class Event,class FSM>
  153. void on_exit(Event const&,FSM& ) {std::cout << "finishing: LightOff" << std::endl;}
  154. };
  155. // the initial state. Must be defined
  156. typedef LightOn initial_state;
  157. // transition actions
  158. void turn_light_off(ThreeSec const&) { std::cout << "3s off::turn light off\n"; }
  159. // guard conditions
  160. typedef Song1_ s; // makes transition table cleaner
  161. // Transition table for Song1
  162. struct transition_table : mpl::vector1<
  163. // Start Event Next Action Guard
  164. // +---------+-------------+---------+---------------------+----------------------+
  165. a_row < LightOn , ThreeSec , LightOff, &s::turn_light_off >
  166. // +---------+-------------+---------+---------------------+----------------------+
  167. > {};
  168. // Replaces the default no-transition response.
  169. template <class FSM,class Event>
  170. void no_transition(Event const& e, FSM&,int state)
  171. {
  172. std::cout << "no transition from state " << state
  173. << " on event " << typeid(e).name() << std::endl;
  174. }
  175. };
  176. typedef msm::back::state_machine<Song1_> Song1;
  177. struct Song2 : public msm::front::state<my_visitable_state>
  178. {
  179. template <class Event,class FSM>
  180. void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
  181. template <class Event,class FSM>
  182. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
  183. };
  184. struct Song3 : public msm::front::state<my_visitable_state>
  185. {
  186. template <class Event,class FSM>
  187. void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
  188. template <class Event,class FSM>
  189. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
  190. };
  191. // the initial state. Must be defined
  192. typedef Song1 initial_state;
  193. // transition actions
  194. void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; }
  195. void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; }
  196. // guard conditions
  197. typedef Playing_ pl; // makes transition table cleaner
  198. // Transition table for Playing
  199. struct transition_table : mpl::vector4<
  200. // Start Event Next Action Guard
  201. // +---------+-------------+---------+---------------------+----------------------+
  202. a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
  203. a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
  204. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  205. a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
  206. // +---------+-------------+---------+---------------------+----------------------+
  207. > {};
  208. // Replaces the default no-transition response.
  209. template <class FSM,class Event>
  210. void no_transition(Event const& e, FSM&,int state)
  211. {
  212. std::cout << "no transition from state " << state
  213. << " on event " << typeid(e).name() << std::endl;
  214. }
  215. };
  216. typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
  217. // the player state machine contains a state which is himself a state machine (2 of them, Playing and Paused)
  218. struct Paused_ : public msm::front::state_machine_def<Paused_,my_visitable_state>
  219. {
  220. typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
  221. template <class Event,class FSM>
  222. void on_entry(Event const&,FSM& ) {std::cout << "entering: Paused" << std::endl;}
  223. template <class Event,class FSM>
  224. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Paused" << std::endl;}
  225. // The list of FSM states
  226. struct StartBlinking : public msm::front::state<my_visitable_state>
  227. {
  228. template <class Event,class FSM>
  229. void on_entry(Event const&,FSM& ) {std::cout << "starting: StartBlinking" << std::endl;}
  230. template <class Event,class FSM>
  231. void on_exit(Event const&,FSM& ) {std::cout << "finishing: StartBlinking" << std::endl;}
  232. };
  233. struct StopBlinking : public msm::front::state<my_visitable_state>
  234. {
  235. template <class Event,class FSM>
  236. void on_entry(Event const&,FSM& ) {std::cout << "starting: StopBlinking" << std::endl;}
  237. template <class Event,class FSM>
  238. void on_exit(Event const&,FSM& ) {std::cout << "finishing: StopBlinking" << std::endl;}
  239. };
  240. // the initial state. Must be defined
  241. typedef StartBlinking initial_state;
  242. // transition actions
  243. void start_blinking(TenSec const&) { std::cout << "Paused::start_blinking\n"; }
  244. void stop_blinking(TenSec const&) { std::cout << "Paused::stop_blinking\n"; }
  245. // guard conditions
  246. typedef Paused_ pa; // makes transition table cleaner
  247. // Transition table
  248. struct transition_table : mpl::vector2<
  249. // Start Event Next Action Guard
  250. // +---------------+-------------+--------------+---------------------+----------------------+
  251. a_row < StartBlinking , TenSec , StopBlinking , &pa::stop_blinking >,
  252. a_row < StopBlinking , TenSec , StartBlinking , &pa::start_blinking >
  253. // +---------------+-------------+---------------+--------------------+----------------------+
  254. > {};
  255. // Replaces the default no-transition response.
  256. template <class FSM,class Event>
  257. void no_transition(Event const& e, FSM&,int state)
  258. {
  259. std::cout << "no transition from state " << state
  260. << " on event " << typeid(e).name() << std::endl;
  261. }
  262. };
  263. typedef msm::back::state_machine<Paused_> Paused;
  264. struct SleepMode : public msm::front::state<my_visitable_state>
  265. {
  266. }; // dummy state just to test the automatic id generation
  267. struct AllOk : public msm::front::state<my_visitable_state>
  268. {
  269. template <class Event,class FSM>
  270. void on_entry(Event const&,FSM& ) {std::cout << "starting: AllOk" << std::endl;}
  271. template <class Event,class FSM>
  272. void on_exit(Event const&,FSM& ) {std::cout << "finishing: AllOk" << std::endl;}
  273. };
  274. struct ErrorMode : //public terminate_state<>
  275. public msm::front::interrupt_state<end_error,my_visitable_state>
  276. {
  277. template <class Event,class FSM>
  278. void on_entry(Event const&,FSM& ) {std::cout << "starting: ErrorMode" << std::endl;}
  279. template <class Event,class FSM>
  280. void on_exit(Event const&,FSM& ) {std::cout << "finishing: ErrorMode" << std::endl;}
  281. };
  282. // the initial state of the player SM. Must be defined
  283. typedef mpl::vector<Empty,AllOk> initial_state;
  284. //typedef Empty initial_state; // this is to have only one active state
  285. // transition actions
  286. void start_playback(play const&) { std::cout << "player::start_playback\n"; }
  287. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  288. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  289. void store_cd_info(cd_detected const&)
  290. {
  291. std::cout << "player::store_cd_info\n";
  292. // generate another event to test the queue
  293. //process_event(play());
  294. }
  295. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  296. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  297. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  298. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  299. void stopped_again(stop const&){std::cout << "player::stopped_again\n";}
  300. void start_sleep(go_sleep const&) { }
  301. void report_error(error_found const&) {std::cout << "player::report_error\n";}
  302. void report_end_error(end_error const&) {std::cout << "player::report_end_error\n";}
  303. // guard conditions
  304. typedef player_ p; // makes transition table cleaner
  305. // Transition table for player
  306. struct transition_table : mpl::vector<
  307. // Start Event Next Action Guard
  308. // +---------+-------------+---------+---------------------+----------------------+
  309. a_row < Stopped , play , Playing , &p::start_playback >,
  310. a_row < Stopped , open_close , Open , &p::open_drawer >,
  311. a_row < Stopped , stop , Stopped , &p::stopped_again >,
  312. // +---------+-------------+---------+---------------------+----------------------+
  313. a_row < Open , open_close , Empty , &p::close_drawer >,
  314. // +---------+-------------+---------+---------------------+----------------------+
  315. a_row < Empty , open_close , Open , &p::open_drawer >,
  316. a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
  317. // +---------+-------------+---------+---------------------+----------------------+
  318. a_row < Playing , stop , Stopped , &p::stop_playback >,
  319. a_row < Playing , pause , Paused , &p::pause_playback >,
  320. a_row < Playing , open_close , Open , &p::stop_and_open >,
  321. // +---------+-------------+---------+---------------------+----------------------+
  322. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  323. a_row < Paused , stop , Stopped , &p::stop_playback >,
  324. a_row < Paused , open_close , Open , &p::stop_and_open >,
  325. a_row < Paused , go_sleep ,SleepMode, &p::start_sleep >,
  326. // +---------+-------------+---------+---------------------+----------------------+
  327. a_row < AllOk , error_found ,ErrorMode, &p::report_error >,
  328. a_row <ErrorMode,end_error ,AllOk , &p::report_end_error >
  329. // +---------+-------------+---------+---------------------+----------------------+
  330. > {};
  331. // Replaces the default no-transition response.
  332. template <class FSM,class Event>
  333. void no_transition(Event const& e, FSM&,int state)
  334. {
  335. std::cout << "no transition from state " << state
  336. << " on event " << typeid(e).name() << std::endl;
  337. }
  338. };
  339. // back-end
  340. typedef msm::back::state_machine<player_> player;
  341. //
  342. // Testing utilities.
  343. //
  344. void pstate(player const& p)
  345. {
  346. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode","SleepMode" };
  347. for (unsigned int i=0;i<player::nr_regions::value;++i)
  348. {
  349. std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
  350. }
  351. }
  352. void test()
  353. {
  354. player p;
  355. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  356. p.start();
  357. std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl; //=> false (no CD yet)
  358. // test deferred event
  359. // deferred in Empty and Open, will be handled only after event cd_detected
  360. p.process_event(play());
  361. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  362. p.process_event(open_close()); pstate(p);
  363. // visiting Paused and AllOk, but only Paused cares
  364. SomeVisitor vis;
  365. p.visit_current_states(boost::ref(vis),1);
  366. p.process_event(open_close()); pstate(p);
  367. // visiting Empty and AllOk, but only Empty cares
  368. p.visit_current_states(boost::ref(vis),2);
  369. p.process_event(cd_detected("louie, louie"));
  370. // no need to call play() as the previous event does it in its action method
  371. //p.process_event(play());
  372. // at this point, Play is active, along FirstSong and LightOn
  373. pstate(p);
  374. // visiting Playing+Song1 and AllOk, but only Playing+Song1 care
  375. p.visit_current_states(boost::ref(vis),3);
  376. std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
  377. // call on_exit on LightOn,FirstSong,Play like stated in the UML spec.
  378. // and of course on_entry on Paused and StartBlinking
  379. p.process_event(pause()); pstate(p);
  380. std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
  381. // forward events to Paused
  382. p.process_event(TenSec());
  383. p.process_event(TenSec());
  384. // go back to Playing
  385. p.process_event(end_pause()); pstate(p);
  386. std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl; //=> true
  387. p.process_event(ThreeSec()); pstate(p);
  388. p.process_event(NextSong());pstate(p);
  389. // We are now in second song, Flag inactive
  390. std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false
  391. // visiting Playing+Song2 and AllOk, but only Playing cares
  392. p.visit_current_states(boost::ref(vis),4);
  393. p.process_event(NextSong());pstate(p);
  394. // 2nd song active
  395. p.process_event(PreviousSong());pstate(p);
  396. // Pause
  397. p.process_event(pause()); pstate(p);
  398. // go back to Playing
  399. // but end_pause is an event activating the History
  400. // => keep the last active State (SecondSong)
  401. p.process_event(end_pause()); pstate(p);
  402. // test of an event from a state to itself. According to UML spec, call again exit/entry from Stopped
  403. p.process_event(stop()); pstate(p);
  404. p.process_event(stop()); pstate(p);
  405. std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl;//=> true
  406. std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> false
  407. std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false
  408. std::cout << "CDLoaded active with AND:" << std::boolalpha << p.is_flag_active<CDLoaded,player::Flag_AND>() << std::endl;//=> false
  409. std::cout << "CDLoaded active with OR:" << std::boolalpha << p.is_flag_active<CDLoaded,player::Flag_OR>() << std::endl;//=> true
  410. // go back to Playing
  411. // but play is not leading to Shallow History => do not remember the last active State (SecondSong)
  412. // and activate again FirstSong and LightOn
  413. p.process_event(play()); pstate(p);
  414. p.process_event(error_found()); pstate(p);
  415. // try generating more events
  416. std::cout << "Trying to generate another event" << std::endl; // will not work, fsm is terminated or interrupted
  417. p.process_event(NextSong());pstate(p);
  418. std::cout << "Trying to end the error" << std::endl; // will work only if ErrorMode is interrupt state
  419. p.process_event(end_error());pstate(p);
  420. std::cout << "Trying to generate another event" << std::endl; // will work only if ErrorMode is interrupt state
  421. p.process_event(NextSong());pstate(p);
  422. std::cout << "Simulate error. Event play is not valid" << std::endl;
  423. p.process_event(play()); pstate(p);
  424. }
  425. }
  426. int main()
  427. {
  428. test();
  429. return 0;
  430. }