package bicat.run_machine;

import javax.swing.JOptionPane;

import bicat.gui.BicatGui;

/**
 * <p>
 * Title: BicAT Tool
 * </p>
 * <p>
 * Copyright: Copyright (c) 2005
 * </p>
 * 
 * @author Simon Barkow
 * @version 1.0
 * 
 * Run Machine for (native) bimax algorithm (2000).
 * 
 * For its execution, a shared library is needed (lib/linux/libcc.so for Linux).
 * 
 */

public class RunMachine_Plaid extends RunMachine {

	// ===========================================================================
	public RunMachine_Plaid(BicatGui o) {
		owner = o;
	}

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

	public native void run_Plaid(int[] data);

	// get the results from a .temp file in the current directory (this file
	// will be removed)

	static {
		System.load(owner.currentDirectoryPath + "/Plaid.exe");
		System.out.println("Plaid library loaded");
	}

	public synchronized void runBiclustering(final Arguments_Plaid plaida) {

		// Before calling the Native Method (plaid), prepare the calling
		// arguments
		// the parameters needed for bimax can be retrieved by the owner:

		// =======================================================================

		// Sa SwingWorker of BiMax ??
		// start bimax in a separate thread

		final SwingWorker worker = new SwingWorker() {

			public Object construct() {
				if (BicatGui.debug)
			//System.out.println("Number of array: ");
				run_Plaid(plaida.getBinaryDataAsVector());

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

				return null;
			}

			public void finished() {

				System.out.println("Getting results from Plaid... "
						+ owner.currentDirectoryPath + "/plaid.out");

				computedBiclusters = getOutputBiclusters(owner.currentDirectoryPath
						+ "/plaid.out");

				outputBiclusters = computedBiclusters;
				// is a LinkedList of
				// BitSet
				// representated-BCs
				// (bicat.runMachine.Bicluster_bitset)

				transferList();

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

			}
		};
		worker.start();
	}
}
