/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/NormalizeDatabase.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package com.calebfontenot.mp5_calebfontenot;
import java.util.ArrayList;
public class NormalizeDatabase
{
attributes
fds
public static String closure(String attributes, ArrayList<FD> fds )
{
attributes = attributes.toUpperCase();
for (int j = 0; j < fds.size(); ++j)
{
FD capitalsFD = new FD(fds.get(j).getLhs().toUpperCase(),
fds.get(j).getRhs().toUpperCase());
fds.set(j, capitalsFD);
}
String closure = attributes;
String closurePreveious = attributes;
while (true)
{
for (int i = 0; i < fds.size(); ++i)
{
if (closure.contains(fds.get(i).getRhs()))
continue;
if (closure.contains(fds.get(i).getLhs()))
closure += fds.get(i).getRhs();
}
if (closurePreveious.equals(closure))
break;
else
closurePreveious = closure;
}
return closure;
}
fds
public static ArrayList<FD> eliminateRedundantAttributes(ArrayList<FD> fds)
{
for (int j = 0; j < fds.size(); ++j)
{
int s = fds.get(j).getLhs().length();
if (s < 2)
{
continue;
}
else
{
String fl = fds.get(j).getLhs().substring(0, 1);
ArrayList<FD> = new ArrayList<FD>();
String s1 = " ";
if (fds.get(j).getLhs().length() == 2)
{
s1 = fds.get(j).getLhs().substring(1);
if (closure(s1,fds).contains(fl))
{
fds.add(new FD (fds.get(j).getLhs().substring(1, 2), fds.get(j).getRhs()));
fds.remove(j);
}
}
else if (fds.get(j).getLhs().charAt(1) == 3)
{
s1 = fds.get(j).getLhs().substring(1);
if (closure(s1,fds).contains(fl))
{
fds.add(new FD (fds.get(j).getLhs().substring(1, 2), fds.get(j).getRhs()));
fds.add(new FD (fds.get(j).getLhs().substring(2, 3), fds.get(j).getRhs()));
fds.remove(j);
}
}
else if (fds.get(j).getLhs().charAt(1) == 4)
{
s1 = fds.get(j).getLhs().substring(1);
if (closure(s1,fds).contains(fl))
{
fds.add(new FD (fds.get(j).getLhs().substring(1, 2), fds.get(j).getRhs()));
fds.add(new FD (fds.get(j).getLhs().substring(2, 3), fds.get(j).getRhs()));
fds.add(new FD (fds.get(j).getLhs().substring(3, 4), fds.get(j).getRhs()));
fds.remove(j);
}
}
else
{
return fds;
}
}
}
return fds;
}
public static void main(String[] args)
{
ArrayList<FD> fds = new ArrayList<FD>();
FD fd = new FD("a", "BC");
FD[] fdDecomposed = fd.decomposeRightHandSide();
for (int i = 0; i < fdDecomposed.length; ++i)
{
fds.add( new FD(fdDecomposed[i].getLhs(), fdDecomposed[i].getRhs()));
}
fds.add(new FD("B", "C"));
fds.add(new FD("AB", "B"));
fds.add(new FD("C", "A"));
System.out.println(fds);
System.out.println(closure("b", fds));
System.out.println(eliminateRedundantAttributes(fds));
}
}