<commit_before>package net.sf.markov4jmeter.behaviormodelextractor.extraction.transformation.clustering;

import java.io.File;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

import weka.clusterers.SimpleKMeans;
import weka.core.Attribute;
import weka.core.DistanceFunction;
import weka.core.EuclideanDistance;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.filters.Filter;
import net.sf.markov4jmeter.behavior.BehaviorMix;
import net.sf.markov4jmeter.behavior.BehaviorMixEntry;
import net.sf.markov4jmeter.behavior.BehaviorModelAbsolute;
import net.sf.markov4jmeter.behavior.BehaviorModelRelative;
import net.sf.markov4jmeter.behavior.Transition;
import net.sf.markov4jmeter.behavior.UseCase;
import net.sf.markov4jmeter.behavior.UseCaseRepository;
import net.sf.markov4jmeter.behavior.Vertex;
import net.sf.markov4jmeter.behaviormodelextractor.CommandLineArgumentsHandler;
import net.sf.markov4jmeter.behaviormodelextractor.extraction.ExtractionException;
import net.sf.markov4jmeter.behaviormodelextractor.extraction.transformation.ABMToRBMTransformer;

/**
 * This class represents a <i>Menasc-based</i> clustering strategy.
 * 
 * @author <NAME> (<EMAIL>)
 * @version 1.0
 */
public class KMeansClusteringStrategy extends AbstractClusteringStrategy {	
	
	private double sampleMeanInterCluster = 0;
	private double sampleVarianceInterCluster = 0;
	private double sampleCoefficientOfVarianceInterCluster = 0;
	private double sampleMeanIntraCluster = 0;
	private double sampleVarianceIntraCluster = 0;
	private double sampleCoefficientOfVarianceIntraCluster = 0;
	private double betacv = 0;
	private double betavar = 0;

	/* ************************** public methods ************************** */

