-
Notifications
You must be signed in to change notification settings - Fork 0
/
probabilisticDatabase.py
772 lines (690 loc) · 21.6 KB
/
probabilisticDatabase.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
probabilities = {}
import sys
import copy
from itertools import combinations
import getopt
import matplotlib.pyplot as plt
import random
import numpy as np
import re
import math
from scipy.stats import multivariate_normal
sd=1
mean=0
root2pi=math.sqrt(2*math.pi)
dr=2*(sd**2)
safeQuery = 0
def decision(prob):
if(random.random()<float(prob)):
return True
else:
return False
def generateconcDatabases(database):
concDatabases={}
for key in database:
list=database[key]
#print(list)
concDatabase=[]
for element in list:
k=next(iter(element))
val=element[k]
flip=decision(val)
if flip:
concDatabase.append(k)
concDatabases[key]=concDatabase
return concDatabases
def exponentialDistribution(x):
if x<0:
return 0
else:
return math.exp(-x)
def gaussianDistribution(x):
nr=-(x-mean)**2
return (1/(sd*root2pi))*math.exp(nr/dr)
def metropolisHastings(copyDatabase):
for key in copyDatabase:
list=copyDatabase[key]
new_list=[]
for element in list:
k=next(iter(element))
currentState=element[k]
#proposedState=abs(currentState+np.random.normal(currentState,1,size=1)[0])%1
proposedState=np.clip(currentState+np.random.normal(0,1,size=1)[0],0,1)
acceptanceProbability=min(1,exponentialDistribution(proposedState)/exponentialDistribution(currentState))
randomNumber=np.random.uniform(0,1)
toAdd={}
if randomNumber<=acceptanceProbability:
toAdd[k]=proposedState
else:
toAdd[k]=currentState
new_list.append(toAdd)
copyDatabase[key]=new_list
return generateconcDatabases(copyDatabase)
def getProposedState(list,k):
for element in list:
prob=1
key=next(iter(element))
val=element[key]
if key==k:
prob=float(val)
break
if prob!=1:
return prob/(1-prob)
else:
return 0
def gibbsSampling(copyDatabase):
for key in copyDatabase:
list=copyDatabase[key]
new_list=[]
for element in list:
k=next(iter(element))
currentState=element[k]
proposedState=getProposedState(list,k)
proposedState=gaussianDistribution(proposedState)
toAdd={}
toAdd[k]=proposedState
new_list.append(toAdd)
copyDatabase[key]=new_list
return generateconcDatabases(copyDatabase)
def findTuples(concDatabase, table):
tuples = set()
entries = concDatabase[table]
for var in entries:
if len(var) == 1:
tuples.add(var)
else:
for i in var:
tuples.add(i)
#print("tuples are: ", tuples)
return tuples
#Generate all possible assignments for variables
def getAssignments(variables, index, domains, assignment, allAssignments):
if (index == len(variables)):
tempAssignment = copy.deepcopy(assignment)
allAssignments.append(tempAssignment)
else:
for i in range(0, len(domains[index])):
assignment.append(domains[index][i])
getAssignments(variables, index+1, domains, assignment, allAssignments)
del assignment[-1]
def processClause(clause, concDatabase):
literals = clause.split("^")
variables = set()
tables = []
domains = []
for l in literals:
l = l[:-1]
values = l.split("(")
vars = values[1].split(",")
for v in vars:
variables.add(v)
variables = list(variables)
for var in variables:
table = []
domain = set()
for l in literals:
if l.find(var) != -1:
table.append(l[0])
domain = domain.union(findTuples(concDatabase,l[0]))
tables.append(table)
domains.append(list(domain))
return variables, tables, domains
def evaluateQuery(inputQuery, concDatabase):
clauses = inputQuery.split("||")
#print("Clauses are: ", clauses)
for clause in clauses:
flag = False
variables, tables, domains = processClause(clause, concDatabase)
#Variables: all vars in the clause e.g. [x, y]
#tables: which var appears in which table, order same as 'variables' e.g. [[R, S], [S, T]]
#Domains: Domain of each variable e.g [[A,B,C],[B,C,D]]
allAssignments = []
assignment = []
getAssignments(variables, 0, domains,assignment, allAssignments)
#ClauseSplit: Variables in each Table e.g {'R': ['x'], 'S': ['x', 'y'], 'T': ['y']}
clauseSplit = {}
literals = clause.split("^")
for literal in literals:
literal = literal[:-1]
temp = literal.split("(")
key = temp[0]
values = temp[1].split(",")
clauseSplit[key] = values
#Evaluate query for each assignment
for assn in allAssignments:
#print("For Assignment ", assn)
flag = False
#Evaluate for each literal
for key, value in clauseSplit.items():
#Tuple will contain assignment for each var for comparison e.g ['A'] or ['A', 'B']
tuple = []
for i in value:
index = variables.index(i)
val = assn[index]
tuple.append(val)
found = False
#Check if that tuple is present in concDatabase
for tuples in concDatabase[key]:
if tuple == list(tuples):
found = True
break
#if tuple not present: literal evaluates to false, clause is false for that assignment, break and move to next assignment
if found == False:
#print("Could not find tuple in concDB")
flag = True
break
#No literal evaluated to false(No break) -> found an assignment for this clause -> makes entire query true
if flag == False:
#print("Found an assignment")
#print("Clause ", clause, " evaluated to true")
return True
return False
def callSampler(sampler, total_no_of_samples, database):
count = 0
total_prob = 0
total_error = 0
x_axis = []
y_axis = []
if safeQuery == 1:
expectedProb = prob
else:
expectedProb = 0
if sampler == 0:
print ("Monte Carlo Sampler")
for no_of_samples in range(1, total_no_of_samples + 1):
count = 0
for iterate in range(0,no_of_samples):
concDatabases=generateconcDatabases(database)
#print ('concDatabases: ', concDatabases)
result = evaluateQuery(inputQuery, concDatabases)
#print("Result of evaluation: ", result)
if result == True:
count = count + 1
probability = count/no_of_samples
#print("Probability of query is: ", probability)
total_prob = probability+ total_prob
error = abs(probability - expectedProb)
total_error = total_error + error
y_axis.append(error)
x_axis.append(no_of_samples)
elif sampler == 1:
print ("Metropolis-Hastings MCMC")
copyDatabase={}
copyDatabase=copy.deepcopy(database)
for no_of_samples in range(1, total_no_of_samples + 1):
count = 0
for iterate in range(0,no_of_samples):
concDatabases=generateconcDatabases(database)
#print("concDatabases: ", concDatabases)
result = evaluateQuery(inputQuery, concDatabases)
if result == True:
count = count + 1
probability = count/no_of_samples
total_prob = probability+ total_prob
error = abs(probability - expectedProb)
total_error = total_error + error
y_axis.append(error)
x_axis.append(no_of_samples)
elif sampler == 2:
print ("Gibbs MCMC")
copyDatabase={}
copyDatabase=copy.deepcopy(database)
for no_of_samples in range(1, total_no_of_samples + 1):
count = 0
for iterate in range(0,no_of_samples):
concDatabases=gibbsSampling(copyDatabase)
result = evaluateQuery(inputQuery, concDatabases)
if result == True:
count = count + 1
probability = count/no_of_samples
total_prob = probability+ total_prob
error = abs(probability - expectedProb)
total_error = total_error + error
y_axis.append(error)
x_axis.append(no_of_samples)
'''
if sampler == 0:
for no_of_samples in range(1, total_no_of_samples + 1):
concDatabases=generateconcDatabases(database)
#print("concDatabases: ", concDatabases)
result = evaluateQuery(inputQuery, concDatabases)
if result == True:
count = count + 1
probability = count/no_of_samples
error = abs(probability - expectedProb)
total_prob = probability+ total_prob
total_error = total_error + error
y_axis.append(error)
x_axis.append(no_of_samples)
elif sampler == 1:
print ("Metropolis-Hastings MCMC")
copyDatabase={}
copyDatabase=copy.deepcopy(database)
for no_of_samples in range(1, total_no_of_samples + 1):
concDatabases=metropolisHastings(copyDatabase)
result = evaluateQuery(inputQuery, concDatabases)
if result == True:
count = count + 1
probability = count/no_of_samples
error = abs(probability - expectedProb)
total_prob = probability+ total_prob
total_error = total_error + error
y_axis.append(error)
x_axis.append(no_of_samples)
elif sampler == 2:
print ("Gibbs MCMC")
copyDatabase={}
copyDatabase=copy.deepcopy(database)
for no_of_samples in range(1, total_no_of_samples + 1):
concDatabases=gibbsSampling(copyDatabase)
result = evaluateQuery(inputQuery, concDatabases)
if result == True:
count = count + 1
probability = count/no_of_samples
error = abs(probability - expectedProb)
total_prob = probability+ total_prob
total_error = total_error + error
y_axis.append(error)
x_axis.append(no_of_samples)
'''
avg_prob = total_prob/total_no_of_samples
avg_error = total_error/total_no_of_samples
print("Average probability is: ", avg_prob)
if safeQuery == 1:
print("Average error is: ", avg_error)
plt.plot(x_axis, y_axis)
plt.xlabel("Number of Samples")
plt.ylabel("Error Rate")
plt.title("Plot of Error Rate vs. Number of Samples")
plt.show()
def getInitialDatabase(tableFiles):
database={}
for tableFile in tableFiles:
table=[]
fh=open(tableFile,"r")
lineCount=1
for line in fh:
if lineCount==1:
lineCount=lineCount+1
mainKey=line.strip()
continue
else:
lineSplit=re.split(",",line)
val=lineSplit[-1].strip()
localDict={}
localList=[]
valueCount=0;
for value in lineSplit:
if value.strip()!=val:
localList.append(value)
if len(localList)>1:
localTuple=tuple(localList)
else:
localTuple=localList[0]
localDict[localTuple]=val
table.append(localDict)
database[mainKey]=table
fh.close()
return database
'''find's separator variable for entire UCQ'''
def find_Separator(UCQ):
for q in quantifier:
for cq in UCQ:
quant_count = 0
for clause in range(len(cq)):
if q in cq[clause][1]["var"]:
quant_count += 1
if quant_count == len(cq) and q.isdigit() == False : # if variable appears in all clauses, it is the separator
quantifier[q] = 0
return q
'''Substitute the seperator variable with constant values'''
def substitute(fp, UCQ, sep):
UCQ1 = copy.deepcopy(UCQ)
for cq in range(len(UCQ1)):
for t in range(len(UCQ1[cq])):
constant = True
temp = UCQ1[cq][t][1]["var"]
for i in range(0, len(temp)):
if (sep == temp[i]):
temp[i] = str(fp)
if (temp[i].isalpha()):
constant = False
if (constant == True):
UCQ1[cq][t][1]["const"] = True
UCQ[cq][t][1]["const"] = True
return UCQ1, UCQ
'''Check independence across entire UCQ, i.e. no repeating table names'''
def check_Independence_UCQ(UCQ):
temp = set()
for cq in range(len(UCQ)):
for k in UCQ[cq]:
# print("k",k)
if k[0] in temp:
return False
else:
temp.add(k[0])
return True
'''Function extracts the probability of particular tuple from the tables'''
def getProbability(UCQ):
given_table_name = UCQ[0][0][0]
constant_values = UCQ[0][0][1]["var"]
flag = True
for table_name, tuples in probabilities.items():
if (table_name == given_table_name):
for my_tuple in tuples:
flag = True
for i in range(len(constant_values)):
if int(constant_values[i]) != my_tuple[0][i]:
flag = False
if (flag == True):
return my_tuple[1]
return 0
'''Checks if all variables have been assigned with a constant number and are not characters anymore'''
def allConstantParameters(subUCQ):
for x in subUCQ[1]["var"]:
if (x.isdigit() == False):
return False
return True
'''Divides the UCQ into its two connected components'''
def split_into_connected_components(sub_UCQ):
ucnf = []
list_of_component_variables = []
for sub in sub_UCQ:
flag = False
for i in range(len(list_of_component_variables)):
for j in sub[1]["var"]:
if j in list_of_component_variables[i]:
if (j.isdigit() == False):
flag = True
ucnf[i][0].append(sub)
break
if (flag == False):
list_of_component_variables.append(set(sub[1]["var"]))
ucnf.append([[sub]])
return ucnf
'''checks the if there are more than two connected components'''
def greaterThanTwoConnectedComponents(q_ucnf):
if(len(q_ucnf)>1):
return True
return False
''' Converts a UCQ to a UCNF '''
def convert_to_ucnf(UCQ):
ucnf = []
if (len(UCQ) == 1):
ucnf = split_into_connected_components(UCQ[0])
return ucnf
q1_ucnf = split_into_connected_components(UCQ[0])
q2_ucnf = split_into_connected_components(UCQ[1])
if (greaterThanTwoConnectedComponents(q1_ucnf)):
q2_ucnf = [x[0] for x in q2_ucnf]
for tp in q1_ucnf:
toadd = convert_to_ucnf(q2_ucnf)[0]
for tadd in toadd:
newtp =[]
newtp.append(copy.deepcopy(tp[0]))
newtp.append(tadd)
ucnf.append(newtp)
return ucnf
elif (greaterThanTwoConnectedComponents(q2_ucnf)):
q1_ucnf = [x[0] for x in q1_ucnf]
for tp in q2_ucnf:
toadd = convert_to_ucnf(q1_ucnf)[0]
for tadd in toadd:
newtp =[]
newtp.append(copy.deepcopy(tp))
newtp.append(tadd[0])
ucnf.append(newtp)
return [ucnf]
else:
return [UCQ]
'''Checks if two cnf's in the UCNF have any common variables '''
def check_Independence_UCNF(ucnf):
cnf_set = set()
for cnf in ucnf:
for q in cnf[0]:
if(q[0] in cnf_set):
return False
else:
cnf_set.add(q[0])
return True
'''Changes variable names if there is no dependence'''
def change_vars_if_needed(UCQ):
cq1 = UCQ[0]
cq2 = UCQ[1]
table_name_1 = set()
table_name_2 = set()
for t in cq1:
table_name_1.add(t[0])
for t in cq2:
table_name_2.add(t[0])
common_table = ''
for tname in table_name_1:
if tname in table_name_2:
common_table = tname
common_table_vars = []
equiv_vars = []
for tables in cq1:
if tables[0] == common_table:
common_table_vars = tables[1]['var']
for tables in cq2:
if tables[0] == common_table:
equiv_vars = tables[1]['var']
equiv_vars_dict = {}
for eq_ind in range(len(equiv_vars)):
equiv_vars_dict[equiv_vars[eq_ind]] = eq_ind
for i in range(len(cq2)):
for j in range(len(cq2[i][1]['var'])):
if cq2[i][1]['var'][j] in equiv_vars_dict.keys():
cq2[i][1]['var'][j] = common_table_vars[equiv_vars_dict[cq2[i][1]['var'][j]]]
del UCQ[1]
UCQ.append(cq2)
return UCQ
''' Simplifies the UCNF'''
def cancellation(UCNF):
name = ''
for i in range(len(UCNF)):
if(UCNF[i][0][0][0] == UCNF[i][1][0][0]):
name = UCNF[i][0][0][0]
UCNF[i].pop()
todel = -1
i = 0
for i in range(len(UCNF)):
if( len(UCNF[i])==2 and (UCNF[i][0][0][0] ==name or UCNF[i][1][0][0] ==name)):
todel = i
del UCNF[todel]
todel = -1
i = 0
for i in range(len(UCNF)):
cnf = UCNF[i]
if( len(cnf)==2 and (cnf[0][0][0] ==name or cnf[1][0][0] ==name)):
todel = i
break
del UCNF[todel]
return UCNF
'''Applies the lifted inference algorithm that has also been shared in the repository to the UCQ'''
def probability(UCQ):
sep = ""
# Base of recursion
if (len(UCQ) == 1 and len(UCQ[0]) == 1 and allConstantParameters(UCQ[0][0])): # is a ground atom
if (UCQ[0][0][1]["negation"] == False):
return getProbability(UCQ) # checks if the given constant values are present in the given tables, if present
else:
return 1 - getProbability(UCQ)
if(len(UCQ)==2):
UCQ = change_vars_if_needed(UCQ)
# convert to ucnf
UCNF = convert_to_ucnf(UCQ)
if(len(UCNF)==4):
UCNF = cancellation(UCNF)
if ((len(UCNF) == 2 )and check_Independence_UCNF(UCNF) and type(UCNF[0]) is list): # both cq are independent of each other
ans = 1 - ((1 - probability(UCNF[0])) * (1 - probability(UCNF[1])))
return ans
#Inclusion Exclusion
if ( check_Independence_UCNF(UCNF)==False and type(UCNF[0]) is list):
incexc = True
for cnf in UCNF:
if (check_Independence_UCQ(cnf) == False):
incexc = False
if (incexc == True):
sign = -1
addition = 0
# Sums all the single terms since combiner function takes arguments for >=2 only.
for i in range(len(UCNF)):
temp_pr = probability(UCNF[i])
if(temp_pr!=-1):
addition = addition + temp_pr
# Adds up all the combinations of 2,3...n terms in UCNF
for i in range(2, len(UCNF) + 1):
# Combination is an inbuilt function accepting array and number of terms in combination
ans = list(combinations(UCNF, i))
final = []
for t in ans:
y = copy.deepcopy(t[0])
for i in range(1, len(t)):
for p in t[i]:
y.append(p)
final.append(y)
for term in final:
temp_pr = probability(term)
if(temp_pr!=-1):
addition = addition + (sign * temp_pr)
sign = sign * -1
return addition
if (len(UCQ) == 2 and check_Independence_UCQ(UCQ)):
Pr = 1.0
for val in UCQ:
Pr *= probability([val])
return Pr
sep = (find_Separator(UCQ))
if (sep is not None):
Pr = 1.0
for d in domain:
UCQ1, UCQ = substitute(d, UCQ, sep)
Pr *= probability(UCQ1)
return Pr
print("UNLIFTABLE", UCQ)
return -1
'''This function gets all the possible values a tuple entry can take. For example, if the elements that can exist in fruit basket 1 with a cerain probability are apple and orange and in fruit basket 2 are orange and banana the domain variable will contain (apple, orange, banana)'''
def get_domain(probabilities):
domain = set()
for table_name, tuples in probabilities.items():
for my_tuple in tuples:
for my_input in my_tuple[0]:
if (my_input not in domain):
domain.add(my_input)
return domain
''' This function parses the query and creates the neccessary datastructures accordingly. The datastructures are discussed in detail in the report'''
def parse_UCQ(input_query):
UCQ = []
quantifier = {}
UCQ_list = input_query.split("||") # split into individual conjunctions
tables = [] # Represent all tables in each CQ
for cq in UCQ_list: # iterate through the above list, cq is each conjunction
cq = cq.strip() # remove spaces
list_cq = cq.split(
"),") # to get list of relations. splitting by ), instead of , to ensure that it splits two relations and not withing a single relation
dict_cq = [] # dictionary representing each conjunctive clause
temp_tables = set()
for q in list_cq: # iterate through the relations within each conjunctive clause
q = q.strip() # remove spaces
q = q.split(
"(") # splitting by ( will ensure that the first index in the list is table name and second is variables
temp_dict = {}
temp_dict["var"] = q[1].strip("").replace(")", "").split(
",") # remove spaces, remove the ) at the endd, and split by comma to get a list of variables
temp_dict["negation"] = True
temp_dict["const"] = False
temp_tables.add(q[0])
for var in temp_dict["var"]: # set the quantifier value as existential for all variables
quantifier[var] = 1
temp_list = [q[0], temp_dict]
dict_cq.append(temp_list) # append relation dictionary to the conjunctive clause dictionary
UCQ.append(dict_cq) # append conjunctive clause dictionary to the list of union of conjunctive clauses
tables.append(temp_tables)
return UCQ, quantifier, tables
def read_query(fname):
f = open(fname, "r")
query = f.read().splitlines()
input_query="".join(query)
return input_query
def parse_tables(tablefiles):
probabilities = {}
for tablefile in tablefiles:
table_list = []
tbf = open(tablefile, "r")
tname = tbf.readline().strip()
templist = tbf.read().splitlines()
rows = []
for t in templist:
t2 = t.split(",")
t2 = [tsub2.strip() for tsub2 in t2]
prob = float(t2[len(t2)-1])
del t2[len(t2)-1]
var = [int(v) for v in t2]
row = [var,prob]
rows.append(row)
probabilities[tname] = rows
return probabilities
def get_input():
l = len(sys.argv)
queryFile = sys.argv[1]
try:
opts, args = getopt.getopt(sys.argv[1:], "h",["query=", "table="])
except getopt.GetoptError as err:
# print help information and exit:
print (str(err)) # will print something like "option -a not recognized"
sys.exit(2)
return opts,args
def parseHardQuery(input_query):
input_query = input_query.strip()
input_query = input_query.replace(" ","")
input_query = input_query.replace("),",")^")
return input_query
# read the command line arguments and store the input gile names
tableFiles = []
opts, args = get_input()
for i in range(1,len(opts)):
tableFiles.append(opts[i][1])
queryFile = opts[0][1]
#parse the tables to obtain probabilities for each component
probabilities = parse_tables(tableFiles)
domain = get_domain(probabilities)
#read the input query from query file
'''Inerchange the two statements below to read query from code instead of file'''
# input_query = "R(x1),S(x1,y1) ||S(x2, y2), T(x2)"
input_query = read_query(queryFile)
#parse the query to get UCQ
UCQ, quantifier, tables = parse_UCQ(input_query)
'''Uncomment the below part when testing lifted inference algorithm ONLY, comment otherwise'''
'''print("UCQ",UCQ)
prob = 1 - probability(UCQ)
print("Probability of query:", prob)
'''
cnf = True
'''Uncomment the below code for testing behaviour on HARD Queries as well as easy queries with Sampling methods'''
database=getInitialDatabase(tableFiles)
inputQuery=parseHardQuery(input_query)
copyQuery=inputQuery.replace("||","^")
print('inputQuery: ',inputQuery)
inputQuery_split=re.split("\^",copyQuery)
try:
#get the probability of a query from the database
prob = 1 - probability(UCQ)
if(prob>=0 and prob<=1):
print("Probability using Lifted Inference:",prob)
safeQuery = 1
callSampler(0,1000,database)
callSampler(1,1000,database)
callSampler(2,1000,database)
else:
print("UNLIFTABLE by lifted inference")
callSampler(0,1000,database)
callSampler(1,1000,database)
callSampler(2,1000,database)
except Exception as e:
print("UNLIFTABLE by lifted inference")
print(e)
safeQuery = 0
callSampler(0,1000,database)
callSampler(1,1000,database)
callSampler(2,1000,database)