package com.github.oxal;

import org.graphstream.algorithm.Toolkit;
import org.graphstream.graph.Graph;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.graph.implementations.SingleGraph;
import org.graphstream.stream.file.FileSource;
import org.graphstream.stream.file.FileSourceEdge;
import org.graphstream.stream.file.FileSourceFactory;
import org.jfree.chart.plot.XYPlot;

import java.io.*;
import java.util.Locale;
import java.util.stream.Collector;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {
        String filePath = "src/main/resources/graph.txt";
        Graph g = new DefaultGraph("g");
        FileSource fs = new FileSourceEdge();

        fs.addSink(g);

        try {
            fs.readAll(filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //nb de node
        System.out.println("nb node " + g.getNodeCount());

        //nb de edge
        System.out.println("nb edge " + g.getEdgeCount());


        //moyenne de degré
        System.out.println("moy degre " + Toolkit.averageDegree(g));

        /**
         *
         System.out.println(g.getNodeSet()
         .stream()
         .mapToDouble(n -> n.getDegree())
         .average()
         .getAsDouble()
         );
         */

        //coef cluster
        System.out.println("coef cluster " + Toolkit.averageClusteringCoefficient(g));


        //connexe
        System.out.println("connexe " + Toolkit.isConnected(g));


        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter("degres.txt"));
            int[] dd = Toolkit.degreeDistribution(g);
            for (int k = 0; k < dd.length; k++) {
                if (dd[k] != 0) {

                    String str = String.format("%d\t%f\n", k, (double)dd[k] / g.getNodeCount()).replace(",", ".");
                    writer.write(str);
                }
            }


            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }




    }
}
