743102ee9f14a511ac5216d35ee55fa2838afbb2
[platform/upstream/boost.git] / libs / msm / test / SerializeWithHistory.cpp
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 <iostream>
12 // back-end
13 #include <boost/msm/back/state_machine.hpp>
14 //front-end
15 #include <boost/msm/front/state_machine_def.hpp>
16 #define BOOST_TEST_MODULE MyTest
17 #include <boost/test/unit_test.hpp>
18 // include headers that implement a archive in simple text format
19 #include <boost/archive/text_oarchive.hpp>
20 #include <boost/archive/text_iarchive.hpp>
21 #include <boost/serialization/tracking.hpp>
22
23 #include <fstream>
24
25 namespace msm = boost::msm;
26 namespace mpl = boost::mpl;
27
28 namespace
29 {
30     // events
31     struct play {};
32     struct end_pause {};
33     struct stop {};
34     struct pause {};
35     struct open_close {};
36     struct NextSong {};
37     struct PreviousSong {};
38     struct cd_detected
39     {
40         cd_detected(std::string name)
41             : name(name)
42         {}
43         std::string name;
44     };
45
46     // front-end: define the FSM structure 
47     struct player_ : public msm::front::state_machine_def<player_>
48     {
49         unsigned int start_playback_counter;
50         unsigned int can_close_drawer_counter;
51
52         player_():
53         start_playback_counter(0),
54         can_close_drawer_counter(0)
55         {}
56         // The list of FSM states
57         struct Empty : public msm::front::state<> 
58         {
59             template <class Event,class FSM>
60             void on_entry(Event const&,FSM& ) {++entry_counter;}
61             template <class Event,class FSM>
62             void on_exit(Event const&,FSM& ) {++exit_counter;}
63             int entry_counter;
64             int exit_counter;
65         };
66         struct Open : public msm::front::state<> 
67         {        
68             template <class Event,class FSM>
69             void on_entry(Event const&,FSM& ) {++entry_counter;}
70             template <class Event,class FSM>
71             void on_exit(Event const&,FSM& ) {++exit_counter;}
72             int entry_counter;
73             int exit_counter;
74         };
75
76         // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
77         struct Stopped : public msm::front::state<> 
78         {        
79             template <class Event,class FSM>
80             void on_entry(Event const&,FSM& ) {++entry_counter;}
81             template <class Event,class FSM>
82             void on_exit(Event const&,FSM& ) {++exit_counter;}
83             int entry_counter;
84             int exit_counter;
85         };
86
87         struct Playing_ : public msm::front::state_machine_def<Playing_>
88         {
89             template <class Event,class FSM>
90             void on_entry(Event const&,FSM& ) {++entry_counter;}
91             template <class Event,class FSM>
92             void on_exit(Event const&,FSM& ) {++exit_counter;}
93             int entry_counter;
94             int exit_counter;
95             unsigned int start_next_song_counter;
96             unsigned int start_prev_song_guard_counter;
97
98             Playing_():
99             start_next_song_counter(0),
100             start_prev_song_guard_counter(0)
101             {}
102
103             // The list of FSM states
104             struct Song1 : public msm::front::state<>
105             {
106                 template <class Event,class FSM>
107                 void on_entry(Event const&,FSM& ) {++entry_counter;}
108                 template <class Event,class FSM>
109                 void on_exit(Event const&,FSM& ) {++exit_counter;}
110                 int entry_counter;
111                 int exit_counter;
112             };
113             struct Song2 : public msm::front::state<>
114             {    
115                 template <class Event,class FSM>
116                 void on_entry(Event const&,FSM& ) {++entry_counter;}
117                 template <class Event,class FSM>
118                 void on_exit(Event const&,FSM& ) {++exit_counter;}
119                 int entry_counter;
120                 int exit_counter;
121             };
122             struct Song3 : public msm::front::state<>
123             {    
124                 template <class Event,class FSM>
125                 void on_entry(Event const&,FSM& ) {++entry_counter;}
126                 template <class Event,class FSM>
127                 void on_exit(Event const&,FSM& ) {++exit_counter;}
128                 int entry_counter;
129                 int exit_counter;
130             };
131             // the initial state. Must be defined
132             typedef Song1 initial_state;
133             // transition actions
134             void start_next_song(NextSong const&)       {++start_next_song_counter; }
135             void start_prev_song(PreviousSong const&)       {  }
136             // guard conditions
137             bool start_prev_song_guard(PreviousSong const&)       {++start_prev_song_guard_counter;return true; }
138
139             typedef Playing_ pl; // makes transition table cleaner
140             // Transition table for Playing
141             struct transition_table : mpl::vector4<
142                 //      Start     Event         Next      Action                                Guard
143                 //    +---------+-------------+---------+---------------------+----------------------+
144                  _row < Song1   , NextSong    , Song2                                                >,
145                   row < Song2   , PreviousSong, Song1   , &pl::start_prev_song,&pl::start_prev_song_guard>,
146                 a_row < Song2   , NextSong    , Song3   , &pl::start_next_song                       >,
147                 g_row < Song3   , PreviousSong, Song2                         ,&pl::start_prev_song_guard>
148                 //    +---------+-------------+---------+---------------------+----------------------+
149             > {};
150             // Replaces the default no-transition response.
151             template <class FSM,class Event>
152             void no_transition(Event const&, FSM&,int)
153             {
154                 BOOST_FAIL("no_transition called!");
155             }
156         };
157         // back-end
158         typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
159
160         // state not defining any entry or exit
161         struct Paused : public msm::front::state<>
162         {
163             template <class Event,class FSM>
164             void on_entry(Event const&,FSM& ) {++entry_counter;}
165             template <class Event,class FSM>
166             void on_exit(Event const&,FSM& ) {++exit_counter;}
167             int entry_counter;
168             int exit_counter;
169         };
170
171         // the initial state of the player SM. Must be defined
172         typedef Empty initial_state;
173
174         // transition actions
175         void start_playback(play const&)       {++start_playback_counter; }
176         void open_drawer(open_close const&)    {  }
177         void store_cd_info(cd_detected const&) {  }
178         void stop_playback(stop const&)        {  }
179         void pause_playback(pause const&)      {  }
180         void resume_playback(end_pause const&)      {  }
181         void stop_and_open(open_close const&)  {  }
182         void stopped_again(stop const&) {}
183         //guards
184         bool can_close_drawer(open_close const&)   
185         {
186             ++can_close_drawer_counter;
187             return true;
188         }
189
190
191         typedef player_ p; // makes transition table cleaner
192
193         // Transition table for player
194         struct transition_table : mpl::vector<
195             //    Start     Event         Next      Action                               Guard
196             //  +---------+-------------+---------+---------------------+----------------------+
197           a_row < Stopped , play        , Playing , &p::start_playback                         >,
198           a_row < Stopped , open_close  , Open    , &p::open_drawer                            >,
199            _row < Stopped , stop        , Stopped                                              >,
200             //  +---------+-------------+---------+---------------------+----------------------+
201           g_row < Open    , open_close  , Empty   ,                      &p::can_close_drawer  >,
202             //  +---------+-------------+---------+---------------------+----------------------+
203           a_row < Empty   , open_close  , Open    , &p::open_drawer                            >,
204           a_row < Empty   , cd_detected , Stopped , &p::store_cd_info                          >,
205             //  +---------+-------------+---------+---------------------+----------------------+
206           a_row < Playing , stop        , Stopped , &p::stop_playback                          >,
207           a_row < Playing , pause       , Paused  , &p::pause_playback                         >,
208           a_row < Playing , open_close  , Open    , &p::stop_and_open                          >,
209             //  +---------+-------------+---------+---------------------+----------------------+
210           a_row < Paused  , end_pause   , Playing , &p::resume_playback                        >,
211           a_row < Paused  , stop        , Stopped , &p::stop_playback                          >,
212           a_row < Paused  , open_close  , Open    , &p::stop_and_open                          >
213             //  +---------+-------------+---------+---------------------+----------------------+
214         > {};
215         // Replaces the default no-transition response.
216         template <class FSM,class Event>
217         void no_transition(Event const& e, FSM&,int state)
218         {
219             BOOST_FAIL("no_transition called!");
220         }
221         // init counters
222         template <class Event,class FSM>
223         void on_entry(Event const&,FSM& fsm) 
224         {
225             fsm.template get_state<player_::Stopped&>().entry_counter=0;
226             fsm.template get_state<player_::Stopped&>().exit_counter=0;
227             fsm.template get_state<player_::Open&>().entry_counter=0;
228             fsm.template get_state<player_::Open&>().exit_counter=0;
229             fsm.template get_state<player_::Empty&>().entry_counter=0;
230             fsm.template get_state<player_::Empty&>().exit_counter=0;
231             fsm.template get_state<player_::Playing&>().entry_counter=0;
232             fsm.template get_state<player_::Playing&>().exit_counter=0;
233             fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().entry_counter=0;
234             fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().exit_counter=0;
235             fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().entry_counter=0;
236             fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().exit_counter=0;
237             fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().entry_counter=0;
238             fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().exit_counter=0;
239             fsm.template get_state<player_::Paused&>().entry_counter=0;
240             fsm.template get_state<player_::Paused&>().exit_counter=0;
241         }
242
243     };
244     // Pick a back-end
245     typedef msm::back::state_machine<player_> player;
246
247 //    static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
248
249
250     BOOST_AUTO_TEST_CASE( my_test )
251     {     
252         player p;
253
254         p.start(); 
255         BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly");
256
257         p.process_event(open_close()); 
258         BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active"); //Open
259         BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly");
260         BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly");
261
262         p.process_event(open_close()); 
263         BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
264         BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
265         BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
266         BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly");
267
268         p.process_event(cd_detected("louie, louie")); 
269         BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
270         BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly");
271         BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly");
272
273         p.process_event(play());
274         BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
275         BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly");
276         BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly");
277         BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly");
278         BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
279         BOOST_CHECK_MESSAGE(
280             p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().entry_counter == 1,
281             "Song1 entry not called correctly");
282
283         p.process_event(NextSong());
284         BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
285         BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
286         BOOST_CHECK_MESSAGE(
287             p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 1,
288             "Song2 entry not called correctly");
289         BOOST_CHECK_MESSAGE(
290             p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().exit_counter == 1,
291             "Song1 exit not called correctly");
292         BOOST_CHECK_MESSAGE(
293             p.get_state<player_::Playing&>().start_next_song_counter == 0,
294             "submachine action not called correctly");
295
296         p.process_event(NextSong());
297         BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
298         BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 2,"Song3 should be active");
299         BOOST_CHECK_MESSAGE(
300             p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().entry_counter == 1,
301             "Song3 entry not called correctly");
302         BOOST_CHECK_MESSAGE(
303             p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().exit_counter == 1,
304             "Song2 exit not called correctly");
305         BOOST_CHECK_MESSAGE(
306             p.get_state<player_::Playing&>().start_next_song_counter == 1,
307             "submachine action not called correctly");
308
309         p.process_event(PreviousSong());
310         BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
311         BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
312         BOOST_CHECK_MESSAGE(
313             p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 2,
314             "Song2 entry not called correctly");
315         BOOST_CHECK_MESSAGE(
316             p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().exit_counter == 1,
317             "Song3 exit not called correctly");
318         BOOST_CHECK_MESSAGE(
319             p.get_state<player_::Playing&>().start_prev_song_guard_counter == 1,
320             "submachine guard not called correctly");
321
322         p.process_event(pause());
323         BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
324         BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly");
325         BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly");
326
327         std::ofstream ofs("fsm.txt");
328         // save fsm to archive (current state is Pause, Playing is in Song2)
329         {
330             boost::archive::text_oarchive oa(ofs);
331             // write class instance to archive
332             oa << p;
333         }
334         // reload fsm in state Open
335         player p2;
336         {
337             // create and open an archive for input
338             std::ifstream ifs("fsm.txt");
339             boost::archive::text_iarchive ia(ifs);
340             // read class state from archive
341             ia >> p2;
342         }
343         // go back to Playing
344         p2.process_event(end_pause());  
345         BOOST_CHECK_MESSAGE(p2.current_state()[0] == 3,"Playing should be active"); //Playing
346         BOOST_CHECK_MESSAGE(p2.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
347
348         p2.process_event(pause()); 
349         BOOST_CHECK_MESSAGE(p2.current_state()[0] == 4,"Paused should be active"); //Paused
350
351         p2.process_event(stop());  
352         BOOST_CHECK_MESSAGE(p2.current_state()[0] == 0,"Stopped should be active"); //Stopped
353
354         p2.process_event(stop());  
355         BOOST_CHECK_MESSAGE(p2.current_state()[0] == 0,"Stopped should be active"); //Stopped
356
357         p2.process_event(play());  
358         BOOST_CHECK_MESSAGE(p2.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
359     }
360 }
361 // eliminate object tracking (even if serialized through a pointer)
362 // at the risk of a programming error creating duplicate objects.
363 // this is to get rid of warning because p is not const
364 BOOST_CLASS_TRACKING(player, boost::serialization::track_never)
365