Ejemplo n.º 1
0
    def task_stack_push(
        self, task: Task, parent_memid: str = None, chat_effect: bool = False
    ) -> "TaskNode":
        """Create a task object in memory, add triples and add to task stack

        Args:
            task (Task): The task to be pushed
            parent_memid (string): Memory ID of the task's parent
            chat_effect (bool): If the task was a result of a chat, add the triple.

        Returns:
            TaskNode: A TaskNode object

        Examples ::
            >>> task = Move(agent, {"target": pos_to_np([0, 0 , 0]), "approx" : 3})
            >>> parent_memid = '10517cc584844659907ccfa6161e9d32'
            >>> task_stack_push(task, parent_memid)
        """

        memid = TaskNode.create(self, task)

        # Relations
        if parent_memid:
            self.add_triple(subj=memid, pred_text="_has_parent_task", obj=parent_memid)
        if chat_effect:
            chat = self.get_most_recent_incoming_chat()
            assert chat is not None, "chat_effect=True with no incoming chats"
            self.add_triple(subj=chat.memid, pred_text="chat_effect_", obj=memid)

        # Return newly created object
        return TaskNode(self, memid)
Ejemplo n.º 2
0
    def task_stack_find_lowest_instance(
        self, cls_names: Union[str, Sequence[str]]
    ) -> Optional["TaskNode"]:
        """Find and return the lowest item in the stack of the given class(es)

        Args:
            cls_names (Sequence): Class names of tasks

        Returns:
            TaskNode: A TaskNode object

        Examples ::
            >>> cls_names = 'Move'
            >>> task_stack_find_lowest_instance(cls_names)
        """
        names = [cls_names] if type(cls_names) == str else cls_names
        (memid,) = self._db_read_one(
            "SELECT uuid FROM Tasks WHERE {} ORDER BY created LIMIT 1".format(
                " OR ".join(["action_name=?" for _ in names])
            ),
            *names,
        )

        if memid is not None:
            return TaskNode(self, memid)
        else:
            return None
Ejemplo n.º 3
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.schematic = task_data["schematic"]  # a list of xyz tuples
     self.block_idm = task_data.get("block_idm", (2, 0))  # default 2: grass
     self.build_task = None
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 4
0
 def __init__(self, agent, task_data, featurizer=None):
     super().__init__(agent, task_data)
     self.target = np.array(task_data["target"])
     self.is_relative = task_data.get("is_relative", 0)
     self.path = None
     self.command_sent = False
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 5
0
    def task_stack_push(
        self, task: Task, parent_memid: str = None, chat_effect: bool = False
    ) -> "TaskNode":

        memid = TaskNode.create(self, task)

        # Relations
        if parent_memid:
            self.add_triple(subj=memid, pred_text="_has_parent_task", obj=parent_memid)
        if chat_effect:
            chat = self.get_most_recent_incoming_chat()
            assert chat is not None, "chat_effect=True with no incoming chats"
            self.add_triple(subj=chat.memid, pred_text="chat_effect_", obj=memid)

        # Return newly created object
        return TaskNode(self, memid)
Ejemplo n.º 6
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.target = task_data.get("target")
     self.yaw = task_data.get("yaw")
     self.pitch = task_data.get("pitch")
     assert self.yaw or self.pitch or self.target
     self.command_sent = False
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 7
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.object_idm = task_data["object_idm"]
     self.mobtype = MOBS_BY_ID[self.object_idm[1]]
     self.pos = task_data["pos"]
     self.PLACE_REACH = task_data.get("PLACE_REACH", 3)
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 8
0
 def task_stack_get_all(self) -> List["TaskNode"]:
     r = self._db_read("""
         SELECT uuid
         FROM Tasks
         WHERE paused=0 AND finished_at<0
         ORDER BY created_at
         """)
     return [TaskNode(self, memid) for memid, in r]
