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
11#include <boost/msm/back/state_machine.hpp>
12#include <boost/msm/front/euml/euml.hpp>
13
14#ifndef BOOST_MSM_NONSTANDALONE_TEST
15#define BOOST_TEST_MODULE MyTest
16#endif
17#include <boost/test/unit_test.hpp>
18
19using namespace std;
20using namespace boost::msm::front::euml;
21namespace msm = boost::msm;
22
23namespace
24{
25 // A "complicated" event type that carries some data.
26 enum DiskTypeEnum
27 {
28 DISK_CD=0,
29 DISK_DVD=1
30 };
31 // events
32 BOOST_MSM_EUML_EVENT(play)
33 BOOST_MSM_EUML_EVENT(end_pause)
34 BOOST_MSM_EUML_EVENT(stop)
35 BOOST_MSM_EUML_EVENT(pause)
36 BOOST_MSM_EUML_EVENT(open_close)
37 BOOST_MSM_EUML_EVENT(next_song)
38 BOOST_MSM_EUML_EVENT(previous_song)
39 BOOST_MSM_EUML_EVENT(region2_evt)
40 // A "complicated" event type that carries some data.
41 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
42 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
43 BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
44 BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
45
46 //states
47 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,entry_counter)
48 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,exit_counter)
49
50 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Empty)
51 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Open)
52 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Stopped)
53 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Paused)
54
55 // Playing is now a state machine itself.
56 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,start_next_song_counter)
57 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,start_prev_song_guard_counter)
58 // It has 5 substates
59 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Song1)
60 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Song2)
61 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Song3)
62 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Region2State1)
63 BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Region2State2)
64
65 // Playing has a transition table
66 BOOST_MSM_EUML_TRANSITION_TABLE((
67 // +------------------------------------------------------------------------------+
68 Song2 == Song1 + next_song ,
69 Song1 == Song2 + previous_song [True_()] / ++fsm_(start_prev_song_guard_counter) ,
70 Song3 == Song2 + next_song / ++fsm_(start_next_song_counter) ,
71 Song2 == Song3 + previous_song [True_()] / ++fsm_(start_prev_song_guard_counter) ,
72 Region2State2 == Region2State1 + region2_evt
73 // +------------------------------------------------------------------------------+
74 ),playing_transition_table )
75
76 BOOST_MSM_EUML_DECLARE_STATE_MACHINE( (playing_transition_table, //STT
77 init_ << Song1 << Region2State1, // Init State
78 ++state_(entry_counter), // Entry
79 ++state_(exit_counter), // Exit
80 attributes_ << entry_counter << exit_counter
81 << start_next_song_counter
82 << start_prev_song_guard_counter // Attributes
83 ),Playing_)
84 // choice of back-end
85 typedef msm::back::state_machine<Playing_> Playing_type;
86 Playing_type const Playing;
87
88
89 //fsm
90 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,start_playback_counter)
91 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,can_close_drawer_counter)
92 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,test_fct_counter)
93 BOOST_MSM_EUML_ACTION(No_Transition)
94 {
95 template <class FSM,class Event>
96 void operator()(Event const&,FSM&,int)
97 {
98 BOOST_FAIL("no_transition called!");
99 }
100 };
101 BOOST_MSM_EUML_ACTION(good_disk_format)
102 {
103 template <class FSM,class EVT,class SourceState,class TargetState>
104 bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
105 {
106 if (evt.get_attribute(cd_type)!=DISK_CD)
107 {
108 return false;
109 }
110 return true;
111 }
112 };
113 BOOST_MSM_EUML_TRANSITION_TABLE((
114 Playing == Stopped + play / (++fsm_(start_playback_counter),++fsm_(test_fct_counter) ),
115 Playing == Paused + end_pause ,
116 // +------------------------------------------------------------------------------+
117 Empty == Open + open_close / ++fsm_(can_close_drawer_counter),
118 // +------------------------------------------------------------------------------+
119 Open == Empty + open_close ,
120 Open == Paused + open_close ,
121 Open == Stopped + open_close ,
122 Open == Playing + open_close ,
123 // +------------------------------------------------------------------------------+
124 Paused == Playing + pause ,
125 // +------------------------------------------------------------------------------+
126 Stopped == Playing + stop ,
127 Stopped == Paused + stop ,
128 Stopped == Empty + cd_detected [good_disk_format ||
129 (event_(cd_type)==Int_<DISK_CD>())] / process_(play) ,
130 Stopped == Stopped + stop
131 // +------------------------------------------------------------------------------+
132 ),transition_table)
133
134 BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
135 init_ << Empty, // Init State
136 no_action, // Entry
137 no_action, // Exit
138 attributes_ << start_playback_counter
139 << can_close_drawer_counter << test_fct_counter, // Attributes
140 configure_ << no_configure_, // configuration
141 No_Transition // no_transition handler
142 ),
143 player_) //fsm name
144
145 typedef msm::back::state_machine<player_> player;
146
147// static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
148
149
150 BOOST_AUTO_TEST_CASE( my_test )
151 {
152 player p;
153
154 p.start();
155 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Empty)&>().get_attribute(entry_counter) == 1,
156 "Empty entry not called correctly");
157
158 p.process_event(evt: open_close());
159 BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Open should be active"); //Open
160 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Empty)&>().get_attribute(exit_counter) == 1,
161 "Empty exit not called correctly");
162 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Open)&>().get_attribute(entry_counter) == 1,
163 "Open entry not called correctly");
164
165 p.process_event(evt: open_close());
166 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Empty should be active"); //Empty
167 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Open)&>().get_attribute(exit_counter) == 1,
168 "Open exit not called correctly");
169 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Empty)&>().get_attribute(entry_counter) == 2,
170 "Empty entry not called correctly");
171 BOOST_CHECK_MESSAGE(p.get_attribute(can_close_drawer_counter) == 1,"guard not called correctly");
172
173 p.process_event(
174 evt: cd_detected("louie, louie",DISK_DVD));
175 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Empty should be active"); //Empty
176 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Open)&>().get_attribute(exit_counter) == 1,
177 "Open exit not called correctly");
178 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Empty)&>().get_attribute(entry_counter) == 2,
179 "Empty entry not called correctly");
180
181 p.process_event(
182 evt: cd_detected("louie, louie",DISK_CD));
183 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
184 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Empty)&>().get_attribute(exit_counter) == 2,
185 "Empty exit not called correctly");
186 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 1,
187 "Stopped entry not called correctly");
188 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(exit_counter) == 1,
189 "Stopped exit not called correctly");
190 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().get_attribute(entry_counter) == 1,
191 "Playing entry not called correctly");
192 BOOST_CHECK_MESSAGE(p.get_attribute(start_playback_counter) == 1,"action not called correctly");
193 BOOST_CHECK_MESSAGE(p.get_attribute(test_fct_counter) == 1,"action not called correctly");
194
195 p.process_event(evt: next_song);
196 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
197 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().current_state()[0] == 1,"Song2 should be active");
198 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().current_state()[1] == 3,"Region2State1 should be active");
199 BOOST_CHECK_MESSAGE(
200 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Region2State1)&>().get_attribute(entry_counter) == 1,
201 "Region2State1 entry not called correctly");
202 BOOST_CHECK_MESSAGE(
203 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Song2)&>().get_attribute(entry_counter) == 1,
204 "Song2 entry not called correctly");
205 BOOST_CHECK_MESSAGE(
206 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Song1)&>().get_attribute(exit_counter) == 1,
207 "Song1 exit not called correctly");
208 BOOST_CHECK_MESSAGE(
209 p.get_state<Playing_type&>().get_attribute(start_next_song_counter) == 0,
210 "submachine action not called correctly");
211
212 p.process_event(evt: next_song);
213 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
214 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().current_state()[0] == 2,"Song3 should be active");
215 BOOST_CHECK_MESSAGE(
216 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Song3)&>().get_attribute(entry_counter) == 1,
217 "Song3 entry not called correctly");
218 BOOST_CHECK_MESSAGE(
219 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Song2)&>().get_attribute(exit_counter) == 1,
220 "Song2 exit not called correctly");
221 BOOST_CHECK_MESSAGE(
222 p.get_state<Playing_type&>().get_attribute(start_next_song_counter) == 1,
223 "submachine action not called correctly");
224
225 p.process_event(evt: previous_song);
226 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
227 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().current_state()[0] == 1,"Song2 should be active");
228 BOOST_CHECK_MESSAGE(
229 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Song2)&>().get_attribute(entry_counter) == 2,
230 "Song2 entry not called correctly");
231 BOOST_CHECK_MESSAGE(
232 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Song3)&>().get_attribute(exit_counter) == 1,
233 "Song3 exit not called correctly");
234 BOOST_CHECK_MESSAGE(
235 p.get_state<Playing_type&>().get_attribute(start_prev_song_guard_counter) == 1,
236 "submachine guard not called correctly");
237 BOOST_CHECK_MESSAGE(
238 p.get_state<Playing_type&>().get_state<BOOST_MSM_EUML_STATE_NAME(Region2State2)&>().get_attribute(entry_counter) == 0,
239 "Region2State2 entry not called correctly");
240
241
242 p.process_event(evt: pause());
243 BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Paused should be active"); //Paused
244 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().get_attribute(exit_counter) == 1,
245 "Playing exit not called correctly");
246 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(entry_counter) == 1,
247 "Paused entry not called correctly");
248
249 // go back to Playing
250 p.process_event(evt: end_pause());
251 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
252 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(exit_counter) == 1,
253 "Paused exit not called correctly");
254 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().get_attribute(entry_counter) == 2,
255 "Playing entry not called correctly");
256
257 p.process_event(evt: pause());
258 BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Paused should be active"); //Paused
259 BOOST_CHECK_MESSAGE(p.get_state<Playing_type&>().get_attribute(exit_counter) == 2,
260 "Playing exit not called correctly");
261 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(entry_counter) == 2,
262 "Paused entry not called correctly");
263
264 p.process_event(evt: stop());
265 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
266 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(exit_counter) == 2,
267 "Paused exit not called correctly");
268 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 2,
269 "Stopped entry not called correctly");
270
271 p.process_event(evt: stop());
272 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
273 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(exit_counter) == 2,
274 "Stopped exit not called correctly");
275 BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 3,
276 "Stopped entry not called correctly");
277 }
278}
279
280

source code of boost/libs/msm/test/CompositeEuml.cpp