From 2cf5114f4d183cbb981c945e35cd8195ba1aefaa Mon Sep 17 00:00:00 2001 From: "2193177243@qq.com" <2193177243@qq.com> Date: Mon, 9 Oct 2023 09:31:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AF=B9ptml=E8=BD=AC?= =?UTF-8?q?=E5=8C=96=E8=BF=87=E7=A8=8B=E4=B8=ADpy=5Ftrees=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ptml/BT_api.py | 70 ----------------------- ptml/CoffeeDelivery.ptml | 2 +- ptml/ptmlTranslateAPI.py | 120 +++++++++++++++++++++++++++++++++++++++ ptml/ptmlTranslator.py | 37 ++---------- 4 files changed, 127 insertions(+), 102 deletions(-) delete mode 100644 ptml/BT_api.py create mode 100644 ptml/ptmlTranslateAPI.py diff --git a/ptml/BT_api.py b/ptml/BT_api.py deleted file mode 100644 index 4c4e972..0000000 --- a/ptml/BT_api.py +++ /dev/null @@ -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 \ No newline at end of file diff --git a/ptml/CoffeeDelivery.ptml b/ptml/CoffeeDelivery.ptml index 82d129d..ea8bbdc 100644 --- a/ptml/CoffeeDelivery.ptml +++ b/ptml/CoffeeDelivery.ptml @@ -13,4 +13,4 @@ sequence: task ReachDestinationA() selector: cond CoffeeCupPlaced() - task PlaceCoffeeCup() \ No newline at end of file + task PlaceCoffeeCup(1.2) \ No newline at end of file diff --git a/ptml/ptmlTranslateAPI.py b/ptml/ptmlTranslateAPI.py new file mode 100644 index 0000000..fd6212a --- /dev/null +++ b/ptml/ptmlTranslateAPI.py @@ -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 \ No newline at end of file diff --git a/ptml/ptmlTranslator.py b/ptml/ptmlTranslator.py index be0f766..848e9fe 100644 --- a/ptml/ptmlTranslator.py +++ b/ptml/ptmlTranslator.py @@ -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 = '' \ No newline at end of file + \ No newline at end of file