Search in sources :

Example 1 with DijkstraShortestPathAlgorithm

use of org.gephi.algorithms.shortestpath.DijkstraShortestPathAlgorithm 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 2 with DijkstraShortestPathAlgorithm

use of org.gephi.algorithms.shortestpath.DijkstraShortestPathAlgorithm in project gephi by gephi.

the class HeatMap method getListeners.

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

        @Override
        public void clickNodes(Node[] nodes) {
            try {
                Node n = nodes[0];
                Color[] colors;
                float[] positions;
                if (heatMapPanel.isUsePalette()) {
                    colors = heatMapPanel.getSelectedPalette().getColors();
                    positions = heatMapPanel.getSelectedPalette().getPositions();
                    dontPaintUnreachable = true;
                } else {
                    gradientColors = colors = heatMapPanel.getGradientColors();
                    gradientPositions = positions = heatMapPanel.getGradientPositions();
                    dontPaintUnreachable = heatMapPanel.isDontPaintUnreachable();
                }
                GraphController gc = Lookup.getDefault().lookup(GraphController.class);
                AbstractShortestPathAlgorithm algorithm;
                if (gc.getGraphModel().isDirected()) {
                    DirectedGraph graph = gc.getGraphModel().getDirectedGraphVisible();
                    algorithm = new BellmanFordShortestPathAlgorithm(graph, n);
                    algorithm.compute();
                } else {
                    Graph graph = gc.getGraphModel().getGraphVisible();
                    algorithm = new DijkstraShortestPathAlgorithm(graph, n);
                    algorithm.compute();
                }
                // Color
                LinearGradient linearGradient = new LinearGradient(colors, positions);
                // Algorithm
                double maxDistance = algorithm.getMaxDistance();
                if (!dontPaintUnreachable) {
                    // +1 to have the maxdistance nodes a ratio<1
                    maxDistance++;
                }
                if (maxDistance > 0) {
                    for (Entry<Node, Double> entry : algorithm.getDistances().entrySet()) {
                        Node node = entry.getKey();
                        if (!Double.isInfinite(entry.getValue())) {
                            float ratio = (float) (entry.getValue() / maxDistance);
                            Color c = linearGradient.getValue(ratio);
                            node.setColor(c);
                        } else if (!dontPaintUnreachable) {
                            Color c = colors[colors.length - 1];
                            node.setColor(c);
                        }
                    }
                }
                Color c = colors[0];
                n.setColor(c);
                heatMapPanel.setStatus(NbBundle.getMessage(HeatMap.class, "HeatMap.status.maxdistance") + new DecimalFormat("#.##").format(algorithm.getMaxDistance()));
            } catch (Exception e) {
                Logger.getLogger("").log(Level.SEVERE, "", e);
            }
        }
    };
    return listeners;
}
Also used : Node(org.gephi.graph.api.Node) Color(java.awt.Color) DecimalFormat(java.text.DecimalFormat) DijkstraShortestPathAlgorithm(org.gephi.algorithms.shortestpath.DijkstraShortestPathAlgorithm) AbstractShortestPathAlgorithm(org.gephi.algorithms.shortestpath.AbstractShortestPathAlgorithm) BellmanFordShortestPathAlgorithm(org.gephi.algorithms.shortestpath.BellmanFordShortestPathAlgorithm) LinearGradient(org.gephi.ui.utils.GradientUtils.LinearGradient) Entry(java.util.Map.Entry) DirectedGraph(org.gephi.graph.api.DirectedGraph) Graph(org.gephi.graph.api.Graph) DirectedGraph(org.gephi.graph.api.DirectedGraph) NodeClickEventListener(org.gephi.tools.spi.NodeClickEventListener) GraphController(org.gephi.graph.api.GraphController)

Aggregations

AbstractShortestPathAlgorithm (org.gephi.algorithms.shortestpath.AbstractShortestPathAlgorithm)2 BellmanFordShortestPathAlgorithm (org.gephi.algorithms.shortestpath.BellmanFordShortestPathAlgorithm)2 DijkstraShortestPathAlgorithm (org.gephi.algorithms.shortestpath.DijkstraShortestPathAlgorithm)2 GraphController (org.gephi.graph.api.GraphController)2 Node (org.gephi.graph.api.Node)2 NodeClickEventListener (org.gephi.tools.spi.NodeClickEventListener)2 Color (java.awt.Color)1 DecimalFormat (java.text.DecimalFormat)1 Entry (java.util.Map.Entry)1 DirectedGraph (org.gephi.graph.api.DirectedGraph)1 Edge (org.gephi.graph.api.Edge)1 Graph (org.gephi.graph.api.Graph)1 GraphModel (org.gephi.graph.api.GraphModel)1 MouseClickEventListener (org.gephi.tools.spi.MouseClickEventListener)1 LinearGradient (org.gephi.ui.utils.GradientUtils.LinearGradient)1