/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Jgrapht;

import gr.forth.ics.graph.Node;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.jgrapht.DirectedGraph;
import org.jgrapht.UndirectedGraph;
import org.jgrapht.graph.DefaultDirectedGraph;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 *
 * @author martin
 */
public class JGraphT {
    
    public JGraphT() {
    }
    
    @BeforeClass
    public static void setUpClass() {
    }
    
    @AfterClass
    public static void tearDownClass() {
    }
    
    @Before
    public void setUp() {
    }
    
    @After
    public void tearDown() {
    }
    
        @Test
    public void benchUnDirected(){
        UndirectedGraph<Object, DefaultEdge> graph =
            new SimpleGraph<Object, DefaultEdge>(DefaultEdge.class);
        
         long a = System.currentTimeMillis();
         List<Integer> li = new ArrayList<>();
         for (int i=0;i<1000;i++){
             li.add(i);
             graph.addVertex(i);
         }
         
         long b = System.currentTimeMillis();
         List<String> ls = new ArrayList<>();
         for (int i=1000;i<2000;i++){
             ls.add(Integer.toString(i));
             graph.addVertex(Integer.toString(i));
         }
         
         long c = System.currentTimeMillis();
         for (String s : ls)
             for (Integer i : li)
                 graph.addEdge(s, i);
                  
         long d = System.currentTimeMillis();
         for (String s : ls)
             for (DefaultEdge i : graph.edgesOf(s)){
                 graph.getEdgeTarget(i);
             }
         
         long f = System.currentTimeMillis();
         graph.addVertex("chien");
         for (String s : ls)
             graph.addEdge(s,"chien");
         
         long g = System.currentTimeMillis();
//         for (String s : ls)
//             for (DefaultEdge i : graph.edgesOf(s))
//                 if ("chien".equals(graph.getEdgeTarget(i)))
//                     graph.removeEdge(i);
         
         long h = System.currentTimeMillis();
                 
            System.out.println("udirected");
         System.out.println("insert "+ls.size()+" int : "+(b-a)+"ms");
         System.out.println("insert "+li.size()+" str : "+(c-b)+"ms");
         System.out.println("link all : "+(d-c)+"ms");
         System.out.println("iterate adjacent of all int : "+(f-d)+"ms");
         System.out.println("for all int insert chien : "+(g-f)+"ms");
         System.out.println("find all chien and destroy : "+(h-g)+"ms");
    }
    
    @Test
    public void benchDirected(){
        DirectedGraph<Object, DefaultEdge> graph =
            new DefaultDirectedGraph<Object, DefaultEdge>(DefaultEdge.class);
        
         long a = System.currentTimeMillis();
         List<Integer> li = new ArrayList<>();
         for (int i=0;i<1000;i++){
             li.add(i);
             graph.addVertex(i);
         }
         
         long b = System.currentTimeMillis();
         List<String> ls = new ArrayList<>();
         for (int i=1000;i<2000;i++){
             ls.add(Integer.toString(i));
             graph.addVertex(Integer.toString(i));
         }
         
         long c = System.currentTimeMillis();
         for (String s : ls)
             for (Integer i : li)
                 graph.addEdge(s, i);
                  
         long d = System.currentTimeMillis();
         for (String s : ls)
             for (DefaultEdge i : graph.edgesOf(s)){
                 graph.getEdgeTarget(i);
             }
         
         long f = System.currentTimeMillis();
         graph.addVertex("chien");
         for (String s : ls)
             graph.addEdge(s,"chien");
         
         long g = System.currentTimeMillis();
         for (String s : ls)
             for (DefaultEdge i : graph.edgesOf(s))
                 if ("chien".equals(graph.getEdgeTarget(i)))
                     graph.removeEdge(i);
         
         long h = System.currentTimeMillis();
                 
         System.out.println("directed");
         System.out.println("insert "+ls.size()+" int : "+(b-a)+"ms");
         System.out.println("insert "+li.size()+" str : "+(c-b)+"ms");
         System.out.println("link all : "+(d-c)+"ms");
         System.out.println("iterate adjacent of all int : "+(f-d)+"ms");
         System.out.println("for all int insert chien : "+(g-f)+"ms");
         System.out.println("find all chien and destroy : "+(h-g)+"ms");
    }
    
    @Test
    public void directed(){
        DirectedGraph<URL, DefaultEdge> g =
            new DefaultDirectedGraph<URL, DefaultEdge>(DefaultEdge.class);

        try {
            URL amazon = new URL("http://www.amazon.com");
            URL yahoo = new URL("http://www.yahoo.com");
            URL ebay = new URL("http://www.ebay.com");

            // add the vertices
            g.addVertex(amazon);
            g.addVertex(yahoo);
            g.addVertex(ebay);

            // add edges to create linking structure
            g.addEdge(yahoo, amazon);
            g.addEdge(yahoo, ebay);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    
    @Test
    public void undirected(){
        UndirectedGraph<String, DefaultEdge> g =
            new SimpleGraph<String, DefaultEdge>(DefaultEdge.class);

        String v1 = "v1";
        String v2 = "v2";
        String v3 = "v3";
        String v4 = "v4";

        // add the vertices
        g.addVertex(v1);
        g.addVertex(v2);
        g.addVertex(v3);
        g.addVertex(v4);

        // add edges to create a circuit
        g.addEdge(v1, v2);
        g.addEdge(v2, v3);
        g.addEdge(v3, v4);
        g.addEdge(v4, v1);

    }
}
