package testBench.ids.kdd99.modelHelpers;

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

import testBench.ids.kdd99.Finals;
import weka.classifiers.AbstractClassifier;
import weka.core.Instances;
import weka.core.SerializationHelper;
import weka.core.Utils;

public class ModelFilesHelper {
	public static List<String>  getModelFileNames() {
		final File folder = new File(Finals.MODELS_SAVE_FOLDER);
		return  ModelFilesHelper.getModelFileNames(folder);
	}
	
	public static List<String>  getModelFileNames(final File folder) {
		
		List<String> listOfFileNames = new ArrayList<String>();
		
		String fileName;
	    for (final File fileEntry : folder.listFiles()) {
	        if (fileEntry.isDirectory()) {
	        	continue;
	        } else {
	        	fileName = fileEntry.getName();
	        	if (fileName.endsWith(".model"))
	        	{
	        		
		        	listOfFileNames.add(fileName);
	        	}
	        }
	    }
	    return listOfFileNames;
	}
	
	
	
	public static void saveModels(Instances data, String pModelName
			,String classifierFullName, String optionString) {
		try {
			AbstractClassifier classifier = (AbstractClassifier) Class.forName(classifierFullName).newInstance();
			saveModels(data, pModelName, classifier, optionString);
			
		} catch (Exception e) {
			throw new RuntimeException(e);
			
		}
	}
	
	public static void saveModels(Instances data, String pModelName
			,AbstractClassifier classifier, String optionString) {
		try {
			classifier.setOptions(Utils.splitOptions(optionString));
		classifier.buildClassifier(data); // build classifier

		String modelFullFileName = Finals.MODELS_SAVE_FOLDER + classifier.getClass().getName()
				+ pModelName  + ".model";
		SerializationHelper.write(modelFullFileName, classifier);
		System.out.println("Model ( " +modelFullFileName + ")Saved");
		} catch (Exception e) {
			throw new RuntimeException(e);
			
		}
	}


	
}

@@ -310,16 +310,58 @@
 Helper %7B
+%0A%0A%09static%7B%0A%09%09createModelSaveFolder();%0A%09%7D%0A%0A
 %0A%09public
@@ -1389,24 +1389,209 @@
 %09%09%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09private static void createModelSaveFolder()%0A%09%7B%0A%09%09File modelSaveFolder = new File(Finals.MODELS_SAVE_FOLDER);%0A%09%09if (!modelSaveFolder.exists())%0A%09%09%7B%0A%09%09modelSaveFolder.mkdirs();%0A%09%09%7D%0A%0A%09%7D%0A%0A%0A
 %09public stat
@@ -1709,16 +1709,17 @@
 %09%09try %7B%0A
+%0A
 %09%09%09class
@@ -1772,16 +1772,17 @@
 ng));%0A%09%09
+%09
 classifi
@@ -1828,16 +1828,17 @@
 sifier%0A%0A
+%09
 %09%09String
@@ -1918,16 +1918,17 @@
 tName()%0A
+%09
 %09%09%09%09+ pM
@@ -1951,16 +1951,17 @@
 del%22;%0A%09%09
+%09
 Serializ
@@ -2010,16 +2010,17 @@
 ifier);%0A
+%09
 %09%09System
@@ -2075,20 +2075,25 @@
 aved%22);%0A
+%0A%0A
 %09%09%7D 
+%0A%09%09
 catch (E

refactor code, model save folder mkdir