コード例 #1
0
    def _ask_sub_goal(self, agent_action):
        if not self.sub_goal_exists():
            return None
        asked_action = agent_action['action']
        sub_goal = self.next_sub_goal()
        if asked_action['intent'] == 'Change_directory' and sub_goal['name'] == 'Change_directory' \
                and asked_action['new_directory'] in sub_goal['dirs']:
            return {'intent': self.confirm}
        if asked_action['intent'] in (
                'Move_file',
                'Copy_file') and sub_goal['name'] == asked_action['intent']:
            keys = ['origin', 'dest']
            asked_action['file'] = asked_action['file_name']
            response = {'intent': self.confirm}
            for key in keys:
                if not FileTreeSimulator.equal_paths(sub_goal[key],
                                                     asked_action[key]):
                    return {'intent': self.deny}
            if asked_action['file'] != sub_goal['file']:
                return {'intent': self.deny}
            return response
        if asked_action['intent'] == 'Open_file' and sub_goal[
                'name'] == asked_action['intent']:
            p_dir = FileTreeSimulator.last_dir_in_path(asked_action['path'])
            if p_dir == sub_goal['parent_directory'] and asked_action[
                    'file_name'] == sub_goal['file']:
                return {'intent': self.confirm}
            else:
                return {'intent': self.deny}
        if sub_goal['name'] == 'Rename_file' and asked_action[
                'intent'] == sub_goal['name']:
            p_dir = FileTreeSimulator.last_dir_in_path(asked_action['path'])
            asked_action['parent_directory'] = p_dir
            keys = ['old_name', 'new_name', 'parent_directory']
            response = {'intent': self.confirm}
            for key in keys:
                if asked_action[key] != sub_goal[key]:
                    return {'intent': self.deny}
            return response

        return None
コード例 #2
0
    def update_sub_goals(self, agent_action):
        for sub_goal in self.goal['sub_goal']:
            if sub_goal['name'] == 'Change_directory':
                last_dir = sub_goal['dirs'][-1]
                current_dir = self.state['current_directory']
                if current_dir[-1] == '/': current_dir = current_dir[:-1]
                if last_dir[-1] == '/': last_dir = last_dir[:-1]
                if current_dir == last_dir:
                    self.goal['sub_goal'].remove(sub_goal)
                    self.subgoal_reward = len(sub_goal['dirs'])
                elif current_dir in sub_goal['dirs']:
                    self.subgoal_reward = sub_goal['dirs'].index(
                        current_dir) + 1
                    del sub_goal['dirs'][:self.subgoal_reward]
            if sub_goal['name'] == 'Search_file':
                if agent_action[
                        'intent'] == 'inform' and 'paths' in agent_action:
                    self.subgoal_reward = 0
                    paths = agent_action['paths']
                    r = self.state['current_file_tree'].lookup_file_name(
                        sub_goal['file'])
                    self.subgoal_reward = -2
                    if r is not None:
                        f, m = r
                        for path in paths:
                            if FileTreeSimulator.equal_paths(
                                    m['tree_sim'].path(True), path):
                                self.subgoal_reward = 2
                                break

                    self.goal['sub_goal'].remove(sub_goal)
            if sub_goal['name'] == 'Rename_file':
                if agent_action['intent'] == 'Rename_file':
                    p_dir = FileTreeSimulator.last_dir_in_path(
                        agent_action['path'])
                    agent_action['parent_directory'] = p_dir
                    keys = ['old_name', 'new_name', 'parent_directory']
                    self.subgoal_reward = 2
                    for key in keys:
                        if agent_action[key] != sub_goal[key]:
                            self.subgoal_reward = -3

                    self.goal['sub_goal'].remove(sub_goal)
            if sub_goal['name'] == 'Open_file':
                if agent_action['intent'] == sub_goal['name']:
                    p_dir = FileTreeSimulator.last_dir_in_path(
                        agent_action['path'])
                    if p_dir != sub_goal['parent_directory'] or agent_action[
                            'file_name'] != sub_goal['file']:
                        self.subgoal_reward = -3
                    else:
                        self.subgoal_reward = 2
                    self.goal['sub_goal'].remove(sub_goal)
            if sub_goal['name'] == 'Move_file' or sub_goal[
                    'name'] == 'Copy_file':
                if agent_action['intent'] == sub_goal['name']:
                    keys = ['origin', 'dest']
                    agent_action['file'] = agent_action['file_name']
                    self.subgoal_reward = 2
                    for key in keys:
                        if not FileTreeSimulator.equal_paths(
                                sub_goal[key], agent_action[key]):
                            self.subgoal_reward = -3
                            break
                    if agent_action['file'] != sub_goal['file']:
                        self.subgoal_reward = -3
                    self.goal['sub_goal'].remove(sub_goal)
                    break