-
Notifications
You must be signed in to change notification settings - Fork 23
/
PDDL.py
296 lines (268 loc) · 11.4 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python
# Four spaces as indentation [no tabs]
# This file is part of PDDL Parser, available at <https://github.com/pucrs-automated-planning/pddl-parser>.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import re
from .action import Action
class PDDL_Parser:
SUPPORTED_REQUIREMENTS = [':strips', ':negative-preconditions', ':typing']
# -----------------------------------------------
# Tokens
# -----------------------------------------------
def scan_tokens(self, filename):
with open(filename) 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:
li = list
list = stack.pop()
list.append(li)
else:
raise Exception('Missing open parentheses')
else:
list.append(t)
if stack:
raise Exception('Missing close parentheses')
if len(list) != 1:
raise Exception('Malformed expression')
return list[0]
# -----------------------------------------------
# Parse domain
# -----------------------------------------------
def parse_domain(self, domain_filename, requirements=SUPPORTED_REQUIREMENTS):
tokens = self.scan_tokens(domain_filename)
if type(tokens) is list and tokens.pop(0) == 'define':
self.domain_name = None
self.requirements = []
self.types = {}
self.objects = {}
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 req not in requirements:
raise Exception('Requirement ' + req + ' not supported')
self.requirements = group
elif t == ':constants':
self.parse_objects(group, t)
elif t == ':predicates':
self.parse_predicates(group)
elif t == ':types':
self.parse_types(group)
elif t == ':action':
self.parse_action(group)
else: self.parse_domain_extended(t, group)
else:
raise Exception('File ' + domain_filename + ' does not match domain pattern')
def parse_domain_extended(self, t, group):
print(str(t) + ' is not recognized in domain')
# -----------------------------------------------
# Parse hierarchy
# -----------------------------------------------
def parse_hierarchy(self, group, structure, name, redefine):
list = []
while group:
if redefine and group[0] in structure:
raise Exception('Redefined supertype of ' + group[0])
elif group[0] == '-':
if not list:
raise Exception('Unexpected hyphen in ' + name)
group.pop(0)
type = group.pop(0)
if type not in structure:
structure[type] = []
structure[type] += list
list = []
else:
list.append(group.pop(0))
if list:
if 'object' not in structure:
structure['object'] = []
structure['object'] += list
# -----------------------------------------------
# Parse objects
# -----------------------------------------------
def parse_objects(self, group, name):
self.parse_hierarchy(group, self.objects, name, False)
# -----------------------------------------------
# Parse types
# -----------------------------------------------
def parse_types(self, group):
self.parse_hierarchy(group, self.types, 'types', True)
# -----------------------------------------------
# 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 type(name) is not 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 = []
extensions = []
while group:
t = group.pop(0)
if t == ':parameters':
if type(group) is not 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:
group.insert(0, t)
extensions.append(group)
action = Action(name, parameters, positive_preconditions, negative_preconditions, add_effects, del_effects)
self.parse_action_extended(action, extensions)
self.actions.append(action)
def parse_action_extended(self, action, group):
while group:
t = group.pop(0)
print(str(t) + ' is not recognized in action ' + action.name)
# -----------------------------------------------
# Parse problem
# -----------------------------------------------
def parse_problem(self, problem_filename):
def frozenset_of_tuples(data):
return frozenset([tuple(t) for t in data])
tokens = self.scan_tokens(problem_filename)
if type(tokens) is list and tokens.pop(0) == 'define':
self.problem_name = None
self.state = frozenset()
self.positive_goals = frozenset()
self.negative_goals = frozenset()
while tokens:
group = tokens.pop(0)
t = group.pop(0)
if t == 'problem':
self.problem_name = group[0]
elif t == ':domain':
if self.domain_name != group[0]:
raise Exception('Different domain specified in problem file')
elif t == ':requirements':
pass # Ignore requirements in problem, parse them in the domain
elif t == ':objects':
self.parse_objects(group, t)
elif t == ':init':
self.state = frozenset_of_tuples(group)
elif t == ':goal':
positive_goals = []
negative_goals = []
self.split_predicates(group[0], positive_goals, negative_goals, '', 'goals')
self.positive_goals = frozenset_of_tuples(positive_goals)
self.negative_goals = frozenset_of_tuples(negative_goals)
else: self.parse_problem_extended(t, group)
else:
raise Exception('File ' + problem_filename + ' does not match problem pattern')
def parse_problem_extended(self, t, group):
print(str(t) + ' is not recognized in problem')
# -----------------------------------------------
# Split predicates
# -----------------------------------------------
def split_predicates(self, group, positive, negative, name, part):
if type(group) is not list:
raise Exception('Error with ' + name + part)
if group:
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)
negative.append(predicate[-1])
else:
positive.append(predicate)
# -----------------------------------------------
# Main
# -----------------------------------------------
if __name__ == '__main__':
import sys, pprint
domain = sys.argv[1]
problem = sys.argv[2]
parser = PDDL_Parser()
print('----------------------------')
pprint.pprint(parser.scan_tokens(domain))
print('----------------------------')
pprint.pprint(parser.scan_tokens(problem))
print('----------------------------')
parser.parse_domain(domain)
parser.parse_problem(problem)
print('Domain name: ' + str(parser.domain_name))
for act in parser.actions:
print(act)
print('----------------------------')
print('Problem name: ' + str(parser.problem_name))
print('Objects: ' + str(parser.objects))
print('State: ' + str([list(i) for i in parser.state]))
print('Positive goals: ' + str([list(i) for i in parser.positive_goals]))
print('Negative goals: ' + str([list(i) for i in parser.negative_goals]))