package BCNF_3NF /********************************************************************************************* * @author Shasha(Amy) Liu * @version 1.0 */ /********************************************************************************************* * Object containing functions for Functional Depandency computings */ object FDs extends Application { /***************************************************************************************** * Compute the closure of a set of attributes. * @param x Attribute set * @param fd Set of functional dependencies * @return Closure of x (x+) */ def closure (x: Set [Char], fd: List [Tuple2 [Set [Char], Set [Char]]]): Set [Char] = { var z = x var changes = true while (changes) { changes = false for (f <- fd if (f._1 subsetOf z) && ! (f._2 subsetOf z)) { z ++= f._2 changes = true } // for } // while z } // closure /***************************************************************************************** * Check whether a decomposition is dependency preserving or not * @param f A functional dependency in the original FDs set * @param fd Set of functional dependencies * @param rho Schemas derived from the decomposition * @return Whether f in fd is preserved in the decomposition or not */ def preserve (f: Tuple2 [Set [Char], Set [Char]], rho: List[Set [Char]], fd: List [Tuple2 [Set [Char], Set [Char]]]): Boolean = { var Z = f._1 var changes = true while (changes) { changes = false for(ri <- rho) { val ZZ = (closure ((Z intersect ri), fd)) intersect ri if (! (ZZ subsetOf Z) ) { Z ++= ZZ changes = true }//if }//for }//while f._2 subsetOf Z } // def isSuperkey(ri: Set[Char], r: Set[Char], // fd: List [Tuple2 [Set [Char], Set [Char]]]): Boolean = { // val riPlus = closure(ri, fd) // r subsetOf riPlus // } // // def findKey(r: Set[Char], fd: List[Tuple2[Set[Char], Set[Char]]]): Set[Char] = { // var key: Set[Char] = Set() // var keyPls: Set[Char] = Set() // for(f <- fd) { // var riPls = closure(f._1, fd) // if( !(riPls subsetOf keyPls)) { // key ++= f._1 // keyPls ++= riPls // } // else if((keyPls subsetOf riPls) && (f._1.size <= key.size)) { // key = f._1 // } // } // if(r != keyPls) { // key ++= (r -- keyPls) // keyPls = r // } // key // } // // /** // * Test findKey // */ // val r = Set('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H') // val fd = List( (Set('B', 'H'), Set('C')), // (Set('A'), Set('D')), // (Set('C'), Set('E')), // (Set('F'), Set('A')), // (Set('E'), Set('F')) ) // // var key = findKey(r, fd) // println(key) }