-
Notifications
You must be signed in to change notification settings - Fork 7
/
process_solution.py
91 lines (69 loc) · 2.89 KB
/
process_solution.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
from __future__ import print_function
import sys, json, os
from utils.parser import Problem
def getActionDetails(task, plan, output):
actions = []
act_map = {}
for a in task.actions:
act_map[a.name] = a
for act_line in plan:
while ' )' == act_line[-2:]:
act_line = act_line[:-2] + ')'
a_name = act_line[1:-1].split(' ')[0]
if len(act_line.split(' ')) > 1:
a_params = act_line[1:-1].split(' ')[1:]
else:
a_params = False
a = act_map[a_name]
actions.append({'name': act_line, 'action': a.export(grounding=a_params)})
return json.dumps({'parse_status': 'ok',
'type': 'full',
'length': len(plan),
'plan': actions,
'output': output}, indent=4)
def getSimplePlan(task, plan, err_msg, output):
return json.dumps({'parse_status': 'ok',
'type': 'simple',
'length': len(plan),
'plan': plan,
'error': err_msg,
'output': output}, indent=4)
def doit(domain, problem, solution, outfile):
solver_output = None
try:
file = open(outfile, 'r')
solver_output = file.read()
file.close()
except Exception as e:
return json.dumps({'parse_status': 'err', 'output': solver_output,
'error': "Failed to read solver output -- %s" % str(e)})
try:
task = Problem(domain, problem)
except Exception as e:
return json.dumps({'parse_status': 'err', 'output': solver_output,
'error': "Failed to parse the problem -- %s\n\n%s" % (str(e), solver_output)})
try:
if not os.path.isfile(solution):
return json.dumps({'parse_status': 'err', 'output': solver_output,
'error': "Solver failed.\n\n%s" % solver_output})
file = open(solution, 'r')
plan = list(map(lambda x: x.strip().lower(), file.readlines()))
file.close()
if (len(plan) == 0) or (len(plan) == 1 and plan[0] == ''):
err_str = "Suspected timeout.\n\n%s" % solver_output
return json.dumps({'parse_status': 'err', 'output': solver_output, 'error': err_str})
if '' == plan[-1]:
plan = plan[:-1]
except Exception as e:
return json.dumps({'parse_status': 'err', 'output': solver_output,
'error': "Failed to parse plan -- %s\n\n%s" % (str(e), solver_output)})
try:
return getActionDetails(task, plan, solver_output)
except Exception as e:
return getSimplePlan(task, plan, str(e), solver_output)
if __name__ == '__main__':
domain = sys.argv[1]
problem = sys.argv[2]
solution = sys.argv[3]
solverout = sys.argv[4]
print(doit(domain, problem, solution, solverout))