Skip to main content

Async behavior tree

Project description

Overview

Async behavior tree for python

Alpha Status (project and code refactorisation)

Unix Build Status Coverage Status Codacy Badge Scrutinizer Code Quality PyPI Version PyPI License

Versions following Semantic Versioning

What's a behavior tree ?

Unlike a Finite State Machine, a Behaviour Tree is a tree of hierarchical nodes that controls the flow of decision and the execution of "tasks" or, as we will call them further, "Actions". -- behaviortree

If your new (or not) about behavior tree, you could spend some time on this few links:

Few implementation libraries:

  • task_behavior_engine A behavior tree based task engine written in Python
  • pi_trees a Python/ROS library for implementing Behavior Trees
  • pr_behavior_tree A simple python behavior tree library based on coroutines
  • btsk Behavior Tree Starter Kit
  • behave A behavior tree implementation in Python

Why another library so ?

When you study behavior tree implementation, reactive node, dynamic change, runtime execution, etc ... At a moment you're build more or less something that mimic an evaluator 'eval/apply' or a compilator, with a complex hierachical set of class. All complexity came with internal state management, using tree of blackboard to avoid global variable, multithreading issue, maybe few callback etc ... This break the simplicity and beauty of your initial design.

What I find usefull with behavior tree:

  • clarity of expression
  • node tree representation
  • possibility to reuse behavior
  • add external measure to dynamicaly change a behavior, a first step on observable pattern...

As I've used OOP for years (very long time), I will try to avoid class tree and prefer using the power of functionnal programming to obtain what I want: add metadata on a sematic construction, deal with closure, use function in parameters or in return value...

And a last reason, more personal, it that i would explore python expressivity.

So, in this module, I purpose you to use the concept of coroutines, and their mecanisms to manage the execution flow. By this way:

  • we reuse simple language idiom to manage state, parameter, etc
  • no design constraint on action implementation
  • most of language build block could be reused

You could build expression like this:

async def a_func():
    """A great function"""
    return "a"

async def b_decorator(child_value, other=""):
    """A great decorator..."""
    return f"b{child_value}{other}"

assert run(decorate(a_func, b_decorator)) == "ba"

This expression apply b_decorator on function a_func. Note that decorate(a_func, b_decorator) is not an async function, only action, or condition are async function.

Few guidelines of this implementation:

  • In order to mimic all NodeStatus (success, failure, running), I replace this by truthy/falsy meaning of evaluation value. A special dedicated exception decorate standard exception in order to give them a Falsy meaning.
  • Blackboard pattern, act as a manager of context variable for behavior tree. With python 3, please... simply use contextvars !
  • In order to be able to build a sematic tree, I've introduce a metadata tuple added on function implementation.

The rest is just implementation details..

A little note:

You should not use this until you're ready to think about what you're doing :)

Note about 'async' framework

As we use async function as underlaying mechanism to manage the execution flow, the standard library asyncio is pretty fine. But, (always a but somewhere isn't it...), you should read this [amazing blog post}(https://vorpus.org/blog/some-thoughts-on-asynchronous-api-design-in-a-post-asyncawait-world/) by Nathaniel J. Smith. And next study curio framework in deep.

As curio say:

Don't Use Curio if You're Allergic to Curio

Personaly, after few time of testing and reading curio code, I'm pretty addict.

Setup

Requirements

  • Python 3.7+

Installation

Install this library directly into an activated virtual environment:

$ pip install async-btree

or add it to your Poetry project:

$ poetry add async-btree

Usage

After installation, the package can imported:

$ python
>>> import async_btree
>>> async_btree.__version__

Contributing

You could find all information in Contributing page.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

async_btree-0.1.0.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

async_btree-0.1.0-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file async_btree-0.1.0.tar.gz.

File metadata

  • Download URL: async_btree-0.1.0.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.16 CPython/3.7.3 Darwin/18.6.0

File hashes

Hashes for async_btree-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6de026b5aa0a83ee243113ba0d9f02ba6ce2558205c98f1a11bff35e0cc26e6a
MD5 30ca5d89404581a76cc111d1b87c7377
BLAKE2b-256 ab7364c9c1fbc49a133c4ac78b7ccf8441e02ab0a6f979acd465626231da2ffd

See more details on using hashes here.

File details

Details for the file async_btree-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: async_btree-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.16 CPython/3.7.3 Darwin/18.6.0

File hashes

Hashes for async_btree-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 da34ea9a635a42d24380adc45d62b063f55056b75eafbb442860f65330ca6a0a
MD5 c6e7191e163260919a06f6fe5c4a7516
BLAKE2b-256 0f41cfce8b8beb53733b2d77d9b0570b3eef8dcdf6aafc32db7a8fa9d5164d31

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page