/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.cnr.iasi.giant.util;

import it.cnr.iasi.giant.algorithm.distance.Dijkstra;
import it.cnr.iasi.giant.algorithm.distance.GraphUtil;
import it.cnr.iasi.giant.thread.NetworkLoaderThread;
import java.io.IOException;
import java.util.HashMap;
import net.sf.javaml.core.Dataset;

/**
 *
 * @author Fabio Cumbo
 * @info GIANT
 * @version v1.0
 * @organization IASI-CNR : Istituto di Analisi dei Sistemi ed Informatica 
 *                  "Antonio Ruberti" - Consiglio Nazionale delle Ricerche
 * 
 */
public class ClusteringUtil {
    
    private static boolean matrix = false;
    private static String lastMetric = "NULL";
    private static HashMap<Integer, Dataset[]> clusters = null;
    private static String tmpDistanceFile = "NULL";
    private static int[][] distanceMatrix = null;

    public static HashMap<Integer, Dataset[]> getClusters() {
        return clusters;
    }

    public static String getLastMetric() {
        return lastMetric;
    }

    public static boolean isMatrix() {
        return matrix;
    }

    public static String getTmpDistanceFile() {
        return tmpDistanceFile;
    }

    public static void setClusters(HashMap<Integer, Dataset[]> clusters) {
        ClusteringUtil.clusters = clusters;
    }

    public static void setLastMetric(String lastMetric) {
        ClusteringUtil.lastMetric = lastMetric;
    }

    public static void setMatrix(boolean matrix) {
        ClusteringUtil.matrix = matrix;
    }

    public static void setTmpDistanceFile(String tmpDistanceFile) {
        ClusteringUtil.tmpDistanceFile = tmpDistanceFile;
    }
    
    public static int[][] getDistMatrix() {
        return distanceMatrix;
    }
    
    public static int[][] getDistanceMatrix(String distance, GraphUtil graph, double[][] adjMatrix) throws IOException {
            int[][] result = new int[NetworkLoaderThread.getDegreeMap().keySet().size()][NetworkLoaderThread.getDegreeMap().keySet().size()];    
            if (distance.equals("SP")) {
                for (int i=0; i<graph.vertices(); i++) {
                        int[] dist = Dijkstra.dijkstra_function(graph, i);
                        for (int j=i; j<dist.length; j++) {
                            result[i][j] = dist[j];
                            result[j][i] = dist[j];
                            System.out.print(dist[j] + "  ");
                        }
                        System.out.println();
                    }
            }
            else if (distance.equals("HAMMING")) {
                for (int i=0; i<NetworkLoaderThread.getDegreeMap().keySet().size(); i++) {
                        for (int j=i; j<NetworkLoaderThread.getDegreeMap().keySet().size(); j++) {
                            int hamm = 0;
                            if (i != j) {
                                for (int k=0; k<NetworkLoaderThread.getDegreeMap().keySet().size(); k++) {
                                    if (adjMatrix[k][i] != adjMatrix[k][j])
                                        hamm++;
                                }
                            }
                            result[i][j]=hamm;
                            result[j][i]=hamm;
                            System.out.print(hamm + " ");
                        }
                        System.out.println();
                    }
            }
            
            distanceMatrix = result;
            return result;
    }
    
}
