Search in sources :

Example 1 with KKLayout

use of edu.uci.ics.jung.algorithms.layout.KKLayout in project opennms by OpenNMS.

the class KKLayoutAlgorithm method updateLayout.

@Override
public void updateLayout(final Graph graph) {
    final Layout graphLayout = graph.getLayout();
    SparseGraph<VertexRef, Edge> jungGraph = new SparseGraph<VertexRef, Edge>();
    Collection<? extends Vertex> vertices = graph.getDisplayVertices();
    for (Vertex v : vertices) {
        jungGraph.addVertex(v);
    }
    Collection<? extends Edge> edges = graph.getDisplayEdges();
    for (Edge e : edges) {
        jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
    }
    KKLayout<VertexRef, Edge> layout = new KKLayout<VertexRef, Edge>(jungGraph);
    layout.setInitializer(initializer(graphLayout));
    layout.setSize(selectLayoutSize(graph));
    while (!layout.done()) {
        layout.step();
    }
    for (Vertex v : vertices) {
        graphLayout.setLocation(v, new Point(layout.getX(v), layout.getY(v)));
    }
}
Also used : SparseGraph(edu.uci.ics.jung.graph.SparseGraph) Vertex(org.opennms.features.topology.api.topo.Vertex) KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) Layout(org.opennms.features.topology.api.Layout) Point(org.opennms.features.topology.api.Point) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge)

Example 2 with KKLayout

use of edu.uci.ics.jung.algorithms.layout.KKLayout in project opennms by OpenNMS.

the class DefaultBusinessServiceStateMachine method renderGraphToPng.

@Override
public void renderGraphToPng(File tempFile) {
    m_rwLock.readLock().lock();
    try {
        Layout<GraphVertex, GraphEdge> layout = new KKLayout<GraphVertex, GraphEdge>(m_g);
        // Size of the layout
        layout.setSize(new Dimension(1024, 1024));
        VisualizationImageServer<GraphVertex, GraphEdge> vv = new VisualizationImageServer<GraphVertex, GraphEdge>(layout, layout.getSize());
        // Viewing area size
        vv.setPreferredSize(new Dimension(1200, 1200));
        vv.getRenderContext().setVertexLabelTransformer(new Transformer<GraphVertex, String>() {

            @Override
            public String transform(GraphVertex vertex) {
                if (vertex.getBusinessService() != null) {
                    return String.format("BS[%s]", vertex.getBusinessService().getName());
                }
                if (vertex.getIpService() != null) {
                    IpService ipService = vertex.getIpService();
                    return String.format("IP_SERVICE[%s,%s]", ipService.getId(), ipService.getServiceName());
                }
                if (vertex.getReductionKey() != null) {
                    return String.format("RK[%s]", vertex.getReductionKey());
                }
                return "UNKNOWN";
            }
        });
        vv.getRenderContext().setEdgeLabelTransformer(new Transformer<GraphEdge, String>() {

            @Override
            public String transform(GraphEdge edge) {
                return String.format("%s", edge.getMapFunction().getClass().getSimpleName());
            }
        });
        // Create the buffered image
        BufferedImage image = (BufferedImage) vv.getImage(new Point2D.Double(vv.getGraphLayout().getSize().getWidth() / 2, vv.getGraphLayout().getSize().getHeight() / 2), new Dimension(vv.getGraphLayout().getSize()));
        // Render
        try {
            ImageIO.write(image, "png", tempFile);
        } catch (IOException e) {
            throw Throwables.propagate(e);
        }
    } finally {
        m_rwLock.readLock().unlock();
    }
}
Also used : KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) Dimension(java.awt.Dimension) IOException(java.io.IOException) IpService(org.opennms.netmgt.bsm.service.model.IpService) BufferedImage(java.awt.image.BufferedImage) GraphVertex(org.opennms.netmgt.bsm.service.model.graph.GraphVertex) VisualizationImageServer(edu.uci.ics.jung.visualization.VisualizationImageServer) GraphEdge(org.opennms.netmgt.bsm.service.model.graph.GraphEdge)

Example 3 with KKLayout

use of edu.uci.ics.jung.algorithms.layout.KKLayout in project titan.EclipsePlug-ins by eclipse.

the class LayoutBuilder method build.

