Skip to main content

Action decomposition and execution framework

Project description

Action Trees

Source Code: https://gitlab.com/roxautomation/action-trees


Summary

Action Trees is an asyncio-based Python library for managing hierarchical and asynchronous tasks. It breaks down complex tasks into simpler, independent steps that can be run sequentially or in parallel.

Functionality

  • ActionItem Class: Central class for representing tasks, supporting states like INITIALIZING, RUNNING, PAUSED, and FAILED.
  • Task Management: Provides APIs to start, pause, resume, and cancel tasks asynchronously.
  • Hierarchical Actions: Actions can have child actions, supporting structured, tree-like task management.
  • Parallel Execution: Supports running child tasks concurrently using asyncio.
  • State and Exception Handling: Handles task state transitions and exceptions, ensuring proper state flow.

Example

The following example illustrates a high-level task to prepare and make a cappuccino using the ActionItem class:

import asyncio
from action_trees import ActionItem

class AtomicAction(ActionItem):
    """Basic machine action with no children."""
    def __init__(self, name: str, duration: float = 0.1):
        super().__init__(name=name)
        self._duration = duration

    async def _on_run(self) -> None:
        await asyncio.sleep(self._duration)

class PrepareMachineAction(ActionItem):
    """Prepare the machine."""
    def __init__(self) -> None:
        super().__init__(name="prepare")
        self.add_child(AtomicAction(name="initialize"))
        self.add_child(AtomicAction(name="clean"))

    async def _on_run(self) -> None:
        for child in self.children:
            await child.start()

class MakeCappuccinoAction(ActionItem):
    """Make cappuccino."""
    def __init__(self) -> None:
        super().__init__(name="make_cappuccino")
        self.add_child(AtomicAction(name="boil_water"))
        self.add_child(AtomicAction(name="grind_coffee"))

    async def _on_run(self) -> None:
        await self.run_children_parallel()

class CappuccinoOrder(ActionItem):
    """High-level action to make a cappuccino."""
    def __init__(self) -> None:
        super().__init__(name="cappuccino_order")
        self.add_child(PrepareMachineAction())
        self.add_child(MakeCappuccinoAction())

    async def _on_run(self) -> None:
        for child in self.children:
            await child.start()

async def main() -> None:
    order = CappuccinoOrder()
    await order.start()
    order.display_tree()

if __name__ == "__main__":
    asyncio.run(main())

This code creates a hierarchical task to prepare a machine and make a cappuccino, with some actions running sequentially and others in parallel.

More examples : see examples

State transitions

ActionItem contains a state machine with these transitions. (spec from VDA5050)

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

action_trees-1.0.1.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

action_trees-1.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file action_trees-1.0.1.tar.gz.

File metadata

  • Download URL: action_trees-1.0.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.3

File hashes

Hashes for action_trees-1.0.1.tar.gz
Algorithm Hash digest
SHA256 dda7f44978dbffb4867937bcb67b60adb2f73428800d8ad82f2ab9d73759c5bc
MD5 43f0b08ef97d9decfda196208298f261
BLAKE2b-256 b902458153bafa97fba40201e1fc67bbbd715e98a7f7e9d02fc7a40fecc7dc67

See more details on using hashes here.

File details

Details for the file action_trees-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for action_trees-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 87b663c898c21d054775fe434e7ed418aee17bb2987bbcfdb70d0f782ab6c4a8
MD5 bf7c7752e7713064cb9648d80caff038
BLAKE2b-256 f4c1e5434727a83f95580087eaa898fea3468df789c3186d3522e32a41003310

See more details on using hashes here.

Supported by

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