Skip to content

Commit

Permalink
move starting behavior_tree and behavior_tree into import file approach
Browse files Browse the repository at this point in the history
  • Loading branch information
jashlu committed Sep 16, 2024
1 parent 268a549 commit d8a8290
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 161 deletions.
73 changes: 0 additions & 73 deletions src/social_norms_trees/action_library.py

This file was deleted.

12 changes: 12 additions & 0 deletions src/social_norms_trees/behavior_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class BehaviorLibrary:
def __init__(self, behaviors):
self.behaviors = behaviors

def get_behavior_by_nickname(self, nickname):
return self.behaviors.get(nickname)

def get_behavior_by_id(self, id_):
for behavior in self.behaviors.values():
if behavior.id == id_:
return behavior
return None
4 changes: 2 additions & 2 deletions src/social_norms_trees/custom_node_library.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import py_trees

class CustomBehavior(py_trees.behaviours.Dummy):
def __init__(self, name, id, nickname):
def __init__(self, name, id_, nickname):
super().__init__(name)
self.id_ = id
self.id_ = id_
self.nickname = nickname


Expand Down
62 changes: 61 additions & 1 deletion src/social_norms_trees/mutate_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from datetime import datetime

from social_norms_trees.custom_node_library import CustomBehavior


T = TypeVar("T", bound=py_trees.behaviour.Behaviour)

Expand Down Expand Up @@ -502,4 +504,62 @@ def exchange_nodes(
"nodes": nodes,
"timestamp": datetime.now().isoformat(),
}
return tree, action_log
return tree, action_log


def prompt_select_node(behavior_library, text):

for idx, tree_name in enumerate(behavior_library.behaviors.keys(), 1):
print(f"{idx}. {tree_name}")


node_index = click.prompt(
text=text,
type=int,
)

node_key = list(behavior_library.behaviors.keys())[node_index-1]

return behavior_library.behaviors[node_key]


def add_node(
tree: T,
behavior_library: object,
) -> T:
"""Exchange two behaviours in the tree
Examples:
>>> tree = py_trees.composites.Sequence("", False, children=[])
"""


behavior = prompt_select_node(behavior_library, f"Which behavior do you want to add?")

new_node = CustomBehavior(
name=behavior['nickname'],
id_=behavior['id'],
nickname=behavior['nickname']
)

new_parent = prompt_identify_parent_node(
tree, f"What should its parent be?", display_nodes=True
)

index = prompt_identify_child_index(new_parent)

assert isinstance(new_parent, py_trees.composites.Composite)

new_parent.insert_child(new_node, index)

action_log = {
"type": "add_node",
"node": {
"id": new_node.id_,
"nickname": new_node.nickname
},
"timestamp": datetime.now().isoformat(),
}

return tree, action_log
104 changes: 104 additions & 0 deletions src/social_norms_trees/resources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"behavior_tree": {
"name": "Sequence A",
"type": "Sequence",
"children": [
{
"name": "Behavior A",
"type": "Behavior",
"children": []
},
{
"name": "Behavior B",
"type": "Behavior",
"children": []
},
{
"name": "Sequence B",
"type": "Sequence",
"children": [
{
"name": "Behavior C",
"type": "Behavior",
"children": []
},
{
"name": "Behavior D",
"type": "Behavior",
"children": []
}
]
},
{
"name": "Sequence C",
"type": "Sequence",
"children": [
{
"name": "Behavior E",
"type": "Behavior",
"children": []
},
{
"name": "Behavior F",
"type": "Behavior",
"children": []
},
{
"name": "Behavior G",
"type": "Behavior",
"children": []
},
{
"name": "Behavior H",
"type": "Behavior",
"children": []
}
]
}
]
},
"behavior_library":
{
"Behavior A": {
"id": "001",
"nickname": "Behavior A",
"type": "Dummy"
},
"Behavior B": {
"id": "002",
"nickname": "Behavior B",
"type": "Dummy"
},
"Behavior C": {
"id": "003",
"nickname": "Behavior C",
"type": "Dummy"
},
"Behavior D": {
"id": "004",
"nickname": "Behavior D",
"type": "Dummy"
},
"Behavior E": {
"id": "005",
"nickname": "Behavior E",
"type": "Dummy"
},
"Behavior F": {
"id": "006",
"nickname": "Behavior F",
"type": "Dummy"
},
"Behavior G": {
"id": "007",
"nickname": "Behavior G",
"type": "Dummy"
},
"Behavior H": {
"id": "008",
"nickname": "Behavior H",
"type": "Dummy"
}

}
}
Loading

0 comments on commit d8a8290

Please sign in to comment.