package bicat.run_machine;

import javax.swing.JOptionPane;

import bicat.gui.BicatGui;
import java.io.File;
/**
 * <p>
 * Title: BicAT Tool
 * </p>
 * <p>
 * Copyright: Copyright (c) 2004
 * </p>
 * 
 * @author Amela Prelic
 * @version 1.0
 * 
 * Run Machine for Cheng & Church algorithm (2000).
 * 
 * For its execution, a shared library is needed (lib/linux/libcc.so for Linux).
 * 
 */

public class RunMachine_Bicluster_Find extends RunMachine {

	// ===========================================================================
	// public RunMachine_CC() { }
	public RunMachine_Bicluster_Find(BicatGui o) {
		owner = o;
	}

	// ===========================================================================
	// public native void displayHelloWorld(); // Native Testmethode

	// public native void runCC();
	public native void run_Bicluster_Find(double[] data, int noise_Th, int pr,
	// String[] g_names, String[] c_names,
			int nc, int po,char model_Type);

	// get the results from a .temp file in the current directory (this file
	// will be removed)
	// public native Object[] get_biclusters_CC();

//        public native void run_Bicluster_Find(String s1,String s2,String s3,String s4,String s5   );
//        
	static {
	    
            //System.loadLibrary("BiclusterFind.mexw32");
            System.out.println("java lib>>>"+ System.getProperty("java.library.path"));
            //System.load("D:\\Bicat\\BiclusterFind.mexw32");    
            //System.setProperty("java.library.path", "C:/Documents and Settings/Mohamed Hamed/My Documents/NetBeansProjects/Bicat");
            System.loadLibrary("BiclusterFind");
            //System.loadLibrary("getGeneMerge.bat");
           // System.load("perl.exe");
////            try { 
////              //Runtime.getRuntime().exec( owner.currentDirectoryPath +"/GeneMerge/getGeneMerge.bat" );
////            //  System.getProperty(key)
////                //System.setProperty("java.library.path", owner.currentDirectoryPath+"\\genemerge");
////                //works//Runtime.getRuntime().exec(new String [] {"cmd" ,"/c", "start","getgenemerge","genemerge/S_cerevisiae.BP","genemerge/GO.BP.use","genemerge/pop.txt","genemerge/testc.txt","genemerge/testout.out"});
////             // works Runtime.getRuntime().exec(new String [] {"cmd" ,"/c", "start","getgenemerge","S_cerevisiae.BP","GO.BP.use","pop.txt","testc.txt","testout.out"},null,new File(owner.currentDirectoryPath+"/genemerge"));//,"genemerge/S_cerevisiae.BP","genemerge/GO.BP.use","genemerge/pop.txt","genemerge/testc.txt","genemerge/testout.out"});
////             //Runtime.getRuntime().exec(new String [] {"cmd" ,"/c", "start","getgenemerge","S_cerevisiae.BP","GO.BP.use","pop.txt","testc.txt",owner.currentDirectoryPath+"/"+owner.GO_DIRECTORY+"/testout.out"},null,new File(owner.currentDirectoryPath+"/genemerge"));//,"genemerge/S_cerevisiae.BP","genemerge/GO.BP.use","genemerge/pop.txt","genemerge/testc.txt","genemerge/testout.out"});
////                // Runtime.getRuntime().exec(new String [] {"cmd.exe" ,"/E:1900","/c", "start","getgenemerge"},new String []{" "},new File (owner.currentDirectoryPath+"\\genemerge"));
////              //works Runtime.getRuntime().exec("cmd /c start getGeneMerge1.bat"); 
////                // Runtime.getRuntime().exec("start"+ " cmd /c "+ "D:\\Master_nottingham\\work\\Bicat\\GeneMerge\\getGeneMerge.bat" );
////               //Runtime.getRuntime().exec( "notepad" );                
////            }catch(Exception ex)
////            {
////                ex.printStackTrace();
////                
////            }
           // System.load(owner.currentDirectoryPath + "/geneMerge/GeneMerge1.2.pl");
            //System.load("D:\\Master_nottingham\\work\\Bicat\\GeneMerge\\getGeneMerge.bat");
	    System.out.println("BiclusterFind library loaded");
	
        }

	public synchronized void runBiclustering(final Arguments_Bicluster_Find bicluster_Finda) {

		// Before calling the Native Method (CC), prepare the calling arguments
		// the parameters needed for CC can be retrieved by the owner:
		// data, tg, tc, seed, delta, alpha, pr1-3, logarithm, randomize, n,
		// out_file

		// =======================================================================
		// ???
		// Sa SwingWorker of BiMax ??
		// start CC in a separate thread

		final SwingWorker worker = new SwingWorker() {

			public Object construct() {
				// new RunMachineCC(null).runCC();
				if (BicatGui.debug)System.out.println("Noise Th : "+ bicluster_Finda.getNoise_Th() +" ,  PR: "+  bicluster_Finda.getPr());
				
				run_Bicluster_Find(bicluster_Finda.getData(), bicluster_Finda.getNoise_Th(), bicluster_Finda.getPr(),
						bicluster_Finda.getNc(), bicluster_Finda.getPo(), bicluster_Finda.getModel_Type());

				if (owner.debug) {
					System.out.println("Started bicluster in a new thread!\n");
				}

				return null;
			}

			public void finished() {

				System.out.println("Getting results from bicluster_find... "
						+ owner.currentDirectoryPath + "/output.bicluster_find");

				computedBiclusters = getOutputBiclusters(owner.currentDirectoryPath
						+ "/output.bicluster_find");
								
				outputBiclusters = computedBiclusters; // is a LinkedList of
				// BitSet
				// representated-BCs
				// (bicat.runMachine.Bicluster_bitset)
				
				transferList();

				owner.finishUpRun(bicluster_Finda.getDatasetIdx(), outputBiclusters, bicluster_Finda
						.getPreprocessOptions(), "bicluster_find");
				JOptionPane
						.showMessageDialog(
								null,
								"Calculations finished.\nThe results can be found in the bicluster results section of the current dataset.");

			}
		};
		worker.start();
	}
}