/**
 * This method implements the building of the chosen layout with chosen parameters
 * @return The built layout
 * @throws BadLayoutException If the chosen layout doesn't exist (invalid code was provided)
 */
public Layout<NodeDescriptor, EdgeDescriptor> build() throws BadLayoutException {
    Layout<NodeDescriptor, EdgeDescriptor> layout;
    final String layoutCode = layoutEntry.getCode();
    if (layoutCode.equals(Layouts.LAYOUT_ISOM.getCode())) {
        layout = new TitaniumISOMLayout<NodeDescriptor, EdgeDescriptor>(g);
        ((TitaniumISOMLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
    } else if (layoutCode.equals(Layouts.LAYOUT_KK.getCode())) {
        layout = new KKLayout<NodeDescriptor, EdgeDescriptor>(g);
        ((KKLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
    } else if (layoutCode.equals(Layouts.LAYOUT_FR.getCode())) {
        layout = new FRLayout<NodeDescriptor, EdgeDescriptor>(g);
        ((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setAttractionMultiplier(0.6);
        ((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setRepulsionMultiplier(0.8);
        ((FRLayout<NodeDescriptor, EdgeDescriptor>) layout).setMaxIterations(Activator.getDefault().getPreferenceStore().getInt(PreferenceConstants.NO_ITERATIONS));
    } else if (layoutCode.equals(Layouts.LAYOUT_SPRING.getCode())) {
        layout = new SpringLayout<NodeDescriptor, EdgeDescriptor>(g);
    } else if (layoutCode.equals(Layouts.LAYOUT_CIRCLE.getCode())) {
        layout = new CircleLayout<NodeDescriptor, EdgeDescriptor>(g);
    } else if (layoutCode.equals(Layouts.LAYOUT_RTDAG.getCode())) {
        layout = new ReverseDAGLayout<NodeDescriptor, EdgeDescriptor>(g, size);
    } else if (layoutCode.equals(Layouts.LAYOUT_TDAG.getCode())) {
        layout = new TitaniumDAGLayout<NodeDescriptor, EdgeDescriptor>(g, size);
    } else if (layoutCode.equals(Layouts.METRIC_LAYOUT_CODE)) {
        if (!(layoutEntry instanceof MetricsLayoutEntry)) {
            throw new IllegalStateException("A metric must be chosen before using metric layout!");
        }
        layout = new MetricLayout<EdgeDescriptor>(g, size, ((MetricsLayoutEntry) layoutEntry).getMetric());
    } else if (layoutCode.equals(Layouts.S_LAYOUT_CLUSTER)) {
        if (clusters == null) {
            throw new IllegalStateException("A clustering must be set before using cluster layout!");
        }
        final ClusterTransformer trf = new ClusterTransformer(new FRLayout<NodeDescriptor, EdgeDescriptor>(g), clusters, size);
        layout = new StaticLayout<NodeDescriptor, EdgeDescriptor>(g, trf);
    } else if ("STATIC".equals(layoutCode)) {
        if (pointTransformer == null) {
            throw new IllegalStateException("A point transformer must be set before using static layout!");
        }
        layout = new StaticLayout<NodeDescriptor, EdgeDescriptor>(g, pointTransformer);
    } else {
        throw new BadLayoutException("There is no such layout! (Layout=" + layoutCode + ")", ErrorType.NOT_EXISITING_LAYOUT);
    }
    layout.setSize(size);
    return layout;
}
Also used : KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) ClusterTransformer(org.eclipse.titanium.graph.clustering.gui.ClusterTransformer) MetricsLayoutEntry(org.eclipse.titanium.graph.gui.utils.MetricsLayoutEntry) FRLayout(edu.uci.ics.jung.algorithms.layout.FRLayout) NodeDescriptor(org.eclipse.titanium.graph.components.NodeDescriptor) StaticLayout(edu.uci.ics.jung.algorithms.layout.StaticLayout) EdgeDescriptor(org.eclipse.titanium.graph.components.EdgeDescriptor) ReverseDAGLayout(org.eclipse.titanium.graph.gui.layouts.ReverseDAGLayout) MetricLayout(org.eclipse.titanium.graph.gui.layouts.MetricLayout) SpringLayout(edu.uci.ics.jung.algorithms.layout.SpringLayout) TitaniumISOMLayout(org.eclipse.titanium.graph.gui.layouts.TitaniumISOMLayout)

Example 4 with KKLayout

use of edu.uci.ics.jung.algorithms.layout.KKLayout in project opennms by OpenNMS.

the class GraphUtils method renderGraphToFile.

public static void renderGraphToFile(Graph<VertexRef, Edge> jungGraph, File file) {
    final edu.uci.ics.jung.algorithms.layout.Layout<VertexRef, Edge> jungLayout = new KKLayout<>(jungGraph);
    // Size of the layout
    jungLayout.setSize(new Dimension(1800, 1800));
    final Set<VertexRef> roots = jungGraph.getVertices().stream().filter(v -> jungGraph.getInEdges(v).isEmpty()).collect(Collectors.toSet());
    VisualizationImageServer<VertexRef, Edge> vv = new VisualizationImageServer<>(jungLayout, jungLayout.getSize());
    // Viewing area size
    vv.setPreferredSize(new Dimension(2000, 2000));
    vv.getRenderContext().setVertexLabelTransformer(VertexRef::getLabel);
    vv.getRenderContext().setEdgeLabelTransformer(Edge::getLabel);
    vv.getRenderContext().setVertexFillPaintTransformer(vertexRef -> {
        if (roots.contains(vertexRef)) {
            return Color.RED;
        }
        return Color.BLUE;
    });
    // Draw vertices according to in/out edge count. The more edges, the bigger the vertex
    vv.getRenderContext().setVertexShapeTransformer(vertexRef -> {
        Collection<Edge> inEdges = jungGraph.getInEdges(vertexRef);
        Collection<Edge> outEdges = jungGraph.getOutEdges(vertexRef);
        int edgeCount = inEdges.size() + outEdges.size();
        int widthHeight = (edgeCount / 4 + 1) * 20;
        return new Ellipse2D.Float(-1 * widthHeight / 2, -1 * widthHeight / 2, widthHeight, widthHeight);
    });
    // Create the buffered image
    BufferedImage image = (BufferedImage) vv.getImage(new Point2D.Double(vv.getGraphLayout().getSize().getWidth() / 2, vv.getGraphLayout().getSize().getHeight() / 2), new Dimension(vv.getGraphLayout().getSize()));
    // Render
    try {
        ImageIO.write(image, "png", file);
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : Color(java.awt.Color) Point2D(java.awt.geom.Point2D) BufferedImage(java.awt.image.BufferedImage) Edge(org.opennms.features.topology.api.topo.Edge) Collection(java.util.Collection) Set(java.util.Set) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Dimension(java.awt.Dimension) Ellipse2D(java.awt.geom.Ellipse2D) KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) ImageIO(javax.imageio.ImageIO) Graph(edu.uci.ics.jung.graph.Graph) VertexRef(org.opennms.features.topology.api.topo.VertexRef) VisualizationImageServer(edu.uci.ics.jung.visualization.VisualizationImageServer) KKLayout(edu.uci.ics.jung.algorithms.layout.KKLayout) Dimension(java.awt.Dimension) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) VisualizationImageServer(edu.uci.ics.jung.visualization.VisualizationImageServer) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge)

Aggregations

KKLayout (edu.uci.ics.jung.algorithms.layout.KKLayout)4 VisualizationImageServer (edu.uci.ics.jung.visualization.VisualizationImageServer)2 Dimension (java.awt.Dimension)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 Edge (org.opennms.features.topology.api.topo.Edge)2 VertexRef (org.opennms.features.topology.api.topo.VertexRef)2 Throwables (com.google.common.base.Throwables)1 FRLayout (edu.uci.ics.jung.algorithms.layout.FRLayout)1 SpringLayout (edu.uci.ics.jung.algorithms.layout.SpringLayout)1 StaticLayout (edu.uci.ics.jung.algorithms.layout.StaticLayout)1 Graph (edu.uci.ics.jung.graph.Graph)1 SparseGraph (edu.uci.ics.jung.graph.SparseGraph)1 Color (java.awt.Color)1 Ellipse2D (java.awt.geom.Ellipse2D)1 Point2D (java.awt.geom.Point2D)1 File (java.io.File)1 Collection (java.util.Collection)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1