Search in sources :

Example 1 with Graph

use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.

the class AverageEuclideanDistance method execute.

@Override
public void execute(GraphModel graphModel) {
    Graph graph = graphModel.getGraphVisible();
    //Look if the result column already exist and create it if needed
    Table nodeTable = graphModel.getNodeTable();
    Column col = nodeTable.getColumn(AVG_EUCLIDEAN_DISTANCE);
    if (col == null) {
        col = nodeTable.addColumn(AVG_EUCLIDEAN_DISTANCE, "Average Euclidean Distance", Double.class, Origin.DATA);
    }
    //Lock to graph. This is important to have consistent results if another
    //process is currently modifying it.
    graph.readLock();
    //Iterate on all nodes
    Node[] nodes = graph.getNodes().toArray();
    for (Node n : nodes) {
        double avg = 0;
        int count = 0;
        if (useOnlyConnections) {
            //Calculate distance with neighbors
            for (Node m : graph.getNeighbors(n)) {
                double xDist = n.x() - m.x();
                double yDist = n.y() - m.y();
                double dist = Math.sqrt(xDist * xDist + yDist * yDist);
                avg = (dist + avg) / ++count;
            }
        } else {
            //Calculate distance with all other nodes
            for (Node m : nodes) {
                if (n != m) {
                    double xDist = n.x() - m.x();
                    double yDist = n.y() - m.y();
                    double dist = Math.sqrt(xDist * xDist + yDist * yDist);
                    avg = (dist + avg) / ++count;
                }
            }
        }
        //Store the average in the node attribute
        n.setAttribute(col, avg);
    }
    graph.readUnlock();
}
Also used : Graph(org.gephi.graph.api.Graph) Table(org.gephi.graph.api.Table) Column(org.gephi.graph.api.Column) Node(org.gephi.graph.api.Node)

Example 2 with Graph

use of org.gephi.graph.api.Graph in project gephi-plugins-bootcamp by gephi.

the class AddNodesTool method getListeners.

@Override
public ToolEventListener[] getListeners() {
    return new ToolEventListener[] { new MouseClickEventListener() {

        @Override
        public void mouseClick(int[] positionViewport, float[] position3d) {
            //Get current graph
            GraphController gc = Lookup.getDefault().lookup(GraphController.class);
            Graph graph = gc.getGraphModel().getGraph();
            GraphFactory factory = gc.getGraphModel().factory();
            //Add node
            Node node = factory.newNode();
            node.setX(position3d[0]);
            node.setY(position3d[1]);
            node.setSize(10f);
            graph.addNode(node);
        }
    }, new NodeClickEventListener() {

        @Override
        public void clickNodes(Node[] nodes) {
            //Get mouse position
            float[] position3d = VizController.getInstance().getGraphIO().getMousePosition3d();
            //Get current graph
            GraphController gc = Lookup.getDefault().lookup(GraphController.class);
            Graph graph = gc.getGraphModel().getGraph();
            GraphFactory factory = gc.getGraphModel().factory();
            //Add node
            Node node = factory.newNode();
            node.setX(position3d[0]);
            node.setY(position3d[1]);
            node.setSize(10f);
            graph.addNode(node);
            //Add edges with the clicked nodes
            for (Node n : nodes) {
                Edge edge = factory.newEdge(node, n);
                graph.addEdge(edge);
            }
        }
    } };
}
Also used : MouseClickEventListener(org.gephi.tools.spi.MouseClickEventListener) GraphFactory(org.gephi.graph.api.GraphFactory) ToolEventListener(org.gephi.tools.spi.ToolEventListener) Graph(org.gephi.graph.api.Graph) Node(org.gephi.graph.api.Node) NodeClickEventListener(org.gephi.tools.spi.NodeClickEventListener) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Example 3 with Graph

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

the class StatisticsControllerImpl method executeDynamic.

