예제 #1
0
def test_tick_tock_behaviour_tree():
    print(
        console.bold +
        "\n****************************************************************************************"
        + console.reset)
    print(console.bold + "* Tick Tock Behaviour Tree" + console.reset)
    print(
        console.bold +
        "****************************************************************************************\n"
        + console.reset)

    a = py_trees.behaviours.Count(name="A")
    sequence = py_trees.Sequence(name="Sequence")
    b = py_trees.behaviours.Count(name="B")
    c = py_trees.behaviours.Count(name="C")
    sequence.add_child(b)
    sequence.add_child(c)
    d = py_trees.behaviours.Count(name="D")
    root = py_trees.Selector(name="Root")
    root.add_child(a)
    root.add_child(sequence)
    root.add_child(d)

    tree = py_trees.BehaviourTree(root)
    py_trees.display.print_ascii_tree(tree.root)

    visitor = py_trees.trees.DebugVisitor()
    tree.visitors.append(visitor)
    tree.tick_tock(100, 5, pre_tick_handler=pre_tick_visitor)

    print("\n--------- Assertions ---------\n")
    print("a.status == py_trees.Status.RUNNING")
    assert (a.status == py_trees.Status.RUNNING)
예제 #2
0
def test_prune_behaviour_tree():
    print(
        console.bold +
        "\n****************************************************************************************"
        + console.reset)
    print(console.bold + "* Prune Behaviour Tree" + console.reset)
    print(
        console.bold +
        "****************************************************************************************\n"
        + console.reset)

    a = py_trees.behaviours.Count(name="A")
    sequence = py_trees.Sequence(name="Sequence")
    b = py_trees.behaviours.Count(name="B")
    c = py_trees.behaviours.Count(name="C")
    sequence.add_child(b)
    sequence.add_child(c)
    d = py_trees.behaviours.Count(name="D")
    root = py_trees.Selector(name="Root")
    root.add_child(a)
    root.add_child(sequence)
    root.add_child(d)

    tree = py_trees.BehaviourTree(root)
    py_trees.display.print_ascii_tree(tree.root)
    assert (len(sequence.children) == 2)
    tree.prune_subtree(c.id)
    py_trees.display.print_ascii_tree(tree.root)
    assert (len(sequence.children) == 1)
    tree.prune_subtree(sequence.id)
    py_trees.display.print_ascii_tree(tree.root)
    assert (len(root.children) == 2)
예제 #3
0
def test_replace_behaviour_tree():
    console.banner("Replace Behaviour Subtree")

    a = py_trees.behaviours.Count(name="A")
    sequence1 = py_trees.Sequence(name="Sequence1")
    b = py_trees.behaviours.Count(name="B")
    c = py_trees.behaviours.Count(name="C")
    sequence1.add_child(b)
    sequence1.add_child(c)
    d = py_trees.behaviours.Count(name="D")
    root = py_trees.Selector(name="Root")
    root.add_child(a)
    root.add_child(sequence1)
    root.add_child(d)

    tree = py_trees.BehaviourTree(root)
    py_trees.display.print_ascii_tree(tree.root)
    assert (len(sequence1.children) == 2)

    sequence2 = py_trees.Sequence(name="Sequence2")
    e = py_trees.behaviours.Count(name="E")
    f = py_trees.behaviours.Count(name="F")
    g = py_trees.behaviours.Count(name="G")
    sequence2.add_child(e)
    sequence2.add_child(f)
    sequence2.add_child(g)

    tree.replace_subtree(sequence1.id, sequence2)
    py_trees.display.print_ascii_tree(tree.root)
    assert (len(sequence2.children) == 3)
예제 #4
0
def test_failed_tree():
    console.banner("Failed Tree")

    root = py_trees.Selector("Root")
    f1 = py_trees.behaviours.Failure("Failure 1")
    f2 = py_trees.behaviours.Failure("Failure 2")
    f3 = py_trees.behaviours.Failure("Failure 3")
    root.add_child(f1)
    root.add_child(f2)
    root.add_child(f3)
    tree = py_trees.BehaviourTree(root)
    py_trees.display.print_ascii_tree(tree.root)
    tree.tick()
    print("\n--------- Assertions ---------\n")
    print("root.tip().name == Failure 3")
    assert (root.tip().name == "Failure 3")
예제 #5
0
def test_condition():
    print(
        console.bold +
        "\n****************************************************************************************"
        + console.reset)
    print(console.bold + "* Condition" + console.reset)
    print(
        console.bold +
        "****************************************************************************************\n"
        + console.reset)

    # behaviours will be running the first time they are seen, then success for subsequent ticks

    d = py_trees.behaviours.Count(name="D",
                                  fail_until=2,
                                  running_until=2,
                                  success_until=10)
    condition = py_trees.behaviours.Condition('condition', d,
                                              py_trees.Status.SUCCESS)

    tree = py_trees.BehaviourTree(condition)
    py_trees.display.print_ascii_tree(tree.root)

    visitor = py_trees.trees.DebugVisitor()
    tree.visitors.append(visitor)
    tree.tick()

    print("\n--------- Assertions ---------\n")
    assert (condition.status == py_trees.Status.RUNNING)
    assert (d.status == py_trees.Status.FAILURE)

    tree.tick()

    print("\n--------- Assertions ---------\n")
    assert (condition.status == py_trees.Status.RUNNING)
    assert (d.status == py_trees.Status.FAILURE)

    tree.tick()

    print("\n--------- Assertions ---------\n")
    assert (condition.status == py_trees.Status.SUCCESS)
    assert (d.status == py_trees.Status.SUCCESS)
