forked from splintered-reality/py_trees
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_parallels.py
516 lines (456 loc) · 21.4 KB
/
test_parallels.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#!/usr/bin/env python
#
# License: BSD
# https://raw.githubusercontent.com/splintered-reality/py_trees/devel/LICENSE
#
##############################################################################
# Imports
##############################################################################
import itertools
import py_trees
import py_trees.console as console
import py_trees.tests
import pytest
##############################################################################
# Logging Level
##############################################################################
py_trees.logging.level = py_trees.logging.Level.DEBUG
logger = py_trees.logging.Logger("Tests")
##############################################################################
# Tests
##############################################################################
def test_parallel_failure() -> None:
console.banner("Parallel Failure")
success_on_one = py_trees.common.ParallelPolicy.SuccessOnOne()
success_on_all_synchronised = py_trees.common.ParallelPolicy.SuccessOnAll(
synchronise=True
)
success_on_all_not_synchronised = py_trees.common.ParallelPolicy.SuccessOnAll(
synchronise=False
)
for policy in [
success_on_one,
success_on_all_synchronised,
success_on_all_not_synchronised,
]:
root = py_trees.composites.Parallel(name="Parallel", policy=policy)
failure = py_trees.behaviours.Failure("Failure")
success = py_trees.behaviours.Success("Success")
root.add_child(failure)
root.add_child(success)
print(py_trees.display.unicode_tree(root))
visitor = py_trees.visitors.DebugVisitor()
py_trees.tests.tick_tree(root, 1, 1, visitors=[visitor])
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.FAILURE")
assert root.status == py_trees.common.Status.FAILURE
print("failure.status == py_trees.common.Status.FAILURE")
assert failure.status == py_trees.common.Status.FAILURE
print("success.status == py_trees.common.Status.SUCCESS")
assert success.status == py_trees.common.Status.SUCCESS
failure = py_trees.behaviours.Failure("Failure")
success = py_trees.behaviours.Success("Success")
for child, synchronise in itertools.product([success, failure], [True, False]):
policy = py_trees.common.ParallelPolicy.SuccessOnSelected(
children=[child], synchronise=synchronise
)
root = py_trees.composites.Parallel(name="Parallel", policy=policy)
root.add_child(failure)
root.add_child(success)
print(py_trees.display.unicode_tree(root))
visitor = py_trees.visitors.DebugVisitor()
py_trees.tests.tick_tree(root, 1, 1, visitors=[visitor])
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.FAILURE")
assert root.status == py_trees.common.Status.FAILURE
print("failure.status == py_trees.common.Status.FAILURE")
assert failure.status == py_trees.common.Status.FAILURE
print("success.status == py_trees.common.Status.SUCCESS")
assert success.status == py_trees.common.Status.SUCCESS
# cleanup so they can be added again
root.remove_child(failure)
root.remove_child(success)
def test_parallel_success() -> None:
console.banner("Parallel Success")
root = py_trees.composites.Parallel(
name="Parallel", policy=py_trees.common.ParallelPolicy.SuccessOnAll()
)
success1 = py_trees.behaviours.Success("Success1")
success2 = py_trees.behaviours.Success("Success2")
root.add_child(success1)
root.add_child(success2)
print(py_trees.display.unicode_tree(root))
visitor = py_trees.visitors.DebugVisitor()
py_trees.tests.tick_tree(root, 1, 1, visitors=[visitor])
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.SUCCESS")
assert root.status == py_trees.common.Status.SUCCESS
print("success1.status == py_trees.common.Status.SUCCESS")
assert success1.status == py_trees.common.Status.SUCCESS
print("success2.status == py_trees.common.Status.SUCCESS")
assert success2.status == py_trees.common.Status.SUCCESS
def test_parallel_running() -> None:
console.banner("Parallel Running")
root = py_trees.composites.Parallel(
name="Parallel", policy=py_trees.common.ParallelPolicy.SuccessOnAll()
)
success_after_1 = py_trees.behaviours.StatusQueue(
name="SuccessAfter1",
queue=[py_trees.common.Status.RUNNING],
eventually=py_trees.common.Status.SUCCESS,
)
running = py_trees.behaviours.Running("Running")
success_every_other = py_trees.behaviours.SuccessEveryN("SuccessEveryOther", 2)
root.add_child(success_after_1)
root.add_child(running)
root.add_child(success_every_other)
print(py_trees.display.unicode_tree(root))
visitor = py_trees.visitors.DebugVisitor()
py_trees.tests.tick_tree(root, 1, 1, visitors=[visitor])
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.FAILURE")
assert root.status == py_trees.common.Status.FAILURE
print("success_after_1.status == py_trees.common.Status.INVALID")
assert success_after_1.status == py_trees.common.Status.INVALID
print("running.status == py_trees.common.Status.INVALID")
assert running.status == py_trees.common.Status.INVALID
print("success_every_other.status == py_trees.common.Status.FAILURE")
assert success_every_other.status == py_trees.common.Status.FAILURE
py_trees.tests.tick_tree(root, 2, 2, visitors=[visitor])
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.RUNNING")
assert root.status == py_trees.common.Status.RUNNING
print("success_after_1.status == py_trees.common.Status.SUCCESS")
assert success_after_1.status == py_trees.common.Status.SUCCESS
print("running.status == py_trees.common.Status.RUNNING")
assert running.status == py_trees.common.Status.RUNNING
print("success_every_other.status == py_trees.common.Status.SUCCESS")
assert success_every_other.status == py_trees.common.Status.SUCCESS
def test_parallel_success_on_one() -> None:
console.banner("Parallel Success on One")
print("")
root = py_trees.composites.Parallel(
name="Parallel", policy=py_trees.common.ParallelPolicy.SuccessOnOne()
)
running1 = py_trees.behaviours.Running("Running1")
success = py_trees.behaviours.Success("Success")
running2 = py_trees.behaviours.Running("Running2")
root.add_child(running1)
root.add_child(success)
root.add_child(running2)
print(py_trees.display.unicode_tree(root))
visitor = py_trees.visitors.DebugVisitor()
py_trees.tests.tick_tree(root, 1, 1, visitors=[visitor], print_snapshot=True)
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.SUCCESS")
assert root.status == py_trees.common.Status.SUCCESS
print("running1.status == py_trees.common.Status.INVALID")
assert running1.status == py_trees.common.Status.INVALID
print("success.status == py_trees.common.Status.SUCCESS")
assert success.status == py_trees.common.Status.SUCCESS
print("running2.status == py_trees.common.Status.INVALID")
assert running2.status == py_trees.common.Status.INVALID
def test_parallel_success_on_selected() -> None:
console.banner("Parallel Success on Selected")
print("")
running1 = py_trees.behaviours.Running(name="Running1")
success1 = py_trees.behaviours.StatusQueue(
name="Success1",
queue=[py_trees.common.Status.RUNNING],
eventually=py_trees.common.Status.SUCCESS,
)
success2 = py_trees.behaviours.StatusQueue(
name="Success1",
queue=[py_trees.common.Status.RUNNING],
eventually=py_trees.common.Status.SUCCESS,
)
running2 = py_trees.behaviours.Running(name="Running2")
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnSelected(
children=[success1, success2]
),
)
root.add_children([running1, success1, success2, running2])
print(py_trees.display.unicode_tree(root))
visitor = py_trees.visitors.DebugVisitor()
py_trees.tests.tick_tree(root, 1, 1, visitors=[visitor], print_snapshot=True)
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.RUNNING")
assert root.status == py_trees.common.Status.RUNNING
print("running1.status == py_trees.common.Status.RUNNING")
assert running1.status == py_trees.common.Status.RUNNING
print("success1.status == py_trees.common.Status.RUNNING")
assert success1.status == py_trees.common.Status.RUNNING
print("success2.status == py_trees.common.Status.RUNNING")
assert success2.status == py_trees.common.Status.RUNNING
print("running2.status == py_trees.common.Status.RUNNING")
assert running2.status == py_trees.common.Status.RUNNING
py_trees.tests.tick_tree(root, 2, 3, visitors=[visitor], print_snapshot=True)
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.SUCCESS")
assert root.status == py_trees.common.Status.SUCCESS
print("running1.status == py_trees.common.Status.INVALID")
assert running1.status == py_trees.common.Status.INVALID
print("success1.status == py_trees.common.Status.SUCCESS")
assert success1.status == py_trees.common.Status.SUCCESS
print("success2.status == py_trees.common.Status.SUCCESS")
assert success2.status == py_trees.common.Status.SUCCESS
print("running2.status == py_trees.common.Status.INVALID")
assert running2.status == py_trees.common.Status.INVALID
def test_parallel_success_on_selected_invalid_configuration() -> None:
console.banner("Parallel Success on Selected [Invalid Configuration]")
print("")
running1 = py_trees.behaviours.Running(name="Running1")
running2 = py_trees.behaviours.Running(name="Running2")
running3 = py_trees.behaviours.Running(name="Running3")
different_policy = py_trees.common.ParallelPolicy.SuccessOnSelected(
children=[running1, running2]
)
empty_policy = py_trees.common.ParallelPolicy.SuccessOnSelected(children=[])
parallel = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnAll(),
children=[running1, running3],
)
for policy in [different_policy, empty_policy]:
print("")
print(
console.cyan
+ "Policy Children: "
+ console.yellow
+ str([c.name for c in policy.children])
+ console.reset
)
print(
console.cyan
+ "Parallel Children: "
+ console.yellow
+ str([c.name for c in parallel.children])
+ console.reset
)
parallel.policy = policy
print("\n--------- Assertions ---------\n")
print("setup() raises a 'RuntimeError' due to invalid configuration")
with pytest.raises(RuntimeError) as context: # if raised, context survives
parallel.setup()
py_trees.tests.print_assert_details(
"RuntimeError raised", "raised", "not raised"
)
py_trees.tests.print_assert_details("RuntimeError raised", "yes", "yes")
assert "RuntimeError" == context.typename
py_trees.tests.print_assert_details(
"Substring match", "SuccessOnSelected", f"{context.value}"
)
assert "SuccessOnSelected" in str(context.value)
print("initialise() raises a 'RuntimeError' due to invalid configuration")
with pytest.raises(RuntimeError) as context: # if raised, context survives
parallel.tick_once()
py_trees.tests.print_assert_details(
"RuntimeError raised", "raised", "not raised"
)
py_trees.tests.print_assert_details("RuntimeError raised", "yes", "yes")
assert "RuntimeError" == context.typename
py_trees.tests.print_assert_details(
"Substring match", "SuccessOnSelected", f"{context.value}"
)
assert "SuccessOnSelected" in str(context.value)
def test_parallel_synchronisation() -> None:
console.banner("Parallel Synchronisation")
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnAll(synchronise=True),
)
success = py_trees.behaviours.Success(name="Success")
success_every_second = py_trees.decorators.FailureIsRunning(
name="SuccessEverySecond",
child=py_trees.behaviours.SuccessEveryN(name="Flipper", n=2),
)
root.add_children([success, success_every_second])
print(py_trees.display.unicode_tree(root))
debug_visitor = py_trees.visitors.DebugVisitor()
snapshot_visitor = py_trees.visitors.SnapshotVisitor()
py_trees.tests.tick_tree(
root, 1, 1, visitors=[debug_visitor, snapshot_visitor], print_snapshot=True
)
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.RUNNING")
assert root.status == py_trees.common.Status.RUNNING
print("success.status == py_trees.common.Status.SUCCESS")
assert success.status == py_trees.common.Status.SUCCESS
print("success_every_second.status == py_trees.common.Status.RUNNING")
assert success_every_second.status == py_trees.common.Status.RUNNING
snapshot_visitor.initialise()
py_trees.tests.tick_tree(
root, 2, 2, visitors=[debug_visitor, snapshot_visitor], print_snapshot=True
)
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.SUCCESS")
assert root.status == py_trees.common.Status.SUCCESS
print("success.status == py_trees.common.Status.SUCCESS")
assert success.status == py_trees.common.Status.SUCCESS
print("success_every_second.status == py_trees.common.Status.SUCCESS")
assert success_every_second.status == py_trees.common.Status.SUCCESS
print(
"success [id: {}] did not get ticked [snapshot: {}]".format(
success.id, [str(ident) for ident in snapshot_visitor.visited.keys()]
)
)
assert success.id not in snapshot_visitor.visited
snapshot_visitor.initialise()
py_trees.tests.tick_tree(
root, 3, 3, visitors=[debug_visitor, snapshot_visitor], print_snapshot=True
)
print("\n--------- Assertions ---------\n")
print("root.status == py_trees.common.Status.RUNNING")
assert root.status == py_trees.common.Status.RUNNING
print("success.status == py_trees.common.Status.SUCCESS")
assert success.status == py_trees.common.Status.SUCCESS
print("success_every_second.status == py_trees.common.Status.RUNNING")
assert success_every_second.status == py_trees.common.Status.RUNNING
def test_parallel_no_synchronisation() -> None:
console.banner("Parallel No Synchronisation")
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnAll(synchronise=False),
)
success_every_two = py_trees.decorators.FailureIsRunning(
name="SuccessEverySecond",
child=py_trees.behaviours.SuccessEveryN(name="Flipper2", n=2),
)
success_every_three = py_trees.decorators.FailureIsRunning(
name="SuccessEveryThird",
child=py_trees.behaviours.SuccessEveryN(name="Flipper3", n=3),
)
debug_visitor = py_trees.visitors.DebugVisitor()
snapshot_visitor = py_trees.visitors.SnapshotVisitor()
root.add_children([success_every_two, success_every_three])
for counter in range(1, 6):
snapshot_visitor.initialise()
py_trees.tests.tick_tree(
root,
counter,
counter,
visitors=[debug_visitor, snapshot_visitor],
print_snapshot=True,
)
print(counter)
if counter % 2 == 0:
print("success_every_two.status == py_trees.common.Status.SUCCESS")
assert success_every_two.status == py_trees.common.Status.SUCCESS
elif counter % 3 == 0:
print("success_every_three.status == py_trees.common.Status.SUCCESS")
assert success_every_three.status == py_trees.common.Status.SUCCESS
else:
print("success_every_two.status == py_trees.common.Status.RUNNING")
assert success_every_two.status == py_trees.common.Status.RUNNING
print("success_every_three.status == py_trees.common.Status.RUNNING")
assert success_every_three.status == py_trees.common.Status.RUNNING
print("root.status == py_trees.common.Status.RUNNING")
assert root.status == py_trees.common.Status.RUNNING
snapshot_visitor.initialise()
py_trees.tests.tick_tree(root, 6, 6, print_snapshot=True)
print("success_every_two.status == py_trees.common.Status.SUCCESS")
assert success_every_two.status == py_trees.common.Status.SUCCESS
print("success_every_three.status == py_trees.common.Status.SUCCESS")
assert success_every_three.status == py_trees.common.Status.SUCCESS
print("root.status == py_trees.common.Status.SUCCESS")
assert root.status == py_trees.common.Status.SUCCESS
def test_add_tick_remove_with_current_child() -> None:
console.banner("Add (Failure)-Tick-Remove with Current Child")
py_trees.tests.print_assert_banner()
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnAll(synchronise=False),
)
child = py_trees.behaviours.Failure(name="Failure")
root.add_child(child)
root.tick_once()
print(py_trees.display.unicode_tree(root, show_status=True))
root.remove_child(child)
print(py_trees.display.unicode_tree(root, show_status=True))
py_trees.tests.print_assert_details("Current Child", None, root.current_child)
assert root.current_child is None
root.add_child(child)
root.tick_once()
print(py_trees.display.unicode_tree(root, show_status=True))
root.remove_all_children()
print(py_trees.display.unicode_tree(root, show_status=True))
py_trees.tests.print_assert_details("Current Child", None, root.current_child)
assert root.current_child is None
replacement = py_trees.behaviours.Success(name="Replacement")
root.add_child(child)
root.tick_once()
print(py_trees.display.unicode_tree(root, show_status=True))
root.replace_child(child=child, replacement=replacement)
print(py_trees.display.unicode_tree(root, show_status=True))
py_trees.tests.print_assert_details("Current Child", None, root.current_child)
assert root.current_child is None
root.remove_all_children()
root.add_child(child)
root.tick_once()
print(py_trees.display.unicode_tree(root, show_status=True))
root.remove_child_by_id(child_id=child.id)
print(py_trees.display.unicode_tree(root, show_status=True))
py_trees.tests.print_assert_details("Current Child", None, root.current_child)
assert root.current_child is None
def test_tick_add_with_current_child() -> None:
console.banner("Tick-Add with Current Child")
py_trees.tests.print_assert_banner()
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnAll(synchronise=False),
)
root.tick_once()
child = py_trees.behaviours.Failure(name="Failure")
root.add_child(child)
py_trees.tests.print_assert_details("Current Child", None, root.current_child)
assert root.current_child is None
def test_add_tick_add_with_current_child() -> None:
console.banner("Add-Tick-Add with Current Child")
py_trees.tests.print_assert_banner()
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnAll(synchronise=False),
)
run1 = py_trees.behaviours.Running("Run1")
run2 = py_trees.behaviours.Running("Run2")
root.add_child(run1)
root.tick_once()
print(py_trees.display.unicode_tree(root, show_status=True))
assert root.current_child is not None
py_trees.tests.print_assert_details(
"Current Child", run1.name, root.current_child.name
)
assert root.current_child.name == run1.name
root.add_child(run2)
print(py_trees.display.unicode_tree(root, show_status=True))
assert root.current_child is not None
py_trees.tests.print_assert_details(
"Current Child", run1.name, root.current_child.name
)
assert root.current_child.name == run1.name
def test_add_tick_insert_with_current_child() -> None:
console.banner("Add-Tick-Insert with Current Child")
py_trees.tests.print_assert_banner()
root = py_trees.composites.Parallel(
name="Parallel",
policy=py_trees.common.ParallelPolicy.SuccessOnAll(synchronise=False),
)
run1 = py_trees.behaviours.Running("Run1")
run2 = py_trees.behaviours.Running("Run2")
root.add_child(run1)
root.tick_once()
print(py_trees.display.unicode_tree(root, show_status=True))
assert root.current_child is not None
py_trees.tests.print_assert_details(
"Current Child", run1.name, root.current_child.name
)
assert root.current_child.name == run1.name
root.insert_child(run2, index=0)
print(py_trees.display.unicode_tree(root, show_status=True))
assert root.current_child is not None
py_trees.tests.print_assert_details(
"Current Child", run1.name, root.current_child.name
)
assert root.current_child.name == run1.name