Search in sources :

Example 56 with GraphModel

use of org.gephi.graph.api.GraphModel in project gephi by gephi.

the class GraphDistanceNGTest method testDirectedCyclicGraphBetweenness.

@Test
public void testDirectedCyclicGraphBetweenness() {
    GraphModel graphModel = GraphGenerator.generateCyclicDirectedGraph(5);
    GraphDistance d = new GraphDistance();
    d.initializeStartValues();
    DirectedGraph directedGraph = graphModel.getDirectedGraph();
    HashMap<Node, Integer> indicies = d.createIndiciesMap(graphModel.getGraph());
    HashMap<String, double[]> metricsMap = (HashMap) d.calculateDistanceMetrics(graphModel.getGraph(), indicies, true, false);
    double[] betweenness = metricsMap.get(GraphDistance.BETWEENNESS);
    Node n1 = directedGraph.getNode("0");
    Node n3 = directedGraph.getNode("2");
    int index1 = indicies.get(n1);
    int index3 = indicies.get(n3);
    assertEquals(betweenness[index1], 6.0, TOLERANCE);
    assertEquals(betweenness[index3], 6.0, TOLERANCE);
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) HashMap(java.util.HashMap) GraphModel(org.gephi.graph.api.GraphModel) Node(org.gephi.graph.api.Node) Test(org.testng.annotations.Test)

Example 57 with GraphModel

use of org.gephi.graph.api.GraphModel in project gephi by gephi.

the class AttributeColumnsControllerImpl method setAttributeValue.

@Override
public boolean setAttributeValue(Object value, Element row, Column column) {
    if (!canChangeColumnData(column)) {
        return false;
    }
    Class targetType = column.getTypeClass();
    if (value != null && !value.getClass().equals(targetType)) {
        try {
            GraphModel graphModel = column.getTable().getGraph().getModel();
            String stringValue = AttributeUtils.print(value, graphModel.getTimeFormat(), graphModel.getTimeZone());
            //Try to convert to target type from string representation
            value = AttributeUtils.parse(stringValue, targetType);
        } catch (Exception ex) {
            //Could not parse
            return false;
        }
    }
    if (value == null && !canClearColumnData(column)) {
        //Do not set a null value when the column can't have a null value
        return false;
    } else {
        try {
            if (value == null) {
                row.removeAttribute(column);
            } else {
                row.setAttribute(column, value);
            }
            return true;
        } catch (Exception e) {
            Logger.getLogger("").log(Level.SEVERE, null, e);
            return false;
        }
    }
}
Also used : GraphModel(org.gephi.graph.api.GraphModel) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 58 with GraphModel

use of org.gephi.graph.api.GraphModel in project gephi by gephi.

the class ManageColumnEstimators method getColumns.

public List<Column> getColumns() {
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
    Table table;
    if (Lookup.getDefault().lookup(DataTablesController.class).isNodeTableMode()) {
        table = graphModel.getNodeTable();
    } else {
        table = graphModel.getEdgeTable();
    }
    List<Column> availableColumns = new ArrayList<>();
    for (Column column : table) {
        if (TimeMap.class.isAssignableFrom(column.getTypeClass())) {
            availableColumns.add(column);
        }
    }
    return availableColumns;
}
Also used : Table(org.gephi.graph.api.Table) Column(org.gephi.graph.api.Column) GraphModel(org.gephi.graph.api.GraphModel) ArrayList(java.util.ArrayList) GraphController(org.gephi.graph.api.GraphController) DataTablesController(org.gephi.datalab.api.datatables.DataTablesController)

Example 59 with GraphModel

use of org.gephi.graph.api.GraphModel in project gephi by gephi.

the class ShortestPath method getListeners.

@Override
public ToolEventListener[] getListeners() {
    listeners = new ToolEventListener[2];
    listeners[0] = new NodeClickEventListener() {

        @Override
        public void clickNodes(Node[] nodes) {
            Node n = nodes[0];
            if (sourceNode == null) {
                sourceNode = n;
                shortestPathPanel.setResult("");
                shortestPathPanel.setStatus(NbBundle.getMessage(ShortestPath.class, "ShortestPath.status2"));
            } else if (n != sourceNode) {
                color = shortestPathPanel.getColor();
                Node targetNode = n;
                GraphController gc = Lookup.getDefault().lookup(GraphController.class);
                GraphModel gm = gc.getGraphModel();
                AbstractShortestPathAlgorithm algorithm;
                if (gm.isDirected()) {
                    algorithm = new BellmanFordShortestPathAlgorithm(gm.getDirectedGraphVisible(), sourceNode);
                } else {
                    algorithm = new DijkstraShortestPathAlgorithm(gm.getGraphVisible(), sourceNode);
                }
                algorithm.compute();
                double distance;
                if ((distance = algorithm.getDistances().get(targetNode)) != Double.POSITIVE_INFINITY) {
                    targetNode.setColor(color);
                    VizController.getInstance().selectNode(targetNode);
                    Edge predecessorEdge = algorithm.getPredecessorIncoming(targetNode);
                    Node predecessor = algorithm.getPredecessor(targetNode);
                    while (predecessorEdge != null && predecessor != sourceNode) {
                        predecessorEdge.setColor(color);
                        VizController.getInstance().selectEdge(predecessorEdge);
                        predecessor.setColor(color);
                        VizController.getInstance().selectNode(predecessor);
                        predecessorEdge = algorithm.getPredecessorIncoming(predecessor);
                        predecessor = algorithm.getPredecessor(predecessor);
                    }
                    predecessorEdge.setColor(color);
                    VizController.getInstance().selectEdge(predecessorEdge);
                    sourceNode.setColor(color);
                    VizController.getInstance().selectNode(sourceNode);
                    shortestPathPanel.setResult(NbBundle.getMessage(ShortestPath.class, "ShortestPath.result", distance));
                } else {
                    //No path
                    shortestPathPanel.setResult(NbBundle.getMessage(ShortestPath.class, "ShortestPath.noresult"));
                }
                sourceNode = null;
                shortestPathPanel.setStatus(NbBundle.getMessage(ShortestPath.class, "ShortestPath.status1"));
            }
        }
    };
    listeners[1] = new MouseClickEventListener() {

        @Override
        public void mouseClick(int[] positionViewport, float[] position3d) {
            if (sourceNode != null) {
                //Cancel
                shortestPathPanel.setStatus(NbBundle.getMessage(ShortestPath.class, "ShortestPath.status1"));
                sourceNode = null;
            } else {
                VizController.getInstance().resetSelection();
            }
        }
    };
    return listeners;
}
Also used : MouseClickEventListener(org.gephi.tools.spi.MouseClickEventListener) Node(org.gephi.graph.api.Node) DijkstraShortestPathAlgorithm(org.gephi.algorithms.shortestpath.DijkstraShortestPathAlgorithm) AbstractShortestPathAlgorithm(org.gephi.algorithms.shortestpath.AbstractShortestPathAlgorithm) BellmanFordShortestPathAlgorithm(org.gephi.algorithms.shortestpath.BellmanFordShortestPathAlgorithm) GraphModel(org.gephi.graph.api.GraphModel) NodeClickEventListener(org.gephi.tools.spi.NodeClickEventListener) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Example 60 with GraphModel

