-
Notifications
You must be signed in to change notification settings - Fork 0
/
start_experiment.py
282 lines (233 loc) · 6.97 KB
/
start_experiment.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#!/usr/bin/env python
__author__ = 'kemele'
import getopt
import string
import sys, os, signal
from multiprocessing import Process, Queue, active_children, Manager
from multiprocessing.queues import Empty
from time import time
import logging
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
from mulder.access_control import User, AccessPolicy, Operation
from mulder.access_control.AccessControl import AccessControl
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.FileHandler('.decompositions.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
def runQuery(queryfile, configfile, user, isEndpoint, res, qplan, adaptive, withoutCounts, printResults):
'''if isEndpoint:
contact = contactSource
else:
contact = contactWrapper
'''
query = open(queryfile).read()
pos = queryfile.rfind("/")
qu = queryfile[pos+1:]
pos2 = qu.rfind(".")
tempType = 'MULDER'
if pos2 > -1:
qu = qu[:pos2]
global qname
global t1
global tn
global c1
global cn
global dt
global pt
c1 = 0
cn = 0
t1 = -1
tn = -1
dt = -1
global time1
qname = qu
endpointType = 'V'
logger.info("Query: " + qname)
config = ConfigFile(configfile)
if user is None:
user = User("P5", url='http://www.example.org/access-control-ontology#auth_partner_094451')
else:
user = User("P5", url=user)
server = 'http://localhost:9999/validate/retrieve'
accesscontrol = AccessControl(server)
time1 = time()
joinstarslocally = False
mdq = MediatorDecomposer(query, config, user, accesscontrol, tempType, joinstarslocally)
new_query = mdq.decompose()
dt = time() - time1
if (new_query == None): # if the query could not be answered by the endpoints
time2 = time() - time1
t1 = time2
tn = time2
pt = time2
printInfo()
return
planner = MediatorPlanner(new_query, True, contactSource, None, config)
plan = planner.createPlan()
logger.info("Plan:")
logger.info(plan)
pt = time() - time1
processqueue = Queue()
p2 = Process(target=plan.execute, args=(res,processqueue, ))
p2.start()
p3 = Process(target=conclude, args=(res, p2, printResults))
p3.start()
signal.signal(12, onSignal1)
while True:
if not p3.is_alive():
if p2.is_alive():
try:
os.kill(p2.pid, 9)
except OSError as ex:
# print("Exception while terminating execution process", ex)
continue
else:
break
# print('Number of sub-processes to terminate: ', processqueue.qsize())
# while True:
# try:
# p = processqueue.get(False)
# if check_pid(p):
# try:
# os.kill(p, 9)
# # print("Process ", p, ' has been terminated')
# except OSError as err:
# print("ERROR: Process ", p, ' cannot be terminated. Trying again ..', err)
# # processqueue.put(p)
# continue
#
# if check_pid(p):
# processqueue.put(p)
#
# except Empty:
# break
def check_pid(pid):
""" Check For the existence of a unix pid. """
try:
os.kill(pid, 0)
except OSError:
return False
else:
return True
def conclude(res, p2, printResults):
signal.signal(12, onSignal2)
global t1
global tn
global c1
global cn
ri = res.get()
if (printResults):
if (ri == "EOF"):
time2 = time() - time1
t1 = time2
tn = time2
print ("Empty set.")
printInfo(p2)
return
while (ri != "EOF"):
cn = cn + 1
if cn == 1:
time2=time() - time1
t1 = time2
c1 = 1
print (ri)
ri = res.get(True)
printInfo(p2)
else:
if (ri == "EOF"):
time2 = time() - time1
t1 = time2
tn = time2
printInfo(p2)
return
while (ri != "EOF"):
cn = cn + 1
if cn == 1:
time2 = time() - time1
t1 = time2
c1 = 1
#print cn, ri
ri = res.get(True)
printInfo(p2)
def printInfo(p2=None):
global tn
if tn == -1:
tn = time() - time1
l = (qname + "\t" + str(dt) + "\t" + str(pt) + "\t" + str(t1) + "\t" + str(tn) + "\t" + str(c1) + "\t" + str(cn))
print(l)
logger.info(l)
if p2 is not None:
p2.terminate()
def onSignal1(s, stackframe):
printInfo()
cs = active_children()
for c in cs:
try:
os.kill(c.pid, s)
except OSError as ex:
try:
os.kill(c.pid, s)
except:
pass
continue
sys.exit(s)
def onSignal2(s, stackframe):
printInfo()
sys.exit(s)
def get_options(argv):
try:
opts, args = getopt.getopt(argv, "h:c:q:u:p:s:")
except getopt.GetoptError:
usage()
sys.exit(1)
configfile = None
queryfile = None
#buffersize = 1638400
tempType = "MULDER"
user = None
isEndpoint = True
plan = "b"
adaptive = True
withoutCounts = False
printResults = False
for opt, arg in opts:
if opt == "-h":
usage()
sys.exit()
elif opt == "-c":
configfile = arg
elif opt == "-q":
queryfile = arg
elif opt == "-u":
user = arg
elif opt == "-s":
isEndpoint = arg == "True"
elif opt == '-p':
printResults = eval(arg)
if not configfile or not queryfile:
usage()
sys.exit(1)
return (configfile, queryfile, user, isEndpoint, plan, adaptive, withoutCounts, printResults)
def usage():
usage_str = ("Usage: {program} -c <config.json_file> -q <query_file> -u "
+"<user-url> " # -o <sparql1.1> -d " +"<decomposition> -k <special>
+"\n where \n"
"<user-url> user url"
+"\n")
print(usage_str.format(program = sys.argv[0]))
def main(argv):
res = Queue()
time1 = time()
(configfile, queryfile, user, isEndpoint, plan, adaptive, withoutCounts, printResults) = get_options(argv[1:])
try:
runQuery(queryfile, configfile, user, isEndpoint, res, plan, adaptive, withoutCounts, printResults)
except Exception as ex:
print (ex)
if __name__ == '__main__':
main(sys.argv)