Search in sources :

Example 16 with ElkEdge

use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.

the class LabelSideSelectorTest method configurator.

@Configurator
public void configurator(final ElkNode graph) {
    Deque<ElkNode> nodeQueue = new LinkedList<>();
    nodeQueue.add(graph);
    Random rand = new Random(RANDOM_SEED);
    while (!nodeQueue.isEmpty()) {
        ElkNode node = nodeQueue.poll();
        for (ElkEdge edge : node.getContainedEdges()) {
            if (edge.getLabels().isEmpty()) {
                ElkGraphUtil.createLabel(edge.getIdentifier(), edge);
            }
            ElkLabel label = edge.getLabels().get(0);
            double r = rand.nextDouble();
            if (r < 0.3) {
                label.setProperty(LayeredOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.HEAD);
            } else if (r < 0.7) {
                label.setProperty(LayeredOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.CENTER);
            } else {
                label.setProperty(LayeredOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.TAIL);
            }
        }
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) Random(java.util.Random) ElkLabel(org.eclipse.elk.graph.ElkLabel) LinkedList(java.util.LinkedList) ElkEdge(org.eclipse.elk.graph.ElkEdge) Configurator(org.eclipse.elk.alg.test.framework.annotations.Configurator)

Example 17 with ElkEdge

use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.

the class OverallLayoutTest method testEdgeCoordinates.

/**
 * Tests if the source and target coordinates of all edges are > 0. Doesn't work for
 * hierarchical graphs.
 */
@Test
public void testEdgeCoordinates() {
    for (ElkEdge edge : simpleGraph.getContainedEdges()) {
        for (ElkEdgeSection section : edge.getSections()) {
            assertTrue(section.getStartX() > 0);
            assertTrue(section.getStartY() > 0);
            assertTrue(section.getEndX() > 0);
            assertTrue(section.getEndY() > 0);
        }
    }
}
Also used : ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) ElkEdge(org.eclipse.elk.graph.ElkEdge) Test(org.junit.Test)

Example 18 with ElkEdge

use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.

the class InteractiveLayeredGraphVisitor method shiftOtherNodes.

/**
 * Shifts nodes to the right such that edges in the same layer do not exist.
 *
 * @param movedNode
 *            The node which connected nodes must be shifted .
 * @param layer
 *            The layer {@code moveNode} is in.
 * @param layerNodes
 *            All existing layers with the containing nodes.
 * @param incoming
 *            Determines if incoming or outgoing edges should be considered. True: incoming edges.
 */
