-
Notifications
You must be signed in to change notification settings - Fork 0
/
EndpointService.py
113 lines (94 loc) · 3.18 KB
/
EndpointService.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
#!/usr/bin/env python
from flask import Flask, request
from flask.json import jsonify
import sys
import os
import json
import getopt
from time import time
from multiprocessing import Process, Queue, active_children
from mulder.molecule.MTManager import ConfigFile
from mulder.mediator.decomposition.MediatorDecomposer import MediatorDecomposer
from mulder.mediator.planner.MediatorPlanner import MediatorPlanner
from mulder.mediator.planner.MediatorPlanner import contactSource as clm
__author__ = 'kemele'
app = Flask(__name__)
configuration = None
tempType = "MULDER"
configfile = 'config/config.json'
@app.route("/sparql", methods=['POST', 'GET'])
def sparql():
if request.method == 'GET':
try:
query = request.args.get("query", '')
# query = query.replace('\n', ' ').replace('\r', ' ')
print('query:', query)
configuration = ConfigFile(configfile)
start = time()
dc = MediatorDecomposer(query, configuration, tempType)
quers = dc.decompose()
print ("Mediator Decomposer: \n", quers)
if quers is None:
print ("Query decomposer returns None")
return jsonify({"result": []})
res = []
planner = MediatorPlanner(quers, True, clm, None, configuration)
plan = planner.createPlan()
print ("Mediator Planner: \n", plan)
output = Queue()
plan.execute(output)
i = 0
first = 0
vars = []
while True:
r = output.get()
if i == 0:
first = time() - start
if r == "EOF":
print ("END of results ....")
break
vars = r.keys()
#print (r)
res.append(r)
i += 1
total = time() - start
return jsonify({"vars": vars, "result": res, "execTime": total, "firstResult": first, "totalRows": i})
except Exception as e:
print ("Exception: ", e)
print ({"result": [], "error": e})
return jsonify({"result": [], "error": e})
else:
return jsonify({"result": [], "error": "Invalid HTTP method used. Use GET "})
def usage():
usage_str = ("Usage: {program} -c <path_to_config> "
+ "\n where \n<path_to_config> "
+ " is configuration file for Ontario "
+ "\n")
print (usage_str.format(program=sys.argv[0]),)
def get_options(argv):
try:
opts, args = getopt.getopt(argv, "h:c:")
except getopt.GetoptError:
usage()
sys.exit(1)
configfile = None
for opt, arg in opts:
if opt == "-h":
usage()
sys.exit()
elif opt == "-c":
configfile = arg
if not configfile:
usage()
sys.exit(1)
return configfile
if __name__ == "__main__":
mapping = ""
tempType = "MULDER"
argv = sys.argv
configfile = get_options(argv[1:])
conf = ConfigFile(configfile)
port = 5000
# config = json.load(open(configfile))
configuration = conf
app.run(port=port, host="0.0.0.0")