完成对ptml转化过程中py_trees接口的定义

This commit is contained in:
2193177243@qq.com 2023-10-09 09:31:13 +08:00
parent a7f5ec47df
commit 2cf5114f4d
4 changed files with 127 additions and 102 deletions

View File

@ -1,70 +0,0 @@
import py_trees as bt
import typing
class BTAPI():
"""
"""
def __init__(self) -> None:
self.string = None
def newTree(self) -> bt.trees.BehaviourTree:
"""_summary_
Returns:
bt.trees.BehaviourTree: _description_
"""
def newSequenceNode(self, tag: str='') -> None:
"""_summary_
Args:
tag (str, optional): _description_. Defaults to ''.
"""
return
def newSelectorNode(self, tag: str='') -> None:
"""_summary_
Args:
tag (str, optional): _description_. Defaults to ''.
"""
return
def newParallelNode(self, threshold:int) -> None:
"""_summary_
Args:
threshold (int): _description_
"""
return
def newDecoratorNode(self, tag: str='') -> None:
"""_summary_
Args:
tag (str, optional): _description_. Defaults to ''.
"""
return
def newBehaviourNode(self, name: str, args: typing.Optional[typing.List[str]]=[], tag:str='', isCond:bool=False) -> None:
"""_summary_
Args:
name (str): _description_
args (typing.Optional[typing.List[str]], optional): _description_. Defaults to [].
tag (str, optional): _description_. Defaults to ''.
isCond (bool, optional): _description_. Defaults to False.
"""
return
def attach_to_parent(self, uuid_child:str, uuid_parent:str) -> None:
"""_summary_
Args:
uuid_child (str): _description_
uuid_parent (str): _description_
"""
return

View File

@ -13,4 +13,4 @@ sequence:
task ReachDestinationA()
selector:
cond CoffeeCupPlaced()
task PlaceCoffeeCup()
task PlaceCoffeeCup(1.2)

120
ptml/ptmlTranslateAPI.py Normal file
View File

@ -0,0 +1,120 @@
import os
import typing
import shortuuid
short_uuid = lambda : shortuuid.ShortUUID().random(length=8)
class PyTreesAPI():
"""
"""
def __init__(self) -> None:
self.pystr = ''
self.prepare()
def prepare(self) -> None:
# import libraries
self.pystr += 'import py_trees\n'
self.pystr += '\n' * 2
def write_to_file(self, dir: str = './', filename: str = 'auto_generate.py') -> None:
"""_summary_
Args:
dir (str, optional): _description_. Defaults to './'.
filename (str, optional): _description_. Defaults to 'auto_generate.py'.
Raises:
FileNotFoundError: _description_
"""
# exceptions handling
filepath = os.path.join(dir, filename)
if not os.path.exists(dir):
raise FileNotFoundError(
''
)
# if os.path.exists(filepath):
# raise FileExistsError(
# ''
# )
with open(filepath, 'w') as file:
file.write(self.pystr)
def run() -> None:
"""_summary_
"""
def newTree(self) -> None:
"""_summary_
"""
def newSequenceNode(self) -> str:
"""_summary_
Returns:
str: _description_
"""
tag = 'sequence_' + short_uuid()
return tag
def newSelectorNode(self) -> str:
"""_summary_
Returns:
str: _description_
"""
tag = 'selector_' + short_uuid()
return tag
def newParallelNode(self, threshold:int) -> str:
"""_summary_
Args:
threshold (int): _description_
Returns:
str: _description_
"""
tag = 'parallel_' + short_uuid()
return tag
def newDecoratorNode(self) -> str:
"""_summary_
Returns:
str: _description_
"""
tag = 'decorator_' + short_uuid()
return tag
def newBehaviourNode(self,
name: str,
args: typing.Optional[typing.List[str]] = [],
isCond: bool = False
) -> str:
"""_summary_
Args:
name (str): _description_
args (typing.Optional[typing.List[str]], optional): _description_. Defaults to [].
isCond (bool, optional): _description_. Defaults to False.
Returns:
str: _description_
"""
tag = 'behavior_' + short_uuid()
return tag
def connect_to_parent(self, uuid_child:str, uuid_parent:str) -> None:
"""_summary_
Args:
uuid_child (str): _description_
uuid_parent (str): _description_
"""
return

View File

@ -8,7 +8,7 @@ sys.path.append(project_path)
from antlr4 import *
from ptmlListener import ptmlListener
from ptmlParser import ptmlParser
from BT_api import BTAPI
from ptml.ptmlTranslateAPI import PyTreesAPI
class ptmlTranslator(ptmlListener):
"""Translate the ptml language to BT.
@ -20,7 +20,7 @@ class ptmlTranslator(ptmlListener):
def __init__(self) -> None:
super().__init__()
self.stack = []
self.api = BTAPI()
self.api = PyTreesAPI()
# Enter a parse tree produced by ptmlParser#root.
@ -71,11 +71,12 @@ class ptmlTranslator(ptmlListener):
if len(ctx.children) > 4:
# have params
args = ctx.action_parm()
print(args.Float())
if type == 'cond':
self.api.newBehaviourNode(name, cond=True)
self.api.newBehaviourNode(name, isCond=True)
else:
self.api.newBehaviourNode(name, cond=False)
self.api.newBehaviourNode(name, isCond=False)
@ -93,24 +94,6 @@ class ptmlTranslator(ptmlListener):
pass
# Enter a parse tree produced by ptmlParser#var_decls.
def enterVar_decls(self, ctx:ptmlParser.Var_declsContext):
pass
# Exit a parse tree produced by ptmlParser#var_decls.
def exitVar_decls(self, ctx:ptmlParser.Var_declsContext):
pass
# Enter a parse tree produced by ptmlParser#var_type.
def enterVar_type(self, ctx:ptmlParser.Var_typeContext):
pass
# Exit a parse tree produced by ptmlParser#var_type.
def exitVar_type(self, ctx:ptmlParser.Var_typeContext):
pass
# Enter a parse tree produced by ptmlParser#boolean.
def enterBoolean(self, ctx:ptmlParser.BooleanContext):
pass
@ -118,12 +101,4 @@ class ptmlTranslator(ptmlListener):
# Exit a parse tree produced by ptmlParser#boolean.
def exitBoolean(self, ctx:ptmlParser.BooleanContext):
pass
class BtNode():
"""
"""
def __init__(self, type:str='BtNode') -> None:
self.type = type
self.name:str = ''