use of org.gephi.graph.api.GraphModel in project gephi by gephi.

the class PageRankNGTest method testUndirectedWeightedGraphPageRank.

@Test
public void testUndirectedWeightedGraphPageRank() {
    pc.newProject();
    GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getGraphModel();
    UndirectedGraph undirectedGraph = graphModel.getUndirectedGraph();
    Node node1 = graphModel.factory().newNode("0");
    Node node2 = graphModel.factory().newNode("1");
    Node node3 = graphModel.factory().newNode("2");
    Node node4 = graphModel.factory().newNode("3");
    Node node5 = graphModel.factory().newNode("4");
    Node node6 = graphModel.factory().newNode("5");
    undirectedGraph.addNode(node1);
    undirectedGraph.addNode(node2);
    undirectedGraph.addNode(node3);
    undirectedGraph.addNode(node4);
    undirectedGraph.addNode(node5);
    undirectedGraph.addNode(node6);
    Edge edge12 = graphModel.factory().newEdge(node1, node2, false);
    Edge edge23 = graphModel.factory().newEdge(node2, node3, 0, 10, false);
    Edge edge34 = graphModel.factory().newEdge(node3, node4, false);
    Edge edge45 = graphModel.factory().newEdge(node4, node5, false);
    Edge edge56 = graphModel.factory().newEdge(node5, node6, false);
    Edge edge61 = graphModel.factory().newEdge(node6, node1, false);
    undirectedGraph.addEdge(edge12);
    undirectedGraph.addEdge(edge23);
    undirectedGraph.addEdge(edge34);
    undirectedGraph.addEdge(edge45);
    undirectedGraph.addEdge(edge56);
    undirectedGraph.addEdge(edge61);
    UndirectedGraph graph = graphModel.getUndirectedGraph();
    PageRank pr = new PageRank();
    double[] pageRank;
    HashMap<Node, Integer> indicies = pr.createIndiciesMap(graph);
    pageRank = pr.calculatePagerank(graph, indicies, false, true, 0.001, 0.85);
    int index1 = indicies.get(node1);
    int index2 = indicies.get(node2);
    int index3 = indicies.get(node3);
    int index6 = indicies.get(node6);
    double diff = 0.01;
    double pr1 = pageRank[index1];
    double pr2 = pageRank[index2];
    double pr3 = pageRank[index3];
    double pr6 = pageRank[index6];
    assertTrue(Math.abs(pr2 - pr3) < diff);
    assertTrue(pr1 < pr2);
    assertTrue(pr1 < pr6);
}
Also used : GraphModel(org.gephi.graph.api.GraphModel) UndirectedGraph(org.gephi.graph.api.UndirectedGraph) Node(org.gephi.graph.api.Node) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController) Test(org.testng.annotations.Test)

Aggregations

GraphModel (org.gephi.graph.api.GraphModel)235 Node (org.gephi.graph.api.Node)169 Test (org.testng.annotations.Test)168 UndirectedGraph (org.gephi.graph.api.UndirectedGraph)115 GraphController (org.gephi.graph.api.GraphController)105 DirectedGraph (org.gephi.graph.api.DirectedGraph)92 Edge (org.gephi.graph.api.Edge)80 Graph (org.gephi.graph.api.Graph)57 HashMap (java.util.HashMap)52 Column (org.gephi.graph.api.Column)16 LinkedList (java.util.LinkedList)12 ArrayList (java.util.ArrayList)11 FilterBuilder (org.gephi.filters.spi.FilterBuilder)6 GraphView (org.gephi.graph.api.GraphView)6 XMLStreamException (javax.xml.stream.XMLStreamException)5 IOException (java.io.IOException)4 AppearanceController (org.gephi.appearance.api.AppearanceController)4 AppearanceModel (org.gephi.appearance.api.AppearanceModel)4 TimeFormat (org.gephi.graph.api.TimeFormat)4 Element (org.gephi.graph.api.Element)3