def test_analyze_sequence():

    a_tree = analyze(
        alias(
            child=repeat_until(
                child=sequence(children=[action(hello), action(hello), action(hello)]), condition=success_until_zero
            ),
            name="btree_1",
        )
    )
    print_test = """ --> btree_1:\n     --(child)--> repeat_until:\n         --(condition)--> success_until_zero:\n         --(child)--> sequence:\n                      succes_threshold: 3\n             --(children)--> action:\n                             target: hello\n             --(children)--> action:\n                             target: hello\n             --(children)--> action:\n                             target: hello\n"""  # noqa: E501, B950
    assert stringify_analyze(a_tree) == print_test
示例#2
0
    async def tick():
        value = counter.get()
        counter.set(value - 1)
        if value <= 0:
            return FAILURE
        if value == 3:
            raise RuntimeError('3')
        return SUCCESS

    assert kernel.run(repeat_until(
        condition=tick, child=a_func)) == 'a', 'return last sucess result'
    assert counter.get() == 2


from curio import Kernel
k = Kernel()

tree1 = sequence(children=[a_func, failure_func, success_func])
r = k.run(tree1)
print(f"result={r}")

tree2 = selector(children=[exception_func, failure_func, a_func])
r = k.run(tree2)
print(f"result={r}")

assert k.run(decision(condition=success_func, success_tree=a_func)) == 'a'
print("===")
test_repeat_until_falsy_condition(k)

k.run(shutdown=True)
示例#3
0
class GripperInterface:
    def __init__(self):
        self._open = False

    def open(self):
        print("GripperInterface Open")
        self._open = True

    def close(self):
        print("GripperInterface Close")
        self._open = False


gripper = GripperInterface()

b_tree = bt.sequence(
    children=[
        bt.always_success(child=bt.action(target=say_hello, name="John")),
        bt.action(target=check_battery),
        check_again_battery,  # this will be encapsulated at runtime
        bt.always_success(child=bt.action(target=gripper.open)),
        bt.always_success(child=bt.action(target=approach_object, name="house")),
        bt.always_success(child=bt.action(target=gripper.close)),
    ]
)


if __name__ == '__main__':
    curio.run(b_tree)
示例#4
0
    return name.get() != ""


async def ask_for_name():
    new_name = input("Hello, whats your name? \n")
    if new_name != "":
        name.set(new_name)
        return bt.SUCCESS
    else:
        return bt.FAILURE


name = contextvars.ContextVar('name', default="")

greet_with_name = bt.decision(condition=is_name_set,
                              success_tree=say_hello,
                              failure_tree=bt.sequence(
                                  [ask_for_name, say_hello]))

b_tree = bt.sequence(children=[greet_with_name, some_action])

if __name__ == '__main__':

    name = contextvars.ContextVar('name', default="")

    curio.run(b_tree)

    # You can take a look at the final behavior tree
    abstract_tree_tree_1 = bt.analyze(b_tree)
    print(bt.stringify_analyze(abstract_tree_tree_1))
示例#5
0
async def say_hello(name: str):
    print(f"Hello: {name}")


class GripperInterface:
    def __init__(self):
        self._open = False

    def open(self):
        print("GripperInterface Open")
        self._open = True

    def close(self):
        print("GripperInterface Close")
        self._open = False


gripper = GripperInterface()

b_tree = bt.sequence(children=[
    bt.always_success(child=bt.action(target=say_hello, name="John")),
    bt.always_success(child=bt.action(target=check_battery)),
    bt.always_success(child=bt.action(target=gripper.open)),
    bt.always_success(child=bt.action(target=approach_object, name="house")),
    bt.always_success(child=bt.action(target=gripper.close)),
])

if __name__ == '__main__':
    curio.run(b_tree)
示例#6
0
def test_sequence(kernel):
    assert not kernel.run(
        sequence(children=[a_func, failure_func, success_func
                           ])), "default behaviour fail of one failed"

    assert kernel.run(
        sequence(children=[a_func, failure_func, success_func],
                 succes_threshold=2))
    assert kernel.run(
        sequence(children=[a_func, success_func, failure_func],
                 succes_threshold=2))
    assert kernel.run(
        sequence(children=[failure_func, a_func, success_func],
                 succes_threshold=2)), 'must continue after first failure'

    assert kernel.run(
        sequence(children=[exception_func, failure_func, a_func],
                 succes_threshold=1))
    assert not kernel.run(
        sequence(children=[exception_func, failure_func], succes_threshold=1))

    assert not kernel.run(sequence(children=[]))
    # negative
    with pytest.raises(AssertionError):
        sequence(children=[exception_func, failure_func], succes_threshold=-2)
    # upper than len children
    with pytest.raises(AssertionError):
        sequence(children=[exception_func, failure_func], succes_threshold=3)