예제 #6
0
def test_failed_tree():
    print(
        console.bold +
        "\n****************************************************************************************"
        + console.reset)
    print(console.bold + "* Failed Tree" + console.reset)
    print(
        console.bold +
        "****************************************************************************************\n"
        + console.reset)

    root = py_trees.Selector("Root")
    f1 = py_trees.behaviours.Failure("Failure 1")
    f2 = py_trees.behaviours.Failure("Failure 2")
    f3 = py_trees.behaviours.Failure("Failure 3")
    root.add_child(f1)
    root.add_child(f2)
    root.add_child(f3)
    tree = py_trees.BehaviourTree(root)
    tree.tick()
    print("\n--------- Assertions ---------\n")
    print("root.tip().name == Failure 3")
    assert (root.tip().name == "Failure 3")
예제 #7
0
def test_tip_complex():
    print(
        console.bold +
        "\n****************************************************************************************"
        + console.reset)
    print(console.bold + "* Tip Complex" + console.reset)
    print(
        console.bold +
        "****************************************************************************************\n"
        + console.reset)

    # behaviours will be running the first time they are seen, then success for subsequent ticks
    sel = py_trees.composites.Selector(name="Selector")
    seq1 = py_trees.composites.Sequence(name="Sequence1")
    seq2 = py_trees.composites.Sequence(name="Sequence2")

    # selector left branch fails the two times, so seq2 behaviours run. The
    # third time it is running, stopping seq2
    a = py_trees.behaviours.Count(name="A",
                                  fail_until=1,
                                  running_until=0,
                                  success_until=10)
    b = py_trees.behaviours.Count(name="B",
                                  fail_until=0,
                                  running_until=1,
                                  success_until=10)
    c = py_trees.behaviours.Count(name="C",
                                  fail_until=0,
                                  running_until=2,
                                  success_until=10)
    d = py_trees.behaviours.Count(name="D",
                                  fail_until=0,
                                  running_until=1,
                                  success_until=10)

    seq1.add_child(a)
    seq1.add_child(b)
    seq2.add_child(c)
    seq2.add_child(d)

    sel.add_child(seq1)
    sel.add_child(seq2)

    tree = py_trees.BehaviourTree(sel)
    py_trees.display.print_ascii_tree(tree.root)

    visitor = py_trees.trees.DebugVisitor()
    tree.visitors.append(visitor)
    tree.tick()

    print("\n--------- Assertions ---------\n")
    assert (a.status == py_trees.Status.FAILURE)
    assert (b.status == py_trees.Status.INVALID)
    assert (c.status == py_trees.Status.RUNNING)
    assert (d.status == py_trees.Status.INVALID)

    # the root of sequence and tree should be the currently running node
    assert (seq1.tip() == a)
    assert (seq2.tip() == c)
    assert (tree.root.tip() == c)

    tree.tick()
    print("\n--------- Assertions ---------\n")
    assert (a.status == py_trees.Status.SUCCESS)
    assert (b.status == py_trees.Status.RUNNING)
    assert (c.status == py_trees.Status.INVALID)
    assert (d.status == py_trees.Status.INVALID)

    assert (seq1.tip() == b)
    assert (seq2.tip() == None)
    assert (tree.root.tip() == b)

    tree.tick()
예제 #8
0
def test_tip_simple():
    print(
        console.bold +
        "\n****************************************************************************************"
        + console.reset)
    print(console.bold + "* Tip Simple" + console.reset)
    print(
        console.bold +
        "****************************************************************************************\n"
        + console.reset)

    # behaviours will be running the first time they are seen, then success for subsequent ticks
    seq = py_trees.composites.Sequence(name="Sequence")
    a = py_trees.behaviours.Count(name="A",
                                  fail_until=0,
                                  running_until=1,
                                  success_until=100)
    b = py_trees.behaviours.Count(name="B",
                                  fail_until=0,
                                  running_until=1,
                                  success_until=100)
    seq.add_child(a)
    seq.add_child(b)

    tree = py_trees.BehaviourTree(seq)
    py_trees.display.print_ascii_tree(tree.root)

    visitor = py_trees.trees.DebugVisitor()
    tree.visitors.append(visitor)

    print("\n--------- Assertions (before initialisation) ---------\n")
    # an uninitialised tree/behaviour always has a tip of None
    assert (tree.root.tip() == None)
    assert (seq.tip() == None)
    assert (a.tip() == None)
    assert (b.tip() == None)

    tree.tick()
    print("\n--------- Assertions ---------\n")
    assert (a.status == py_trees.Status.RUNNING)
    assert (b.status == py_trees.Status.INVALID)
    # the root of sequence and tree should be the currently running node
    assert (tree.root.tip() == a)
    assert (seq.tip() == a)
    # when a node is running/has run, its tip is itself
    assert (a.tip() == a)
    assert (b.tip() == None)

    tree.tick()
    print("\n--------- Assertions ---------\n")
    assert (a.status == py_trees.Status.SUCCESS)
    assert (b.status == py_trees.Status.RUNNING)
    # the root of sequence and tree should be the currently running node
    assert (tree.root.tip() == b)
    assert (seq.tip() == b)
    # when a node is running/has run, its tip is itself
    assert (a.tip() == a)
    assert (b.tip() == b)

    tree.tick()
    print("\n--------- Assertions ---------\n")
    assert (a.status == py_trees.Status.SUCCESS)
    assert (b.status == py_trees.Status.SUCCESS)
    # the root of sequence and tree should be the currently running node
    assert (tree.root.tip() == b)
    assert (seq.tip() == b)
    # when a node is running/has run, its tip is itself
    assert (a.tip() == a)
    assert (b.tip() == b)