private void executeDynamic(DynamicStatistics statistics, DynamicLongTask dynamicLongTask) {
    GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
    GraphModel graphModel = graphController.getGraphModel();
    double window = statistics.getWindow();
    double tick = statistics.getTick();
    GraphView currentView = graphModel.getVisibleView();
    Interval bounds = statistics.getBounds();
    if (bounds == null) {
        if (currentView.isMainView()) {
            bounds = graphModel.getTimeBounds();
        } else {
            bounds = currentView.getTimeInterval();
        }
        statistics.setBounds(bounds);
    }
    if (dynamicLongTask != null) {
        //Count
        int c = (int) ((bounds.getHigh() - window - bounds.getLow()) / tick);
        dynamicLongTask.start(c);
    }
    //Init
    statistics.execute(graphModel);
    //Loop
    for (double low = bounds.getLow(); low <= bounds.getHigh() - window; low += tick) {
        double high = low + window;
        Graph graph = graphModel.getGraphVisible();
        graph.writeLock();
        try {
            GraphView view = graphModel.createView();
            Subgraph g = graphModel.getGraph(view);
            TimeIndex<Node> nodeIndex = graphModel.getNodeTimeIndex(currentView);
            if (Double.isInfinite(nodeIndex.getMinTimestamp()) && Double.isInfinite(nodeIndex.getMaxTimestamp())) {
                for (Node node : graph.getNodes()) {
                    g.addNode(node);
                }
            } else {
                for (Node node : nodeIndex.get(new Interval(low, high))) {
                    g.addNode(node);
                }
            }
            TimeIndex<Edge> edgeIndex = graphModel.getEdgeTimeIndex(currentView);
            if (Double.isInfinite(edgeIndex.getMinTimestamp()) && Double.isInfinite(edgeIndex.getMaxTimestamp())) {
                for (Edge edge : graph.getEdges()) {
                    if (g.contains(edge.getSource()) && g.contains(edge.getTarget())) {
                        g.addEdge(edge);
                    }
                }
            } else {
                for (Edge edge : edgeIndex.get(new Interval(low, high))) {
                    if (g.contains(edge.getSource()) && g.contains(edge.getTarget())) {
                        g.addEdge(edge);
                    }
                }
            }
            statistics.loop(g.getView(), new Interval(low, high));
        } finally {
            graph.writeUnlock();
        }
        //Cancelled?
        if (dynamicLongTask != null && dynamicLongTask.isCancelled()) {
            return;
        } else if (dynamicLongTask != null) {
            dynamicLongTask.progress();
        }
    }
    statistics.end();
    model.addReport(statistics);
}
Also used : Graph(org.gephi.graph.api.Graph) GraphModel(org.gephi.graph.api.GraphModel) Node(org.gephi.graph.api.Node) Subgraph(org.gephi.graph.api.Subgraph) GraphView(org.gephi.graph.api.GraphView) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController) Interval(org.gephi.graph.api.Interval)

Example 4 with Graph

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

the class ClusteringCoefficient method execute.

@Override
public void execute(GraphModel graphModel) {
    isDirected = graphModel.isDirected();
    Graph graph = null;
    if (isDirected) {
        graph = graphModel.getDirectedGraphVisible();
    } else {
        graph = graphModel.getUndirectedGraphVisible();
    }
    execute(graph);
}
Also used : DirectedGraph(org.gephi.graph.api.DirectedGraph) Graph(org.gephi.graph.api.Graph)

Example 5 with Graph

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

the class FilterControllerImpl method exportToLabelVisible.

@Override
public void exportToLabelVisible(Query query) {
    Graph result;
    if (model.getCurrentQuery() == query) {
        GraphView view = model.getCurrentResult();
        if (view == null) {
            return;
        }
        result = model.getGraphModel().getGraph(view);
    } else {
        FilterProcessor processor = new FilterProcessor();
        result = (Graph) processor.process((AbstractQueryImpl) query, model.getGraphModel());
    }
    Graph fullGraph = model.getGraphModel().getGraph();
    fullGraph.writeLock();
    try {
        for (Node n : fullGraph.getNodes()) {
            boolean inView = result.contains(n);
            n.getTextProperties().setVisible(inView);
        }
        for (Edge e : fullGraph.getEdges()) {
            boolean inView = result.contains(e);
            e.getTextProperties().setVisible(inView);
        }
    } finally {
        fullGraph.readUnlockAll();
        fullGraph.writeUnlock();
    }
}
Also used : Graph(org.gephi.graph.api.Graph) Node(org.gephi.graph.api.Node) GraphView(org.gephi.graph.api.GraphView) Edge(org.gephi.graph.api.Edge)

Aggregations

Graph (org.gephi.graph.api.Graph)104 GraphModel (org.gephi.graph.api.GraphModel)57 Node (org.gephi.graph.api.Node)50 DirectedGraph (org.gephi.graph.api.DirectedGraph)43 Test (org.testng.annotations.Test)36 GraphController (org.gephi.graph.api.GraphController)27 UndirectedGraph (org.gephi.graph.api.UndirectedGraph)24 Edge (org.gephi.graph.api.Edge)21 Column (org.gephi.graph.api.Column)9 ArrayList (java.util.ArrayList)8 Function (org.gephi.appearance.api.Function)6 GraphView (org.gephi.graph.api.GraphView)6 AttributeFunction (org.gephi.appearance.api.AttributeFunction)5 AppearanceController (org.gephi.appearance.api.AppearanceController)4 AppearanceModel (org.gephi.appearance.api.AppearanceModel)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 TransformerUI (org.gephi.appearance.spi.TransformerUI)3 GraphElementsController (org.gephi.datalab.api.GraphElementsController)3 DataTablesController (org.gephi.datalab.api.datatables.DataTablesController)3