	/**
	 * {@inheritDoc}
	 * 
	 * <p>
	 * This method is specialized for <b>Menasc-based</b> clustering.
	 */
    @Override
    public BehaviorMix apply (
            final BehaviorModelAbsolute[] behaviorModelsAbsolute,
            final UseCaseRepository useCaseRepository) {

        final ABMToRBMTransformer abmToRbmTransformer =
                new ABMToRBMTransformer();
        
		// Behavior Mix to be returned;
		final BehaviorMix behaviorMix = this.createBehaviorMix();

		try {

			// Returns a valid instances set, generated based on the absolut
			// behavior models
			Instances instances = getInstances(behaviorModelsAbsolute); 
			
			// save input data as arff file. This arff file can be opened with weka application.
			ArffSaver saver = new ArffSaver();
			saver.setInstances(instances);
			saver.setFile(new File(CommandLineArgumentsHandler.getOutputDirectory() + "/data_clustering.arff"));
			saver.writeBatch();	
		
			// KMeans --> Weka
			SimpleKMeans kmeans = new SimpleKMeans();
		
			kmeans.setDontReplaceMissingValues(false);
			kmeans.setPreserveInstancesOrder(true);
			
			DistanceFunction euclideanDistance = new EuclideanDistance();		
			String[] options = new String[1];
			options[0] = "-D";
			euclideanDistance.setInstances(instances);
			euclideanDistance.setOptions(options);			
			kmeans.setDistanceFunction(euclideanDistance);
			
			int[] clustersize = null;
			int[] assignments = null;
			
			System.out.println("Cluster" + ";" +
					"SqrtError" + ";" +
					"sampleMeanInterCluster" + ";" +
					"sampleVarianceInterCluster" + ";" +
					"sampleCoefficientOfVarianceInterCluster" + ";" +
					"sampleMeanIntraCluster" + ";" +
					"sampleVarianceIntraCluster" + ";" +
					"sampleCoefficientOfVarianceIntraCluster" + ";" +
					"betavar" + ";" +
					"betacv"
					); 
			
			// TODO : Metriken berechnen: AVG Session length, Histogramm TreeMap
		
			for (int cnt = 3; cnt <= 16; cnt++) {
				// must be specified in a fix way
				kmeans.setNumClusters(cnt);

				// build cluster<commit_msg>rename Menasce Clustering to kmeansclustering <commit_after>package net.sf.markov4jmeter.behaviormodelextractor.extraction.transformation.clustering;

import java.io.File;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

import weka.clusterers.SimpleKMeans;
import weka.core.Attribute;
import weka.core.DistanceFunction;
import weka.core.EuclideanDistance;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.filters.Filter;
import net.sf.markov4jmeter.behavior.BehaviorMix;
import net.sf.markov4jmeter.behavior.BehaviorMixEntry;
import net.sf.markov4jmeter.behavior.BehaviorModelAbsolute;
import net.sf.markov4jmeter.behavior.BehaviorModelRelative;
import net.sf.markov4jmeter.behavior.Transition;
import net.sf.markov4jmeter.behavior.UseCase;
import net.sf.markov4jmeter.behavior.UseCaseRepository;
import net.sf.markov4jmeter.behavior.Vertex;
import net.sf.markov4jmeter.behaviormodelextractor.CommandLineArgumentsHandler;
import net.sf.markov4jmeter.behaviormodelextractor.extraction.ExtractionException;
import net.sf.markov4jmeter.behaviormodelextractor.extraction.transformation.ABMToRBMTransformer;

/**
 * This class represents a <i>Menasc-based</i> clustering strategy.
 * 
 * @author <NAME> (<EMAIL>)
 * @version 1.0
 */
public class KMeansClusteringStrategy extends AbstractClusteringStrategy {	
	
	private double sampleMeanInterCluster = 0;
	private double sampleVarianceInterCluster = 0;
	private double sampleCoefficientOfVarianceInterCluster = 0;
	private double sampleMeanIntraCluster = 0;
	private double sampleVarianceIntraCluster = 0;
	private double sampleCoefficientOfVarianceIntraCluster = 0;
	private double betacv = 0;
	private double betavar = 0;

	/* ************************** public methods ************************** */

	/**
	 * {@inheritDoc}
	 * 
	 * <p>
	 * This method is specialized for <b>Menasc-based</b> clustering.
	 */
    @Override
    public BehaviorMix apply (
            final BehaviorModelAbsolute[] behaviorModelsAbsolute,
            final UseCaseRepository useCaseRepository) {

        final ABMToRBMTransformer abmToRbmTransformer =
                new ABMToRBMTransformer();
        
		// Behavior Mix to be returned;
		final BehaviorMix behaviorMix = this.createBehaviorMix();

		try {

			// Returns a valid instances set, generated based on the absolut
			// behavior models
			Instances instances = getInstances(behaviorModelsAbsolute); 
			
			// save input data as arff file. This arff file can be opened with weka application.
			ArffSaver saver = new ArffSaver();
			saver.setInstances(instances);
			saver.setFile(new File(CommandLineArgumentsHandler.getOutputDirectory() + "/data_clustering.arff"));
			saver.writeBatch();	
		
			// KMeans --> Weka
			SimpleKMeans kmeans = new SimpleKMeans();
		
			kmeans.setDontReplaceMissingValues(false);
			kmeans.setPreserveInstancesOrder(true);
			
			DistanceFunction euclideanDistance = new EuclideanDistance();		
			String[] options = new String[1];
			options[0] = "-D";
			euclideanDistance.setInstances(instances);
			euclideanDistance.setOptions(options);			
			kmeans.setDistanceFunction(euclideanDistance);
			
			int[] clustersize = null;
			int[] assignments = null;
			
			System.out.println("Cluster" + ";" +
					"SqrtError" + ";" +
					"sampleMeanInterCluster" + ";" +
					"sampleVarianceInterCluster" + ";" +
					"sampleCoefficientOfVarianceInterCluster" + ";" +
					"sampleMeanIntraCluster" + ";" +
					"sampleVarianceIntraCluster" + ";" +
					"sampleCoefficientOfVarianceIntraCluster" + ";" +
					"betavar" + ";" +
					"betacv"
					); 
			
			// TODO : Metriken berechnen: AVG Session length, Histogramm TreeMap
		
			for (int cnt = 6; cnt <= 6; cnt++) {
				// must be specified in a fix way
				kmeans.setNumClusters(cnt);

				// build cluster
{'fraction_non_alphanumeric': 0.11171310629514963, 'fraction_numerical': 0.006836945304437564, 'mean_word_length': 4.346896551724138, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7065547', 'n_tokens_mistral': 2781, 'n_tokens_neox': 2361, 'n_words': 476}