Ejemplo n.º 1
0
def convert(original_pddl_name, sas_name, new_pddl_name):
    state_values = read_initial_state(sas_name)
    pddl = parse_pddl_file("task", original_pddl_name)
    replace_init_values(state_values, find_pddl_init(pddl))
    with file_open(new_pddl_name, "w") as new_pddl_file:
        write_pddl(new_pddl_file, pddl)
    print("New PDDL file successfully written: %s" % new_pddl_name)
Ejemplo n.º 2
0
def open(filename="_inax.json"):
    # since we're introducing a new function called `open`
    from builtins import open as file_open

    def to_json_fn(value, name, pos):
        # assume the value is valid JSON
        return value

    def from_json_fn(value, name, pos):
        # assume the JSON is a valid value
        return value

    try:
        f = file_open(filename, "r")
    except FileNotFoundError:
        cache = _Cache.empty(to_json_fn)
    else:
        cache = _Cache.from_json(json.load(f),
                                 from_json_fn=from_json_fn,
                                 to_json_fn=to_json_fn)
        f.close()

    # in case the cwd has changed when serialization happens
    path = os.path.abspath(filename)

    def serialize_fn(cache):
        with file_open(path, "w") as f:
            json.dump(cache, f, separators=",:")
            f.write('\n')

    return Context(cache, serialize_fn=serialize_fn)
Ejemplo n.º 3
0
 async def upload_from_filename(self, bucket: str, object_name: str,
                                filename: str,
                                **kwargs: Any) -> Dict[str, Any]:
     async with file_open(  # type: ignore[attr-defined]
             filename,
             mode='rb',
     ) as file_object:
         return await self.upload(bucket, object_name, file_object,
                                  **kwargs)
Ejemplo n.º 4
0
 async def download_to_filename(self, bucket: str, object_name: str,
                                filename: str, **kwargs: Any) -> None:
     async with file_open(  # type: ignore[attr-defined]
             filename,
             mode='wb+',
     ) as file_object:
         await file_object.write(await
                                 self.download(bucket, object_name,
                                               **kwargs))
Ejemplo n.º 5
0
def parse_lisp_file(filename):
    try:
        # The builtin open function is shadowed by this module's open function.
        # We use the Latin-1 encoding (which allows a superset of ASCII, of the
        # Latin-* encodings and of UTF-8) to allow special characters in
        # comments. In all other parts, we later validate that only ASCII is
        # used.
        return parse_nested_list(file_open(filename, encoding='ISO-8859-1'))
    except IOError as e:
        raise SystemExit("Error: Could not read file: %s\nReason: %s." %
                         (e.filename, e))
    except ParseError as e:
        raise SystemExit("Error: Could not parse %s file: %s\nReason: %s." %
                         (type, filename, e))
def parse_pddl_file(type, filename):
    try:
        # The builtin open function is shadowed by this module's open function.
        # We use the Latin-1 encoding (which allows a superset of ASCII, of the
        # Latin-* encodings and of UTF-8) to allow special characters in
        # comments. In all other parts, we later validate that only ASCII is
        # used.
        return lisp_parser.parse_nested_list(file_open(filename,
                                                       encoding='ISO-8859-1'))
    except IOError as e:
        raise SystemExit("Error: Could not read file: %s\nReason: %s." %
                         (e.filename, e))
    except lisp_parser.ParseError as e:
        raise SystemExit("Error: Could not parse %s file: %s\nReason: %s." %
                         (type, filename, e))
Ejemplo n.º 7
0
def parse_pddl_file(type, filename):
    try:
        tokens = tokenize(file_open(filename, encoding='ISO-8859-1'))
        next_token = next(tokens)
        if next_token != "(":
            raise ParseError("Expected '(', got %s." % next_token)
        result = list(parse_list_aux(tokens))
        for tok in tokens:  # Check that generator is exhausted.
            raise ParseError("Unexpected token: %s." % tok)
        return result

    except IOError as e:
        raise SystemExit("Error: Could not read file: %s\nReason: %s." %
                         (e.filename, e))
    except ParseError as e:
        raise SystemExit("Error: Could not parse %s file: %s\nReason: %s." %
                         (type, filename, e))
Ejemplo n.º 8
0
        out_file.write("    MOVE-DIR %s)\n" % move)

    # Object state
    out_file.write("    (at player-01 %s)\n" % stage.player_pos)
    for i in range(len(stage.stone_pos)):
        out_file.write("    (at stone-%s %s)\n" % (i, stage.stone_pos[i]))
    out_file.write("    (= (total-cost) 0)\n")
    out_file.write("  )\n")

    # Goal
    out_file.write("  (:goal (and\n")
    for i in range(len(stage.stone_pos)):
        out_file.write("    (at-goal stone-%s)\n" % i)
    out_file.write("  )\n")
    out_file.write("  (:metric minimize (total-cost))\n")
    out_file.write(")\n")


if __name__ == "__main__":
    in_file = sys.argv[1]
    out_file = sys.argv[2]
    name = sys.argv[3]

    with file_open(in_file, 'r') as file:
        stage = read_stage(file, name)

    with file_open(out_file, 'w') as file:
        write_pddl(file, stage)

    print("Stage %s written succesfully to %s" % (name, out_file))
Ejemplo n.º 9
0
 def serialize_fn(cache):
     with file_open(path, "w") as f:
         json.dump(cache, f, separators=",:")
         f.write('\n')