Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: include save_tree_to_path from #54 #58

Merged
merged 4 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion beams/tests/test_tree_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
from epics import caget

from beams.behavior_tree.CheckAndDo import CheckAndDo
from beams.tree_config import get_tree_from_path
from beams.tree_config import (CheckAndDoItem, get_tree_from_path,
save_tree_item_to_path)


def test_tree_obj_ser():
Expand Down Expand Up @@ -66,3 +67,12 @@ def test_father_tree_execution(request):
check_insert = caget("RET:INSERT")

assert check_insert == 1


def test_save_tree_item_round_trip(tmp_path: Path):
filepath = tmp_path / "temp_egg.json"
item = CheckAndDoItem(name="test_save_tree_item_round_trip")
save_tree_item_to_path(path=filepath, root=item)
loaded_tree = get_tree_from_path(path=filepath)
assert isinstance(loaded_tree.root, CheckAndDo)
assert loaded_tree.root.name == item.name
27 changes: 25 additions & 2 deletions beams/tree_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Any, Callable, List, Optional, Union

import py_trees
from apischema import deserialize
from apischema import deserialize, serialize
from epics import caget, caput
from py_trees.behaviour import Behaviour
from py_trees.behaviours import (CheckBlackboardVariableValue,
Expand All @@ -27,14 +27,37 @@


def get_tree_from_path(path: Path) -> py_trees.trees.BehaviourTree:
"""Deserialize a json file, return the tree it specifies"""
"""
Deserialize a json file, return the tree it specifies.

This can be used internally to conveniently and consistently load
serialized json files as ready-to-run behavior trees.
"""
with open(path, "r") as fd:
deser = json.load(fd)
tree_item = deserialize(BehaviorTreeItem, deser)

return tree_item.get_tree()


def save_tree_item_to_path(path: Union[Path, str], root: BaseItem):
"""
Serialize a behavior tree item to a json file.

This can be used to generate serialized trees from python scripts.
The user needs to create various interwoven tree items, pick the
correct item to be the root node, and then use this function to
save the serialized file.

These files are ready to be consumed by get_tree_from_path.
"""
ser = serialize(BehaviorTreeItem(root=root))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually invoke serialize the same way I do deserialize, to be explicit. I think apischema does this conversion for you if you don't provide it, but I have trust issues:

ser = serialize(BehaviourTreeItem, tree_item)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I quickly understand what tree_item is in this case.


with open(path, "w") as fd:
json.dump(ser, fd, indent=2)
fd.write("\n")


@dataclass
class BehaviorTreeItem:
root: BaseItem
Expand Down
23 changes: 23 additions & 0 deletions docs/source/upcoming_release_notes/58-enh_add_serialize_helper.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
58 enh_add_serialize_helper
###########################

API Breaks
----------
- N/A

Features
--------
- Add ``save_tree_item_to_path``, a helper function for serializing user-made
behavior trees into the format usable by ``get_tree_from_path``.

Bugfixes
--------
- N/A

Maintenance
-----------
- Improve docstring on ``get_tree_from_path``.

Contributors
------------
- zllentz