<?php
	//connection
	include("connection.php");
    include("frames_header.php");
?>


<?php
/*Fonction arbre anatomique*/
		/*Requete hierarchique*/
	/* SIMULATION D'UNE REQUETE SUR HIERARCHIE PAR UNE PROCEDURE DE REQUETES RECURSIVES */

/* VARIANTE DANS LA REQUETE POUR LA CONNEXION ET LE RESULTAT DU SELECT */
/* VARIANTE SUR LE PASSAGE EN PARAMETRE DU NIVEAU COURANT DE RECURSIVITE POUR L'AFFICHAGE DES BARRES VERTICALES */



function select_recursif_descendant_2_W2($conn,$start,$res)
{
global $word;
global $wordstwo;
global $wordsone;
global $count;

/*ON ECRIT SA REQUETE*/

if ($start!='')
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"ANAT_ID\"= $start ";
else return;


$stmt2=pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));

while($row2=pg_fetch_array($stmt2))
	{
	$res=select_recursif_descendant_2_W2($conn,$row2["CHILD_OF"],$res);

			$word=$row2["ANAT_ID"];
			//if( (! in_array($word,$wordstwo))and(strlen(OCIResult($stmt2, "CHILD_OF"))>0) ){
			if( (! in_array($word,$wordstwo)) ){
			$wordstwo[$count++]=$word;
			//$poidtwo[$word]=$level[$word]/$level[$region];///$level[$word]/$maxbranch;
			//print "score: $poidtwo[$word]<BR>";
			}
			
	}
return $res;
}

function select_recursif_descendant_2_W1($conn,$start,$res)
{
global $word;
global $wordstwo;
global $wordsone;
global $count;

/*ON ECRIT SA REQUETE*/

if ($start!='')
$requete2="SELECT * FROM \"ANAT_ENTITY\" WHERE  \"ANAT_ID\"= $start ";
else return;


$stmt2=pg_query($conn,$requete2) or die("<strong>Error</strong> ".pg_last_error($c));

while($row2=pg_fetch_array($stmt2))
	{
	$res=select_recursif_descendant_2_W1($conn,$row2["CHILD_OF"],$res);

			$word=$row2["ANAT_ID"];
			//if( (! in_array($word,$wordstwo))and(strlen(OCIResult($stmt2, "CHILD_OF"))>0) ){
			if( (! in_array($word,$wordsone)) ){
			$wordsone[$count++]=$word;
			//$poidtwo[$word]=$level[$word]/$level[$region];///$level[$word]/$maxbranch;
			//print "score: $poidtwo[$word]<BR>";
			}
			
	}
return $res;
}

?>

<?php
    $testmode = $_POST["test"];
	$maxdist = $_POST["dist"];
	$full = $_POST["full"];
//	$is = $_POST["is"];//selection d'in situ
	
	if($testmode!='y')
	    $testmode='n';
	else
	    print 'Test mode selected. No data will be recorded.<br>';

	
	if($full!='y')
	    $full='n';
	else
	    print 'Full update mode<br>';
	
	
    print 'Searching...<br><br>';
	$coucounter=0; //compte les enregistrements
	
