class UtgBasedInputPolicy(InputPolicy): """ state-based input policy """ def __init__(self, device, app, random_input): super(UtgBasedInputPolicy, self).__init__(device, app) self.random_input = random_input self.script = None self.master = None self.script_events = [] self.last_event = None self.last_state = None self.current_state = None self.utg = UTG(device=device, app=app, random_input=random_input) self.script_event_idx = 0 def generate_event(self): """ generate an event @return: """ # Get current device state self.current_state = self.device.get_current_state() self.__update_utg() event = None # if the previous operation is not finished, continue if len(self.script_events) > self.script_event_idx: event = self.script_events[self.script_event_idx].get_transformed_event(self) self.script_event_idx += 1 # First try matching a state defined in the script if event is None and self.script is not None: operation = self.script.get_operation_based_on_state(self.current_state) if operation is not None: self.script_events = operation.events # restart script event = self.script_events[0].get_transformed_event(self) self.script_event_idx = 1 if event is None: event = self.generate_event_based_on_utg() self.last_state = self.current_state self.last_event = event return event def __update_utg(self): self.utg.add_transition(self.last_event, self.last_state, self.current_state) @abstractmethod def generate_event_based_on_utg(self): """ generate an event based on UTG :return: InputEvent """ pass
def __init__(self, device, app): super(UtgBasedInputPolicy, self).__init__(device, app) self.script = None self.script_events = [] self.last_event = None self.last_state = None self.current_state = None self.utg = UTG(device=device, app=app) self.script_event_idx = 0
def __init__(self, device, app, random_input, is_gym_policy=False): super(UtgBasedInputPolicy, self).__init__(device, app, is_gym_policy) self.random_input = random_input self.script = None self.master = None self.script_events = [] self.last_event = None self.last_state = None self.current_state = None self.utg = UTG(device=device, app=app, random_input=random_input) self.script_event_idx = 0 if self.device.humanoid is not None: self.humanoid_view_trees = [] self.humanoid_events = []
class UtgBasedInputPolicy(InputPolicy): """ state-based input policy """ def __init__(self, device, app, random_input, is_gym_policy=False): super(UtgBasedInputPolicy, self).__init__(device, app, is_gym_policy) self.random_input = random_input self.script = None self.master = None self.script_events = [] self.last_event = None self.last_state = None self.current_state = None self.utg = UTG(device=device, app=app, random_input=random_input) self.script_event_idx = 0 if self.device.humanoid is not None: self.humanoid_view_trees = [] self.humanoid_events = [] def generate_event(self): """ generate an event @return: """ # Get current device state self.current_state = self.device.get_current_state() if self.current_state is None: import time time.sleep(5) return KeyEvent(name="BACK") self.__update_utg() # update last view trees for humanoid if self.device.humanoid is not None: self.humanoid_view_trees = self.humanoid_view_trees + [self.current_state.view_tree] if len(self.humanoid_view_trees) > 4: self.humanoid_view_trees = self.humanoid_view_trees[1:] event = None # if the previous operation is not finished, continue if len(self.script_events) > self.script_event_idx: event = self.script_events[self.script_event_idx].get_transformed_event(self) self.script_event_idx += 1 # First try matching a state defined in the script if event is None and self.script is not None: operation = self.script.get_operation_based_on_state(self.current_state) if operation is not None: self.script_events = operation.events # restart script event = self.script_events[0].get_transformed_event(self) self.script_event_idx = 1 if event is None: event = self.generate_event_based_on_utg() # update last events for humanoid if self.device.humanoid is not None: self.humanoid_events = self.humanoid_events + [event] if len(self.humanoid_events) > 3: self.humanoid_events = self.humanoid_events[1:] self.last_state = self.current_state self.last_event = event return event def __update_utg(self): self.utg.add_transition(self.last_event, self.last_state, self.current_state) @abstractmethod def generate_event_based_on_utg(self): """ generate an event based on UTG :return: InputEvent """ pass