Ejemplo n.º 1
0
def check_one_program(helper,
                      script,
                      precond,
                      graph_dict,
                      w_graph_list,
                      modify_graph=True,
                      place_other_objects=True,
                      id_mapping={},
                      **info):

    helper.initialize(graph_dict)
    script, precond = modify_objects_unity2script(helper, script, precond)
    if modify_graph:
        ## add missing object from scripts (id from 1000) and set them to default setting
        ## id mapping can specify the objects that already specify in the graphs
        helper.set_to_default_state(graph_dict,
                                    None,
                                    id_checker=lambda v: True)

        id_mapping, first_room, room_mapping = helper.add_missing_object_from_script(
            script, precond, graph_dict, id_mapping)
        info = {'room_mapping': room_mapping}
        objects_id_in_script = [v for v in id_mapping.values()]
        helper.set_to_default_state(
            graph_dict,
            first_room,
            id_checker=lambda v: v in objects_id_in_script)

        ## place the random objects (id from 2000)
        if place_other_objects:
            max_node_to_place = max_nodes - len(graph_dict["nodes"])
            n = random.randint(max_node_to_place - 20, max_node_to_place)
            helper.add_random_objs_graph_dict(graph_dict, n=max(n, 0))
            helper.set_to_default_state(graph_dict,
                                        None,
                                        id_checker=lambda v: v >= 2000)
            helper.random_change_object_state(
                id_mapping,
                graph_dict,
                id_checker=lambda v: v not in objects_id_in_script)

        ## set relation and state from precondition
        helper.check_binary(graph_dict,
                            id_checker=lambda v: True,
                            verbose=False)
        random_objects_id = helper.random_objects_id
        helper.prepare_from_precondition(precond, id_mapping, graph_dict)

        helper.open_all_doors(graph_dict)
        helper.ensure_light_on(
            graph_dict, id_checker=lambda v: v not in objects_id_in_script)

        helper.check_binary(graph_dict,
                            id_checker=lambda v: v >= random_objects_id,
                            verbose=False)
        helper.check_binary(graph_dict,
                            id_checker=lambda v: True,
                            verbose=True)

        assert len(graph_dict["nodes"]) <= max_nodes

    elif len(id_mapping) != 0:
        # Assume that object mapping specify all the objects in the scripts
        helper.modify_script_with_specified_id(script, id_mapping, **info)

    graph = EnvironmentGraph(graph_dict)
    name_equivalence = utils.load_name_equivalence()
    executor = ScriptExecutor(graph, name_equivalence)
    executable, final_state, graph_state_list = executor.execute(
        script, w_graph_list=w_graph_list)

    if executable:
        message = 'Script is executable'
    else:
        message = 'Script is not executable, since {}'.format(
            executor.info.get_error_string())

    return message, executable, final_state, graph_state_list, id_mapping, info, script
Ejemplo n.º 2
0
def load_graph(file_name):
    with open(file_name) as f:
        data = json.load(f)
    return EnvironmentGraph(data)
Ejemplo n.º 3
0
def example_5():

    properties_data = utils.load_properties_data()
    object_states = utils.load_object_states()
    object_placing = utils.load_object_placing()
    graph_dict = {
        'nodes': [
            {
                "id": 1,
                'class_name': "kitchen",
                "category": "Rooms",
                "properties": [],
                "states": []
            },
            {
                "id": 2,
                'class_name': "bedroom",
                "category": "Rooms",
                "properties": [],
                "states": []
            },
            {
                "id": 3,
                'class_name': "home_office",
                "category": "Rooms",
                "properties": [],
                "states": []
            },
            {
                "id": 4,
                'class_name': "bathroom",
                "category": "Rooms",
                "properties": [],
                "states": []
            },
            {
                "id": 5,
                'class_name': "character",
                "category": "",
                "properties": [],
                "states": []
            },
            {
                "id": 6,
                'class_name': "door",
                "category": "",
                "properties": ["CAN_OPEN"],
                "states": ["OPEN"]
            },
            {
                "id": 7,
                'class_name': "door",
                "category": "",
                "properties": ["CAN_OPEN"],
                "states": ["OPEN"]
            },
            {
                "id": 8,
                'class_name': "door",
                "category": "",
                "properties": ["CAN_OPEN"],
                "states": ["OPEN"]
            },
        ],
        'edges': [
            {
                "from_id": 5,
                "to_id": 2,
                "relation_type": "INSIDE"
            },
            {
                "from_id": 6,
                "to_id": 1,
                "relation_type": "BETWEEN"
            },
            {
                "from_id": 6,
                "to_id": 2,
                "relation_type": "BETWEEN"
            },
            {
                "from_id": 7,
                "to_id": 1,
                "relation_type": "BETWEEN"
            },
            {
                "from_id": 7,
                "to_id": 3,
                "relation_type": "BETWEEN"
            },
            {
                "from_id": 8,
                "to_id": 3,
                "relation_type": "BETWEEN"
            },
            {
                "from_id": 8,
                "to_id": 4,
                "relation_type": "BETWEEN"
            },
        ]
    }

    helper = utils.graph_dict_helper(properties_data,
                                     object_placing,
                                     object_states,
                                     max_nodes=15)
    helper.initialize(graph_dict)

    print()
    print('Example 5')
    print('---------')
    script = read_script('example_scripts/example_script_4.txt')
    with open('example_scripts/example_precond_script_4.json', 'r') as f:
        precond = json.load(f)

    id_mapping = {}
    id_mapping, first_room, room_mapping = helper.add_missing_object_from_script(
        script, precond, graph_dict, id_mapping)
    print(id_mapping)

    # add random objects
    graph = EnvironmentGraph(graph_dict)

    name_equivalence = utils.load_name_equivalence()
    executor = ScriptExecutor(graph, name_equivalence)
    state = executor.execute(script)
    if state is None:
        print('Script is not executable, since {}'.format(
            executor.info.get_error_string()))
    else:
        print('Script is executable')