-
Notifications
You must be signed in to change notification settings - Fork 0
/
Synthesis.rb
440 lines (395 loc) · 12.2 KB
/
Synthesis.rb
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
require 'set'
require_relative 'FD'
require_relative 'Relation'
# Code by: Matt Antonelli
def exclude_fd(fd, f) # Returns F minus a specific FD
f = f.dup
f.each do |fd_check|
if fd.x == fd_check.x && fd.multi == fd_check.multi && fd.y == fd_check.y
return f.delete(fd_check) # Return F with removed FD
end
end
return f # FD not found - return original F
end
def find_dep(x, rel) # Calculates the dependency basis of x in reference to the given relation
rel = rel.clone
s = Set.new [rel.u - x]
# 1. Change all FDs to MVDs
rel.f.each do |fd|
fd.set_multi(true)
end
# 2. Check (Y intersect W == empty) and (Y intersect V != empty) for each MVD
old_y = Set.new
new_y = Set.new
rel.f.each do |mvd|
s.each do |y|
if (y & mvd.y).size > 0 && (y & mvd.x).size == 0
old_y = y
new_y.add(y & mvd.y)
new_y.add(y - mvd.y)
break
end
end
if new_y.size > 0
s.delete(old_y)
s.merge(new_y)
old_y.clear
new_y.clear
next
end
end
s.delete_if { |e| e.empty? } # Removes any empty sets
puts "\nFinal result of DEP"
s.each do |mvd|
print_set(mvd)
end
puts ""
return s # Return the resulting dependency basis
end
def find_minimal_set(rel) # Returns the relation as a minimal set
f = rel.f.dup
new_set = Set.new
f.each do |fd|
if !fd.multi # Do not reduce MVDs in the minimal set
fd.y.each do |y| # Canonize the right-side of the FD
new_set.add( FD.new(fd.x, (Set.new [y]), false) )
end
else
new_set.add(fd) # Add MVDs to set without modification
end
end
f.clear # Clear original set
f.merge(new_set) # Add all values of new set
new_set.clear
f.each do |fd|
if !fd.multi && fd.x.size > 1 # Must be FD and |x| > 1
new_set.add(reduce_left(fd, f)) # Attempt to reduce x
else
new_set.add(fd) # If not an FD or |x| == 1, add to set as is
end
end
f.clear
f.merge(new_set)
f_copy = Set.new.merge(f)
f.each do |fd|
if !fd.multi # Do not worry about MVDs
x = fd.x
x_plus = find_xclosure(x, exclude_fd(fd, f_copy))
if fd.y.subset?x_plus # If X+ can still find the Y values without the FD in question
f_copy.delete(fd) # Remove the FD from the set
end
end
end
f.clear
f.merge(f_copy)
return Relation.new(f)
end
def find_xclosure(x, f) # Calculates X+ for x in reference to the given F
x_plus = Set.new.merge(x)
old_x = Set.new
begin
old_x.clear
old_x.merge(x_plus)
f.each do |fd|
if fd.x.subset?(x_plus) && !fd.multi # If the FD is not an MVD and its X is a subset of X+
x_plus.merge(fd.y) # Add the Y values of the FD to X+
end
end
end while x_plus != old_x
return x_plus
end
def is_bcnf?(fd, rel) # Determines whether an FD in reference to a minimal set can determine all attributes
minimal_set = find_minimal_set(rel)
if !minimal_set.u.subset?(find_xclosure(fd.x, minimal_set.f)) # If X+ does not contain U of the minimal set
return false # The FD is preventing the relation from being in BCNF
end
return true # If X+ contains U of the minimal set, the FD is not preventing the relation from being in BCNF
end
def is_nontrivial?(mvd, rel) # Determines whether or not an MVD is non-trivial
return !(mvd.y.size == 0 || rel.u.subset?(mvd.y) || mvd.y.subset?(mvd.x))
end
def is_superkey?(x, rel) # Determines whether or not an MVD is a superkey of the given relation
dep_basis = find_dep(x, rel)
if dep_basis.include?(rel.u)
rel.each do |fd|
if !fd.multi && !rel.u.include?(fd.x) && fd.y.subset?(rel.u)
return true
end
end
end
return false
end
def print_set(set) # Prints the values of the given set
set.each do |x|
$stdout.print x
end
end
def read_fds(file) # Reads FDs from the file. File must end with a line break or space!
fds = Set.new
begin
if !/.txt/.match(file)
raise "Input must be a .txt file!"
end
File.open(file, 'r') do |f|
while line = f.gets
if line.size < 3
raise line
end
if !(/\A[[:alpha:]]+>{1,2}(([[:alpha:]]*)|0)\Z/.match(line)) # Regex
raise line.chomp + " is not properly formatted!"
end
count = 0
for i in 0...line.size
if line[i] == '>'
count += 1
end
end
if count < 1 || count > 2
raise line.chomp + " is not properly formatted!"
end
sep = line.index('>')
multi = line.count('>') == 2 ? true : false
x = Set.new
for i in 0...sep
x.add(line[i])
end
y = Set.new
for i in (sep + (multi ? 2 : 1))...(line.size - 1)
y.add(line[i])
end
if y.size == 0
y.add('0')
end
fds.add(FD.new(x, y, multi))
end
end
rescue Exception => e
puts e.message
exit
end
return fds
end
def reduce_left(fd, f) # Reduces left side of an FD
original_fd = fd.clone
original_fd.x.each do |x|
new_fd = fd.clone
new_fd.remove_x(x)
x_plus = find_xclosure(new_fd.x, exclude_fd(original_fd, f))
if fd.y.subset?x_plus # If the X+ of the FD minus attribute X still contains all Y values
fd = new_fd.clone # Set the FD to the tested FD with the removed value
if new_fd.x.size == 1
return new_fd
end
end
end
return fd
end
def synth_3nf(rel)
rel = find_minimal_set(rel) # 1. Calculate the minimal set for the relation
# 2. Find key. This operation is automatically done within the Relation class.
# 3. Break into multiple relations that share the same determinant.
rels = Set.new
fds = rel.f.to_a
rel.f.each do |fd|
rel_fds = Set.new
fd_copy = Array.new(fds)
for i in 0...fd_copy.size
if fd_copy[i].x == fd.x
rel_fds.add(fd_copy[i])
fds.delete(fd_copy[i])
end
end
if rel_fds.size > 0
rels.add(Relation.new(rel_fds))
end
end
# 4. If the key is not contained within any of the relations, add a relation containing the key.
contains_key = false
rels.each do |r|
if rel.key.subset?r.u
contains_key = true
break
end
end
if !contains_key
rels.add(Relation.new(Set.new [FD.new(rel.key, nil, false)]))
end
# 5. Remove redundant relations (those where the U of one is the subset of another) by merging FDs
new_rels = Set.new
rels_copy = Set.new.merge(rels)
rels.each do |r|
new_fds = Set.new
new_rel = Relation.new(Set.new.merge(r.f))
duplicates = Set.new
rels_copy.each do |s|
if s != r && s.u.subset?(r.u) # If the relation is not comparing to itself and its U is a subset of the compared's U
duplicates.add(s) # Add the relation to a set of duplicates to be removed
s.f.each do |fd|
new_fds.add(fd) # Add each of the relation's FD to a set
end
end
end
rels.subtract(duplicates) # Remove the duplicate relations
new_fds.merge(r.f) # Merge these FDs with those in the comparing relation
new_rels.add(Relation.new(new_fds)) # Add a new relation containing the merged relations
end
return new_rels # Return set of relations in 3NF
end
def synth_bcnf(rel)
rel = rel.clone
original_f = rel.f.dup
new_rels = Set.new.add(rel)
begin
all_bcnf = true
new_fds = Set.new
rel.f.each do |fd|
# If the FD is not an MVD, does not determine empty set, and fails the BCNF requirement
if !fd.y.include?('0') && !is_bcnf?(fd, rel.clone)
new_rels.add(Relation.new(Set.new [fd.clone])) # Add it to its own relation
new_fds = rel.remove_att(fd, fd.y) # Remove its y-value from the original relation
all_bcnf = false # Set the loop to continue
break
end
end
if !all_bcnf
new_f = Set.new
new_fds.each do |fd|
if fd.y.include?('0')
new_rels.add(Relation.new(Set.new [fd.clone])) # Move FDs determining empty set to their own relations
else
new_f.add(fd) # Otherwise, just keep the FD in the original relation
end
end
rel.set_f(new_f) # Update the original relation to the new FDs
end
end while all_bcnf == false
new_f = Set.new
remove_set = Set.new
new_rels.each do |r|
new_f.merge(r.f)
if r.u.size == 0
remove_set.add(r) # Find relations that were left with nil values
end
end
new_rels.subtract(remove_set) # Remove empty relations
return [new_rels, (original_f - new_f)] # Return the set of relations in BCNF & set of lost FDs
end
def synth_4nf(rel)
rel = rel.clone
original_f = rel.f.dup
new_rels = Set.new.add(rel)
begin
change_mvd = nil
no_change = true
rel.f.each do |mvd|
# If the MVD is non-trivial and is not a superkey
if mvd.multi && is_nontrivial?(mvd, rel) && !is_superkey?(mvd.x, rel)
change_mvd = mvd.clone # Set the MVD to reference
no_change = false # Set the loop to continue
break
end
end
if !no_change
new_rels.add(Relation.new(Set.new [change_mvd])) # Add the MVD to its own relation
rel.f.delete(change_mvd) # Delete the MVD from the original relation
new_fds = rel.remove_att(change_mvd, change_mvd.y) # Remvoe its y-value from the original relation
new_f = Set.new
new_fds.each do |fd|
if fd.y.include?('0')
new_rels.add(Relation.new(Set.new [fd.clone])) # Move FDs determining empty set to their own relations
else
new_f.add(fd) # Otherwise, just keep the FD in the original relation
end
end
rel.set_f(new_f) # Update the original relation to the new FDs
end
end while !no_change
new_rels.add(rel)
new_f = Set.new
remove_set = Set.new
new_rels.each do |r|
new_f.merge(r.f)
if r.u.size == 0
remove_set.add(r) # Find relations that were left with nil values
end
end
new_rels.subtract(remove_set) # Remove empty relations
return [new_rels, (original_f - new_f)] # Return the set of relations in 4NF & set of lost FDs
end
puts "Provide a path for a .txt file containing your FDs."
puts "The FDs should be formatted as: AB>C, A>>BC, A>0, or A>"
puts "You must include a blank line at the end of your file!"
print "\nEnter the file path: "
file = gets.chomp
rel = Relation.new(read_fds(file)) # Read FDs from file and create a relation R(U, F)
puts "\nOriginal relation:"
rel.print
contains_multi = false
rel.f.each do |fd|
if fd.multi
contains_multi = true
break
end
end
while true
puts "\nWhat would you like to do with your relation?"
puts "a) Decompose into 3NF preserving dependencies and lossless join."
puts "b) Decompose into BCNF preserving lossless join."
puts "c) Decompose into 4NF preserving lossless join."
puts "d) Nothing. Exit the program.\n\n"
print "> "
choice = gets.chomp
if /\A[abcd]\Z/.match(choice)
case choice
when 'a'
if !contains_multi
tnf = synth_3nf(rel) # Synthesize the original relation to 3NF & print the results
puts "\nFinal set of relations in 3NF\nPreserving Dependencies & Lossless Join:\n\n"
i = 1
tnf.each do |r|
puts "R" + i.to_s
r.print
i += 1
end
else
puts "\nThe relation contains an MVD, so the it can only be decomposed to 4NF."
end
when 'b'
if !contains_multi
bcnf = synth_bcnf(rel) # Synthesize the original relation to BCNF & print the results
puts "\nFinal set of relations in BCNF\nPreserving Lossless Join:\n\n"
i = 1
bcnf[0].each do |r|
puts "R" + i.to_s
r.print
i += 1
end
puts "\nFDs lost during the BCNF Synthesis:\n\n"
bcnf[1].each do |fd|
fd.print
puts ""
end
else
puts "\nThe relation contains an MVD, so the it can only be decomposed to 4NF."
end
when 'c'
fnf = synth_4nf(rel) # Synthesize the original relation to 4NF & print the results
puts "\nFinal set of relations in 4NF\nPreserving Lossless Join:\n\n"
i = 1
fnf[0].each do |r|
puts "R" + i.to_s
r.print
i += 1
end
puts "\nFDs lost during the 4NF Synthesis:\n\n"
fnf[1].each do |fd|
fd.print
puts ""
end
when 'd'
exit
end
else
puts "\nInvalid input!"
end
end