Ejemplo n.º 9
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.schematic = task_data["schematic"]  # list[(xyz, idm)]
     self.dig_message = True if "dig_message" in task_data else False
     self.build_task = None
     self.DIG_REACH = task_data.get("DIG_REACH", 3)
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 10
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.origin = task_data["origin"]
     self.length = task_data["length"]
     self.width = task_data["width"]
     self.depth = task_data["depth"]
     self.destroy_task = None
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 11
0
 def __init__(self, agent, task_data):
     super().__init__(agent, task_data)
     if self.finished:
         return
     self.target = to_block_pos(np.array(task_data["target"]))
     self.approx = task_data.get("approx", 1)
     self.path = None
     self.replace = set()
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 12
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.idm = task_data["idm"]
     self.pos = task_data["pos"]
     self.eid = task_data["eid"]
     self.memid = task_data["memid"]
     self.approx = 1
     self.attempts = 10
     self.item_count_before_get = agent.get_inventory_item_count(
         self.idm[0], self.idm[1])
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 13
0
 def task_stack_peek(self) -> Optional["TaskNode"]:
     r = self._db_read_one("""
         SELECT uuid
         FROM Tasks
         WHERE finished_at < 0 AND paused = 0
         ORDER BY created_at DESC
         LIMIT 1
         """)
     if r:
         return TaskNode(self, r[0])
     else:
         return None
Ejemplo n.º 14
0
    def __init__(self, agent, task_data):
        super(DanceMove, self).__init__(agent)
        self.relative_yaw = task_data.get("relative_yaw")
        self.relative_pitch = task_data.get("relative_pitch")

        # look_turn is (yaw, pitch).  pitch = 0 is head flat
        self.head_yaw_pitch = task_data.get("head_yaw_pitch")
        self.head_xyz = task_data.get("head_xyz")

        self.translate = task_data.get("translate")
        self.last_stepped_time = agent.memory.get_time()
        TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 15
0
    def step(self):
        """Ask a confirmation question and wait for response."""
        # Step 1: ask the question
        if not self.asked:
            self.dialogue_stack.append_new(AwaitResponse)
            self.dialogue_stack.append_new(Say, self.question)
            self.asked = True
            return "", None

        # Step 2: check the response and add the task if necessary
        self.finished = True
        if len(self.progeny_data) == 0:
            return None, None
        if hasattr(self.progeny_data[-1]["response"], "chat_text"):
            response_str = self.progeny_data[-1]["response"].chat_text
        else:
            response_str = "UNK"
        if response_str in MAP_YES:
            for task in self.tasks:
                mem = TaskNode(self.agent.memory, task.memid)
                mem.get_update_status({"prio": 1, "running": 1})
        return None, None
Ejemplo n.º 16
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     # get target should be a ReferenceObjectNode memid
     self.get_target = task_data["get_target"]
     self.give_target = task_data["give_target"]
     # steps take values "not_started", "started", "complete"
     if not self.give_target:
         # TODO all movements simultaneous- change look while driving
         # approach_pickup, look_at_object, grab
         self.steps = ["not_started"] * 3
     else:
         # approach_pickup, look_at_object, grab, approach_dropoff, give/drop
         self.steps = ["not_started"] * 5
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 17
0
    def task_stack_find_lowest_instance(
            self, cls_names: Union[str,
                                   Sequence[str]]) -> Optional["TaskNode"]:
        """Find and return the lowest item in the stack of the given class(es)"""
        names = [cls_names] if type(cls_names) == str else cls_names
        memid, = self._db_read_one(
            "SELECT uuid FROM Tasks WHERE {} ORDER BY created_at LIMIT 1".
            format(" OR ".join(["action_name=?" for _ in names])),
            *names,
        )

        if memid is not None:
            return TaskNode(self, memid)
        else:
            return None
