Search in sources :

Example 1 with Graph

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

the class OSGiVerticesUpdateManagerTest method createGraph.

private GraphContainer createGraph(int... vertIds) {
    final List<Vertex> vertexRefsWithIds = createVerticsWithIds(vertIds);
    final Graph graphMock = EasyMock.createNiceMock(Graph.class);
    final GraphContainer graphContainerMock = EasyMock.createNiceMock(GraphContainer.class);
    EasyMock.expect(graphMock.getDisplayVertices()).andReturn(vertexRefsWithIds).anyTimes();
    EasyMock.expect(graphContainerMock.getGraph()).andReturn(graphMock).anyTimes();
    EasyMock.replay(graphMock, graphContainerMock);
    return graphContainerMock;
}
Also used : GraphContainer(org.opennms.features.topology.api.GraphContainer) Vertex(org.opennms.features.topology.api.topo.Vertex) Graph(org.opennms.features.topology.api.Graph)

Example 2 with Graph

use of org.opennms.features.topology.api.Graph 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) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Point(org.opennms.features.topology.api.Point) DefaultVertexRef(org.opennms.features.topology.api.topo.DefaultVertexRef) VertexRef(org.opennms.features.topology.api.topo.VertexRef)

Example 3 with Graph

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

the class ManualLayoutAlgorithm method updateLayout.

@Override
public void updateLayout(Graph graph) {
    final LayoutEntity layoutEntity = layoutManager != null ? layoutManager.loadLayout(graph) : null;
    if (layoutEntity != null) {
        // if we have a persisted layout, we apply it ...
        final Layout layout = graph.getLayout();
        final Collection<Vertex> vertices = graph.getDisplayVertices();
        for (Vertex vertex : vertices) {
            PointEntity pointEntity = layoutEntity.getPosition(vertex.getNamespace(), vertex.getId());
            layout.setLocation(vertex, new Point(pointEntity.getX(), pointEntity.getY()));
        }
    } else {
        // otherwise we apply the manual layout ...
        final Collection<Vertex> vertices = graph.getDisplayVertices();
        final Layout layout = graph.getLayout();
        final long notLayedOutCount = vertices.stream().filter(v -> {
            Point location = layout.getLocation(v);
            return location.getX() == 0 && location.getY() == 0;
        }).count();
        final long noVertexLocationCount = vertices.stream().filter(v -> {
            boolean hasNoX = v.getX() == null || v.getX().intValue() == 0;
            boolean hasNoY = v.getY() == null || v.getY().intValue() == 0;
            return hasNoX && hasNoY;
        }).count();
        // manually apply the Grid Layout
        if (notLayedOutCount == vertices.size() && noVertexLocationCount == vertices.size()) {
            new GridLayoutAlgorithm().updateLayout(graph);
        } else if (noVertexLocationCount != vertices.size()) {
            // If we have at least one vertex with coordinates != (0,0), we apply them to the layout
            for (Vertex vertex : vertices) {
                layout.setLocation(vertex, new Point(vertex.getX(), vertex.getY()));
            }
        } else {
            // This is only done when the user explicitly saves the layout
            for (Vertex vertex : vertices) {
                Point p = layout.getLocation(vertex);
                layout.setLocation(vertex, p);
            }
        }
    }
}
Also used : LayoutManager(org.opennms.features.topology.app.internal.support.LayoutManager) Point(org.opennms.features.topology.api.Point) GridLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm) Layout(org.opennms.features.topology.api.Layout) Collection(java.util.Collection) Vertex(org.opennms.features.topology.api.topo.Vertex) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) Graph(org.opennms.features.topology.api.Graph) LayoutAlgorithm(org.opennms.features.topology.api.LayoutAlgorithm) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Vertex(org.opennms.features.topology.api.topo.Vertex) Layout(org.opennms.features.topology.api.Layout) LayoutEntity(org.opennms.netmgt.topology.persistence.api.LayoutEntity) PointEntity(org.opennms.netmgt.topology.persistence.api.PointEntity) Point(org.opennms.features.topology.api.Point) GridLayoutAlgorithm(org.opennms.features.topology.app.internal.jung.GridLayoutAlgorithm)

Example 4 with Graph

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

the class TopologyComponent method graphChanged.

@Override
public void graphChanged(GraphContainer container) {
    Graph graph = container.getGraph();
    setGraph(graph);
    if (!m_blockSelectionEvents) {
        computeBoundsForSelected(m_graphContainer.getSelectionManager());
    }
    updateGraph();
}
Also used : Graph(org.opennms.features.topology.api.Graph)

Example 5 with Graph

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

the class TopologyComponent method updateGraph.

public void updateGraph() {
    BoundingBox boundingBox = getBoundingBox();
    getState().setBoundX(boundingBox.getX());
    getState().setBoundY(boundingBox.getY());
    getState().setBoundWidth(boundingBox.getWidth());
    getState().setBoundHeight(boundingBox.getHeight());
    getState().setActiveTool(m_activeTool);
    Graph graph = getGraph();
    GraphVisitor painter = new GraphPainter(m_graphContainer, graph.getLayout(), m_iconRepoManager, getState());
    try {
        graph.visit(painter);
    } catch (Exception e) {
        LoggerFactory.getLogger(getClass()).error(e.getMessage(), e);
    }
}
Also used : Graph(org.opennms.features.topology.api.Graph) GraphVisitor(org.opennms.features.topology.api.GraphVisitor) BoundingBox(org.opennms.features.topology.api.BoundingBox)

Aggregations

Graph (org.opennms.features.topology.api.Graph)10 Vertex (org.opennms.features.topology.api.topo.Vertex)5 Test (org.junit.Test)4 VertexRef (org.opennms.features.topology.api.topo.VertexRef)4 SparseGraph (edu.uci.ics.jung.graph.SparseGraph)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 GraphContainer (org.opennms.features.topology.api.GraphContainer)2 Layout (org.opennms.features.topology.api.Layout)2 Point (org.opennms.features.topology.api.Point)2 DefaultVertexRef (org.opennms.features.topology.api.topo.DefaultVertexRef)2 EdgeRef (org.opennms.features.topology.api.topo.EdgeRef)2 LayoutEntity (org.opennms.netmgt.topology.persistence.api.LayoutEntity)2 PointEntity (org.opennms.netmgt.topology.persistence.api.PointEntity)2 Hashing (com.google.common.hash.Hashing)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Comparator (java.util.Comparator)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1