Search in sources :

Example 6 with Layout

use of org.opennms.features.topology.api.Layout 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 7 with Layout

use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.

the class RealUltimateLayoutAlgorithm method updateLayout.

@Override
public void updateLayout(Graph graph) {
    final Layout graphLayout = graph.getLayout();
    SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>();
    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());
    }
    Dimension size = selectLayoutSize(graph);
    Dimension paddedSize = new Dimension((int) (size.getWidth() * .75), (int) (size.getHeight() * .75));
    doISOMLayout(graphLayout, jungGraph, size);
    doSpringLayout(graphLayout, jungGraph, size, SPRING_LAYOUT_REPULSION);
    doFRLayout(graphLayout, jungGraph, paddedSize, (int) (size.getWidth() / 8.0), (int) (size.getHeight() / 8.0));
    doSpringLayout(graphLayout, jungGraph, size, SPRING_LAYOUT_REPULSION);
}
Also used : SparseGraph(edu.uci.ics.jung.graph.SparseGraph) Vertex(org.opennms.features.topology.api.topo.Vertex) NonStupidISOMLayout(org.opennms.features.topology.app.internal.jung.ISOMLayoutAlgorithm.NonStupidISOMLayout) Layout(org.opennms.features.topology.api.Layout) FRLayout(edu.uci.ics.jung.algorithms.layout.FRLayout) SpringLayout(edu.uci.ics.jung.algorithms.layout.SpringLayout) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) Dimension(java.awt.Dimension) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge)

Example 8 with Layout

use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.

the class GridLayoutAlgorithm method updateLayout.

/**
 * Updates the current layout by extracting the containers graph and then perform a (x,y) transformation
 * of all vertices.
 *
 * @param graph The container of the current graph. Contains all relevant information to perform the transformation
 *                       of the {@link Graph} by changing its {@link Layout}
 */
@Override
public void updateLayout(Graph graph) {
    final Layout graphLayout = graph.getLayout();
    // Sort the vertices
    final List<Vertex> sortedVertices = graph.getDisplayVertices().stream().sorted(new Comparator<Vertex>() {

        @Override
        public int compare(Vertex v1, Vertex v2) {
            return ComparisonChain.start().compare(getIndex(v1), getIndex(v2)).compare(v1.getLabel(), v2.getLabel()).compare(v1.getId(), v2.getId()).result();
        }
    }).collect(Collectors.toList());
    // Find the smallest rectangle (grid) that will fit all the vertices
    // while attempting to preserve the aspect ration of the view port
    final int numberOfVertices = sortedVertices.size();
    final BoundingBox layoutBounds = graphLayout.computeBoundingBox(new ArrayList<>(sortedVertices));
    final BoundingBox grid = calculateGrid(numberOfVertices, layoutBounds.getWidth(), layoutBounds.getHeight());
    // Layout the (sorted) vertices in the grid
    int k = 0;
    for (int y = 0; y < grid.getHeight(); y++) {
        for (int x = 0; x < grid.getWidth(); x++) {
            if (k >= numberOfVertices) {
                break;
            }
            graphLayout.setLocation(sortedVertices.get(k++), new Point(x * ELBOW_ROOM * 2, y * ELBOW_ROOM * 2));
        }
    }
}
Also used : Vertex(org.opennms.features.topology.api.topo.Vertex) Layout(org.opennms.features.topology.api.Layout) BoundingBox(org.opennms.features.topology.api.BoundingBox) Point(org.opennms.features.topology.api.Point) Point(org.opennms.features.topology.api.Point) Comparator(java.util.Comparator)

Example 9 with Layout

use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.

the class LayoutManager method isPersistedLayoutEqualToCurrentLayout.

