-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
guards_actions.cpp
229 lines (193 loc) · 6.6 KB
/
guards_actions.cpp
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "hsm/hsm.h"
#include <boost/hana.hpp>
#include <boost/hana/experimental/printable.hpp>
#include <boost/hof.hpp>
#include <gtest/gtest.h>
#include <future>
#include <memory>
using namespace ::testing;
using namespace boost::hana;
namespace hsm::test {
// States
struct S1 {
bool isS1 = true;
};
struct S2 {
bool isS2 = true;
};
struct S3 {
};
// Events
struct e1 {
e1(const std::shared_ptr<std::promise<void>>& called)
: called(called)
{
}
std::shared_ptr<std::promise<void>> called;
};
struct e2 {
};
struct e3 {
};
struct e4 {
};
struct e5 {
};
struct e6 {
};
struct e7 {
};
struct e8 {
};
struct e9 {
};
struct e10 {
};
struct e11 {
};
template <class Event, class Source, class Target>
auto print_transition(Event event, Source source, Target target, std::string context)
{
std::cout << boost::hana::experimental::print(boost::hana::typeid_(source)) << " + "
<< boost::hana::experimental::print(boost::hana::typeid_(event)) << " = "
<< boost::hana::experimental::print(boost::hana::typeid_(target)) << "(" << context
<< ")" << std::endl;
}
// Guards
const auto fail = [](auto /*event*/, auto /*source*/, auto /*target*/) { return false; };
const auto success = [](auto /*event*/, auto /*source*/, auto /*target*/) { return true; };
// Actions
const auto a2 = [](auto event, auto /*source*/, auto /*target*/) { event.called->set_value(); };
const auto accessSourceAndTarget = [](auto /*event*/, auto source, auto target) {
EXPECT_TRUE(source.isS1);
EXPECT_TRUE(target.isS2);
};
struct Functor {
template <class Event, class Source, class Target>
auto operator()(Event event, Source source, Target target) const
{
print_transition(event, source, target, "Functor based action");
}
};
template <class Event, class Source, class Target>
auto free_function(Event event, Source source, Target target)
{
print_transition(event, source, target, "Function based action");
}
auto lambda = [](auto event, auto source, auto target) {
print_transition(event, source, target, "Lambda based action");
};
struct SubState {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> / a2 = hsm::state<S1>
);
// clang-format on
}
};
struct MainState {
BOOST_HOF_LIFT_CLASS(ff, free_function);
static constexpr auto make_transition_table()
{
// Instead of writing adapter from hand you
// can create these kind of adapters with boost hof
// using BOOST_HOF_LIFT or BOOST_HOF_LIFT_CLASS.
// See the boost hof documentation for more details.
// I did not provide these example with boost hof because
// there were problems building it on windows.
constexpr auto free_function_adapter
= [](auto&&... args) { free_function(std::forward<decltype(args)>(args)...); };
constexpr auto member_function_adapter = [](auto&&... args) {
MainState::member_function(std::forward<decltype(args)>(args)...);
};
// clang-format off
return hsm::transition_table(
* hsm::state<S1> + hsm::event<e1> / a2 = hsm::state<S1>
, hsm::state<S1> + hsm::event<e2> = hsm::state<SubState>
, hsm::state<S1> + hsm::event<e3> [fail] = hsm::state<S2>
, hsm::state<S1> + hsm::event<e4> [success] = hsm::state<S2>
, hsm::state<S1> + hsm::event<e9> [fail] = hsm::state<S2>
, hsm::state<S1> + hsm::event<e9> [success] = hsm::state<S2>
, hsm::state<S1> + hsm::event<e10> = hsm::state<S3>
, hsm::state<S1> + hsm::event<e11> / accessSourceAndTarget = hsm::state<S2>
, hsm::state<S3> [fail] = hsm::state<S1>
// The following transitions show different possibilities to provide actions
, hsm::state<S1> + hsm::event<e5> / Functor{} = hsm::state<S2>
, hsm::state<S1> + hsm::event<e6> / free_function_adapter = hsm::state<S2>
, hsm::state<S1> + hsm::event<e7> / lambda = hsm::state<S2>
, hsm::state<S1> + hsm::event<e8> / member_function_adapter = hsm::state<S2>
);
// clang-format on
}
private:
template <class Event, class Source, class Target>
static constexpr auto member_function(Event event, Source source, Target target)
{
print_transition(event, source, target, "Member function based action");
}
};
class GuardsActionsTests : public Test {
protected:
hsm::sm<MainState> sm;
};
TEST_F(GuardsActionsTests, should_call_action)
{
auto actionCalled = std::make_shared<std::promise<void>>();
sm.process_event(e1 { actionCalled });
ASSERT_TRUE(
std::future_status::ready == actionCalled->get_future().wait_for(std::chrono::seconds(1)));
}
TEST_F(GuardsActionsTests, should_call_substate_action)
{
auto actionCalled = std::make_shared<std::promise<void>>();
sm.process_event(e2 {});
sm.process_event(e1 { actionCalled });
ASSERT_TRUE(
std::future_status::ready == actionCalled->get_future().wait_for(std::chrono::seconds(1)));
}
TEST_F(GuardsActionsTests, should_block_transition_guard)
{
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e3 {});
ASSERT_TRUE(sm.is(hsm::state<S1>));
}
TEST_F(GuardsActionsTests, should_not_block_transition_by_guard)
{
ASSERT_TRUE(sm.is(hsm::state<S1>));
sm.process_event(e4 {});
ASSERT_TRUE(sm.is(hsm::state<S2>));
}
TEST_F(GuardsActionsTests, should_use_functor)
{
sm.process_event(e5 {});
}
TEST_F(GuardsActionsTests, should_use_free_function)
{
sm.process_event(e6 {});
}
TEST_F(GuardsActionsTests, should_use_lambda_function)
{
sm.process_event(e7 {});
}
TEST_F(GuardsActionsTests, should_use_member_function)
{
sm.process_event(e8 {});
}
TEST_F(GuardsActionsTests, should_recognize_same_transition_with_different_guards)
{
sm.process_event(e9 {});
ASSERT_TRUE(sm.is(hsm::state<S2>));
}
TEST_F(GuardsActionsTests, should_guard_anon_transition)
{
sm.process_event(e10 {});
ASSERT_TRUE(sm.is(hsm::state<S3>));
}
TEST_F(GuardsActionsTests, should_access_source_and_target_member)
{
sm.process_event(e11 {});
ASSERT_TRUE(sm.is(hsm::state<S2>));
}
}