Ejemplo n.º 18
0
    def step(self) -> Tuple[Optional[str], Any]:
        assert self.action_dict["dialogue_type"] == "HUMAN_GIVE_COMMAND"
        try:
            actions = []
            if "action" in self.action_dict:
                actions.append(self.action_dict["action"])
            elif "action_sequence" in self.action_dict:
                actions = self.action_dict["action_sequence"]

            if len(actions) == 0:
                # The action dict is in an unexpected state
                raise ErrorWithResponse(
                    "I thought you wanted me to do something, but now I don't know what"
                )
            tasks_to_push = []
            for action_def in actions:
                action_type = action_def["action_type"]
                r = self.action_handlers[action_type](self.speaker, action_def)
                if len(r) == 3:
                    task, response, dialogue_data = r
                else:
                    # FIXME don't use this branch, uniformize the signatures
                    task = None
                    response, dialogue_data = r
                if task:
                    tasks_to_push.append(task)
            task_mem = None
            if tasks_to_push:
                T = maybe_task_list_to_control_block(tasks_to_push, self.agent)
                task_mem = TaskNode(self.agent.memory, tasks_to_push[0].memid)
            if task_mem:
                chat = self.agent.memory.get_most_recent_incoming_chat()
                TripleNode.create(
                    self.agent.memory,
                    subj=chat.memid,
                    pred_text="chat_effect_",
                    obj=task_mem.memid,
                )
            self.finished = True
            return response, dialogue_data
        except NextDialogueStep:
            return None, None
        except ErrorWithResponse as err:
            self.finished = True
            return err.chat, None
Ejemplo n.º 19
0
    def task_stack_peek(self) -> Optional["TaskNode"]:
        """Return the top of task stack

        Returns:
            TaskNode: TaskNode object or None

        Examples ::
            >>> task_stack_peek()
        """
        r = self._db_read_one(
            """
            SELECT uuid
            FROM Tasks
            WHERE finished < 0 AND paused = 0 AND prio > 0
            ORDER BY created DESC
            LIMIT 1
            """
        )
        if r:
            return TaskNode(self, r[0])
        else:
            return None
Ejemplo n.º 20
0
    def get_last_finished_root_task(self,
                                    action_name: str = None,
                                    recency: int = None):
        q = """
        SELECT uuid
        FROM Tasks
        WHERE finished_at >= ? {}
        ORDER BY created_at DESC
        """.format(" AND action_name=?" if action_name else "")
        if recency is None:
            recency = self.time.round_time(300)
        args: List = [self.get_time() - recency]
        if action_name:
            args.append(action_name)
        memids = [r[0] for r in self._db_read(q, *args)]
        for memid in memids:
            if self._db_read_one(
                    "SELECT uuid FROM Triples WHERE pred_text='_has_parent_task' AND subj=?",
                    memid):
                # not a root task
                continue

            return TaskNode(self, memid)
Ejemplo n.º 21
0
    def get_last_finished_root_task(self, action_name: str = None, recency: int = None):
        """Get last task that was marked as finished

        Args:
            action_name (string): Name of action associated with task
            recency (int): How recent should the task be

        Returns:
            TaskNode: A TaskNode object

        Examples ::
            >>> action_name = "BUILD"
            >>> get_last_finished_root_task (action_name=action_name)
        """
        q = """
        SELECT uuid
        FROM Tasks
        WHERE finished >= ? {}
        ORDER BY created DESC
        """.format(
            " AND action_name=?" if action_name else ""
        )
        if recency is None:
            recency = self.time.round_time(300)
        args: List = [self.get_time() - recency]
        if action_name:
            args.append(action_name)
        memids = [r[0] for r in self._db_read(q, *args)]
        for memid in memids:
            if self._db_read_one(
                "SELECT uuid FROM Triples WHERE pred_text='_has_parent_task' AND subj=?", memid
            ):
                # not a root task
                continue

            return TaskNode(self, memid)
