#!/usr/bin/perl -w
use DBI; # Load the DBI Switch

$class = "language";
$database = "willand";
$user = "willand";
$password = "interact";
$dataset = "Flags";

open(File3, "PRISM.ini") or die "Cannot open file: $!\n";
while($line3 = <File3>)
{
        @headers = split("=", $line3);
        if ($headers[0] eq "database")
        {
                $database = $headers[1];
                $database =~ s/^\s*(.*?)\s*$/$1/;
        }
        elsif ($headers[0] eq "user")
        {
                $user = $headers[1];
                $user =~ s/^\s*(.*?)\s*$/$1/;
        }
        elsif ($headers[0] eq "password")
        {
                $password = $headers[1];
                $password =~ s/^\s*(.*?)\s*$/$1/;
        }
        elsif ($headers[0] eq "dataset")
        {
                $dataset = $headers[1];
                $dataset =~ s/^\s*(.*?)\s*$/$1/;
        }
        elsif ($headers[0] eq "class")
        {
                $class = $headers[1];
                $class =~ s/^\s*(.*?)\s*$/$1/;
        }
}
close(File3);

$drh = DBI->install_driver( 'mysql' ) or die "Can not load driver";
$dbh = $drh->connect( $database, $user, $password) or die "Can't connect to the database: $database $user $password  \n";

$sql = "select $class, count(*) from $dataset group by 1 order by 1";
$sth = $dbh->prepare($sql); 
$sth->execute or die "Unable to execute query 1: $sql; $dbh->errstr\n"; 

open(File, "$dataset.attr") or die "Cannot open file: $!\n";
my @AttributeNames;
my @ClassArray;
my @ClassCount;
$count = 0;

while($row = $sth->fetchrow_arrayref) 
{ 
	#Putting the query output into two arrays 
	($ClassArray[$count],$ClassCount[$count]) = @$row;
	$count = $count + 1;
	#print "$ClassArray\t$ClassCount\n";
}

	#reading in the attribute names here
	while ($line = <File>)
	{
        	@AttributeNames = split(",", $line);
 
        	foreach $p (@AttributeNames)
        	{
                	$p =~ s/^\s*(.*?)\s*$/$1/;
					
			$i = 0;
			#creating queries for each of the attribute names
			for ($i=0;$i<$count;$i++)
			{
				$sql = "select $p, count(*) from $dataset where $class = '$ClassArray[$i]' and $p <> $class ";
				$sql .= "group by 1 having count(*) = $ClassCount[$i]";

				$sth = $dbh->prepare($sql); 
				$sth->execute or die "Unable to execute query 2: $sql; $dbh->errstr\n"; 

				$cnt = 0;
				if($row = $sth->fetchrow_arrayref) 
				{
					($q,$cnt) = @$row;
					$dbh->do("insert into PRISM values ('$p','$q','$ClassArray[$i]')");
				}
			}

        	}
	}
			$RulesFound = 0;

			for($i = 0; $i < $count; $i++)
			{
				$sql = "select distinct Determinant, DeterminantValue from PRISM";
				$sql .= " where ClassValue = '$ClassArray[$i]' order by 1,2";
				#print "$sql\n";			
				
				$sth = $dbh->prepare($sql);
				$sth->execute or die "Unable to execute query 3: $sql; $dbh->errstr\n";
				
				$cnt = 0;
				$firsttime = 0;
				
				while ($row = $sth->fetchrow_arrayref)
				{
					($w, $cnt) = @$row;
					#print "if $p = $q then $class = $ClassArray[$i]; \n";
					if ($firsttime)
					{
						print "AND ";
					}
					else
					{
						print "if ";
						$firsttime++;
					}
					print "($w = $cnt) ";

					if (!$RulesFound)
					{
						$RulesFound++;
					}
				}

				if ($firsttime)
				{
					print "THEN $class = $ClassArray[$i] \n\n";
				}
			}

if (!$RulesFound)
{
	print "No rules were found for attribute $class from $dataset\n";
}

$sth->finish;			
$dbh->disconnect; 
       
exit;

