-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathParser.py
81 lines (72 loc) · 2.72 KB
/
Parser.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
from action import Action
from proposition import Proposition
class Parser(object):
"""
A utility class for parsing the domain and problem.
"""
def __init__(self, domainFile, problemFile):
"""
Constructor
"""
self.domainFile = domainFile
self.problemFile = problemFile
def parseActionsAndPropositions(self):
propositions = []
f = open(self.domainFile, 'r')
line = f.readline()
propositionLine = f.readline()
words = [word.rstrip() for word in propositionLine.split(" ") if len(word.rstrip()) > 0]
for i in range(0, len(words)):
propositions.append(Proposition(words[i]))
actions = []
f = open(self.domainFile, 'r')
line = f.readline()
while(line != ''):
words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
if(words[0] == 'Name:'):
name = words[1]
line = f.readline()
precond = []
add = []
delete = []
words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
for i in range(1, len(words)):
precond.append(Proposition(words[i]))
line = f.readline()
words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
for i in range(1, len(words)):
add.append(Proposition(words[i]))
line = f.readline()
words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
for i in range(1, len(words)):
delete.append(Proposition(words[i]))
act = Action(name,precond,add,delete)
for prop in add:
self.findPropByName(prop, propositions).addProducer(act)
actions.append(act)
line = f.readline()
for a in actions:
new_pre = [p for p in propositions if p.name in [q.name for q in a.pre]]
new_add = [p for p in propositions if p.name in [q.name for q in a.add]]
new_delete = [p for p in propositions if p.name in [q.name for q in a.delete]]
a.pre = new_pre
a.add = new_add
a.delete = new_delete
return [actions, propositions]
def findPropByName(self, name, propositions):
for prop in propositions:
if prop == name:
return prop
def pasreProblem(self):
init = []
goal = []
f = open(self.problemFile, 'r')
line = f.readline()
words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
for i in range(2, len(words)):
init.append(Proposition(words[i]))
line = f.readline()
words = [word.rstrip() for word in line.split(" ") if len(word.rstrip()) > 0]
for i in range(2, len(words)):
goal.append(Proposition(words[i]))
return [init, goal]