private void shiftOtherNodes(final ElkNode movedNode, final int layer, final List<List<ElkNode>> layerNodes, final boolean incoming) {
    List<ElkNode> nodesOfLayer = layerNodes.get(layer);
    // get edges
    List<ElkEdge> edges = new ArrayList<>();
    if (incoming) {
        ElkNode root = movedNode.getParent();
        for (ElkEdge edge : root.getContainedEdges()) {
            for (ElkConnectableShape target : edge.getTargets()) {
                if (target.equals(movedNode) || (target instanceof ElkPort && target.eContainer().equals(movedNode))) {
                    edges.add(edge);
                }
            }
        }
    } else {
        ElkNode root = movedNode.getParent();
        for (ElkEdge edge : root.getContainedEdges()) {
            for (ElkConnectableShape target : edge.getSources()) {
                if (target.equals(movedNode) || (target instanceof ElkPort && target.eContainer().equals(movedNode))) {
                    edges.add(edge);
                }
            }
        }
    }
    for (ElkEdge edge : edges) {
        // get connected node
        ElkNode node = null;
        if (incoming) {
            if (edge.getSources().get(0) instanceof ElkPort) {
                node = (ElkNode) edge.getSources().get(0).eContainer();
            } else if (edge.getSources().get(0) instanceof ElkNode) {
                node = (ElkNode) edge.getSources().get(0);
            }
        } else {
            if (edge.getTargets().get(0) instanceof ElkPort) {
                node = (ElkNode) edge.getTargets().get(0).eContainer();
            } else if (edge.getTargets().get(0) instanceof ElkNode) {
                node = (ElkNode) edge.getTargets().get(0);
            }
        }
        // shift node to the next layer
        if (nodesOfLayer.contains(node)) {
            nodesOfLayer.remove(node);
            List<ElkNode> newLayer;
            if (layer + 1 < layerNodes.size()) {
                newLayer = layerNodes.get(layer + 1);
                newLayer.add(node);
                // the connected nodes in the layer the node is shifted to must be shifted too
                shiftOtherNodes(node, layer + 1, layerNodes, false);
                shiftOtherNodes(node, layer + 1, layerNodes, true);
            } else {
                layerNodes.add(new ArrayList<>(Arrays.asList(node)));
            }
        }
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) ArrayList(java.util.ArrayList) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 19 with ElkEdge

use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.

the class DotImporter method applyLayout.

/*---------- Layout Transfer KGraph to Dot ----------*/
/**
 * Apply layout to a parent node and its children.
 *
 * @param parent a parent node
 * @param offset the node's offset in the graph
 * @param graph the Graphviz graph
 */
private void applyLayout(final ElkNode parent, final KVector offset, final Graph graph) {
    for (ElkNode elknode : parent.getChildren()) {
        Statement statement = elknode.getProperty(PROP_STATEMENT);
        if (statement == null) {
            // the node was only declared implicitly - create an explicit declaration
            NodeStatement stm = DotFactory.eINSTANCE.createNodeStatement();
            Node node = DotFactory.eINSTANCE.createNode();
            node.setName(elknode.getProperty(PROP_ID));
            stm.setNode(node);
            graph.getStatements().add(stm);
            statement = stm;
        }
        if (statement instanceof NodeStatement) {
            List<Attribute> attributes = ((NodeStatement) statement).getAttributes();
            // transfer node position
            removeAttributes(attributes, Attributes.POS);
            double xpos = elknode.getX() + elknode.getWidth() / 2 + offset.x;
            double ypos = elknode.getY() + elknode.getHeight() / 2 + offset.y;
            String posString = "\"" + Double.toString(xpos) + "," + Double.toString(ypos) + "\"";
            attributes.add(DotExporter.createAttribute(Attributes.POS, posString));
            // transfer node size
            removeAttributes(attributes, Attributes.WIDTH);
            attributes.add(DotExporter.createAttribute(Attributes.WIDTH, elknode.getWidth() / DotExporter.DPI));
            removeAttributes(attributes, Attributes.HEIGHT);
            attributes.add(DotExporter.createAttribute(Attributes.HEIGHT, elknode.getHeight() / DotExporter.DPI));
        } else if (statement instanceof Subgraph) {
            applyLayout(elknode, new KVector(offset).add(elknode.getX(), elknode.getY()), graph);
        }
        for (ElkEdge elkedge : ElkGraphUtil.allOutgoingEdges(elknode)) {
            applyLayout(elkedge, offset, graph);
        }
    }
    // transfer graph size to bounding box
    List<Statement> statements;
    Statement graphStm = parent.getProperty(PROP_STATEMENT);
    if (graphStm instanceof Subgraph) {
        statements = ((Subgraph) graphStm).getStatements();
    } else {
        statements = graph.getStatements();
    }
    removeGraphAttributes(statements, Attributes.BOUNDINGBOX);
    String bbString = "\"0,0," + Double.toString(parent.getWidth()) + "," + Double.toString(parent.getHeight()) + "\"";
    statements.add(DotExporter.createAttribute(Attributes.BOUNDINGBOX, bbString));
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) EdgeStatement(org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement) NodeStatement(org.eclipse.elk.alg.graphviz.dot.dot.NodeStatement) AttributeStatement(org.eclipse.elk.alg.graphviz.dot.dot.AttributeStatement) Statement(org.eclipse.elk.alg.graphviz.dot.dot.Statement) NodeStatement(org.eclipse.elk.alg.graphviz.dot.dot.NodeStatement) ElkNode(org.eclipse.elk.graph.ElkNode) Node(org.eclipse.elk.alg.graphviz.dot.dot.Node) Subgraph(org.eclipse.elk.alg.graphviz.dot.dot.Subgraph) KVector(org.eclipse.elk.core.math.KVector) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 20 with ElkEdge

use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.

the class ElkGraphImporter method importFlatGraph.

/**
 * Imports the direct children of the given graph.
 *
 * @param elkgraph
 *            graph to import.
 * @param lgraph
 *            graph to add the imported elements to.
 */
private void importFlatGraph(final ElkNode elkgraph, final LGraph lgraph) {
    // Transform the node's children, unless we're told not to
    int index = 0;
    for (ElkNode child : elkgraph.getChildren()) {
        if (!child.getProperty(LayeredOptions.NO_LAYOUT)) {
            if (elkgraph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY) != OrderingStrategy.NONE || elkgraph.getProperty(LayeredOptions.CYCLE_BREAKING_STRATEGY) == CycleBreakingStrategy.MODEL_ORDER) {
                child.setProperty(InternalProperties.MODEL_ORDER, index);
                index++;
            }
            transformNode(child, lgraph);
        }
    }
    // iterate the list of contained edges to preserve the 'input order' of the edges
    // (this is not part of the previous loop since all children must have already been transformed)
    index = 0;
    for (ElkEdge elkedge : elkgraph.getContainedEdges()) {
        if (elkgraph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY) != OrderingStrategy.NONE || elkgraph.getProperty(LayeredOptions.CYCLE_BREAKING_STRATEGY) == CycleBreakingStrategy.MODEL_ORDER) {
            elkedge.setProperty(InternalProperties.MODEL_ORDER, index);
            index++;
        }
        ElkNode source = ElkGraphUtil.getSourceNode(elkedge);
        ElkNode target = ElkGraphUtil.getTargetNode(elkedge);
        // Is inside self loop processing enabled for this node?
        boolean enableInsideSelfLoops = source.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE);
        // Find out basic information about the edge
        boolean isToBeLaidOut = !elkedge.getProperty(LayeredOptions.NO_LAYOUT);
        boolean isInsideSelfLoop = enableInsideSelfLoops && elkedge.isSelfloop() && elkedge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
        boolean connectsSiblings = source.getParent() == elkgraph && source.getParent() == target.getParent();
        boolean connectsToGraph = (source.getParent() == elkgraph && target == elkgraph) ^ (target.getParent() == elkgraph && source == elkgraph);
        // level of hierarchy (which implies that here we don't transform inside self loops)
        if (isToBeLaidOut && !isInsideSelfLoop && (connectsToGraph || connectsSiblings)) {
            transformEdge(elkedge, elkgraph, lgraph);
        }
    }
    // now collect inside self loops of 'elkgraph'
    if (elkgraph.getParent() != null) {
        for (ElkEdge elkedge : elkgraph.getParent().getContainedEdges()) {
            ElkNode source = ElkGraphUtil.getSourceNode(elkedge);
            if (source == elkgraph && elkedge.isSelfloop()) {
                boolean isInsideSelfLoop = source.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE) && elkedge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
                if (isInsideSelfLoop) {
                    transformEdge(elkedge, elkgraph, lgraph);
                }
            }
        }
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Aggregations

ElkEdge (org.eclipse.elk.graph.ElkEdge)73 ElkNode (org.eclipse.elk.graph.ElkNode)59 ElkPort (org.eclipse.elk.graph.ElkPort)27 KVector (org.eclipse.elk.core.math.KVector)23 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)21 ElkLabel (org.eclipse.elk.graph.ElkLabel)20 ElkConnectableShape (org.eclipse.elk.graph.ElkConnectableShape)10 LinkedList (java.util.LinkedList)9 ElkBendPoint (org.eclipse.elk.graph.ElkBendPoint)9 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)9 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 ElkPadding (org.eclipse.elk.core.math.ElkPadding)6 KVectorChain (org.eclipse.elk.core.math.KVectorChain)5 HashSet (java.util.HashSet)4 Lists (com.google.common.collect.Lists)3 Collection (java.util.Collection)3 Set (java.util.Set)3