def parse_pddl_file(type, filename): try: return parser.parse_nested_list(file(filename)) except IOError as e: raise SystemExit("Error: Could not read file: %s\nReason: %s." % (e.filename, e)) except parser.ParseError as e: raise SystemExit("Error: Could not parse %s file: %s\n" % (type, filename))
def parse_pddl_file(type, filename): try: # The builtin open function is shadowed by this module's open function. return parser.parse_nested_list(builtins.open(filename)) except IOError as e: raise SystemExit("Error: Could not read file: %s\nReason: %s." % (e.filename, e)) except parser.ParseError as e: raise SystemExit("Error: Could not parse %s file: %s\n" % (type, filename))
def parse_pddl_file(type, filename): try: return parser.parse_nested_list(file(filename)) except IOError, e: raise SystemExit("Error: Could not read file: %s\nReason: %s." % (e.filename, e))
def schedule_timed_initials(predicates_, actions_, init_, goal_, timed_initials_): # find all predicates and fuctions and build value timeline predicate_values = {} # {predicate: [ti1=TimedInitial(), ti2, ...]} for ti in timed_initials_: if ti.predicate not in predicate_values: predicate_values[ti.predicate] = [ti.create_initial_value(init_)] predicate_values[ti.predicate].append(ti) # add init initial_state_predicate = predicates.Predicate('initial_state', []) predicates_.append(initial_state_predicate) init_.append(conditions.Atom('initial_state', [])) disable_initial_state = '\n (at start (not (initial_state)))' # merge timed initials occuring at the same time into one conjuction merged_initials = {} # {time: [ti1=TimedInitial, ti2, ...]} for ti in timed_initials_: if not ti.time in merged_initials: merged_initials[ti.time] = [ti] else: merged_initials[ti.time].append(ti) goal_list = [] ti_actions = [] counter = itertools.count() for tis in sorted(merged_initials.values(), key=lambda tis: tis[0].time): name = 'timed_initial_{}__'.format(next(counter)) # add predicates scheduled_predicate = predicates.Predicate(name+'-scheduled', []) predicates_.append(scheduled_predicate) # add goal goal_list.append(conditions.Atom(scheduled_predicate.name, [])) # add action facts = [] previous_facts = [] for ti in tis: facts.append(' (at end {})'.format(ti.to_effect())) value_timeline = predicate_values[ti.predicate] #print map(str, value_timeline) previous_value = max((ti2 for ti2 in value_timeline if ti2.time < ti.time), key=lambda ti2: ti2.time) #print previous_value.time, previous_value.to_effect() previous_facts.append(' (at end {})'.format(previous_value.to_effect())) action_string = ''' (:durative-action {name}scheduled :parameters () :duration (= ?duration {time}) :condition (and (at start (initial_state)) {previous_facts} ) :effect (and{disable_initial_state} (at end ({predicate})) {facts} ) )'''.format(name=name, time=ti.time, predicate=scheduled_predicate.name, disable_initial_state=disable_initial_state, facts='\n'.join(facts), previous_facts='\n'.join(previous_facts)) print(action_string) #assert 0 action = actions.DurativeAction.parse(parser.parse_nested_list([action_string])) ti_actions.append(action) disable_initial_state = '' # add goals goal_.parts += tuple(goal_list) #goal_.dump() # add negated precontition to all other actions [at_start, overall, at_end] = conditions.parse_durative_condition(parser.parse_nested_list(['(at start (not (initial_state)))'])) for da in actions_: da.condition[0].parts += (at_start, ) actions_.extend(ti_actions)