-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathtest_oneshot.py
243 lines (227 loc) · 8.98 KB
/
test_oneshot.py
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
#
# License: BSD
# https://raw.githubusercontent.com/splintered-reality/py_trees/devel/LICENSE
#
##############################################################################
# Imports
##############################################################################
# enable some python3 compatibility options:
# (unicode_literals not compatible with python2 uuid module)
from __future__ import absolute_import, print_function
import py_trees
import py_trees.console as console
##############################################################################
# Logging Level
##############################################################################
py_trees.logging.level = py_trees.logging.Level.DEBUG
logger = py_trees.logging.Logger("Nosetest")
##############################################################################
# Subtrees
##############################################################################
def test_oneshot_with_fail_causes_reentry() -> None:
def decorator_oneshot(
child: py_trees.behaviour.Behaviour, policy: py_trees.common.OneShotPolicy
) -> py_trees.decorators.OneShot:
return py_trees.decorators.OneShot(name="OneShot", child=child, policy=policy)
def idiom_oneshot(
child: py_trees.behaviour.Behaviour, policy: py_trees.common.OneShotPolicy
) -> py_trees.behaviour.Behaviour:
return py_trees.idioms.oneshot(
name="Oneshot Idiom",
variable_name="oneshot",
behaviour=child,
policy=policy,
)
for title, create_tree in [
("Idiom", idiom_oneshot),
("Decorator", decorator_oneshot),
]:
for policy in py_trees.common.OneShotPolicy:
# Setup
console.banner(
"{} w/ Failure Causes Reentry [policy: {}]".format(title, policy.name)
)
py_trees.tests.clear_blackboard()
# Tree
fail_then_run = py_trees.behaviours.StatusQueue(
name="Fail Then Run",
queue=[
py_trees.common.Status.FAILURE,
py_trees.common.Status.RUNNING,
py_trees.common.Status.RUNNING,
],
eventually=py_trees.common.Status.FAILURE,
)
oneshot = create_tree(fail_then_run, policy)
# Ticking
py_trees.tests.tick_tree(
root=oneshot,
from_tick=1,
to_tick=1,
print_snapshot=True,
print_blackboard=True,
)
print(console.green + "Asserts (After Failure)" + console.reset)
print(
console.cyan
+ " OneShot Status: "
+ console.yellow
+ "{}".format(oneshot.status)
+ console.reset
+ " [{}]".format(py_trees.common.Status.FAILURE)
)
print(
console.cyan
+ " Child Status : "
+ console.yellow
+ "{}".format(fail_then_run.status)
+ console.reset
+ " [{}]".format(py_trees.common.Status.FAILURE)
)
assert oneshot.status == py_trees.common.Status.FAILURE
assert fail_then_run.status == py_trees.common.Status.FAILURE
# Ticking
py_trees.tests.tick_tree(
root=oneshot,
from_tick=2,
to_tick=2,
print_snapshot=True,
print_blackboard=True,
)
expected_oneshot_status = {
py_trees.common.OneShotPolicy.ON_COMPLETION: py_trees.common.Status.FAILURE,
py_trees.common.OneShotPolicy.ON_SUCCESSFUL_COMPLETION: py_trees.common.Status.RUNNING,
}
expected_behaviour_status = {
"Idiom": {
py_trees.common.OneShotPolicy.ON_COMPLETION: py_trees.common.Status.INVALID,
py_trees.common.OneShotPolicy.ON_SUCCESSFUL_COMPLETION: py_trees.common.Status.RUNNING,
},
"Decorator": {
py_trees.common.OneShotPolicy.ON_COMPLETION: py_trees.common.Status.FAILURE,
py_trees.common.OneShotPolicy.ON_SUCCESSFUL_COMPLETION: py_trees.common.Status.RUNNING,
},
}
print(console.green + "Asserts (After Running)" + console.reset)
print(
console.cyan
+ " OneShot Status: "
+ console.yellow
+ "{}".format(oneshot.status)
+ console.reset
+ " [{}]".format(expected_oneshot_status[policy])
)
print(
console.cyan
+ " Child Status : "
+ console.yellow
+ "{}".format(fail_then_run.status)
+ console.reset
+ " [{}]".format(expected_behaviour_status[title][policy])
)
assert oneshot.status == expected_oneshot_status[policy]
assert fail_then_run.status == expected_behaviour_status[title][policy]
def untest_oneshot_with_subtrees_and_interrupt() -> None:
sequence_subtree = py_trees.composites.Sequence(
name="Sequence",
memory=True,
children=[
py_trees.behaviours.Success(name="Success 1"),
py_trees.behaviours.Success(name="Success 2"),
],
)
selector_subtree = py_trees.composites.Selector(
name="Selector",
memory=False,
children=[
py_trees.behaviours.Failure(name="Failure"),
py_trees.behaviours.Success(name="Success"),
],
)
for worker_subtree in [sequence_subtree, selector_subtree]:
decorator_oneshot = py_trees.decorators.OneShot(
name="OneShot",
child=worker_subtree,
policy=py_trees.common.OneShotPolicy.ON_SUCCESSFUL_COMPLETION,
)
idiom_oneshot = py_trees.idioms.oneshot(
name="OneShot", variable_name="oneshot", behaviour=worker_subtree
)
for title, oneshot in [
("Idiom", idiom_oneshot),
("Decorator", decorator_oneshot),
]:
console.banner("{} w/ {} and Interrupt".format(title, worker_subtree.name))
py_trees.tests.clear_blackboard()
# Tree with higher priority branch
root = py_trees.composites.Selector(name="Root", memory=False)
high_priority = py_trees.behaviours.StatusQueue(
name="High Priority",
queue=[
py_trees.common.Status.FAILURE,
py_trees.common.Status.SUCCESS,
],
eventually=None,
)
root.add_children([high_priority, oneshot])
# Ticking
py_trees.tests.tick_tree(
root=root,
from_tick=1,
to_tick=1,
print_snapshot=True,
print_blackboard=True,
)
py_trees.tests.tick_tree(
root=root,
from_tick=2,
to_tick=2,
print_snapshot=True,
print_blackboard=True,
)
print(console.green + "Asserts (After Interrupt)" + console.reset)
print(
console.cyan
+ " OneShot Status: "
+ console.yellow
+ "{}".format(oneshot.status)
+ console.reset
+ " [{}]".format(py_trees.common.Status.INVALID)
)
print(
console.cyan
+ " Child Status : "
+ console.yellow
+ "{}".format(worker_subtree.status)
+ console.reset
+ " [{}]".format(py_trees.common.Status.INVALID)
)
assert oneshot.status == py_trees.common.Status.INVALID
assert worker_subtree.status == py_trees.common.Status.INVALID
py_trees.tests.tick_tree(
root=root,
from_tick=3,
to_tick=3,
print_snapshot=True,
print_blackboard=True,
)
print(console.green + "Asserts (After Re-entry)" + console.reset)
print(
console.cyan
+ " OneShot Status: "
+ console.yellow
+ "{}".format(oneshot.status)
+ console.reset
+ " [{}]".format(py_trees.common.Status.SUCCESS)
)
print(
console.cyan
+ " Child Status : "
+ console.yellow
+ "{}".format(worker_subtree.status)
+ console.reset
+ " [{}]".format(py_trees.common.Status.INVALID)
)
assert oneshot.status == py_trees.common.Status.SUCCESS
assert worker_subtree.status == py_trees.common.Status.INVALID # type: ignore[unreachable]