Ejemplo n.º 22
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     # movement should be a Movement object from dance.py
     self.movement = task_data.get("movement")
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 23
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.command_sent = False
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 24
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     # currently unused, we can expand this soon?
     self.object_to_drop = task_data.get("object", None)
     self.command_sent = False
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 25
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     # this is a ref object memid
     self.target = task_data["target"]
     self.command_sent = False
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 26
0
 def __init__(self, agent, task_data, featurizer=None):
     super().__init__(agent)
     # movement should be a Movement object from dance.py
     self.movement = dance.DanceMovement(self.agent, None)
     self.movement_type = task_data.get("movement_type", None)
     TaskNode(self.agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 27
0
 def __init__(self, agent, task_data):
     super().__init__(agent)
     self.to_undo_memid = task_data["memid"]
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 28
0
 def __init__(self, agent, task_data):
     super(Point, self).__init__(agent)
     self.target = task_data.get("target")
     self.start_time = agent.memory.get_time()
     self.last_stepped_time = agent.memory.get_time()
     TaskNode(agent.memory, self.memid).update_task(task=self)
Ejemplo n.º 29
0
 def get_task_by_id(self, memid: str) -> "TaskNode":
     return TaskNode(self, memid)
Ejemplo n.º 30
0
    def __init__(self, agent, task_data):
        super().__init__(agent)
        self.task_data = task_data
        self.embed = task_data.get("embed", False)
        self.schematic, _ = blocks_list_to_npy(task_data["blocks_list"])
        self.origin = task_data["origin"]
        self.verbose = task_data.get("verbose", True)
        self.relations = task_data.get("relations", [])
        self.default_behavior = task_data.get("default_behavior")
        self.force = task_data.get("force", False)
        self.attempts = 3 * np.ones(self.schematic.shape[:3], dtype=np.uint8)
        self.fill_message = task_data.get("fill_message", False)
        self.schematic_memid = task_data.get("schematic_memid", None)
        self.schematic_tags = task_data.get("schematic_tags", [])
        self.giving_up_message_sent = False
        self.wait = False
        self.old_blocks_list = None
        self.old_origin = None
        self.PLACE_REACH = task_data.get("PLACE_REACH", 3)

        # negative schematic related
        self.is_destroy_schm = task_data.get("is_destroy_schm", False)
        self.dig_message = task_data.get("dig_message", False)
        self.blockobj_memid = None
        self.DIG_REACH = task_data.get("DIG_REACH", 3)
        self.last_stepped_time = agent.memory.get_time()

        if self.is_destroy_schm:
            # is it destroying a whole block object? if so, save its tags
            self.destroyed_block_object_triples = []
            xyzs = set(strip_idmeta(task_data["blocks_list"]))
            mem = agent.memory.get_block_object_by_xyz(next(iter(xyzs)))
            # TODO what if there are several objects being destroyed?
            if mem and all(xyz in xyzs for xyz in mem.blocks.keys()):
                for pred in ["has_tag", "has_name", "has_colour"]:
                    self.destroyed_block_object_triples.extend(
                        agent.memory.get_triples(subj=mem.memid,
                                                 pred_text=pred))
                logging.debug("Destroying block object {} tags={}".format(
                    mem.memid, self.destroyed_block_object_triples))

        # modify the schematic to avoid placing certain blocks
        for bad, good in BUILD_BLOCK_REPLACE_MAP.items():
            self.schematic[self.schematic[:, :, :, 0] == bad] = good
        self.new_blocks = []  # a list of (xyz, idm) of newly placed blocks

        # snap origin to ground if bottom level has dirt blocks
        # NOTE(kavyasrinet): except for when we are rebuilding the old dirt blocks, we
        # don't want to change the origin then, hence the self.force check.
        if not self.force and not self.embed and np.isin(
                self.schematic[:, :, :, 0], (2, 3)).any():
            h = ground_height(agent, self.origin, 0)
            self.origin[1] = h[0, 0]

        # get blocks occupying build area and save state for undo()
        ox, oy, oz = self.origin
        sy, sz, sx, _ = self.schematic.shape
        current = agent.get_blocks(ox, ox + sx - 1, oy, oy + sy - 1, oz,
                                   oz + sz - 1)
        self.old_blocks_list = npy_to_blocks_list(current, self.origin)
        if len(self.old_blocks_list) > 0:
            self.old_origin = np.min(strip_idmeta(self.old_blocks_list),
                                     axis=0)

        TaskNode(agent.memory, self.memid).update_task(task=self)