-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViolationsAlgorithm.py
290 lines (261 loc) · 10.6 KB
/
ViolationsAlgorithm.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
import random
import pandas as pd
import re
import numpy as numpy
def equals_handler(rowA, fieldA, rowB, fieldB, df, t1, t2):
"""
Handler function for the case of the equality operator (=) as well as for the <= and >= operators.
In case the condition is of the form t.A=t'.B, the function will update the value of t'.B to the value of t.A.
Parameters
----------
rowA : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the left-hand side of the = operator)
rowB : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the right-hand side of the = operator)
fieldA : string
the attribute of the first tuple
fieldB : string
the attribute of the second tuple
t1 :
the database tuple corresponding to rowA
t2:
the database tuple corresponding to rowB
df : dataframe
the database frame
Returns
-------
the updated tuples t1 and t2
"""
if rowA == "t1" and rowB == "t2":
value = getattr(t1, fieldA)
setattr(t2, fieldB, value)
if rowA == "t2" and rowB == "t1":
value = getattr(t2, fieldA)
setattr(t1, fieldB, value)
if rowA == "t1" and rowB == "t1":
value = getattr(t1, fieldA)
setattr(t1, fieldB, value)
if rowA == "t2" and rowB == "t2":
value = getattr(t2, fieldA)
setattr(t2, fieldB, value)
return t1,t2
def aux_not_equal_handler(rowA, fieldA, rowB, fieldB, comp, df, t1 , t2):
"""
An auxilary function for the disequality handler.
The function will find an appropriate value from the active domain of the given attribute if such a value
exists or choose a random value (that depends on the type of the object) otherwise.
Parameters
----------
rowA : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the left-hand side of the = operator)
rowB : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the right-hand side of the = operator)
fieldA : string
the attribute of the first tuple
fieldB : string
the attribute of the second tuple
comp : object
the object of the chosen attribute
t1 :
the database tuple corresponding to rowA
t2 :
the database tuple corresponding to rowB
df : dataframe
the database frame
Returns
-------
object:
a new value for the chosen attribute
"""
attr_set = df[fieldA].unique()
attr_set = attr_set[attr_set != comp]
if len(attr_set) > 0:
val = random.choice(attr_set)
else:
old_val = getattr(t1, fieldA)
if(type(old_val) is str):
val = old_val + "1"
elif(type(old_val) is numpy.int64 or type(old_val) is numpy.float64 or type(old_val) is numpy.double64):
val = random.uniform(comp+1, comp+100)
elif(type(old_val) is date):
start_date = old_val
end_date = datetime.today
time_between_dates = end_date - start_date
days_between_dates = time_between_dates.days
random_number_of_days = random.randrange(days_between_dates)
val = start_date + datetime.timedelta(days=random_number_of_days)
return val
def not_equal_handler(rowA, fieldA, rowB, fieldB, df, t1, t2):
"""
Handler function for the case of the disequality operator (!=).
In case the condition is of the form t.A!=t'.B, the function will update the value of t.A to a different value
from the active domain of A that is different from t'.B, if such a value exists, and to a random value otherwise.
Parameters
----------
rowA : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the left-hand side of the = operator)
rowB : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the right-hand side of the = operator)
fieldA : string
the attribute of the first tuple
fieldB : string
the attribute of the second tuple
t1 :
the database tuple corresponding to rowA
t2 :
the database tuple corresponding to rowB
df : dataframe
the database frame
Returns
-------
the updated tuples t1 and t2
"""
if rowA == "t1":
# in case the violation already exists
if getattr(t1, fieldA) != getattr(t2, fieldB):
return t1,t2
comp = getattr(t1, fieldA)
val = aux_not_equal_handler(rowA, fieldA, rowB, fieldB, comp, df, t1, t2)
setattr(t1, fieldA, val)
return t1,t2
if rowA == "t2":
# in case the violation already exists
if getattr(t2, fieldA) != getattr(t1, fieldB):
return t1,t2
comp = getattr(t2, fieldA)
val = aux_not_equal_handler(rowA, fieldA, rowB, fieldB, comp, df, t1, t2)
setattr(t2, fieldA, val)
return t1,t2
def less_more_handler(rowA, fieldA, rowB, fieldB, op, df, t1, t2):
"""
Handler function for the case of the less or more operators (< or >).
In case the condition is of the form t.A<t'.B or t.A>t'.B, the function will update the value of t.A to a different value
from the active domain of A that sarisfies the condition with t'.B, if such a value exists, and to a random value otherwise.
Parameters
----------
rowA : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the left-hand side of the = operator)
rowB : string
either t1 (in which case the tuple t1 is considered on the left-hand side of the = operator)
or t2 (in which case the tuple t2 is considered on the right-hand side of the = operator)
fieldA : string
the attribute of the first tuple
fieldB : string
the attribute of the second tuple
op: string
the comparison operator
t1 :
the database tuple corresponding to rowA
t2 :
the database tuple corresponding to rowB
df : dataframe
the database frame
Returns
-------
the updated tuples t1 and t2
"""
if rowA == "t1":
if op == ">":
# in case the violation already exists
if getattr(t1, fieldA) > getattr(t2, fieldB):
return t1,t2
attr_set = df[fieldA].unique()
comp = getattr(t2, fieldB)
attr_set = attr_set[attr_set > comp]
if len(attr_set) > 0:
val = random.choice(attr_set)
else:
val = random.uniform(comp+1, comp+100)
setattr(t1, fieldA, val)
return t1,t2
if op == "<":
# in case the violation already exists
if getattr(t1, fieldA) < getattr(t2, fieldB):
return t1,t2
attr_set = df[fieldA].unique()
comp = getattr(t2, fieldB)
attr_set = attr_set[attr_set < comp]
if len(attr_set) > 0:
val = random.choice(attr_set)
else:
val = random.uniform(comp-100, comp-1)
setattr(t1, fieldA, val)
return t1,t2
if rowA == "t2":
if op == ">":
# in case the violation already exists
if getattr(t2, fieldA) > getattr(t1, fieldB):
return t1,t2
attr_set = df[fieldA].unique()
comp = getattr(t1, fieldB)
attr_set = attr_set[attr_set > comp]
if len(attr_set) > 0:
val = random.choice(attr_set)
else:
val = random.uniform(comp+1, comp+100)
setattr(t2, fieldA, val)
return t1,t2
if op == "<":
# in case the violation already exists
if getattr(t2, fieldA) < getattr(t1, fieldB):
return t1,t2
attr_set = df[fieldA].unique()
comp = getattr(t1, fieldB)
attr_set = attr_set[attr_set < comp]
if len(attr_set) > 0:
val = random.choice(attr_set)
else:
val = random.uniform(comp-100, comp-1)
setattr(t2, fieldA, val)
return t1,t2
def fittingViolationAlgorithm(constraintSet,df,t1,t2):
"""
fittingViolationAlgorithm - changes the database to violate a given constraints.
For each condition in constraintSet, an appropriate function will be used to ensure that the selected tuples
jointly satisfy the condition, based on the operator.
When all the conditions are satisfied, the constraint is violated.
Parameters
----------
constraintSet : set of string values
each string represents a single condition in the selected denial constraint.
df : dataframe
the database frame
t1 :
the first database tuple
t2 :
the second database tuple
Returns
-------
the updated tuples t1 and t2
"""
for cst in constraintSet:
op = cst[1]
rowA, fieldA = cst[0].split(".")
rowB, fieldB = cst[2].split(".")
coin = random.randint(1, 2)
if op == "=" or op == ">=" or op == "<=" :
if coin == 1:
t = equals_handler(rowA, fieldA, rowB, fieldB, df, t1, t2)
if coin == 2:
t = equals_handler(rowB, fieldB, rowA, fieldA, df, t1, t2)
if op == "!=" :
if coin == 1:
t = not_equal_handler(rowA, fieldA, rowB, fieldB, df, t1, t2)
if coin == 2:
t = not_equal_handler(rowB, fieldB, rowA, fieldA, df, t1, t2)
if op == ">" or op == "<" :
if coin == 1:
t = less_more_handler(rowA, fieldA, rowB, fieldB, op, df, t1, t2)
if coin == 2:
t = less_more_handler(rowB, fieldB, rowA, fieldA, op, df, t1, t2)
return t
def updateTable(df,t1,t2,sample):
df.loc[sample.index[0]] = list(t1)
df.loc[sample.index[1]] = list(t2)