package learn.struct.it;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import dataset.Dataset;
import dataset.DatasetType;
import dataset.FileHandler;
import dataset.adtree.ADNode;

public class TestOfBayesianTest {
	private static String dir = "/home/fschluter/experiments/datasets/sampled/undirected/mpl";
	private static String datasetName = "generatingModel2.n5";
	private static Dataset dataset;
	private static String trainSufix = ".ts.data";
	private static List<Integer> Ds = Arrays.asList(10); //, 100, 1000, 10000);
	private static boolean useADTree;
	private static int maxCondSize = 4;

	public static void main(String[] args) {
		parseParameters(args);

		for (int D : Ds) {
			System.out.println("# D = " + D);
			DatasetType type = DatasetType.CSV;
			File fileDs = new java.io.File(dir + File.separator + datasetName
					+ trainSufix);
			dataset = FileHandler.readDataset(fileDs, D, type);

			int n = dataset.getDomainVariables().size();
			int[] card = new int[n];
			for (int i = 0; i < n; ++i)
				card[i] = 2;
			dataset.setCard(card);

			for (int x = 0; x < n - 1; x++) {
				for (int y = x + 1; y < n; y++) {
					List<Integer> complement = dataset.getDomainVariables();
					complement.remove((Integer) x);
					complement.remove((Integer) y);
					for (int k = 0; k < maxCondSize; k++) {
						List<Integer> z = new ArrayList<Integer>();
						for (int l = 0; l < k; l++) {
							z.add(complement.get(l));
						}
						runTest(x, y, z);
					}
				}
			}
		}

	}

	/**
	 * Runs a test for severals subsets of the original dataset, in each
	 * iteration incorporates more datapoints
	 * 
	 * @param x
	 * @param y
	 * @param z
	 */
	private static void runTest(int x, int y,
			List<Integer> z) {
		System.out.println("x " +x + " y " + y + " z " + z);
		BayesianTest test = new BayesianTest();
		test.setUseADTree(useADTree);
		if(useADTree) {
			ADNode aDTree = ADNode.makeADNode(dataset);
			test.setADTree(aDTree);
		}

		List<Integer> xList = new ArrayList<Integer>();
		xList.add(x);
		List<Integer> yList = new ArrayList<Integer>();
		yList.add(y);
		test.independent(dataset, xList,yList,z);
		System.out.println(Arrays.toString(test.getLoglikelihoods()));
	}

	private static void parseParameters(String[] args) {
		for (int i = 0; i < args.length; i++) {
			if (args[i].equals("-dir")) {
				dir = args[i + 1];
			} else if (args[i].equals("-Ds")) {
				Ds = new ArrayList<Integer>();
				int dIndex = i + 1;
				while (!args[dIndex].contains("-") || dIndex == args.length - 1) {
					Ds.add(new Integer(args[dIndex]));
					++dIndex;
				}
			} else if (args[i].equals("-dataset")) {
				datasetName = args[i + 1];
			} else if (args[i].equals("-trainSufix")) {
				trainSufix = args[i + 1];
			} else if (args[i].equals("-useADTree")) {
				useADTree = new Boolean(args[i + 1]);
			}
		}
	}

}
