1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
|
// Copyright 2010 Christophe Henry
// henry UNDERSCORE christophe AT hotmail DOT com
// This is an extended version of the state machine available in the boost::mpl library
// Distributed under the same license as the original.
// Copyright for the original version:
// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
// under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <vector>
#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/euml/euml.hpp>
using namespace std;
using namespace boost::msm::front::euml;
namespace msm = boost::msm;
// how long the timer will ring when countdown elapsed.
#define RINGING_TIME 5
namespace // Concrete FSM implementation
{
// events
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_timer)
BOOST_MSM_EUML_ATTRIBUTES((attributes_ << m_timer ), start_timer_attr)
BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(start_timer,start_timer_attr)
BOOST_MSM_EUML_EVENT(stop_timer)
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_tick)
BOOST_MSM_EUML_ATTRIBUTES((attributes_ << m_tick ), tick_attr)
BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(tick,tick_attr)
BOOST_MSM_EUML_EVENT(start_ringing)
// Concrete FSM implementation
// The list of FSM states
BOOST_MSM_EUML_ACTION(Stopped_Entry)
{
template <class Event,class FSM,class STATE>
void operator()(Event const&,FSM&,STATE& )
{
std::cout << "entering: Stopped" << std::endl;
}
};
BOOST_MSM_EUML_STATE(( Stopped_Entry ),Stopped)
BOOST_MSM_EUML_ACTION(Started_Entry)
{
template <class Event,class FSM,class STATE>
void operator()(Event const&,FSM&,STATE& )
{
std::cout << "entering: Started" << std::endl;
}
};
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_counter)
BOOST_MSM_EUML_STATE(( Started_Entry,
no_action,
attributes_ << m_counter
),
Started)
BOOST_MSM_EUML_ACTION(Ringing_Entry)
{
template <class Event,class FSM,class STATE>
void operator()(Event const&,FSM&,STATE& )
{
std::cout << "entering: Ringing" << std::endl;
}
};
BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int,m_ringing_cpt)
BOOST_MSM_EUML_STATE(( Ringing_Entry,
no_action,
attributes_ << m_ringing_cpt
),
Ringing)
// external function
void do_ring(int ringing_time) {std::cout << "ringing " << ringing_time << " s" << std::endl;}
// create functor and eUML function
BOOST_MSM_EUML_FUNCTION(Ring_ , do_ring , ring_ , void , void )
// replaces the old transition table
BOOST_MSM_EUML_TRANSITION_TABLE((
// +------------------------------------------------------------------------------+
// When we start the countdown, the countdown value is not hardcoded but contained in the start_timer event.
// We copy this value into Started
Started == Stopped + start_timer /(target_(m_counter)= event_(m_timer)) ,
Stopped == Started + stop_timer ,
// internal transition
Started + tick
// we here use the message queue to move to Started when the countdown is finished
// to do this we put start_ringing into the message queue
/ if_then_( (source_(m_counter) -= event_(m_tick) ) <= Int_<0>(),
process_(start_ringing) ) ,
// when we start ringing, we give to the state its hard-coded ringing time.
Ringing == Started + start_ringing
/ (target_(m_ringing_cpt) = Int_<RINGING_TIME>(),
// call the external do_ring function
ring_(Int_<RINGING_TIME>())) ,
// to change a bit, we now do not use the message queue but a transition conflict to solve the same problem.
// When tick is fired, we have an internal transition Ringing -> Ringing, as long as Counter > 0
Ringing + tick [ source_(m_ringing_cpt) - event_(m_tick) > Int_<0>() ]
/(target_(m_ringing_cpt) -= event_(m_tick) ) ,
// And we move to Stopped when the counter is 0
Stopped == Ringing + tick[source_(m_ringing_cpt)-event_(m_tick) <= Int_<0>()] ,
// we let the user manually stop the ringing by pressing any button
Stopped == Ringing + stop_timer ,
Stopped == Ringing + start_timer
// +------------------------------------------------------------------------------+
),transition_table)
// create a state machine "on the fly"
BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
init_ << Stopped // Init State
),
SimpleTimer_) //fsm name
// choice of back-end
typedef msm::back::state_machine<SimpleTimer_> SimpleTimer;
//
// Testing utilities.
//
static char const* const state_names[] = { "Stopped", "Started","Ringing" };
void pstate(SimpleTimer const& p)
{
std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
}
void test()
{
SimpleTimer p;
// needed to start the highest-level SM. This will call on_entry and mark the start of the SM
p.start();
p.process_event(start_timer(5));pstate(p); //timer set to 5 ticks
p.process_event(tick(2));pstate(p);
p.process_event(tick(1));pstate(p);
p.process_event(tick(1));pstate(p);
p.process_event(tick(1));pstate(p);
// we are now ringing, let it ring a bit
p.process_event(tick(2));pstate(p);
p.process_event(tick(1));pstate(p);
p.process_event(tick(1));pstate(p);
p.process_event(tick(1));pstate(p);
}
}
int main()
{
test();
return 0;
}
|