|
9 | 9 | ##############################################################################
|
10 | 10 |
|
11 | 11 | import time
|
| 12 | +import typing |
12 | 13 |
|
13 | 14 | import py_trees
|
14 | 15 | import py_trees.console as console
|
@@ -604,3 +605,159 @@ def test_status_to_blackboard() -> None:
|
604 | 605 | result=decorator.status,
|
605 | 606 | )
|
606 | 607 | assert decorator.status == py_trees.common.Status.SUCCESS
|
| 608 | + |
| 609 | + |
| 610 | +@pytest.mark.parametrize( |
| 611 | + "iterable", |
| 612 | + [ |
| 613 | + [1, 2, 3], # list |
| 614 | + (1, 2, 3), # tuple |
| 615 | + (i for i in range(1, 4)), # generator |
| 616 | + ], |
| 617 | + ids=["list", "tuple", "generator"], |
| 618 | +) |
| 619 | +def test_for_each_fixed(iterable: typing.Iterable[int]) -> None: |
| 620 | + console.banner("ForEach - 3 elements on multiple iterables") |
| 621 | + child = py_trees.behaviours.StatusQueue( |
| 622 | + name="R-S", |
| 623 | + queue=[ |
| 624 | + py_trees.common.Status.RUNNING, |
| 625 | + py_trees.common.Status.SUCCESS, |
| 626 | + ], |
| 627 | + eventually=None, |
| 628 | + ) |
| 629 | + blackboard = py_trees.blackboard.Client() |
| 630 | + blackboard.register_key(key="element", access=py_trees.common.Access.READ) |
| 631 | + blackboard.register_key(key="iterable", access=py_trees.common.Access.WRITE) |
| 632 | + blackboard.iterable = iterable |
| 633 | + decorator = py_trees.decorators.ForEach( |
| 634 | + name="ForEach", child=child, source_key="iterable", target_key="element" |
| 635 | + ) |
| 636 | + |
| 637 | + decorator.tick_once() # run first element |
| 638 | + print("\n--------- Tick 1 ---------\n") |
| 639 | + print("decorator.status == py_trees.common.Status.RUNNING") |
| 640 | + assert decorator.status == py_trees.common.Status.RUNNING |
| 641 | + print("child.status == py_trees.common.Status.RUNNING") |
| 642 | + assert child.status == py_trees.common.Status.RUNNING |
| 643 | + assert blackboard.element == 1 |
| 644 | + |
| 645 | + decorator.tick_once() # complete first element; load second |
| 646 | + print("\n--------- Tick 2 ---------\n") |
| 647 | + print("decorator.status == py_trees.common.Status.RUNNING") |
| 648 | + assert decorator.status == py_trees.common.Status.RUNNING |
| 649 | + print("child.status == py_trees.common.Status.SUCCESS") |
| 650 | + assert child.status == py_trees.common.Status.SUCCESS |
| 651 | + assert blackboard.element == 2 # type: ignore[unreachable] |
| 652 | + |
| 653 | + decorator.tick_once() # run second element |
| 654 | + print("\n--------- Tick 3 ---------\n") |
| 655 | + print("decorator.status == py_trees.common.Status.RUNNING") |
| 656 | + assert decorator.status == py_trees.common.Status.RUNNING |
| 657 | + print("child.status == py_trees.common.Status.RUNNING") |
| 658 | + assert child.status == py_trees.common.Status.RUNNING |
| 659 | + assert blackboard.element == 2 |
| 660 | + |
| 661 | + decorator.tick_once() # complete second element; load third |
| 662 | + print("\n--------- Tick 4 ---------\n") |
| 663 | + print("decorator.status == py_trees.common.Status.RUNNING") |
| 664 | + assert decorator.status == py_trees.common.Status.RUNNING |
| 665 | + print("child.status == py_trees.common.Status.SUCCESS") |
| 666 | + assert child.status == py_trees.common.Status.SUCCESS |
| 667 | + assert blackboard.element == 3 |
| 668 | + |
| 669 | + decorator.tick_once() # run third element |
| 670 | + print("\n--------- Tick 5 ---------\n") |
| 671 | + print("decorator.status == py_trees.common.Status.RUNNING") |
| 672 | + assert decorator.status == py_trees.common.Status.RUNNING |
| 673 | + print("child.status == py_trees.common.Status.RUNNING") |
| 674 | + assert child.status == py_trees.common.Status.RUNNING |
| 675 | + assert blackboard.element == 3 |
| 676 | + |
| 677 | + decorator.tick_once() # complete third element |
| 678 | + print("\n--------- Tick 6 ---------\n") |
| 679 | + print("decorator.status == py_trees.common.Status.SUCCESS") |
| 680 | + assert decorator.status == py_trees.common.Status.SUCCESS |
| 681 | + print("child.status == py_trees.common.Status.SUCCESS") |
| 682 | + assert child.status == py_trees.common.Status.SUCCESS |
| 683 | + assert blackboard.element == 3 |
| 684 | + |
| 685 | + |
| 686 | +def test_for_each_append() -> None: |
| 687 | + console.banner("ForEach - 3 + 2 elements") |
| 688 | + child = py_trees.behaviours.StatusQueue( |
| 689 | + name="S", |
| 690 | + queue=[ |
| 691 | + py_trees.common.Status.SUCCESS, |
| 692 | + ], |
| 693 | + eventually=None, |
| 694 | + ) |
| 695 | + blackboard = py_trees.blackboard.Client() |
| 696 | + blackboard.register_key(key="element", access=py_trees.common.Access.READ) |
| 697 | + blackboard.register_key(key="iterable", access=py_trees.common.Access.WRITE) |
| 698 | + blackboard.iterable = [1, 2, 3] |
| 699 | + decorator = py_trees.decorators.ForEach( |
| 700 | + name="ForEach", child=child, source_key="iterable", target_key="element" |
| 701 | + ) |
| 702 | + |
| 703 | + decorator.tick_once() |
| 704 | + print("\n--------- Tick 1 ---------\n") |
| 705 | + assert blackboard.element == 2 |
| 706 | + |
| 707 | + decorator.tick_once() |
| 708 | + print("\n--------- Tick 2 ---------\n") |
| 709 | + assert blackboard.element == 3 |
| 710 | + |
| 711 | + # add two elements, so we need two more ticks to succeed |
| 712 | + blackboard.iterable.extend([4, 5]) |
| 713 | + |
| 714 | + decorator.tick_once() |
| 715 | + print("\n--------- Tick 3 ---------\n") |
| 716 | + assert blackboard.element == 4 |
| 717 | + |
| 718 | + decorator.tick_once() |
| 719 | + print("\n--------- Tick 4 ---------\n") |
| 720 | + assert blackboard.element == 5 |
| 721 | + |
| 722 | + decorator.tick_once() |
| 723 | + print("\n--------- Tick 5 ---------\n") |
| 724 | + print("decorator.status == py_trees.common.Status.SUCCESS") |
| 725 | + assert decorator.status == py_trees.common.Status.SUCCESS |
| 726 | + print("child.status == py_trees.common.Status.SUCCESS") |
| 727 | + assert child.status == py_trees.common.Status.SUCCESS |
| 728 | + |
| 729 | + |
| 730 | +def test_for_each_delete() -> None: |
| 731 | + console.banner("ForEach - 4 + 1 elements") |
| 732 | + child = py_trees.behaviours.StatusQueue( |
| 733 | + name="S", |
| 734 | + queue=[ |
| 735 | + py_trees.common.Status.SUCCESS, |
| 736 | + ], |
| 737 | + eventually=None, |
| 738 | + ) |
| 739 | + blackboard = py_trees.blackboard.Client() |
| 740 | + blackboard.register_key(key="element", access=py_trees.common.Access.READ) |
| 741 | + blackboard.register_key(key="iterable", access=py_trees.common.Access.WRITE) |
| 742 | + blackboard.iterable = [1, 2, 3, 4] |
| 743 | + decorator = py_trees.decorators.ForEach( |
| 744 | + name="ForEach", child=child, source_key="iterable", target_key="element" |
| 745 | + ) |
| 746 | + |
| 747 | + decorator.tick_once() |
| 748 | + print("\n--------- Tick 1 ---------\n") |
| 749 | + assert blackboard.element == 2 |
| 750 | + |
| 751 | + decorator.tick_once() |
| 752 | + print("\n--------- Tick 2 ---------\n") |
| 753 | + assert blackboard.element == 3 |
| 754 | + |
| 755 | + # remove last element, so we skip one tick to succeed |
| 756 | + blackboard.iterable.pop() |
| 757 | + |
| 758 | + decorator.tick_once() |
| 759 | + print("\n--------- Tick 3 ---------\n") |
| 760 | + print("decorator.status == py_trees.common.Status.SUCCESS") |
| 761 | + assert decorator.status == py_trees.common.Status.SUCCESS |
| 762 | + print("child.status == py_trees.common.Status.SUCCESS") |
| 763 | + assert child.status == py_trees.common.Status.SUCCESS |
0 commit comments