-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
117 lines (99 loc) · 3.58 KB
/
app.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
from __future__ import print_function
from flask import Flask, render_template, request, jsonify, redirect
from planner import Planner
from interface import Interface
import json
import sys
app = Flask(__name__)
planner = Planner()
translator = Interface()
# Create a new planning problem
planner.definePlanningProblem()
''' Helper actions '''
def pddl_to_ui_actions(action_list = None):
if not action_list:
# Get the plan to be showed in the planning panel
action_list = planner.get_action_sequence_list()
return translator.actions_to_ui(action_list)
else:
return translator.actions_to_ui(action_list, True)
def ui_to_pddl_actions(request, is_get_request=False):
if is_get_request:
plan = json.loads( request.args.get('plan') )
else:
plan = json.loads( dict(request.form)['plan'][0] )
return translator.ui_to_actions(plan)
@app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
@app.route("/")
def index(was_plan_found=False):
p = pddl_to_ui_actions()
# Should we show the explanation button
can_ask_for_explanations = 0
if was_plan_found:
can_ask_for_explanations = 1
return render_template('index.html', canAskForExplanations=can_ask_for_explanations, plan=p, session=planner.session_id)
@app.route("/validate", methods=['GET', 'POST'])
def validate():
print('[DEBUG] Starting Validation Process ...')
planner.save_plan()
is_plan_valid = planner.get_validated_plan(ui_to_pddl_actions(request, is_get_request=True))
return jsonify({
'plan' : pddl_to_ui_actions(),
'status' : is_plan_valid
})
@app.route("/suggest", methods=['GET', 'POST'])
def suggest():
print('[DEBUG] Starting Suggestion Process ...')
planner.save_plan()
# plan_was_found = True if plan is found, False otherwise
was_plan_found = planner.get_suggested_plan(ui_to_pddl_actions(request, is_get_request=True))
if not was_plan_found:
planner.load_plan()
return jsonify({
'plan' : pddl_to_ui_actions(),
'status' : was_plan_found
})
@app.route("/check", methods=['GET', 'POST'])
def check():
print('[DEBUG] Starting the Checking Process ...')
planner.save_plan()
plan_complete_status = planner.is_plan_complete(ui_to_pddl_actions(request, is_get_request=True))
return jsonify({
'status' : plan_complete_status
})
@app.route("/getPlanExplanation", methods=['GET', 'POST'])
def get_explanations():
print('[DEBUG] Generating Plan Explanation(s) ...')
planner.save_plan()
explanations = planner.get_explanations(ui_to_pddl_actions(request, is_get_request=True))
return jsonify( pddl_to_ui_actions(explanations) )
@app.route("/logs/", methods=['POST'])
def log_activity():
data = request.get_data()
planner.write_user_activity(data)
return json.dumps({'status' : 'OK'})
@app.route("/reset", methods=['GET'])
def reset():
planner.definePlanningProblem()
return redirect("/", code=301)
def main(host='127.0.0.1', port=5000):
app.run(host=host,
port=port,
debug=True)
if __name__ == '__main__':
if len(sys.argv) == 2:
main(host=sys.argv[1])
elif len(sys.argv) == 3:
main(host = sys.argv[1], port = int(sys.argv[2]))
else:
main()