-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart_experiment.py
executable file
·295 lines (234 loc) · 7.09 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
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/env python3.5
__author__ = 'kemele'
import getopt
import string
#from ontario.mediator.planner import Plan
#from ontario.mediator.planner.Plan import contactSource, contactMetaWrapper
#from ontario.molecule.Config import Arango
#from ontario.molecule.MTManager import Arango
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
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, tempType, isEndpoint, res, qplan, adaptive, withoutCounts, printResults, planonly, joinlocally=True):
'''if isEndpoint:
contact = contactSource
else:
contact = contactWrapper
'''
query = open(queryfile).read()
pos = queryfile.rfind("/")
qu = queryfile[pos+1:]
pos2 = qu.rfind(".")
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)
time1 = time()
mdq = MediatorDecomposer(query, config, tempType, joinlocally)
new_query = mdq.decompose()
dt = time() - time1
if new_query is 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, adaptive, contactSource, endpointType, config)
plan = planner.createPlan()
logger.info("Plan:")
logger.info(plan)
pt = time() - time1
if planonly:
print(plan)
print("Decomposition and Planning Time: ", pt)
exit(0)
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":
nexttime(time1)
print ("Empty set.")
printInfo()
return
while ri != "EOF":
cn = cn + 1
if cn == 1:
time2 = time() - time1
t1 = time2
c1 = 1
print(ri)
ri = res.get(True)
nexttime(time1)
printInfo()
else:
if ri == "EOF":
nexttime(time1)
printInfo()
return
while ri != "EOF":
cn = cn + 1
if cn == 1:
time2 = time() - time1
t1 = time2
c1 = 1
ri = res.get(True)
nexttime(time1)
printInfo()
p2.terminate()
def nexttime(time1):
global tn
time2 = time() - time1
tn = time2
def printInfo():
global tn
global cn
global c1
global t1
global pt
global dt
if tn == -1:
tn = time() - time1
lr = (qname + "\t" + str(dt) + "\t" + str(pt) + "\t" + str(t1) + "\t" + str(tn) + "\t" + str(c1) + "\t" + str(cn))
print(lr)
logger.info(lr)
def onSignal1(s, stackframe):
cs = active_children()
for c in cs:
try:
os.kill(c.pid, s)
except OSError as ex:
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:t:s:r:j:p:")
except getopt.GetoptError:
usage()
sys.exit(1)
configfile = None
queryfile = None
#buffersize = 1638400
tempType = "MULDER"
isEndpoint = True
plan = "b"
adaptive = True
withoutCounts = False
printResults = False
planonly = False
joinlocally = True
for opt, arg in opts:
if opt == "-h":
usage()
sys.exit()
elif opt == "-c":
configfile = arg
elif opt == "-q":
queryfile = arg
elif opt == "-t":
tempType = arg
elif opt == "-s":
isEndpoint = arg == "True"
elif opt == '-r':
printResults = eval(arg)
elif opt == '-p':
planonly = eval(arg)
elif opt == '-j':
joinlocally = eval(arg)
if not configfile or not queryfile:
usage()
sys.exit(1)
return (configfile, queryfile, tempType, isEndpoint, plan, adaptive, withoutCounts, printResults, planonly, joinlocally)
def usage():
usage_str = ("Usage: {program} -c <config.json_file> -q <query_file> -t "
+"<templateType> -s <isEndpoint> -r <result_folder>"
+"\n where \n<isEndpoint> - a boolean value "
+"\n<result_folder> - an existing folder to store results.csv and traces.csv"
"\n")
print (usage_str.format(program=sys.argv[0]),)
def main(argv):
res = Queue()
time1 = time()
(configfile, queryfile, buffersize, isEndpoint, plan, adaptive, withoutCounts, printResults, planonly, joinlocally) = get_options(argv[1:])
try:
runQuery(queryfile, configfile, buffersize, isEndpoint, res, plan, adaptive, withoutCounts, printResults, planonly, joinlocally)
except Exception as ex:
print(ex)
if __name__ == '__main__':
main(sys.argv)