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.functions.SMO;
import weka.core.Instance;
import weka.core.Instances;

public class SVMAnomalyDetector extends BaseAnomalyDetector {


	/**
	 * Classifier
	 */
	private SMO svm;
	
	/**
	 * Try to classify the given packets and detect anomalies
	 * @param packets packets to be classified
	 */
	@Override
	public boolean classifyInstance(Instance instance) throws Exception {
		svm.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 {
			svm.buildClassifier(instances);
		} catch (Exception e) {
			System.out.println("SVM classifing failed");
			e.printStackTrace();
		}
	}
	

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