import org.graphstream.graph.Edge;
import org.graphstream.graph.Graph;
import org.graphstream.graph.Node;
import org.graphstream.graph.implementations.DefaultGraph;
import org.graphstream.stream.file.FileSourceEdge;

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

public class CalculEpidemiqueSimulation3 {
    public static void main(String[] args) {
        String filePath = "src/data.txt";
        Graph g = new DefaultGraph("g");
        FileSourceEdge fs = new FileSourceEdge(true);

        fs.addSink(g);

        try {
            fs.begin(filePath);

            while (fs.nextEvents()) {
                // Optionally some code here ...
            }
        } catch( IOException e) {
            e.printStackTrace();
        }

        try {
            fs.end();
        } catch( IOException e) {
            e.printStackTrace();
        } finally {
            fs.removeSink(g);
        }

        List<Node> lstNodeSuppr = new ArrayList<>();

        for(Node n : g){
            if(Math.random() < 1/2.0){
                if(!n.hasAttribute("etat") || n.hasAttribute("etat") && !n.getAttribute("etat").equals("Immuniser"))
                    n.addAttribute("etat","susceptible");
                if(n.getLeavingEdgeSet().size() > 0){
                    int voisinChoisi = (int)(Math.random()*n.getLeavingEdgeSet().size());
                    Edge[] tabEdge = new Edge[n.getLeavingEdgeSet().size()];
                    n.getLeavingEdgeSet().toArray(tabEdge);
                    tabEdge[voisinChoisi].getOpposite(n).setAttribute("etat","Immuniser");
                    lstNodeSuppr.add(n);
                }

            }else{
                n.addAttribute("etat","susceptible");
            }

        }
        for(Node n : lstNodeSuppr){
            g.removeNode(n);
        }
        int nbDegree = 0;
        int nbDegreeCarre = 0;
        for(Node n:g){
            nbDegree += n.getDegree();
            nbDegreeCarre += Math.pow(n.getDegree(), 2);
        }
        double degreeMoyen = nbDegree/(double)g.getNodeCount();
        double degreeMoyenCarre = nbDegreeCarre/(double)g.getNodeCount();
        System.out.println("<k> = " + degreeMoyen );
        System.out.println("<k²> = " + degreeMoyenCarre );
        System.out.println("<k>/<k²> = " + degreeMoyen/degreeMoyenCarre );
    }
}

