def generate_cdf_from_task()

in Luminous4Alfred/TaskParser/cdf_gene/utils.py [0:0]


def generate_cdf_from_task(task, data_url="https://raw.githubusercontent.com/askforalfred/alfred/master/data/json_2.1.0/"):
    json_path = os.path.join(data_url, "train", task['task'], 'traj_data.json')

    with urllib.request.urlopen(json_path) as url:
        ex = json.loads(url.read().decode())

    cdf = {
        "task_desc": "",
        "scene": {
            "scene_type": [],
            "agent_init": [],
            "init_actions": [],
            "required_objects": []
        },
        "script": [],
        "task_goals": [],
        "attributes":{}
    }
    # basic info
    cdf["task_desc"] = task["task"]
    floor_plan = ex['scene']['floor_plan']
    cdf["attributes"]['floor_plan'] = floor_plan
    floor_plan_id = int(floor_plan.split("Plan")[-1])
    cdf['scene']['scene_type'].append(0 if floor_plan_id < 100 else 1 if floor_plan_id < 300 else 2 if floor_plan_id < 400 \
        else 3)
    
    # if cdf['scene']['scene_type'][0] in [0,1,2]:
    #     if "cool" in task["task"] or "heat" in task["task"] or "clean" in task["task"]:
    #         print("buscar para task:", task)
    
    # set goals and script
    task_goal, script = get_task_goals_and_script(ex)
    cdf["task_goals"].append(task_goal)
    cdf["script"] = script
    
    # get required objects and set up scripts to solve the problem
    current_location = ""
    required_object_names = []
    required_object = None
    for plan in ex['plan']['high_pddl']:
        # print("plan",plan['discrete_action'])
        # print(cdf['scene']['required_objects'])
        # print("required_object_names", required_object_names, required_object, "\n\n")


        required_object = None
        discrete_action = plan['discrete_action']
        if discrete_action["action"] == "GotoLocation":
            target_obj = discrete_action["args"][0]
            current_location = sim_obj_name2type[target_obj] + "_1"

            if target_obj not in required_object_names:
                required_object_names.append(target_obj)
                required_object = {
                    "name":current_location,
                }

        elif discrete_action["action"] in ["PickupObject","SliceObject"]:
            target_obj = discrete_action["args"][0]
            
            if ex['task_type'] == "pick_two_obj_and_place":
                if target_obj not in required_object_names:
                    required_object_names.append(target_obj)
                    required_object = {
                        "name":sim_obj_name2type[target_obj] + "_1",
                        "location": [{
                            current_location: "on",
                        }]
                    }
                elif target_obj in required_object_names:
                    #print(plan)
                    if discrete_action["action"] == "PickupObject" and current_location == sim_obj_name2type[target_obj] + "_1":
                        # wrong current location: find the place of the first object
                        for req_obj in cdf['scene']['required_objects']:
                            if req_obj["name"] == current_location:
                                if "location" in req_obj:
                                    current_location = list(req_obj["location"][0].keys())[0]
                                    break
                                
                        required_object = {
                            "name":sim_obj_name2type[target_obj] + "_2",
                            "location": [{
                                current_location: "on",
                            }]
                        }
                    else:
                        required_object = {
                            "name":sim_obj_name2type[target_obj] + "_2",
                            "location": [{
                                current_location: "on",
                            }]
                        }
                        
            
            elif ex['task_type'] == "pick_and_place_with_movable_recep":
                #print("plan", plan["planner_action"])
                
                if "coordinateReceptacleObjectId" in plan["planner_action"]:
                    recep_obj = plan["planner_action"]["coordinateReceptacleObjectId"][0].lower()
                    # print("??????", recep_obj)
                    if recep_obj not in required_object_names:
                        required_object_names.append(recep_obj)
                        required_object = {
                            "name":sim_obj_name2type[recep_obj] + "_1",
                        }
                        current_location = sim_obj_name2type[recep_obj] + "_1"
                    else:
                        current_location = sim_obj_name2type[recep_obj] + "_1"
                    
                if target_obj in required_object_names:
                    required_object_names.append(target_obj)

                    if required_object != None:
                        cdf['scene']['required_objects'][-1] = required_object
                    else:
                        cdf['scene']['required_objects'].remove(cdf['scene']['required_objects'][-1])

                    required_object = {
                        "name":sim_obj_name2type[target_obj] + "_1",
                        "location": [{
                            current_location: "on",
                        }]
                    }
                    
                else:
                    required_object_names.append(target_obj)
                    required_object = {
                        "name":sim_obj_name2type[target_obj] + "_1",
                        "location": [{
                            current_location: "on",
                        }]
                    }
                
            
            else:
                if target_obj not in required_object_names:
                    required_object_names.append(target_obj)
                    required_object = {
                        "name":sim_obj_name2type[target_obj] + "_1",
                        "location": [{
                            current_location: "on",
                        }]
                    }
            
        else:
            pass

        if required_object is not None:
            cdf['scene']['required_objects'].append(required_object)
            
            # turn off the lamp
            if "Lamp" in required_object["name"]:
                init_action = {
                    "action": "ToggleObjectOff",
                    "name": "{}".format(required_object["name"]),
                    "forceAction": True
                }
                cdf["scene"]["init_actions"].append(init_action)
            
        #print("required_object", required_object, "\n\n")

    #print(cdf)
    
    # parse cdf again and make some neccessary changes
    modify(cdf)

    #object_vocab += required_object_names

    return cdf, ex, required_object_names