Beispiel #1
0
 public static bool HasSubSetOf(this List <AttributeList> list, AttributeList attrs)
 {
     foreach (var a in list)
     {
         if (attrs.Contains(a) && a.Count < attrs.Count)
         {
             return(true);
         }
     }
     return(false);
 }
 public bool Contains(AttributeList attrList)
 {
     foreach (var a in attrList.attributes)
     {
         if (!attributes.Contains(a))
         {
             return(false);
         }
     }
     return(true);
 }
Beispiel #3
0
 public static bool HasSupersetOf(this List <AttributeList> list, AttributeList attrs)
 {
     foreach (var a in list)
     {
         if (a.IsSupersetOf(attrs))
         {
             return(true);
         }
     }
     return(false);
 }
 public bool IsSupersetOf(AttributeList other)
 {
     if (other.Count > Count)
     {
         return(false);
     }
     for (var i = 0; i < other.Count; i++)
     {
         if (attributes[i] != other[i])
         {
             return(false);
         }
     }
     return(true);
 }
        public static List <AttributeList> FindKeys(AttributeList relation, List <FunctionalDependency> fdList)
        {
            var superKeys = FindSuperKeys(relation, fdList);

            var result = new List <AttributeList>();

            foreach (var superKey in superKeys)
            {
                if (!superKeys.HasSubSetOf(superKey))
                {
                    result.Add(superKey);
                }
            }
            return(result);
        }
        public static List <AttributeList> FindSuperKeys(
            AttributeList relation, List <FunctionalDependency> fdList)
        {
            var result       = new List <AttributeList>();
            var combinations = GetCombinations(relation);

            foreach (var attrList in combinations)
            {
                var closure = ComputeClosure(attrList, fdList);
                if (closure.Count >= relation.Count)
                {
                    result.Add(attrList);
                }
            }
            return(result);
        }
        public static AttributeList ComputeClosure(
            AttributeList attr, List <FunctionalDependency> fdList)
        {
            var size   = 0;
            var result = new AttributeList(attr);

            do
            {
                size = result.Count;
                foreach (var fd in fdList)
                {
                    if (result.Contains(fd.LeftHandSide))
                    {
                        result.Add(fd.RightHandSide);
                    }
                }
            } while (size != result.Count);

            return(result);
        }
        public static List <AttributeList> GetCombinations(AttributeList list)
        {
            var result = new List <AttributeList>();
            var count  = (int)Math.Pow(2, list.Count);

            for (var i = 1; i <= count - 1; i++)
            {
                string str   = Convert.ToString(i, 2).PadLeft(list.Count, '0');
                var    combi = new AttributeList();
                for (var j = 0; j < str.Length; j++)
                {
                    if (str[j] == '1')
                    {
                        combi.Add(list[j]);
                    }
                }
                result.Add(combi);
            }
            return(result);
        }
        public static void Main(string[] args)
        {
            Console.WriteLine("Exercise_2_1_1\n");

            var relation = new AttributeList("A,B,C,G,H,I");

            Console.WriteLine($"Relation: {relation}");
            Console.WriteLine();
            //test 2

            var fdList = new List <FunctionalDependency>()
            {
                new FunctionalDependency("A", "B"),
                new FunctionalDependency("A", "C"),
                new FunctionalDependency("C G", "H"),
                new FunctionalDependency("C G", "I"),
                new FunctionalDependency("B", "H")
            };

            Console.WriteLine("Functional dependencies:");
            fdList.Print();
            Console.WriteLine();

            var keys = AttributeClosure.FindKeys(relation, fdList);

            Console.WriteLine("Keys:");
            foreach (var key in keys)
            {
                Console.WriteLine(key);
            }
            Console.WriteLine();

            var superkeys = AttributeClosure.FindSuperKeys(relation, fdList);

            Console.WriteLine("Superkeys:");
            foreach (var key in superkeys)
            {
                Console.WriteLine(key);
            }
        }
 public AttributeList(AttributeList attrList)
 {
     attributes = new List <string>(attrList.attributes);
 }