1 // Copyright 2010 Gordon Woodhull
2 // modified from MSMv2.10/libs/msm/doc/HTML/examples/SimpleTutorial.cpp
3 // for the purpose of showing how to run mpl_graph algorithms on MSMs
4 // and distributed same license as source
5 // Copyright 2008 Christophe Henry
6 // henry UNDERSCORE christophe AT hotmail DOT com
7 // This is an extended version of the state machine available in the boost::mpl library
8 // Distributed under the same license as the original.
9 // Copyright for the original version:
10 // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
11 // under the Boost Software License, Version 1.0. (See accompanying
12 // file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14
15 #include <iostream>
16 // back-end
17 #include <boost/msm/back/state_machine.hpp>
18 //front-end
19 #include <boost/msm/front/state_machine_def.hpp>
20
21 // mpl_graph graph implementation and depth first search
22 #include <boost/msm/mpl_graph/incidence_list_graph.hpp>
23 #include <boost/msm/mpl_graph/depth_first_search.hpp>
24 #include <boost/msm/mpl_graph/breadth_first_search.hpp>
25
26 namespace msm = boost::msm;
27 namespace mpl = boost::mpl;
28
29 namespace mpl_graph = boost::msm::mpl_graph;
30
31 namespace
32 {
33 // events
34 struct play {};
35 struct end_pause {};
36 struct stop {};
37 struct pause {};
38 struct open_close {};
39
40 // A "complicated" event type that carries some data.
41 enum DiskTypeEnum
42 {
43 DISK_CD=0,
44 DISK_DVD=1
45 };
46 struct cd_detected
47 {
cd_detected__anon775a6f990111::cd_detected48 cd_detected(std::string name, DiskTypeEnum diskType)
49 : name(name),
50 disc_type(diskType)
51 {}
52
53 std::string name;
54 DiskTypeEnum disc_type;
55 };
56
57 // front-end: define the FSM structure
58 struct player_ : public msm::front::state_machine_def<player_>
59 {
60 // The list of FSM states
61 struct Empty : public msm::front::state<>
62 {
63 // every (optional) entry/exit methods get the event passed.
64 template <class Event,class FSM>
on_entry__anon775a6f990111::player_::Empty65 void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
66 template <class Event,class FSM>
on_exit__anon775a6f990111::player_::Empty67 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
68 };
69 struct Open : public msm::front::state<>
70 {
71 template <class Event,class FSM>
on_entry__anon775a6f990111::player_::Open72 void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
73 template <class Event,class FSM>
on_exit__anon775a6f990111::player_::Open74 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
75 };
76
77 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
78 struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
79 {
80 template <class Event,class FSM>
on_entry__anon775a6f990111::player_::Stopped81 void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
82 template <class Event,class FSM>
on_exit__anon775a6f990111::player_::Stopped83 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
set_sm_ptr__anon775a6f990111::player_::Stopped84 void set_sm_ptr(player_* pl)
85 {
86 m_player=pl;
87 }
88 player_* m_player;
89 };
90
91 struct Playing : public msm::front::state<>
92 {
93 template <class Event,class FSM>
on_entry__anon775a6f990111::player_::Playing94 void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
95 template <class Event,class FSM>
on_exit__anon775a6f990111::player_::Playing96 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
97 };
98
99 // state not defining any entry or exit
100 struct Paused : public msm::front::state<>
101 {
102 };
103
104 // the initial state of the player SM. Must be defined
105 typedef Empty initial_state;
106
107 // transition actions
start_playback__anon775a6f990111::player_108 void start_playback(play const&) { std::cout << "player::start_playback\n"; }
open_drawer__anon775a6f990111::player_109 void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
close_drawer__anon775a6f990111::player_110 void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
store_cd_info__anon775a6f990111::player_111 void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
stop_playback__anon775a6f990111::player_112 void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
pause_playback__anon775a6f990111::player_113 void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
resume_playback__anon775a6f990111::player_114 void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
stop_and_open__anon775a6f990111::player_115 void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
stopped_again__anon775a6f990111::player_116 void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
117 // guard conditions
good_disk_format__anon775a6f990111::player_118 bool good_disk_format(cd_detected const& evt)
119 {
120 // to test a guard condition, let's say we understand only CDs, not DVD
121 if (evt.disc_type != DISK_CD)
122 {
123 std::cout << "wrong disk, sorry" << std::endl;
124 return false;
125 }
126 return true;
127 }
128 // used to show a transition conflict. This guard will simply deactivate one transition and thus
129 // solve the conflict
auto_start__anon775a6f990111::player_130 bool auto_start(cd_detected const&)
131 {
132 return false;
133 }
134
135 typedef player_ p; // makes transition table cleaner
136
137 // Transition table for player
138 struct transition_table : mpl::vector<
139 // Start Event Next Action Guard
140 // +---------+-------------+---------+---------------------+----------------------+
141 a_row < Stopped , play , Playing , &p::start_playback >,
142 a_row < Stopped , open_close , Open , &p::open_drawer >,
143 _row < Stopped , stop , Stopped >,
144 // +---------+-------------+---------+---------------------+----------------------+
145 a_row < Open , open_close , Empty , &p::close_drawer >,
146 // +---------+-------------+---------+---------------------+----------------------+
147 a_row < Empty , open_close , Open , &p::open_drawer >,
148 row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
149 row < Empty , cd_detected , Playing , &p::store_cd_info ,&p::auto_start >,
150 // +---------+-------------+---------+---------------------+----------------------+
151 a_row < Playing , stop , Stopped , &p::stop_playback >,
152 a_row < Playing , pause , Paused , &p::pause_playback >,
153 a_row < Playing , open_close , Open , &p::stop_and_open >,
154 // +---------+-------------+---------+---------------------+----------------------+
155 a_row < Paused , end_pause , Playing , &p::resume_playback >,
156 a_row < Paused , stop , Stopped , &p::stop_playback >,
157 a_row < Paused , open_close , Open , &p::stop_and_open >
158 // +---------+-------------+---------+---------------------+----------------------+
159 > {};
160 // Replaces the default no-transition response.
161 template <class FSM,class Event>
no_transition__anon775a6f990111::player_162 void no_transition(Event const& e, FSM&,int state)
163 {
164 std::cout << "no transition from state " << state
165 << " on event " << typeid(e).name() << std::endl;
166 }
167
168
169 // transition table is already an incidence list;
170 // select Edge, Source, Target = pair<Start,Event>, Start, Next
171 // making Start part of Edge is necessary because Edge tags have to be unique
172
173 template<typename Row>
174 struct row_to_incidence :
175 mpl::vector<mpl::pair<typename Row::Target, typename Row::Evt>, typename Row::Source, typename Row::Target> {};
176
177 typedef mpl::fold<player_::transition_table,
178 mpl::vector<>,
179 mpl::push_back<mpl::_1, row_to_incidence<mpl::_2> > >::type
180 transition_incidence_list;
181
182 typedef mpl_graph::incidence_list_graph<transition_incidence_list>
183 transition_graph;
184
185 struct preordering_dfs_visitor : mpl_graph::dfs_default_visitor_operations {
186 template<typename Node, typename Graph, typename State>
187 struct discover_vertex :
188 mpl::push_back<State, Node>
189 {};
190 };
191
192 typedef mpl::first<mpl_graph::
193 depth_first_search<transition_graph,
194 preordering_dfs_visitor,
195 mpl::vector<>,
196 player_::initial_state>::type>::type
197 dfs_from_initial_state;
198
199 BOOST_MPL_ASSERT(( mpl::equal<dfs_from_initial_state,
200 mpl::vector<Empty,Open,Stopped,Playing,Paused> > ));
201
202 struct preordering_bfs_visitor : mpl_graph::bfs_default_visitor_operations {
203 template<typename Node, typename Graph, typename State>
204 struct discover_vertex :
205 mpl::push_back<State, Node>
206 {};
207 };
208 typedef mpl::first<mpl_graph::
209 breadth_first_search<transition_graph,
210 preordering_bfs_visitor,
211 mpl::vector<>,
212 player_::initial_state>::type>::type
213 bfs_from_initial_state;
214
215 // yawn, BFS happens to produce the same result as DFS for this example
216 BOOST_MPL_ASSERT(( mpl::equal<bfs_from_initial_state,
217 mpl::vector<Empty,Open,Stopped,Playing,Paused> > ));
218
219 };
220 // Pick a back-end
221 typedef msm::back::state_machine<player_> player;
222
223 //
224 // Testing utilities.
225 //
226 static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
pstate(player const & p)227 void pstate(player const& p)
228 {
229 std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
230 }
231
232
test()233 void test()
234 {
235 player p;
236 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
237 p.start();
238 // go to Open, call on_exit on Empty, then action, then on_entry on Open
239 p.process_event(open_close()); pstate(p);
240 p.process_event(open_close()); pstate(p);
241 // will be rejected, wrong disk type
242 p.process_event(
243 cd_detected("louie, louie",DISK_DVD)); pstate(p);
244 p.process_event(
245 cd_detected("louie, louie",DISK_CD)); pstate(p);
246 p.process_event(play());
247
248 // at this point, Play is active
249 p.process_event(pause()); pstate(p);
250 // go back to Playing
251 p.process_event(end_pause()); pstate(p);
252 p.process_event(pause()); pstate(p);
253 p.process_event(stop()); pstate(p);
254 // event leading to the same state
255 // no action method called as it is not present in the transition table
256 p.process_event(stop()); pstate(p);
257 }
258 }
259
main()260 int main()
261 {
262 test();
263 return 0;
264 }
265