Commits

Neil Rubens committed d467367 Merge with conflicts

Merge branch 'master' into dev

Conflicts:
.gitignore

Comments (0)

Files changed (6)

 build
-private.xml
+jgrapht-gephi_utilities.jar symlink

dist/jgrapht-gephi_utilities.jar

Binary file modified.

src/org/activeintel/gephi/jgrapht/JGraphT2GephiConvertingFilter.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.activeintel.gephi.jgrapht;
+
+import java.util.HashSet;
+import java.util.Set;
+import org.gephi.filters.spi.ComplexFilter;
+import org.gephi.filters.spi.FilterProperty;
+import org.gephi.graph.api.Edge;
+import org.gephi.graph.api.EdgeIterator;
+import org.gephi.graph.api.Graph;
+import org.gephi.graph.api.GraphModel;
+import org.gephi.graph.api.Node;
+import org.gephi.graph.api.NodeIterable;
+import org.gephi.graph.api.NodeIterator;
+//import org.jgrapht.Graph;
+
+/**
+ * Returns equalent of JGraphT's graph
+ * by filtering the main view of Gephi.
+ * 
+ * @see filter 
+ * 
+ * @author Neil Rubens
+ */
+public class JGraphT2GephiConvertingFilter implements ComplexFilter {
+    /**
+     * JGraphT graph; is used finding 'equivalent' graph in Gephi's view
+     */
+    private org.jgrapht.Graph jGraph;
+    
+    
+    /**
+     * Initializes the class
+     * 
+     * @param jGraph will be used for finding 'equivalent' graph in Gephi's view
+     */
+    public JGraphT2GephiConvertingFilter(org.jgrapht.Graph jGraph){
+        this.jGraph = jGraph;
+    }
+
+    
+    /**
+     * Keeps only the nodes & edges in graph that are also in jGraph
+     * 
+     * @param graph
+     * @return 
+     */
+    @Override
+    public Graph filter(Graph graph) {
+        // Filter out Nodes
+        Set<Integer> jNodesIds = getNodesIds(jGraph.vertexSet().toArray());
+        Node[] gNodes = graph.getNodes().toArray();
+        for (Node gNode : gNodes){
+            //System.out.println("gnodeID: " + gNode.getId() );
+            if (!jNodesIds.contains(gNode.getId())){
+                graph.removeNode(gNode);
+            }
+        }
+        
+        // Filter out Edges
+        Set jEdgesIds = getEdgesIds(jGraph.edgeSet().toArray()); 
+        Edge[] gEdges = graph.getEdges().toArray();
+        for (Edge gEdge: gEdges){
+            if (!jEdgesIds.contains(gEdge.getId())){
+                graph.removeEdge(gEdge);
+            }
+        }
+
+        return graph;
+    }
+    
+    private Set<Integer> getNodesIds(Object[] nodes) {
+        HashSet<Integer> ids = new HashSet<Integer>();
+                
+        for ( int i = 0; i < nodes.length; i++ ){
+            Node node = (Node) nodes[i];
+            ids.add(node.getId());
+        }
+        
+        //System.out.println("node ids: " + ids );
+
+        return ids;
+    }
+    
+
+    private Set<Integer> getEdgesIds(Object[] edges) {
+        HashSet<Integer> ids = new HashSet<Integer>();
+                
+        for ( int i = 0; i < edges.length; i++ ){
+            Edge edge = (Edge) edges[i];
+            ids.add(edge.getId());
+        }
+
+        return ids;
+    }
+    
+    
+
+    @Override
+    public String getName() {
+        return "JGraphT2GephiConvertingFilter";
+    }
+
+    @Override
+    public FilterProperty[] getProperties() {
+        return null;
+    }
+
+
+    
+    
+}

