# Created by Danyang Zhang @X-Lance. from android_env.wrappers import base_wrapper from android_env.environment import AndroidEnv import numpy as np from .target_handlers import TransformerSet from typing import Union, Optional from typing import List import functools import dm_env class InstructionRewritingWrapper(base_wrapper.BaseWrapper): # class InstructionRewritingWrapper {{{ # """ Transforms the instructions and commands of AndroidEnv. """ def __init__( self , env: AndroidEnv , search_pattern_file: str , article_pattern_file: str , article_command_pattern_file: str , categ_pattern_file: str , author_pattern_file: str , question_pattern_file: str , doccano_file: str ): # method __init__ {{{ # super(InstructionRewritingWrapper, self).__init__(env) rng = np.random.default_rng() self._transformer: TransformerSet =\ TransformerSet( search_pattern_file , article_pattern_file , article_command_pattern_file , categ_pattern_file , author_pattern_file , question_pattern_file , doccano_file , rng ) self._command: Optional[List[str]] = None self._instructions: List[str] = [] # }}} method __init__ # def _reset_state(self): self._command = None self._instructions = [] def command(self) -> List[str]: # method command {{{ # if self._command is None: self._command = list( map( functools.partial( self._transformer.transform , environment="command" ) , self._env.command() ) ) return self._command # }}} method command # def task_instructions(self, latest_only: bool=False) -> Union[str, List[str]]: # method task_instructions {{{ # if latest_only: return self._instructions[-1] if len(self._instructions)>0 else "" else: return self._instructions.copy() # }}} method task_instructions # def _process_timestep(self, timestep: dm_env.TimeStep) -> dm_env.TimeStep: # method _process_timestep {{{ # self._instructions = list( map( functools.partial( self._transformer.transform , environment="instruction" ) , self._env.task_instructions() ) ) return timestep # }}} method _process_timestep # # }}} class InstructionRewritingWrapper #