ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/Printed HTMLs/NormalizeDatabase.html
2023-04-27 12:50:36 -05:00

204 lines
14 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>NormalizeDatabase.java</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
<!--
body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold}
table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold}
.literal {color: #cc7832}
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
.ST0 {color: #287bde}
.number {color: #6897bb}
.string {color: #6a8759}
.ST4 {color: #9876aa}
.ST1 {color: #8a653b}
.comment {color: #808080}
.whitespace {color: #505050}
.ST2 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic}
.ST5 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic}
-->
</style>
</head>
<body>
<table width="100%"><tr><td align="center">/home/caleb/ASDV-Java/Semester 2/Assignments/MP5_CalebFontenot/src/main/java/com/calebfontenot/mp5_calebfontenot/NormalizeDatabase.java</td></tr></table>
<pre>
<span class="comment">/*</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java</span><span class="comment"> to edit this template</span>
<span class="comment"> */</span>
<span class="literal">package</span> com.calebfontenot.mp5_calebfontenot;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
<span class="comment">*/</span>
<span class="comment">/*</span>
<span class="comment"> * To change this license header, choose License Headers in Project Properties.</span>
<span class="comment"> * To change this template file, choose Tools | Templates</span>
<span class="comment"> * and open the template in the editor.</span>
<span class="comment"> */</span>
<span class="literal">import</span> java.util.ArrayList;
<span class="comment">/**</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">ASDV2</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">class</span> NormalizeDatabase
{
<span class="comment">/**</span><span class="comment">Finds</span> <span class="comment">the</span> <span class="comment">closure</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">set</span> <span class="comment">of</span> <span class="comment">attributes</span> <span class="comment">given</span> <span class="comment">a</span> <span class="comment">set</span> <span class="comment">of</span> <span class="comment">FDs</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">relation</span> <span class="comment">R</span>
<span class="comment"> * </span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST1">attributes</span> <span class="comment">attributes</span> <span class="comment">to</span> <span class="comment">find</span> <span class="comment">their</span> <span class="comment">closure</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST1">fds</span> <span class="comment">set</span> <span class="comment">of</span> <span class="comment">FDs</span> <span class="comment">of</span> <span class="comment">relation</span> <span class="comment">R</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">the</span> <span class="comment">closure</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">parameter</span> <span class="comment">attributes</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> String <span class="ST2">closure</span>(String attributes, ArrayList&lt;FD&gt; fds )
{
attributes = attributes.toUpperCase();
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; fds.size(); ++j)
{
FD capitalsFD = <span class="literal">new</span> FD(fds.get(j).getLhs().toUpperCase(),
fds.get(j).getRhs().toUpperCase());
fds.set(j, capitalsFD);
}
<span class="comment">// 1. Set x+ = x</span>
String closure = attributes;
String closurePreveious = attributes;
<span class="comment">// 2. Starting with x+ apply each FD xF —&gt; y in F where</span>
<span class="comment">// xF belongs in closure x+ but but the rhs y is not already in x+, to find determined</span>
<span class="comment">// attributes y</span>
<span class="comment">// 3. x+ = x+ U y</span>
<span class="literal">while</span> (<span class="literal">true</span>)
{
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; fds.size(); ++i)
{
<span class="literal">if</span> (closure.contains(fds.get(i).getRhs()))
<span class="literal">continue</span>;
<span class="comment">// if the left hand side of the FD is contained in the closure</span>
<span class="comment">// then add to the closure the RHS of the FD</span>
<span class="literal">if</span> (closure.contains(fds.get(i).getLhs()))
closure += fds.get(i).getRhs();
}
<span class="literal">if</span> (closurePreveious.equals(closure))
<span class="literal">break</span>;
<span class="literal">else</span>
closurePreveious = closure;
}
<span class="comment">// 4, If y not empty goto (2)</span>
<span class="comment">// 5. Return x+</span>
<span class="literal">return</span> closure;
}
<span class="comment">/**</span>
<span class="comment"> * </span><span class="comment">Eliminates</span> <span class="comment">redundant</span> <span class="comment">attributes</span> <span class="comment">from</span> <span class="comment">the</span> <span class="comment">LHS</span> <span class="comment">of</span> <span class="comment">each</span> <span class="comment">FD</span> <span class="comment">of</span> <span class="comment">a</span> <span class="comment">set</span> <span class="comment">of</span> <span class="comment">FDs</span>
<span class="comment"> * </span><span class="comment">given</span> <span class="comment">as</span> <span class="comment">parameters</span><span class="comment">.</span>
<span class="comment"> *</span>
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST1">fds</span> <span class="comment">the</span> <span class="comment">set</span> <span class="comment">of</span> <span class="comment">FDs</span> <span class="comment">to</span> <span class="comment">eliminate</span> <span class="comment">the</span> <span class="comment">redundancy</span>
<span class="comment"> * </span><span class="comment">@return</span> <span class="comment">and</span> <span class="comment">ArrayList</span> <span class="comment">with</span> <span class="comment">no</span> <span class="comment">redundancy</span> <span class="comment">on</span> <span class="comment">LHS</span> <span class="comment">of</span> <span class="comment">each</span> <span class="comment">FD</span><span class="comment">.</span>
<span class="comment">*/</span>
<span class="literal">public</span> <span class="literal">static</span> ArrayList&lt;FD&gt; <span class="ST2">eliminateRedundantAttributes</span>(ArrayList&lt;FD&gt; fds)
{
<span class="literal">for</span> (<span class="literal">int</span> j = <span class="number">0</span>; j &lt; fds.size(); ++j)
{
<span class="literal">int</span> s = fds.get(j).getLhs().length();
<span class="literal">if</span> (s &lt; <span class="number">2</span>)
{
<span class="literal">continue</span>;
}
<span class="literal">else</span>
{
String fl = fds.get(j).getLhs().substring(<span class="number">0</span>, <span class="number">1</span>);
ArrayList&lt;FD&gt; <span class="comment">fFD</span> = <span class="literal">new</span> ArrayList&lt;FD&gt;();
String s1 = <span class="string">&quot;</span> <span class="string">&quot;</span>;
<span class="literal">if</span> (fds.get(j).getLhs().length() == <span class="number">2</span>)
{
s1 = fds.get(j).getLhs().substring(<span class="number">1</span>);
<span class="literal">if</span> (<span class="ST3">closure</span>(s1,fds).contains(fl))
{
fds.add(<span class="literal">new</span> FD (fds.get(j).getLhs().substring(<span class="number">1</span>, <span class="number">2</span>), fds.get(j).getRhs()));
fds.remove(j);
}
}
<span class="literal">else</span> <span class="literal">if</span> (fds.get(j).getLhs().charAt(<span class="number">1</span>) == <span class="number">3</span>)
{
s1 = fds.get(j).getLhs().substring(<span class="number">1</span>);
<span class="literal">if</span> (<span class="ST3">closure</span>(s1,fds).contains(fl))
{
fds.add(<span class="literal">new</span> FD (fds.get(j).getLhs().substring(<span class="number">1</span>, <span class="number">2</span>), fds.get(j).getRhs()));
fds.add(<span class="literal">new</span> FD (fds.get(j).getLhs().substring(<span class="number">2</span>, <span class="number">3</span>), fds.get(j).getRhs()));
fds.remove(j);
}
}
<span class="literal">else</span> <span class="literal">if</span> (fds.get(j).getLhs().charAt(<span class="number">1</span>) == <span class="number">4</span>)
{
s1 = fds.get(j).getLhs().substring(<span class="number">1</span>);
<span class="literal">if</span> (<span class="ST3">closure</span>(s1,fds).contains(fl))
{
fds.add(<span class="literal">new</span> FD (fds.get(j).getLhs().substring(<span class="number">1</span>, <span class="number">2</span>), fds.get(j).getRhs()));
fds.add(<span class="literal">new</span> FD (fds.get(j).getLhs().substring(<span class="number">2</span>, <span class="number">3</span>), fds.get(j).getRhs()));
fds.add(<span class="literal">new</span> FD (fds.get(j).getLhs().substring(<span class="number">3</span>, <span class="number">4</span>), fds.get(j).getRhs()));
fds.remove(j);
}
}
<span class="literal">else</span>
{
<span class="literal">return</span> fds;
}
}
}
<span class="literal">return</span> fds;
}
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST2">main</span>(String[] args)
{
ArrayList&lt;FD&gt; fds = <span class="literal">new</span> ArrayList&lt;FD&gt;();
FD fd = <span class="literal">new</span> FD(<span class="string">&quot;</span><span class="string">a</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">BC</span><span class="string">&quot;</span>);
FD[] fdDecomposed = fd.decomposeRightHandSide();
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i &lt; fdDecomposed.<span class="ST4">length</span>; ++i)
{
fds.add( <span class="literal">new</span> FD(fdDecomposed[i].getLhs(), fdDecomposed[i].getRhs()));
}
fds.add(<span class="literal">new</span> FD(<span class="string">&quot;</span><span class="string">B</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">C</span><span class="string">&quot;</span>));
fds.add(<span class="literal">new</span> FD(<span class="string">&quot;</span><span class="string">AB</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">B</span><span class="string">&quot;</span>));
fds.add(<span class="literal">new</span> FD(<span class="string">&quot;</span><span class="string">C</span><span class="string">&quot;</span>, <span class="string">&quot;</span><span class="string">A</span><span class="string">&quot;</span>));
System.<span class="ST5">out</span>.println(fds);
System.<span class="ST5">out</span>.println(<span class="ST3">c</span><span class="ST3">losure</span>(<span class="string">&quot;</span><span class="string">b</span><span class="string">&quot;</span>, fds));
System.<span class="ST5">out</span>.println(<span class="ST3">e</span><span class="ST3">liminateRedundantAttributes</span>(fds));
<span class="comment">/* TEST it with </span>
<span class="comment"> Let F1 = {1. A -&gt; BC </span>
<span class="comment"> 2. B -&gt; C, </span>
<span class="comment"> 3. AB -&gt; D }. </span>
<span class="comment"> Attribute B is extraneous in FD 3 AB -&gt; D</span>
<span class="comment"> */</span>
<span class="comment">/*</span>
<span class="comment"> F2 = { 1. AB -&gt; C, </span>
<span class="comment"> 2. C -&gt; A, </span>
<span class="comment"> 3. BC -&gt; D, </span>
<span class="comment"> 4. ACD -&gt; B, </span>
<span class="comment"> 5. D -&gt; E, </span>
<span class="comment"> 6. D -&gt; G, </span>
<span class="comment"> 7. BE -&gt; C, </span>
<span class="comment"> 8. CG -&gt; B,</span>
<span class="comment"> 9. CG -&gt; D, </span>
<span class="comment"> 10. CE -&gt; A, </span>
<span class="comment"> 11. CE -&gt; G}</span>
<span class="comment"> */</span>
}
}
</pre></body>
</html>