def parse_domain(domain_pddl): iterator = iter(domain_pddl) the_functions = [] the_axioms = [] the_actions = [] the_durative_actions = [] assert iterator.next() == "define" domain_line = iterator.next() assert domain_line[0] == "domain" and len(domain_line) == 2 yield domain_line[1] opt_requirements = iterator.next() if opt_requirements[0] == ":requirements": yield Requirements(opt_requirements[1:]) opt_types = iterator.next() else: yield Requirements([":strips"]) opt_types = opt_requirements the_types = [pddl_types.Type("object")] if opt_types[0] == ":types": the_types.extend(pddl_types.parse_typed_list(opt_types[1:], constructor=pddl_types.Type)) opt_constants = iterator.next() else: opt_constants = opt_types if opt_constants[0] == ":constants": yield pddl_types.parse_typed_list(opt_constants[1:],types=the_types) pred = iterator.next() else: yield [] pred = opt_constants the_predicates = [] if pred[0] == ":predicates": the_predicates = ([predicates.Predicate.parse(entry) for entry in pred[1:]] + [predicates.Predicate("=", [pddl_types.TypedObject("?x", "object"), pddl_types.TypedObject("?y", "object")])]) else: the_predicates = [predicates.Predicate("=", [pddl_types.TypedObject("?x", "object"), pddl_types.TypedObject("?y", "object")])] parse_domain_structure(pred,the_functions,the_axioms,the_actions,the_durative_actions,the_types,the_predicates) for entry in iterator: parse_domain_structure(entry,the_functions,the_axioms,the_actions,the_durative_actions,the_types,the_predicates) pddl_types.set_supertypes(the_types) the_types = [type for type in the_types if type.supertype_names != [] or type.name == "object"] yield the_predicates yield the_types yield the_functions yield the_actions yield the_durative_actions yield the_axioms
def compile_objectfunctions_aux(self, used_variables, recurse_object_terms=True): # could be done by postorder visit typed_vars = [] conjunction_parts = [] new_args = [] for arg in self.args: if recurse_object_terms: typed, parts, new_term = arg.compile_objectfunctions_aux( used_variables) typed_vars += typed conjunction_parts += parts new_args.append(new_term) for counter in itertools.count(1): new_var_name = "?v" + str(counter) if new_var_name not in used_variables: used_variables.append(new_var_name) typed_vars.append( pddl_types.TypedObject( new_var_name, tasks.Task.FUNCTION_SYMBOLS[self.name])) new_var = Variable(new_var_name) break if recurse_object_terms: pred_name = function_predicate_name(self.name) new_args.append(new_var) atom = Atom(pred_name, new_args) conjunction_parts = [atom] + conjunction_parts else: conjunction_parts = [self] return (typed_vars, conjunction_parts, new_var)
def parse_domain_structure(entry, the_functions, the_axioms, the_actions, the_durative_actions, the_types, the_predicates): if entry[0] == ":derived": axiom = axioms.Axiom.parse(entry) the_axioms.append(axiom) elif entry[0] == ":durative-action": action = actions.DurativeAction.parse(entry) the_durative_actions.append(action) elif entry[0] == ":functions": the_functions.extend( pddl_types.parse_typed_list( entry[1:], constructor=functions.Function.parse_typed, functions=True, types=the_types)) for function in the_functions: Task.FUNCTION_SYMBOLS[function.name] = function.type if function.type != "number": the_predicates.append( predicates.Predicate( conditions.function_predicate_name(function.name), function.arguments + [pddl_types.TypedObject("?val", function.type)])) elif entry[0] == ":action": action = actions.Action.parse(entry) the_actions.append(action) else: assert False, "unknown entity"
def rename_variables(self, renamings): new_params = [] for param in self.parameters: new_var = renamings.get(conditions.Variable(param.name), conditions.Variable(param.name)) new_param = pddl_types.TypedObject(new_var.name, param.type) new_params.append(new_param) new_effects = [] for effect in self.effects: new_effect = effect.rename_variables(renamings) new_effects.append(new_effect) return self.__class__(self.name, new_params, self.type, self.modulecall, new_effects)
def instantiate(self, var_mapping, new_modules): new_params = [] for param in self.parameters: new_var = var_mapping.get(conditions.Variable(param.name), conditions.Variable(param.name)) new_param = pddl_types.TypedObject(new_var.name, param.type) new_params.append(new_param) new_effects = [] for effect in self.effects: new_effect = effect.rename_variables(var_mapping) new_effects.append(new_effect) # ahh need to inst the effects too! # -- need vars as args # also for effects mc = Module(self.name, new_params, self.type, self.modulecall, new_effects, self) if mc not in new_modules: new_modules.add(mc)
def parse_domain(domain_pddl): iterator = iter(domain_pddl) assert iterator.next() == "define" domain_line = iterator.next() assert domain_line[0] == "domain" and len(domain_line) == 2 yield domain_line[1] opt_requirements = iterator.next() if opt_requirements[0] == ":requirements": yield Requirements(opt_requirements[1:]) opt_types = iterator.next() else: yield Requirements([":strips"]) opt_types = opt_requirements the_types = [pddl_types.Type("object")] if opt_types[0] == ":types": the_types.extend(pddl_types.parse_typed_list(opt_types[1:], constructor=pddl_types.Type)) opt_constants = iterator.next() else: opt_constants = opt_types pddl_types.set_supertypes(the_types) # for type in the_types: # print repr(type), type.supertype_names yield the_types if opt_constants[0] == ":constants": yield pddl_types.parse_typed_list(opt_constants[1:]) pred = iterator.next() else: yield [] pred = opt_constants assert pred[0] == ":predicates" print pred yield ([predicates.Predicate.parse(entry) for entry in pred[1:]] + [predicates.Predicate("=", [pddl_types.TypedObject("?x", "object"), pddl_types.TypedObject("?y", "object")])]) opt_functions = iterator.next() #action costs enable restrictive version of fluents if opt_functions[0] == ":functions": the_functions = pddl_types.parse_typed_list(opt_functions[1:], constructor=functions.Function.parse_typed, functions=True) for function in the_functions: Task.FUNCTION_SYMBOLS[function.name] = function.type yield the_functions first_action = iterator.next() else: yield [] first_action = opt_functions entries = [first_action] + [entry for entry in iterator] the_axioms = [] the_actions = [] for entry in entries: if entry[0] == ":derived": axiom = axioms.Axiom.parse(entry) the_axioms.append(axiom) else: action = actions.Action.parse(entry) the_actions.append(action) yield the_actions yield the_axioms
def parse_domain(domain_pddl): iterator = iter(domain_pddl) assert iterator.next() == "define" domain_line = iterator.next() 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 = Requirements([":strips"]) the_types = [pddl_types.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 >> sys.stderr, msg seen_fields.append(field) if field == ":requirements": requirements = Requirements(opt[1:]) elif field == ":types": the_types.extend( pddl_types.parse_typed_list(opt[1:], constructor=pddl_types.Type)) elif field == ":constants": constants = pddl_types.parse_typed_list(opt[1:]) elif field == ":predicates": the_predicates = [ predicates.Predicate.parse(entry) for entry in opt[1:] ] the_predicates += [ predicates.Predicate("=", [ pddl_types.TypedObject("?x", "object"), pddl_types.TypedObject("?y", "object") ]) ] elif field == ":functions": the_functions = pddl_types.parse_typed_list( opt[1:], constructor=functions.Function.parse_typed, functions=True) for function in the_functions: Task.FUNCTION_SYMBOLS[function.name] = function.type pddl_types.set_supertypes(the_types) # for type in the_types: # print repr(type), type.supertype_names yield requirements yield the_types yield constants yield the_predicates yield the_functions entries = [first_action] + [entry for entry in iterator] the_axioms = [] the_actions = [] for entry in entries: if entry[0] == ":derived": axiom = axioms.Axiom.parse(entry) the_axioms.append(axiom) else: action = actions.Action.parse(entry) the_actions.append(action) yield the_actions yield the_axioms