def test_full_iteration(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Visit Whole Tree" + console.reset) print( console.bold + "****************************************************************************************\n" + console.reset) visitor = py_trees.trees.DebugVisitor() 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) visitations = 0 for child in root.iterate(): visitations += 1 child.visit(visitor) assert (visitations == 6)
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)
def test_success_failure_tree(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Success Failure Tree" + console.reset) print( console.bold + "****************************************************************************************\n" + console.reset) root = py_trees.Selector("Root") failure = py_trees.behaviours.Failure("Failure") failure2 = py_trees.meta.inverter(py_trees.behaviours.Success("Failure2")) success = py_trees.behaviours.Success("Success") root.add_child(failure) root.add_child(failure2) root.add_child(success) py_trees.display.print_ascii_tree(root) visitor = py_trees.trees.DebugVisitor() tick_tree(root, visitor, 1, 1) print("\n--------- Assertions ---------\n") print("success.status == py_trees.Status.SUCCESS") assert (success.status == py_trees.Status.SUCCESS) print("root.status == py_trees.Status.SUCCESS") assert (root.status == py_trees.Status.SUCCESS) print("failure.status == py_trees.Status.FAILURE") assert (failure.status == py_trees.Status.FAILURE) print("failure2.status == py_trees.Status.FAILURE") assert (failure2.status == py_trees.Status.FAILURE)
def test_display(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Display 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) py_trees.display.print_ascii_tree(root) assert (True)
def test_inverter_sequence(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Inverter Sequence Tree" + console.reset) print( console.bold + "****************************************************************************************\n" + console.reset) root = py_trees.meta.inverter(py_trees.Sequence)(name="Root") selector = py_trees.Selector(name="Selector") failure = py_trees.behaviours.Failure(name="Failure") success = py_trees.behaviours.Success(name="Success") selector.add_child(failure) selector.add_child(success) success2 = py_trees.behaviours.Success(name="Success2") root.add_child(selector) root.add_child(success2) py_trees.display.print_ascii_tree(root) visitor = py_trees.visitors.DebugVisitor() py_trees.tests.tick_tree(root, visitor, 1, 1) print("\n--------- Assertions ---------\n") print("root.status == py_trees.Status.FAILURE") assert (root.status == py_trees.Status.FAILURE) print("success.status == py_trees.Status.SUCCESS") assert (success.status == py_trees.Status.SUCCESS) print("success2.status == py_trees.Status.SUCCESS") assert (success2.status == py_trees.Status.SUCCESS) print("failure.status == py_trees.Status.FAILURE") assert (failure.status == py_trees.Status.FAILURE) print("selector.status == py_trees.Status.SUCCESS") assert (selector.status == py_trees.Status.SUCCESS)
def test_failure_is_success_tree(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Failure is Success Tree" + console.reset) print( console.bold + "****************************************************************************************\n" + console.reset) root = py_trees.Selector(name="Root") failure = py_trees.behaviours.Failure(name="Failure") goon = MustGoOnRegardless(name="DontBeAfraidToBeTheGoon") root.add_child(failure) root.add_child(goon) py_trees.display.print_ascii_tree(root) visitor = py_trees.visitors.DebugVisitor() py_trees.tests.tick_tree(root, visitor, 1, 1) print("\n--------- Assertions ---------\n") print("root.status == py_trees.Status.SUCCESS") assert (root.status == py_trees.Status.SUCCESS) print("failure.status == py_trees.Status.FAILURE") assert (failure.status == py_trees.Status.FAILURE) print("goon.status == py_trees.Status.SUCCESS") assert (goon.status == py_trees.Status.SUCCESS)
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)
def test_running_is_failure_tree(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Running is Failure Tree" + console.reset) print( console.bold + "****************************************************************************************\n" + console.reset) root = py_trees.Selector(name="Root") running = py_trees.meta.running_is_failure( py_trees.behaviours.Running)(name="Running") failure = py_trees.meta.running_is_failure( py_trees.behaviours.Failure)(name="Failure") success = py_trees.meta.running_is_failure( py_trees.behaviours.Success)(name="Success") root.add_child(running) root.add_child(failure) root.add_child(success) py_trees.display.print_ascii_tree(root) visitor = py_trees.visitors.DebugVisitor() py_trees.tests.tick_tree(root, visitor, 1, 1) print("\n--------- Assertions ---------\n") print("running.status == py_trees.Status.FAILURE") assert (running.status == py_trees.Status.FAILURE) print("failure.status == py_trees.Status.FAILURE") assert (failure.status == py_trees.Status.FAILURE) print("success.status == py_trees.Status.SUCCESS") assert (success.status == py_trees.Status.SUCCESS) print("root.status == py_trees.Status.SUCCESS") assert (root.status == py_trees.Status.SUCCESS)
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)
def test_success_is_failure_tree(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Success is Failure Tree" + console.reset) print( console.bold + "****************************************************************************************\n" + console.reset) root = py_trees.Selector("Root") failure = py_trees.behaviours.Failure(name="Failure") going_down = py_trees.meta.success_is_failure( py_trees.behaviours.Success)(name="Going Down") root.add_child(failure) root.add_child(going_down) py_trees.display.print_ascii_tree(root) visitor = py_trees.visitors.DebugVisitor() py_trees.tests.tick_tree(root, visitor, 1, 1) print("\n--------- Assertions ---------\n") print("failure.status == py_trees.Status.FAILURE") assert (failure.status == py_trees.Status.FAILURE) print("going_down.status == py_trees.Status.FAILURE") assert (going_down.status == py_trees.Status.FAILURE) print("root.status == py_trees.Status.FAILURE") assert (root.status == py_trees.Status.FAILURE)
def create_tree(): every_n_success = py_trees.behaviours.SuccessEveryN("EveryN", 5) sequence = py_trees.Sequence(name="Sequence") guard = py_trees.behaviours.Success("Guard") periodic_success = py_trees.behaviours.Periodic("Periodic", 3) finisher = py_trees.behaviours.Success("Finisher") sequence.add_child(guard) sequence.add_child(periodic_success) sequence.add_child(finisher) sequence.blackbox_level = py_trees.common.BlackBoxLevel.COMPONENT idle = py_trees.behaviours.Success("Idle") root = py_trees.Selector(name="Demo Tree") root.add_child(every_n_success) root.add_child(sequence) root.add_child(idle) return root
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")
def test_selector_composite(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Selector" + console.reset) print( console.bold + "****************************************************************************************" + console.reset) visitor = py_trees.trees.DebugVisitor() tree = py_trees.Selector(name='Selector') a = py_trees.behaviours.Count(name="A") b = py_trees.behaviours.Count(name="B") c = py_trees.behaviours.Count(name="C", fail_until=0, running_until=3, success_until=15) tree.add_child(a) tree.add_child(b) tree.add_child(c) py_trees.display.print_ascii_tree(tree, 0) tick_tree(tree, visitor, 1, 3) print_summary(nodes=[a, b, c]) print("--------- Assertions ---------\n") print("a.count == 3") assert (a.count == 3) print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("c.status == py_trees.Status.RUNNING") assert (c.status == py_trees.Status.RUNNING) print("c.number_count_resets == 0") assert (c.number_count_resets == 0) tick_tree(tree, visitor, 4, 4) print_summary(nodes=[a, b, c]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.RUNNING") assert (a.status == py_trees.Status.RUNNING) print("c.number_count_resets == 1") assert (c.number_count_resets == 1) print("c.status == py_trees.Status.INVALID") assert (c.status == py_trees.Status.INVALID) tick_tree(tree, visitor, 5, 8) print_summary(nodes=[a, b, c]) print("Done")
def test_display(): console.banner("Display Tree") 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) py_trees.display.print_ascii_tree(root) assert (True)
def test_full_iteration(): console.banner("Visit Whole Tree") visitor = py_trees.visitors.DebugVisitor() 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) visitations = 0 for child in root.iterate(): visitations += 1 child.visit(visitor) assert (visitations == 6)
def test_success_failure_tree(): console.banner("Success Failure Tree") root = py_trees.Selector("Root") failure = py_trees.behaviours.Failure("Failure") failure2 = py_trees.meta.inverter(py_trees.behaviours.Success)("Failure2") success = py_trees.behaviours.Success("Success") root.add_child(failure) root.add_child(failure2) root.add_child(success) py_trees.display.print_ascii_tree(root) visitor = py_trees.visitors.DebugVisitor() py_trees.tests.tick_tree(root, 1, 1, visitor) print("\n--------- Assertions ---------\n") print("success.status == py_trees.common.Status.SUCCESS") assert (success.status == py_trees.common.Status.SUCCESS) print("root.status == py_trees.common.Status.SUCCESS") assert (root.status == py_trees.common.Status.SUCCESS) print("failure.status == py_trees.common.Status.FAILURE") assert (failure.status == py_trees.common.Status.FAILURE) print("failure2.status == py_trees.common.Status.FAILURE") assert (failure2.status == py_trees.common.Status.FAILURE)
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")
def test_mixed_tree(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Mixed Tree" + console.reset) print( console.bold + "****************************************************************************************" + console.reset) visitor = py_trees.visitors.DebugVisitor() a = py_trees.behaviours.Count(name="A", fail_until=3, running_until=5, success_until=7, reset=False) sequence = py_trees.Sequence(name="Sequence") b = py_trees.behaviours.Count(name="B", fail_until=0, running_until=3, success_until=5, reset=False) c = py_trees.behaviours.Count(name="C", fail_until=0, running_until=3, success_until=5, reset=False) sequence.add_child(b) sequence.add_child(c) d = py_trees.behaviours.Count(name="D", fail_until=0, running_until=3, success_until=15) root = py_trees.Selector(name="Selector") root.add_child(a) root.add_child(sequence) root.add_child(d) py_trees.display.print_ascii_tree(root) py_trees.tests.tick_tree(root, visitor, 1, 2) py_trees.tests.print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.RUNNING") assert (sequence.status == py_trees.Status.RUNNING) print("b.status == py_trees.Status.RUNNING") assert (b.status == py_trees.Status.RUNNING) print("root.status == py_trees.Status.RUNNING") assert (root.status == py_trees.Status.RUNNING) py_trees.tests.tick_tree(root, visitor, 3, 9) py_trees.tests.print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.RUNNING") assert (sequence.status == py_trees.Status.RUNNING) print("c.status == py_trees.Status.RUNNING") assert (c.status == py_trees.Status.RUNNING) print("root.status == py_trees.Status.RUNNING") assert (root.status == py_trees.Status.RUNNING) py_trees.tests.tick_tree(root, visitor, 10, 11) py_trees.tests.print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.SUCCESS") assert (sequence.status == py_trees.Status.SUCCESS) print("c.status == py_trees.Status.SUCCESS") assert (c.status == py_trees.Status.SUCCESS) print("root.status == py_trees.Status.SUCCESS") assert (root.status == py_trees.Status.SUCCESS) py_trees.tests.tick_tree(root, visitor, 12, 13) py_trees.tests.print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.FAILURE") assert (sequence.status == py_trees.Status.FAILURE) print("b.status == py_trees.Status.FAILURE") assert (b.status == py_trees.Status.FAILURE) print("d.status == py_trees.Status.RUNNING") assert (d.status == py_trees.Status.RUNNING) print("root.status == py_trees.Status.RUNNING") assert (root.status == py_trees.Status.RUNNING)
def test_mixed_tree(): print( console.bold + "\n****************************************************************************************" + console.reset) print(console.bold + "* Mixed Tree" + console.reset) print( console.bold + "****************************************************************************************" + console.reset) visitor = py_trees.trees.DebugVisitor() a = py_trees.behaviours.Count(name="A", fail_until=3, running_until=5, success_until=7) sequence = py_trees.Sequence(name="Sequence") b = py_trees.behaviours.Count(name="B", fail_until=0, running_until=3, success_until=5) c = py_trees.behaviours.Count(name="C", fail_until=0, running_until=3, success_until=5) sequence.add_child(b) sequence.add_child(c) d = py_trees.behaviours.Count(name="D", fail_until=0, running_until=3, success_until=15) root = py_trees.Selector(name="Root") print("Root.children: %s " % [child.name for child in root.children]) root.add_child(a) root.add_child(sequence) root.add_child(d) tick_tree(root, visitor, 1, 2) print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.RUNNING") assert (sequence.status == py_trees.Status.RUNNING) print("b.status == py_trees.Status.RUNNING") assert (b.status == py_trees.Status.RUNNING) print("root.status == py_trees.Status.RUNNING") assert (root.status == py_trees.Status.RUNNING) tick_tree(root, visitor, 3, 11) print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.RUNNING") assert (sequence.status == py_trees.Status.RUNNING) print("c.status == py_trees.Status.RUNNING") assert (c.status == py_trees.Status.RUNNING) print("root.status == py_trees.Status.RUNNING") assert (root.status == py_trees.Status.RUNNING) tick_tree(root, visitor, 12, 14) print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.SUCCESS") assert (sequence.status == py_trees.Status.SUCCESS) print("c.status == py_trees.Status.SUCCESS") assert (c.status == py_trees.Status.SUCCESS) print("root.status == py_trees.Status.SUCCESS") assert (root.status == py_trees.Status.SUCCESS) tick_tree(root, visitor, 15, 16) print_summary(nodes=[a, b, c, d]) print("--------- Assertions ---------\n") print("a.status == py_trees.Status.FAILURE") assert (a.status == py_trees.Status.FAILURE) print("sequence.status == py_trees.Status.FAILURE") assert (sequence.status == py_trees.Status.FAILURE) print("b.status == py_trees.Status.FAILURE") assert (b.status == py_trees.Status.FAILURE) print("d.status == py_trees.Status.RUNNING") assert (d.status == py_trees.Status.RUNNING) print("root.status == py_trees.Status.RUNNING") assert (root.status == py_trees.Status.RUNNING)