src/org/activeintel/gephi/jgrapht/JGraphTUtils.java

  */
 package org.activeintel.gephi.jgrapht;
 
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 import org.gephi.graph.api.Edge;
 import org.gephi.graph.api.GraphModel;
 import org.gephi.graph.api.Node;
  */
 public class JGraphTUtils {
     
+    
+    private static final Logger log = Logger.getLogger(JGraphTUtils.class.getName());    
+    
     /**
      * Wraps gephi's graphModel as
      * JGraphT's Graph
      * @return graph (of JGrapT)
      */
     public static Graph wrap(GraphModel graphModel){
-        Graph<Node, Edge> jGraph = new DefaultDirectedWeightedGraph<Node, Edge>(Edge.class); //JGraphT Graph
         org.gephi.graph.api.Graph gGraph = graphModel.getGraph(); // Gephi Graph
+                
+        return wrap(gGraph);
+    }
+    
+
+    /**
+     * Wraps gephi's graph as
+     * JGraphT's Graph
+     * 
+     * 
+     * @param graph (of Gephi)
+     * @return graph (of JGrapT)
+     */    
+    public static Graph wrap(org.gephi.graph.api.Graph gGraph){
+        log.setLevel(Level.ALL);
+        
+        Graph<Node, Edge> jGraph = new DefaultDirectedWeightedGraph<Node, Edge>(Edge.class); //JGraphT Graph
         
         // Copy references from Gephi's Graph to JGrapT's graph
         // Nodes
         Node[] nodes = gGraph.getNodes().toArray();
         for(Node node: nodes){
             jGraph.addVertex(node);
+            log.fine("node: " + node);
         }
+        
+        log.fine("nodes: " + jGraph.vertexSet());        
+        
         // Edges
         Edge[] edges = gGraph.getEdges().toArray();
         for (Edge edge: edges){
+  
+
+            // TODO get the node from jGraph
+            Node source = getNode(jGraph, edge.getSource());
+            Node target = getNode(jGraph, edge.getTarget());
+            
+/*            
             Node source = edge.getSource();
             Node target = edge.getTarget();
+*/            
+            
+            log.fine("edge: " + source + ":" + target);                    
             float weight = edge.getWeight();
             jGraph.addEdge(source, target, edge);
         }
         
         
         return jGraph;
+        
+    }    
+    
+    
+    /**
+     * 
+     * @param jGraph
+     * @param gNode (gephi's)
+     * @return jNode that corresponds to node in the jGraph
+     */
+    public static Node getNode(Graph jGraph, Node gNode){
+        for (Object o: jGraph.vertexSet()){
+            Node n = (Node) o;
+            if (n.getId() == gNode.getId()){
+                return n;
+            }
+        }
+        return null;
     }
     
 }

test/org/activeintel/gephi/jgrapht/JGraphT2GephiConvertingFilterTest.java

+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+package org.activeintel.gephi.jgrapht;
+
+import java.util.Iterator;
+import java.util.Set;
+import org.gephi.graph.api.GraphView;
+import org.gephi.filters.api.Query;
+import org.openide.util.Lookup;
+import org.gephi.filters.api.FilterController;
+import junit.framework.Assert;
+import java.io.FileNotFoundException;
+import org.gephi.graph.api.GraphModel;
+import org.activeintel.gephi.utilities.GephiUtilities;
+import java.io.File;
+import java.net.URISyntaxException;
+import org.gephi.filters.spi.FilterProperty;
+import org.gephi.graph.api.Graph;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author neil
+ */
+public class JGraphT2GephiConvertingFilterTest {
+    
+    public JGraphT2GephiConvertingFilterTest() {
+    }
+
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+    
+    @Before
+    public void setUp() {
+    }
+    
+    @After
+    public void tearDown() {
+    }
+
+    /**
+     * Test of filter method, of class JGraphT2GephiConvertingFilter.
+     */
+    @Test
+    public void testFilter() throws URISyntaxException, FileNotFoundException {
+        File file = new File(getClass().getResource("/org/activeintel/gephi/jgrapht/les-miserables.gexf").toURI());        
+        GraphModel graphModel = GephiUtilities.importFile(file); 
+        
+        org.gephi.graph.api.Graph gGraph = graphModel.getGraph(); // Gephi's Graph
+        org.jgrapht.Graph jGraph = JGraphTUtils.wrap(graphModel); // GraphT's Graph
+        
+        // Create & Execute Filter (this one is supposed to keep all of the nodes)
+        JGraphT2GephiConvertingFilter filter = new JGraphT2GephiConvertingFilter(jGraph);
+        FilterController controller = Lookup.getDefault().lookup(FilterController.class);
+        Query query = controller.createQuery(filter);
+        GraphView view = controller.filter(query); 
+        // get the resulting subGraph
+        Graph subGraph = graphModel.getGraph(view);
+        
+        // make sure that subGraph and graph are the same
+        Assert.assertEquals( gGraph.getNodeCount(), subGraph.getNodeCount() );
+        Assert.assertEquals( gGraph.getEdgeCount(), subGraph.getEdgeCount());  
+        
+        
+        // remove a couple of edges & nodes from jGraph; chnages should be reflected in the filtered subgraph
+        // test: remove 2 nodes
+        int delNum = 2;
+        Object[] nodes = jGraph.vertexSet().toArray();
+        for(int i = 0; i < delNum; i++){
+            jGraph.removeVertex(nodes[i]);            
+        }
+        // reinit filter
+        filter = new JGraphT2GephiConvertingFilter(jGraph);        
+        query = controller.createQuery(filter);
+        view = controller.filter(query); 
+        // get the resulting subGraph
+        subGraph = graphModel.getGraph(view);
+        // assert        
+        Assert.assertEquals( gGraph.getNodeCount() - delNum, subGraph.getNodeCount() );        
+
+        // test: remove 2 edges
+        gGraph = subGraph; // update gGraph
+        delNum = 2;
+        Object[] edges = jGraph.edgeSet().toArray();
+        for(int i = 0; i < delNum; i++){
+            jGraph.removeEdge(edges[i]);            
+        }
+        // reinit filter
+        filter = new JGraphT2GephiConvertingFilter(jGraph);        
+        query = controller.createQuery(filter);
+        view = controller.filter(query); 
+        // get the resulting subGraph
+        subGraph = graphModel.getGraph(view);
+        // assert        
+        Assert.assertEquals( gGraph.getEdgeCount() - delNum, subGraph.getEdgeCount() );                                                
+    }
+
+
+}

