package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.anomalydetectors;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;

import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Packet;
import weka.classifiers.trees.J48;
import weka.clusterers.SimpleKMeans;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.SelectedTag;

public class DecisionTreeAnomalyDetector extends BaseAnomalyDetector {

	/**
	 * Classifier
	 */
	private J48 decisionTree;
	
	public DecisionTreeAnomalyDetector() {
		super();
		// Initialize Classifier
		decisionTree = new J48();
		//decisionTree.setSeed(42);
		
	}
	/**
	 * Try to classify the given packets and detect anomalies
	 * @param packets packets to be classified
	 */
	@Override
	public boolean classifyInstance(Instance instance) throws Exception {
		decisionTree.classifyInstance(instance);
		return true;		
	}
	
	/**
	 * Train the model using the given instances
	 * @param instances training set, which should be learned
	 * 
	 */
	@Override
	public void trainModel(Instances instances) {
		try {
			instances.setClassIndex(1);
			decisionTree.buildClassifier(instances);
			//decisionTree.setOptions(new String[] {"-t", "./data/test.arff", "-d", "DTClassifier.model"});
		
		} catch (Exception e) {
			System.out.println("Decision Tree classifing failed");
			e.printStackTrace();
		}
	}
	

	@Override
	public void saveModel() throws Exception {
		weka.core.SerializationHelper.write("./data/models/DT", decisionTree);
	}
}
