def parse_domain(domain_strips_generator):# domain generator rename? iterator = iter(domain_strips_generator) if (iterator.next() != "define"): raise Error("STRIPS domain file does not contain define") domain_definition = iterator.next() #Check for errors in the domain file if (domain_definition[0] == "domain" and len(domain_definition) == 2): yield domain_definition[1] else: raise Error("STRIPS domain file is not structured properly") requirements = iterator.next() if requirements[0] == ":requirements": yield requirements[1] #contains :typing types = iterator.next() else: types = requirements the_types = [strip_types.Type("object")] if types[0] == ":types": the_types.extend(strip_types.parse_typed_list(types[1:], constructor=strip_types.Type)) constants = iterator.next() else: constants = types yield the_types if constants[0] == ":constants": yield strip_types.parse_typed_list(constants[1:]) pred = iterator.next() else: yield [] pred = constants if (pred[0] != ":predicates"): raise Error("STRIPS domain file does not contain predicates") yield ([predicates.Predicate.parse(entry) for entry in pred[1:]] + [predicates.Predicate("=", [strip_types.TypedObject("?x", "object"), strip_types.TypedObject("?y", "object")])]) #Equality predicate; thanks to Walid #Only actions left if it is a STRIPS file; read all the remaining entries in the generator remaining_entries = [action for action in iterator] the_actions = [] for act in remaining_entries: action = actions.Action.parse(act) the_actions.append(action) yield the_actions
def parse(action_list): iterator = iter(action_list) if (iterator.next() != ":action"): raise Error("STRIPS file does not contain actions") name = iterator.next() params = iterator.next() if params == ":parameters": parameters = strip_types.parse_typed_list(iterator.next()) precond = iterator.next() else: parameters = [] precond = params if precond == ":precondition": precondition = conditions.parse_condition(iterator.next()) effect_iter = iterator.next() else: precondition = conditions.Conjunction([]) effect_iter = precond assert effect_iter == ":effect" effect_list = iterator.next() eff = [] cost = effects.parse_effects(effect_list, eff) for rest in iterator: assert False, rest return Action(name, parameters, precondition, eff)
def parse_problem(problem_strips_generator): iterator = iter(problem_strips_generator) if (iterator.next() != "define"): raise Error("STRIPS problem file does not contain define") problem_definition = iterator.next() #Check for errors in the problem file if (problem_definition[0] == "problem" and len(problem_definition) == 2): yield problem_definition[1] else: raise Error("STRIPS problem file is not structured properly") domain_description = iterator.next() #Check for issues in the problem's domain description if (domain_description[0] == ":domain" and len(domain_description) == 2): yield domain_description[1] else: raise Error("STRIPS problem file does not contain a domain description") objects = iterator.next() """if requirements[0] == ":requirements": yield requirements[1] #contains :typing objects = iterator.next() else: objects = requirements """ if objects[0] == ":objects": yield strip_types.parse_typed_list(objects[1:]) init = iterator.next() else: yield [] init = objects if (init[0] != ":init"): raise Error("STRIPS problem contains no init state") initial = [] for fact in init[1:]: initial.append(conditions.Atom(fact[0], fact[1:])) yield initial goal = iterator.next() if (goal[0] == ":goal" and len(goal) == 2): yield conditions.parse_condition(goal[1]) else: raise Error("STRIPS problem contains no goal state")
def parse(pred_list): name = pred_list[0] args = strip_types.parse_typed_list(pred_list[1:]) return Predicate(name, args)