-
Notifications
You must be signed in to change notification settings - Fork 0
/
PDDL.py
257 lines (237 loc) · 10.3 KB
/
PDDL.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#!/usr/bin/env python
# Four spaces as indentation [no tabs]
import re
from collections import defaultdict
from action import Action
class PDDL_Parser:
SUPPORTED_REQUIREMENTS = [':strips', ':negative-preconditions', ':typing']
# ------------------------------------------
# Tokens
# ------------------------------------------
def scan_tokens(self, filename, is_solution=False):
with open(filename,'r') as f:
# Remove single line comments
str = re.sub(r';.*$', '', f.read(), flags=re.MULTILINE).lower()
# Tokenize
stack = []
list = []
for t in re.findall(r'[()]|[^\s()]+', str):
if t == '(':
stack.append(list)
list = []
elif t == ')':
if stack:
l = list
list = stack.pop()
list.append(l)
else:
raise Exception('Missing open parentheses')
else:
list.append(t)
if stack:
raise Exception('Missing close parentheses')
if is_solution:
return list
if len(list) != 1:
raise Exception('Malformed expression')
return list[0]
#-----------------------------------------------
# Parse domain
#-----------------------------------------------
def parse_domain(self, domain_filename):
tokens = self.scan_tokens(domain_filename)
if type(tokens) is list and tokens.pop(0) == 'define':
self.domain_name = 'unknown'
self.requirements = []
self.types = []
self.actions = []
self.predicates = {}
while tokens:
group = tokens.pop(0)
t = group.pop(0)
if t == 'domain':
self.domain_name = group[0]
elif t == ':requirements':
for req in group:
if not req in self.SUPPORTED_REQUIREMENTS:
raise Exception('Requirement ' + req + ' not supported')
self.requirements = group
elif t == ':predicates':
self.parse_predicates(group)
elif t == ':types':
self.types = group
elif t == ':action':
self.parse_action(group)
else: print(str(t) + ' is not recognized in domain')
else:
raise Exception('File ' + domain_filename + ' does not match domain pattern')
#-----------------------------------------------
# Parse predicates
#-----------------------------------------------
def parse_predicates(self, group):
for pred in group:
predicate_name = pred.pop(0)
if predicate_name in self.predicates:
raise Exception('Predicate ' + predicate_name + ' redefined')
arguments = {}
untyped_variables = []
while pred:
t = pred.pop(0)
if t == '-':
if not untyped_variables:
raise Exception('Unexpected hyphen in predicates')
type = pred.pop(0)
while untyped_variables:
arguments[untyped_variables.pop(0)] = type
else:
untyped_variables.append(t)
while untyped_variables:
arguments[untyped_variables.pop(0)] = 'object'
self.predicates[predicate_name] = arguments
#-----------------------------------------------
# Parse action
#-----------------------------------------------
def parse_action(self, group):
name = group.pop(0)
if not type(name) is str:
raise Exception('Action without name definition')
for act in self.actions:
if act.name == name:
raise Exception('Action ' + name + ' redefined')
parameters = []
positive_preconditions = []
negative_preconditions = []
add_effects = []
del_effects = []
while group:
t = group.pop(0)
if t == ':parameters':
if not type(group) is list:
raise Exception('Error with ' + name + ' parameters')
parameters = []
untyped_parameters = []
p = group.pop(0)
while p:
t = p.pop(0)
if t == '-':
if not untyped_parameters:
raise Exception('Unexpected hyphen in ' + name + ' parameters')
ptype = p.pop(0)
while untyped_parameters:
parameters.append([untyped_parameters.pop(0), ptype])
else:
untyped_parameters.append(t)
while untyped_parameters:
parameters.append([untyped_parameters.pop(0), 'object'])
elif t == ':precondition':
self.split_predicates(group.pop(0), positive_preconditions, negative_preconditions, name, ' preconditions')
elif t == ':effect':
self.split_predicates(group.pop(0), add_effects, del_effects, name, ' effects')
else: print(str(t) + ' is not recognized in action')
self.actions.append(Action(name, parameters, positive_preconditions, negative_preconditions, add_effects, del_effects))
#-----------------------------------------------
# Parse problem
#-----------------------------------------------
def parse_problem(self, problem_filename):
tokens = self.scan_tokens(problem_filename)
if type(tokens) is list and tokens.pop(0) == 'define':
self.problem_name = 'unknown'
self.objects = defaultdict(list)
self.state = []
self.positive_goals = []
self.negative_goals = []
while tokens:
group = tokens.pop(0)
t = group[0]
if t == 'problem':
self.problem_name = group[-1]
elif t == ':domain':
if self.domain_name != group[-1]:
raise Exception('Different domain specified in problem file')
elif t == ':requirements':
pass # Ignore requirements in problem, parse them in the domain
elif t == ':objects':
group.pop(0)
object_list = []
while group:
if group[0] == '-':
group.pop(0)
self.objects[group.pop(0)] += object_list
object_list = []
else:
object_list.append(group.pop(0))
if object_list:
if not 'object' in self.objects:
self.objects['object'] = []
self.objects['object'] += object_list
self.objects = dict(self.objects)
elif t == ':init':
group.pop(0)
self.state = group
elif t == ':goal':
self.split_predicates(group[1], self.positive_goals, self.negative_goals, '', 'goals')
else: print(str(t) + ' is not recognized in problem')
else:
raise Exception('File ' + problem_filename + ' does not match problem pattern')
#-----------------------------------------------
# Parse solution
#-----------------------------------------------
def parse_solution(self, solution_filename):
tokens = self.scan_tokens(solution_filename, is_solution=True)
for idx, grounded_action in enumerate(tokens):
for action in self.actions:
if grounded_action[0] == action.name:
variables = [var for var, _ in action.parameters]
assignment = tuple(grounded_action[1:])
positive_preconditions = action.replace(action.positive_preconditions, variables, assignment)
negative_preconditions = action.replace(action.negative_preconditions, variables, assignment)
add_effects = action.replace(action.add_effects, variables, assignment)
del_effects = action.replace(action.del_effects, variables, assignment)
tokens[idx] = Action(action.name, assignment, positive_preconditions, negative_preconditions, add_effects, del_effects)
self.solution = tokens
#-----------------------------------------------
# Split predicates
#-----------------------------------------------
def split_predicates(self, group, pos, neg, name, part):
if not type(group) is list:
raise Exception('Error with ' + name + part)
if group[0] == 'and':
group.pop(0)
else:
group = [group]
for predicate in group:
if predicate[0] == 'not':
if len(predicate) != 2:
raise Exception('Unexpected not in ' + name + part)
neg.append(predicate[-1])
else:
pos.append(predicate)
# ==========================================
# Main
# ==========================================
if __name__ == '__main__':
import sys, pprint
domain = sys.argv[1]
problem = sys.argv[2]
solution = sys.argv[3]
parser = PDDL_Parser()
#print('----------------------------')
#pprint.pprint(parser.scan_tokens(domain))
#print('----------------------------')
#pprint.pprint(parser.scan_tokens(problem))
#print('----------------------------')
#pprint.pprint(parser.scan_tokens(solution))
#print('----------------------------')
parser.parse_domain(domain)
parser.parse_problem(problem)
parser.parse_solution(solution)
#print('Domain name: ' + parser.domain_name)
#for act in parser.actions:
# print(act)
#print('----------------------------')
#print('Problem name: ' + parser.problem_name)
#print('Objects: ' + str(parser.objects))
#print('State: ' + str(parser.state))
#print('Positive goals: ' + str(parser.positive_goals))
#print('Negative goals: ' + str(parser.negative_goals))
#print('Solution ' + str(parser.solution))