-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.py
111 lines (91 loc) · 4.72 KB
/
validator.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
from z3 import *
import random
#from sets import Set
def solve( query ):
x = []
var_num = num_vars(query)
for var in range( var_num ):
x.append( Real( 'x[' + str(var) + ']' ) )
s = Solver()
s.add( eval(query) )
## Ensure that an interval's start time is before its endtime when validating queries
for interval in range( int(var_num/2) ):
s.add(eval("x[" + str(2*interval) + "] < x[" + str(2*interval+1) + "]"))
#print str(var_num/2) + " " + str(s.check())
return s.check()
def generate_queries(num_queries, num_intervals, num_clauses, sat, output_file):
i = 0
while i < num_queries:
query = ""
## Add random interval relationships to the query
for rel in range( int(num_clauses) ):
interval1 = interval2 = random.randint(0, num_intervals-1)
while interval2 == interval1:
interval2 = random.randint(0, num_intervals-1)
query = query + randomRelationshipString(interval1, interval2)
if rel != num_clauses-1:
query = query + ", "
query = query + "\n"
uniques = identifyUniqueElements( query )
next_assignment = 0
for index in range(len(uniques)):
var_index = uniques[index]
if index>0:
if var_index%2!=0 and var_index-1!=uniques[index-1]:
next_assignment = next_assignment + 1
if var_index > next_assignment:
query = query.replace("["+str(var_index)+"]", "["+str(next_assignment)+"]")
if index<len(uniques)-1:
if var_index%2==0 and var_index+1!=uniques[index+1]:
next_assignment = next_assignment + 1
next_assignment = next_assignment + 1
if (str(solve(query)) == 'unsat') == (not sat):
i = i + 1
print("Generated query " + str(i))
output_file.write( query )
def randomRelationshipString( e, e_prime ):
relationship = ""
relation = random.randint(1, 13)
if relation == 1: #before
relationship = "x[" + str(2*e+1) + "] < x[" + str(2*e_prime) + "]"
elif relation == 2: #after
relationship = "x[" + str(2*e) + "] > x[" + str(2*e_prime+1) + "]"
elif relation == 3: #during
relationship = "x[" + str(2*e) + "] > x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] < x[" + str(2*e_prime+1) + "]"
elif relation == 4: # contain
relationship = "x[" + str(2*e) + "] < x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] > x[" + str(2*e_prime+1) + "]"
elif relation == 5: #meet
relationship = "x[" + str(2*e+1) + "] == x[" + str(2*e_prime) + "]"
elif relation == 6: #met by
relationship = "x[" + str(2*e) + "] == x[" + str(2*e_prime+1) + "]"
elif relation == 7: #overlap
relationship = "x[" + str(2*e) + "] < x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] > x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] < x[" + str(2*e_prime+1) + "]"
elif relation == 8: #overlapped by
relationship = "x[" + str(2*e) + "] > x[" + str(2*e_prime) + "], x[" + str(2*e) + "] < x[" + str(2*e_prime+1) + "], x[" + str(2*e+1) + "] > x[" + str(2*e_prime+1) + "]"
elif relation == 9: #start
relationship = "x[" + str(2*e) + "] == x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] < x[" + str(2*e_prime+1) + "]"
elif relation == 10: #started by
relationship = "x[" + str(2*e) + "] == x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] > x[" + str(2*e_prime+1) + "]"
elif relation == 11: #finish
relationship = "x[" + str(2*e) + "] > x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] == x[" + str(2*e_prime+1) + "]"
elif relation == 12: #finished
relationship = "x[" + str(2*e) + "] < x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] == x[" + str(2*e_prime+1) + "]"
elif relation == 13: #equal
relationship = "x[" + str(2*e) + "] == x[" + str(2*e_prime) + "], x[" + str(2*e+1) + "] == x[" + str(2*e_prime+1) + "]"
return relationship
def identifyUniqueElements( query ):
## Identify unique elements
els = query.replace("<", " ").replace(">", " ").replace("==", " ").replace(",", " ").replace("x["," ").replace("]"," ").split()
unique_els = []
for el in els:
num = int(el)
if num not in unique_els:
unique_els.append(num)
unique_els.sort()
return unique_els
def num_vars( query ):
uniques = identifyUniqueElements(query)
max_index = uniques[ len(uniques)-1 ] # Find biggest variable index
max_index = max_index + (1-max_index%2) # If it's even, we need to add that intervals endpoint
return max_index + 1 # Account for zero indexing (since this is a count)
#init('/fs/junkfood/maliks/Courses/724/dbproj/libz3.so')