public boolean isPersistedLayoutEqualToCurrentLayout(Graph graph) {
    LayoutEntity layoutEntity = loadLayout(graph);
    if (layoutEntity != null) {
        // If we have a layout persisted, we verify if it is equal.
        final Map<VertexRef, Point> persistedLocations = layoutEntity.getVertexPositions().stream().collect(Collectors.toMap((Function<VertexPositionEntity, VertexRef>) vertexPositionEntity -> {
            VertexRefEntity vertexRefEntity = vertexPositionEntity.getVertexRef();
            return new DefaultVertexRef(vertexRefEntity.getNamespace(), vertexRefEntity.getId());
        }, vertexPositionEntity -> {
            PointEntity position = vertexPositionEntity.getPosition();
            return new Point(position.getX(), position.getY());
        }));
        // The locations may contain elements currently not visible, we filter them
        final Map<VertexRef, Point> manualLocations = new HashMap<>();
        graph.getLayout().getLocations().forEach((key, value) -> {
            if (persistedLocations.containsKey(key)) {
                // layoutEntity stores int coordinates, but manualLocations are stored as double.
                // Convert to int to make it comparable.
                manualLocations.put(key, new Point((int) value.getX(), (int) value.getY()));
            }
        });
        final boolean layoutIsEqual = manualLocations.equals(persistedLocations);
        return layoutIsEqual;
    }
    // We don't have anything persisted, so they are not equal
    return false;
}
Also used : GraphContainer(org.opennms.features.topology.api.GraphContainer) Date(java.util.Date) Vertex(org.opennms.features.topology.api.topo.Vertex) HashMap(java.util.HashMap) Hashing(com.google.common.hash.Hashing) Function(java.util.function.Function) VertexPositionEntity(org.opennms.netmgt.topology.persistence.api.VertexPositionEntity) Point(org.opennms.features.topology.api.Point) Layout(org.opennms.features.topology.api.Layout) Map(java.util.Map) LayoutDao(org.opennms.netmgt.topology.persistence.api.LayoutDao) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) Collection(java.util.Collection) Graph(org.opennms.features.topology.api.Graph) Collectors(java.util.stream.Collectors) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) TransactionAwareBeanProxyFactory(org.opennms.netmgt.vaadin.core.TransactionAwareBeanProxyFactory) List(java.util.List) VertexRefEntity(org.opennms.netmgt.topology.persistence.api.VertexRefEntity) TransactionOperations(org.springframework.transaction.support.TransactionOperations) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Comparator(java.util.Comparator) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Function(java.util.function.Function) VertexRefEntity(org.opennms.netmgt.topology.persistence.api.VertexRefEntity) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) HashMap(java.util.HashMap) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Point(org.opennms.features.topology.api.Point) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef)

Example 10 with Layout

use of org.opennms.features.topology.api.Layout in project opennms by OpenNMS.

the class TopoFRLayoutAlgorithm method updateLayout.

@Override
public void updateLayout(final Graph graph) {
    final Layout graphLayout = graph.getLayout();
    SparseGraph<VertexRef, EdgeRef> jungGraph = new SparseGraph<VertexRef, EdgeRef>();
    Collection<Vertex> vertices = graph.getDisplayVertices();
    for (Vertex v : vertices) {
        jungGraph.addVertex(v);
    }
    Collection<Edge> edges = graph.getDisplayEdges();
    for (Edge e : edges) {
        jungGraph.addEdge(e, e.getSource().getVertex(), e.getTarget().getVertex());
    }
    TopoFRLayout<VertexRef, EdgeRef> layout = new TopoFRLayout<VertexRef, EdgeRef>(jungGraph);
    // Initialize the vertex positions to the last known positions from the layout
    Dimension size = selectLayoutSize(graph);
    layout.setInitializer(initializer(graphLayout, (int) size.getWidth() / 2, (int) size.getHeight() / 2));
    // Resize the graph to accommodate the number of vertices
    layout.setSize(size);
    while (!layout.done()) {
        layout.step();
    }
    // Store the new positions in the layout
    for (Vertex v : vertices) {
        graphLayout.setLocation(v, new Point(layout.getX(v) - (size.getWidth() / 2), (int) layout.getY(v) - (size.getHeight() / 2)));
    }
}
Also used : Vertex(org.opennms.features.topology.api.topo.Vertex) Dimension(java.awt.Dimension) Point(org.opennms.features.topology.api.Point) SparseGraph(edu.uci.ics.jung.graph.SparseGraph) Layout(org.opennms.features.topology.api.Layout) EdgeRef(org.opennms.features.topology.api.topo.EdgeRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef) Edge(org.opennms.features.topology.api.topo.Edge)

Aggregations

Layout (org.opennms.features.topology.api.Layout)15 Point (org.opennms.features.topology.api.Point)13 Vertex (org.opennms.features.topology.api.topo.Vertex)12 VertexRef (org.opennms.features.topology.api.topo.VertexRef)12 Edge (org.opennms.features.topology.api.topo.Edge)10 SparseGraph (edu.uci.ics.jung.graph.SparseGraph)8 EdgeRef (org.opennms.features.topology.api.topo.EdgeRef)6 Dimension (java.awt.Dimension)4 Collection (java.util.Collection)4 Comparator (java.util.Comparator)4 Graph (org.opennms.features.topology.api.Graph)4 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 LayoutEntity (org.opennms.netmgt.topology.persistence.api.LayoutEntity)3 PointEntity (org.opennms.netmgt.topology.persistence.api.PointEntity)3 Hashing (com.google.common.hash.Hashing)2 FRLayout (edu.uci.ics.jung.algorithms.layout.FRLayout)2 SpringLayout (edu.uci.ics.jung.algorithms.layout.SpringLayout)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Date (java.util.Date)2