test/org/activeintel/gephi/jgrapht/JGraphTUtilsTest.java

  */
 package org.activeintel.gephi.jgrapht;
 
-import org.gephi.graph.api.GraphModel;
-import org.jgrapht.Graph;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-import static org.junit.Assert.*;
 
 import java.io.FileNotFoundException;
 import java.io.File;
 import java.net.URISyntaxException;
-import junit.framework.Assert;
 import org.gephi.graph.api.GraphModel;
 import org.jgrapht.Graph;
 import org.junit.After;
      * Test of wrap method, of class JGraphTUtils.
      */
     @Test
-    public void testWrap() throws URISyntaxException, FileNotFoundException {
+    public void testWrapGraphModel() throws URISyntaxException, FileNotFoundException {
         //File file = new File(getClass().getResource("/org/activeintel/gephi/utilities/les-miserables.gexf").toURI());
         File file = new File(getClass().getResource("/org/activeintel/gephi/jgrapht/les-miserables.gexf").toURI());        
         GraphModel graphModel = GephiUtilities.importFile(file); 
      * Test of wrap method, of class JGraphTUtils.
      */
     @Test
+    public void testWrapGraph() throws URISyntaxException, FileNotFoundException {
+        //File file = new File(getClass().getResource("/org/activeintel/gephi/utilities/les-miserables.gexf").toURI());
+        File file = new File(getClass().getResource("/org/activeintel/gephi/jgrapht/les-miserables.gexf").toURI());        
+        GraphModel graphModel = GephiUtilities.importFile(file); 
+        
+        org.gephi.graph.api.Graph gGraph = graphModel.getGraph(); // Gephi's Graph
+        Graph jGraph = JGraphTUtils.wrap(graphModel); // GraphT's Graph
+        
+        // get subGraph
+        org.gephi.graph.api.Graph gSubGraph = graphModel.getGraph(graphModel.newView());
+        // remove several nodes
+        gSubGraph.removeNode(gSubGraph.getNodes().toArray()[0]);
+        gSubGraph.removeNode(gSubGraph.getNodes().toArray()[0]);        
+        
+        // make sure wrapping does not throw exceptions
+        JGraphTUtils.wrap(gSubGraph);
+        
+/*        
+        // Make sure that both graphs (Gephi's & JGraphT's) are equal:
+        Assert.assertEquals( jGraph.vertexSet().size() , gGraph.getNodeCount());
+        Assert.assertEquals( jGraph.edgeSet().size() , gGraph.getEdgeCount());  
+        
+        // Make sure that changes are not propagated w/in JGraphT graphs (changing g1 should not change g2)
+        Graph jGraph2 = JGraphTUtils.wrap(graphModel); // GraphT's Graph
+        jGraph2.removeEdge(jGraph2.edgeSet().iterator().next());
+        jGraph2.removeVertex(jGraph2.vertexSet().iterator().next());
+        assertThat( jGraph2.vertexSet().size(), not(equalTo(gGraph.getNodeCount())));        
+        assertThat( jGraph2.edgeSet().size(),  not(equalTo(gGraph.getEdgeCount())));        
+*/                
+    }
+    
+    
+    
+    /**
+     * Test of wrap method, of class JGraphTUtils.
+     */
+    @Test
     public void testWrap_Load() throws URISyntaxException, FileNotFoundException {
         //File file = new File(getClass().getResource("/org/activeintel/gephi/utilities/les-miserables.gexf").toURI());
         File file = new File(getClass().getResource("/org/activeintel/gephi/jgrapht/les-miserables.gexf").toURI());