//species dispo
$requete= "SELECT \"NAME\" FROM \"SPECIES\"";
$stmt33 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
while($row33=pg_fetch_array($stmt33)){

	
    //dev stage dispo
	$requete= "SELECT \"DEV_STAGE_ID\", \"NAME\", \"SPECIES\" FROM \"DEV_STAGE\" where \"SPECIES\"='".$row33["NAME"]."'";	
	$stmt3 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
	while($row3=pg_fetch_array($stmt3)){
	    $dev=$row3["DEV_STAGE_ID"];

		
		//enregistrement de tout les id des in situ correspondant a chaque stade.
		$insitu=(array)null;
		$count=0;
		$requete= "SELECT \"IN_SITU_ID\" FROM \"IN_SITU\" where \"DEV_STAGE_ID\"=$dev and \"CHECKED\"='y' and \"PRIVATE\"='n'";
		/*
		if($full=='n'){
			$requete.=" MINUS select IN_SITU_ID from ciona_design.in_situ_cluster";
		}
		*/
		$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
		while($row=pg_fetch_array($stmt)){
			$insitu[$count++]=$row["IN_SITU_ID"];
		}
		print "<br><br>$count in situ available for stage ".$row3["NAME"]." ".$row3["SPECIES"].".<br>";
		//ocifreestatement($stmt);
	/*	///////////////////////////////////////////////////////////////////////////////*/
		/*
		//enregistrement des levels de chaque elt anatomique pour les ponderations.
		$requete= "SELECT LEVEL AS LEV, ANAT_ID FROM CIONA_DESIGN.ANAT_ENTITY  WHERE DEV_STAGE_ID= $dev  CONNECT BY CHILD_OF = PRIOR ANAT_ID START WITH CHILD_OF IS NULL";
		$stmt = OCIParse($connection, $requete);
		OCIExecute($stmt);
		while(OCIFetch($stmt)){
			$level[OCIResult($stmt, "ANAT_ID")]=OCIResult($stmt, "LEV");
		}
		ocifreestatement($stmt);
		*/
		
		
		
		
	/*	///////////////////////////////////////////////////////////////////////////////*/





		for($a=0; $a<count($insitu); ++$a){
			//recup des regions marquees
			$requete= "SELECT \"REGION_ID\" FROM \"TARGET\" where \"IN_SITU_ID\"=$insitu[$a]";
			$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			$words1=(array)null;
			//$poidone=(array)null;
			$count=0;
			while($row=pg_fetch_array($stmt)){
				$region=$row["REGION_ID"];
				if(! in_array($region,$words1))//{
					$words1[$count++]=$region;
				/*
				//recup des parents de ces regions
				$requete= "SELECT ANAT_ID FROM CIONA_DESIGN.ANAT_ENTITY CONNECT BY ANAT_ID = PRIOR CHILD_OF START WITH ANAT_ID=$region ORDER BY LEVEL DESC";
				$stmt2 = OCIParse($connection, $requete);
				OCIExecute($stmt2);
				while(OCIFetch($stmt2)){
					$word=OCIResult($stmt2, "ANAT_ID");
					if(! in_array($word,$wordsone)){
						$wordsone[$count++]=$word;

					}
				}
				ocifreestatement($stmt2);
				*/
			}
			
			//ocifreestatement($stmt);

	/*	///////////////////////////////////////////////////////////////////////////////////*/

			for($b=$a; $b<count($insitu); ++$b){
			    //print "***".$insitu[$b]."*****".$insitu[$a]."*****<br>";
				if($insitu[$b] == $insitu[$a]){
				   continue;
				}
			
			    if($full=='n'){
					$requete= "SELECT * FROM \"IN_SITU_CLUSTER3\" where (\"IN_SITU_ID\"=$insitu[$a] AND \"IN_SITU_ID_2\"=$insitu[$b]) OR (\"IN_SITU_ID\"=$insitu[$b] AND \"IN_SITU_ID_2\"=$insitu[$a])";
					$stmtmp = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
					if($rowtmp=pg_fetch_array($stmtmp)){
                    print "/ ";
						continue;
					}
				}
			
//print $insitu[$a]." Vs ".$insitu[$b]."<br>";
                print ". ";

                $words2=(array)null;
				$count=0;
				//recup des regions marquees
				$requete= "SELECT \"REGION_ID\" FROM \"TARGET\" where \"IN_SITU_ID\"=$insitu[$b]";
				$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
				while($row=pg_fetch_array($stmt)){
					$region=$row["REGION_ID"];

					if(! in_array($region,$words2))
						$words2[$count++]=$region;
						
				}
                //ocifreestatement($stmt);

/*/////				//this algo needs to start with the bigger array*/
				
				$sumdist=0;
				
/*/////			if(count($words2)>count($words1)){*/







				for ($cc=0; $cc<count($words2); ++$cc){
				
				    $region=$words2[$cc];
					//recup des parents de chq region de l'is2
					$count=0;
					$wordstwo=(array)null;

/*Hierarchie*/					select_recursif_descendant_2_W2($c,$region,$res);					
					
					//ocifreestatement($stmt2);

                    $bestdist=1;
                    for($k=0; $k<count($words1); ++$k){
					    $wordsone=(array)null;
						$count=0;
					    $region=$words1[$k];

						//recup des parents de chq region de l'is1
/*Hierarchie*/						select_recursif_descendant_2_W1($c,$region,$res);
						//ocifreestatement($stmt2);
					
					   //comparaison des 2 tableaux de mots pour calculer les communs et les specifiques
					   $commun=0; //$OBJcommun=0;
					   $spec1=""; $spec2="";$dice="";
					   reset($wordsone); reset($wordstwo);
					   for($x=0; $x<count($wordsone); ++$x){
						   for($y=0; $y<count($wordstwo); ++$y){
							   if($wordsone[$x] == $wordstwo[$y]){
								   ++$commun;
							   }
						   }
					   }
					   #calcul de la distance de Dice-Sorenson
					   $spec1=count($wordsone)-$commun;
					   $spec2=count($wordstwo)-$commun;
					   //$dice=($spec1+$spec2)/($spec1+$spec2+$commun+$commun);
                       $dice=($spec1/($spec1+$commun))+($spec2/($spec2+$commun));//test pour une alternative
//print "communs: $commun - spec1: $spec1 - spec2: $spec2 ==> $dice<br>";					
					   if($bestdist>$dice)
					       $bestdist=$dice;
					}
                    $sumdist+=$bestdist;

				}
			    $dice1=$sumdist/count($words2);
			   
			   
/*/////			}else{*/
			   
			   
			   
			   
			   
			    $sumdist=0;
				for ($cc=0; $cc<count($words1); ++$cc){
				
				    $region=$words1[$cc];
					//recup des parents de chq region de l'is2
					
/*Hierarchie*/					select_recursif_descendant_2_W2($c,$region,$res);
						
					//ocifreestatement($stmt2);

                    $bestdist=1;
                    for($k=0; $k<count($words2); ++$k){
					    $wordsone=(array)null;
						$count=0;
					    $region=$words2[$k];

/*Hierarchie*/					select_recursif_descendant_2_W1($c,$region,$res);
						
						//ocifreestatement($stmt2);
					
					   //comparaison des 2 tableaux de mots pour calculer les communs et les specifiques
					   $commun=0; //$OBJcommun=0;
					   $spec1=""; $spec2="";$dice="";
					   reset($wordsone); reset($wordstwo);
					   for($x=0; $x<count($wordsone); ++$x){
						   for($y=0; $y<count($wordstwo); ++$y){
							   if($wordsone[$x] == $wordstwo[$y]){
								   ++$commun;
							   }
						   }
					   }
					   #calcul de la distance de Dice-Sorenson
					   $spec1=count($wordsone)-$commun;
					   $spec2=count($wordstwo)-$commun;
					   //$dice=($spec1+$spec2)/($spec1+$spec2+$commun+$commun);
					   $dice=($spec1/($spec1+$commun))+($spec2/($spec2+$commun));//test pour une alternative

//print "communs: $commun - spec1: $spec1 - spec2: $spec2 ==> $dice<br>";					

					   if($bestdist>$dice)
					       $bestdist=$dice;
					}
                    $sumdist+=$bestdist;

				}
			    $dice2=$sumdist/count($words1);
			    
			   
			   
			   
/*////			   }*/
			   
			   $dice=($dice1+$dice2)/2;
			   print "$dice1  $dice2 =+> $dice<br>";
				//enregistrement des donnees
				if( ($insitu[$a] != $insitu[$b])and($testmode == 'n')and($dice < $maxdist) ){
					$coucounter+=2;
					
/*Init de la Transaction*/pg_query($c,"begin");
					$requete= "select * from \"IN_SITU_CLUSTER3\" where \"IN_SITU_ID\"=$insitu[$a] and \"IN_SITU_ID_2\"=$insitu[$b] and \"TYPE\"='st'";
					$stmt22 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
					if($row22=pg_fetch_array($stmt22)){
						$requete= "update \"IN_SITU_CLUSTER3\" set \"DISTANCE\"=$dice where \"IN_SITU_ID\"=$insitu[$a] and \"IN_SITU_ID_2\"=$insitu[$b] and \"TYPE\"='st'";
						
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
														
						$requete= "update \"IN_SITU_CLUSTER3\" set \"DISTANCE\"=$dice where \"IN_SITU_ID\"=$insitu[$b] and \"IN_SITU_ID_2\"=$insitu[$a] and \"TYPE\"='st'";
						
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						

					}else{
						$requete= "INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\", \"IN_SITU_ID_2\", \"DISTANCE\", \"TYPE\") values ($insitu[$a], $insitu[$b], $dice, 'st' )";
						
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						
						$requete= "INSERT INTO \"IN_SITU_CLUSTER3\" (\"IN_SITU_ID\", \"IN_SITU_ID_2\", \"DISTANCE\", \"TYPE\") values ($insitu[$b], $insitu[$a], $dice, 'st' )";
						
						$stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
						
					}
				}

/*/////////////////////////////////////////////////////////////////////////////////////////////////*/

			}
		}
		
		
	}
}



// Validation de la transaction
pg_query($c,"commit");
	

	print "<br><center><H1>Done</H1></center>";


print '<br><br><center><input name="Exit" type="button" value="-= Close window =-" onClick="javascript:self.close();"></center>';
?>

<?php
    include("frames_footer.php");
?>