-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEndpointService.py
149 lines (123 loc) · 4.46 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
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
#!/usr/bin/env python
from flask import Flask, request, session
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
import logging
logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s")
logger = logging.getLogger()
if not logger.handlers:
logger.setLevel(logging.INFO)
fileHandler = logging.FileHandler("{0}.log".format('ontario'))
fileHandler.setLevel(logging.INFO)
fileHandler.setFormatter(logFormatter)
logger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.INFO)
consoleHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
__author__ = 'kemele'
app = Flask(__name__)
configuration = None
tempType = "MULDER"
configfile = '/home/kemele/git/MULDER/config/config.json'
@app.route("/sparql", methods=['POST', 'GET'])
def sparql():
if request.method == 'GET' or request.method == 'POST':
try:
query = request.args.get("query", '')
# query = query.replace('\n', ' ').replace('\r', ' ')
print('query:', query)
logger.info(query)
global configuration
# if session['configuration'] is None:
# configuration = ConfigFile(configfile)
# else:
# configuration = session.get('configuration')
if configuration is None:
configuration = ConfigFile(configfile)
if query is None or len(query) == 0:
return jsonify({"result": [], "error": "cannot read query"})
start = time()
dc = MediatorDecomposer(query, configuration, tempType)
quers = dc.decompose()
print ("Mediator Decomposer: \n", quers)
logger.info(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)
logger.info(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 = [k for k in 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": str(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)
configfile = '/data/config.json'
return configfile
if __name__ == "__main__":
mapping = ""
tempType = "MULDER"
argv = sys.argv
configfile = get_options(argv[1:])
try:
conf = ConfigFile(configfile)
except:
conf = ConfigFile("/home/kemele/git/MULDER/config/config.json")
print("The default DBpedia template is loaded")
port = 5000
# config = json.load(open(configfile))
configuration = conf
app.run(port=port, host="0.0.0.0")