def visit_type(self, node): """Visits a PDDL type definition.""" # Store matching parent type in node # (if none is given, it's always object) if node.parent == None: self.set_in(node, pddl.Type(node.name, 'object')) ### else: self.set_in(node, pddl.Type(node.name, node.parent))
def make_domain(constants=[], predicates=[], functions=[], actions=[], axioms=[]): types = [pddl.Type(OBJECT)] pddl_parser.parsing_functions.set_supertypes(types) return Domain(name='', requirements=pddl.Requirements([]), types=types, type_dict={ty.name: ty for ty in types}, constants=constants, predicates=predicates, predicate_dict={p.name: p for p in predicates}, functions=functions, actions=actions, axioms=axioms, pddl=None)
def parse_temporal_domain(domain_pddl): translate_path = os.path.join(get_tfd_path(), 'translate/') # tfd & temporal-FD prefixes = ['pddl', 'normalize'] deleted = delete_imports(prefixes) sys.path.insert(0, translate_path) import pddl import normalize temporal_domain = TemporalDomain(*pddl.tasks.parse_domain(pddl.parser.parse_nested_list(domain_pddl.splitlines()))) name, requirements, constants, predicates, types, functions, actions, durative_actions, axioms = temporal_domain fluents = normalize.get_fluent_predicates(temporal_domain) sys.path.remove(translate_path) delete_imports(prefixes) sys.modules.update(deleted) # This is important otherwise classes are messed up import pddl import pddl_parser assert not actions simple_from_durative = simple_from_durative_action(durative_actions, fluents) simple_actions = [action for triplet in simple_from_durative.values() for action in triplet] requirements = pddl.Requirements([]) types = [pddl.Type(ty.name, ty.basetype_name) for ty in types] pddl_parser.parsing_functions.set_supertypes(types) predicates = [pddl.Predicate(p.name, p.arguments) for p in predicates] constants = convert_parameters(constants) axioms = list(map(convert_axiom, axioms)) return SimplifiedDomain(name, requirements, types, {ty.name: ty for ty in types}, constants, predicates, {p.name: p for p in predicates}, functions, simple_actions, axioms, simple_from_durative, domain_pddl)
def __init__(self): self._types = dict() self._predicates = dict() self._nodeHash = dict() self._requirements = set() self._actions = dict() self.domain = None self._objectType = pddl.Type('object', None) self._constants = dict()
def parse_domain_custom(checker): # parse domain file checker.parse_code(checker.files['domain'], checker.parse_token_domain) yield checker.names['domain'] if checker.requirements: yield pddl.Requirements(checker.requirements) else: yield pddl.Requirements([":strips"]) types = [pddl.Type("object")] set_supertypes(types) type_dict = dict((type_.name, type_) for type_ in types) yield types yield type_dict yield [] predicates = [[pred] + checker.predicates[pred]['params'] for pred in checker.predicates] predicates = [parse_predicate(entry) for entry in predicates] predicates += [ pddl.Predicate("=", [ pddl.TypedObject("?x", "object"), pddl.TypedObject("?y", "object") ]) ] predicate_dict = dict((pred.name, pred) for pred in predicates) yield predicates yield predicate_dict yield [] actions = [] entries = [[ ':action', action, ':parameters', checker.actions[action]['params'], ':precondition', checker.actions[action]['logic_precs'], ':effect', checker.actions[action]['logic_effs'] ] for action in checker.actions] for entry in entries: action = parse_action(entry, type_dict, predicate_dict) if action is not None: actions.append(action) yield actions yield []
def parse_domain_pddl(domain_pddl): iterator = iter(domain_pddl) define_tag = next(iterator) assert define_tag == "define" domain_line = next(iterator) assert domain_line[0] == "domain" and len(domain_line) == 2 yield domain_line[1] ## We allow an arbitrary order of the requirement, types, constants, ## predicates and functions specification. The PDDL BNF is more strict on ## this, so we print a warning if it is violated. requirements = pddl.Requirements([":strips"]) the_types = [pddl.Type("object")] constants, the_predicates, the_functions = [], [], [] correct_order = [ ":requirements", ":types", ":constants", ":predicates", ":functions" ] seen_fields = [] for opt in iterator: field = opt[0] if field not in correct_order: first_action = opt break if field in seen_fields: raise SystemExit("Error in domain specification\n" + "Reason: two '%s' specifications." % field) if (seen_fields and correct_order.index(seen_fields[-1]) > correct_order.index(field)): msg = "\nWarning: %s specification not allowed here (cf. PDDL BNF)" % field print(msg, file=sys.stderr) seen_fields.append(field) if field == ":requirements": requirements = pddl.Requirements(opt[1:]) elif field == ":types": the_types.extend(parse_typed_list(opt[1:], constructor=pddl.Type)) elif field == ":constants": constants = parse_typed_list(opt[1:]) elif field == ":predicates": the_predicates = [parse_predicate(entry) for entry in opt[1:]] the_predicates += [ pddl.Predicate("=", [ pddl.TypedObject("?x", "object"), pddl.TypedObject("?y", "object") ]) ] elif field == ":functions": the_functions = parse_typed_list(opt[1:], constructor=parse_function, default_type="number") set_supertypes(the_types) yield requirements yield the_types type_dict = dict((type.name, type) for type in the_types) yield type_dict yield constants yield the_predicates predicate_dict = dict((pred.name, pred) for pred in the_predicates) yield predicate_dict yield the_functions entries = [first_action] + [entry for entry in iterator] the_axioms = [] the_actions = [] for entry in entries: if entry[0] == ":derived": axiom = parse_axiom(entry, type_dict, predicate_dict) the_axioms.append(axiom) else: action = parse_action(entry, type_dict, predicate_dict) if action is not None: the_actions.append(action) yield the_actions yield the_axioms
def parse_domain_pddl(domain_pddl): def typesplit( alist): # recurse nested lists and replace "-type" by "-", "type" # an error which occurs in some sloppyly modeled domains ix = 0 while ix < len(alist): el = alist[ix] # print("checking element %s"%el) if isinstance(el, list): typesplit(alist[ix]) elif len(el) > 1 and el[0] == "-": msg = ( "\nWARNING: %s seems to be a 'type' definition missing a space.\n" "Splitting Element into '-' and '%s'") % (el, el[1:]) print(msg, file=sys.stderr) alist[ix:ix + 1] = el[0], el[1:] ix += 1 iterator = iter(domain_pddl) define_tag = next(iterator) assert define_tag == "define" domain_line = next(iterator) assert domain_line[0] == "domain" and len(domain_line) == 2 yield domain_line[1] ## We allow an arbitrary order of the requirement, types, constants, ## predicates and functions specification. The PDDL BNF is more strict on ## this, so we print a warning if it is violated. requirements = pddl.Requirements([":strips"]) the_types = [pddl.Type("object")] constants, the_functions = [], [] the_predicates = [ pddl.Predicate("=", [ pddl.TypedObject("?x", "object"), pddl.TypedObject("?y", "object") ]) ] # the_free_functions = [] ## support for global constraints with free functions is not implemented yet correct_order = [ ":requirements", ":types", ":constants", ":predicates", ":functions" ] #, ":free_functions"] seen_fields = [] first_action = None for opt in iterator: # print("Options before: ",opt) typesplit( opt) # fix for missing space between dash '-' and type identifier # print("Options after: ",opt) field = opt[0] if field not in correct_order: first_action = opt break if field in seen_fields: raise SystemExit("Error in domain specification\n" + "Reason: two '%s' specifications." % field) if (seen_fields and correct_order.index(seen_fields[-1]) > correct_order.index(field)): msg = "\nWARNING: %s specification not allowed here (cf. PDDL BNF)" % field print(msg, file=sys.stderr) seen_fields.append(field) if field == ":requirements": requirements = pddl.Requirements(opt[1:]) elif field == ":types": the_types.extend(parse_typed_list(opt[1:], constructor=pddl.Type)) elif field == ":constants": constants = parse_typed_list(opt[1:]) elif field == ":predicates": the_predicates += [parse_predicate(entry) for entry in opt[1:]] elif field == ":functions": the_functions = parse_typed_list(opt[1:], constructor=parse_function, default_type="number") # elif field == ":free_functions": # the_free_functions = parse_typed_list( # opt[1:], # constructor=parse_function, # default_type="number") set_supertypes(the_types) yield requirements yield the_types type_dict = dict((pddltype.name, pddltype) for pddltype in the_types) yield type_dict yield constants yield the_predicates predicate_dict = dict((pred.name, pred) for pred in the_predicates) yield predicate_dict total_cost_fluent = pddl.Function("total-cost", [], "number") the_functions.append(total_cost_fluent) # the_functions.append(the_free_functions) yield the_functions entries = [] if first_action is not None: entries.append(first_action) entries.extend(iterator) the_axioms = [] the_actions = [] for entry in entries: # if DEBUG: print("Entries before: ",entry) typesplit( entry ) # fix for missing space between dash '-' and type identifier # if DEBUG: print("Entries after: ",entry) if entry[0] == ":derived": axiom = parse_axiom(entry, type_dict, predicate_dict) the_axioms.append(axiom) elif entry[0] == ":action": action = parse_action(entry, type_dict, predicate_dict) if action is not None: the_actions.append(action) elif entry[ 0] == ":constraint": ## support for global constraints is new in NFD global_constraint = parse_global_constraint( entry, type_dict, predicate_dict) the_axioms.append(global_constraint) else: print("%s could not be parsed" % entry[0]) if entry[0] != ":free_functions": assert False yield the_actions yield the_axioms