Search in sources :

Example 11 with ElkPort

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

the class PortListSorterTest method configurePorts.

// ////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Configuration
@Configurator
public void configurePorts(final ElkNode graph) {
    Deque<ElkNode> nodeQueue = new LinkedList<>(graph.getChildren());
    while (!nodeQueue.isEmpty()) {
        ElkNode node = nodeQueue.poll();
        nodeQueue.addAll(node.getChildren());
        if (node.getProperty(LayeredOptions.PORT_CONSTRAINTS) != PortConstraints.FIXED_ORDER) {
            node.setProperty(LayeredOptions.PORT_CONSTRAINTS, PortConstraints.FIXED_ORDER);
            int index = 0;
            for (ElkPort port : node.getPorts()) {
                port.setProperty(LayeredOptions.PORT_INDEX, index++);
            }
        }
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) LinkedList(java.util.LinkedList) Configurator(org.eclipse.elk.alg.test.framework.annotations.Configurator)

Example 12 with ElkPort

use of org.eclipse.elk.graph.ElkPort 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 13 with ElkPort

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

the class DotImporter method transform.

/**
 * Transform the GraphViz model into a KGraph.
 *
 * @param transData
 *            the transformation data instance that holds the source graph and is enriched with
 *            the new target graphs
 */
public void transform(final IDotTransformationData<GraphvizModel, ElkNode> transData) {
    for (Graph graph : transData.getSourceGraph().getGraphs()) {
        ElkNode parent = ElkGraphUtil.createGraph();
        Map<String, ElkNode> nodeIdMap = Maps.newHashMap();
        transData.setProperty(NODE_ID_MAP, nodeIdMap);
        Map<Pair<ElkNode, String>, ElkPort> portIdMap = Maps.newHashMap();
        transData.setProperty(PORT_ID_MAP, portIdMap);
        transform(graph.getStatements(), parent, transData, new MapPropertyHolder(), new MapPropertyHolder());
        transData.getTargetGraphs().add(parent);
        parent.setProperty(PROP_GRAPH, graph);
    }
}
Also used : Graph(org.eclipse.elk.alg.graphviz.dot.dot.Graph) ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) MapPropertyHolder(org.eclipse.elk.graph.properties.MapPropertyHolder) Pair(org.eclipse.elk.core.util.Pair)

Example 14 with ElkPort

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

the class ElkGraphImporter method importGraph.

// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Import Entry Points
/**
 * Imports the given graph.
 *
 * @param elkgraph
 *            the graph to import.
 * @return the transformed graph.
 */
public LGraph importGraph(final ElkNode elkgraph) {
    // Create the layered graph
    final LGraph topLevelGraph = createLGraph(elkgraph);
    // Assign defined port sides to all external ports
    elkgraph.getPorts().stream().forEach(elkport -> ensureDefinedPortSide(topLevelGraph, elkport));
    // Transform the external ports, if any
    Set<GraphProperties> graphProperties = topLevelGraph.getProperty(InternalProperties.GRAPH_PROPERTIES);
    checkExternalPorts(elkgraph, graphProperties);
    if (graphProperties.contains(GraphProperties.EXTERNAL_PORTS)) {
        for (ElkPort elkport : elkgraph.getPorts()) {
            transformExternalPort(elkgraph, topLevelGraph, elkport);
        }
    }
    // Calculate the graph's minimum size
    if (shouldCalculateMinimumGraphSize(elkgraph)) {
        calculateMinimumGraphSize(elkgraph, topLevelGraph);
    }
    // Remember things
    if (topLevelGraph.getProperty(LayeredOptions.PARTITIONING_ACTIVATE)) {
        graphProperties.add(GraphProperties.PARTITIONS);
    }
    // values of the first layout run would be used (explicitly set spacing values are not overwritten).
    if (topLevelGraph.hasProperty(LayeredOptions.SPACING_BASE_VALUE)) {
        LayeredSpacings.withBaseValue(topLevelGraph.getProperty(LayeredOptions.SPACING_BASE_VALUE)).apply(topLevelGraph);
    }
    // Import the graph either with or without multiple nested levels of hierarchy
    if (elkgraph.getProperty(LayeredOptions.HIERARCHY_HANDLING) == HierarchyHandling.INCLUDE_CHILDREN) {
        importHierarchicalGraph(elkgraph, topLevelGraph);
    } else {
        importFlatGraph(elkgraph, topLevelGraph);
    }
    return topLevelGraph;
}
Also used : GraphProperties(org.eclipse.elk.alg.layered.options.GraphProperties) ElkPort(org.eclipse.elk.graph.ElkPort) LGraph(org.eclipse.elk.alg.layered.graph.LGraph)

Example 15 with ElkPort

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

the class ElkGraphImporter method checkExternalPorts.

// /////////////////////////////////////////////////////////////////////////////////////////////////////////////
// External Port Transformation
/**
 * Checks if external ports processing should be active. This is the case if the parent node has
 * ports and at least one of the following conditions is true:
 * <ul>
 *   <li>
 *     Port label placement is set to {@code INSIDE} and at least one of the ports has a label.
 *   </li>
 *   <li>
 *     At least one of the ports has an edge that connects to the insides of the parent node.
 *   </li>
 *   <li>
 *     There is a self-loop that should be routed inside the node.
 *   </li>
 * </ul>
 *
 * @param elkgraph
 *            a KGraph we want to check for external ports.
 * @param graphProperties
 *            the set of graph properties to store our results in.
 */
private void checkExternalPorts(final ElkNode elkgraph, final Set<GraphProperties> graphProperties) {
    final boolean enableSelfLoops = elkgraph.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE);
    final Set<PortLabelPlacement> portLabelPlacement = elkgraph.getProperty(LayeredOptions.PORT_LABELS_PLACEMENT);
    // We're iterating over the ports until we've determined that we have both external ports and
    // hyperedges, or if there are no more ports left
    boolean hasExternalPorts = false;
    boolean hasHyperedges = false;
    final Iterator<ElkPort> portIterator = elkgraph.getPorts().iterator();
    while (portIterator.hasNext() && (!hasExternalPorts || !hasHyperedges)) {
        final ElkPort elkport = portIterator.next();
        // Find out if there are edges connected to external ports of the graph (this is the case
        // for inside self loops as well as for edges connected to children)
        int externalPortEdges = 0;
        for (ElkEdge elkedge : ElkGraphUtil.allIncidentEdges(elkport)) {
            boolean isInsideSelfLoop = enableSelfLoops && elkedge.isSelfloop() && elkedge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
            boolean connectsToChild = elkedge.getSources().contains(elkport) ? elkgraph == ElkGraphUtil.connectableShapeToNode(elkedge.getTargets().get(0)).getParent() : elkgraph == ElkGraphUtil.connectableShapeToNode(elkedge.getSources().get(0)).getParent();
            if (isInsideSelfLoop || connectsToChild) {
                externalPortEdges++;
                if (externalPortEdges > 1) {
                    break;
                }
            }
        }
        // External ports?
        if (externalPortEdges > 0) {
            hasExternalPorts = true;
        } else if (portLabelPlacement.contains(PortLabelPlacement.INSIDE) && elkport.getLabels().size() > 0) {
            hasExternalPorts = true;
        }
        // Hyperedges, even?
        if (externalPortEdges > 1) {
            hasHyperedges = true;
        }
    }
    // Update graph properties
    if (hasExternalPorts) {
        graphProperties.add(GraphProperties.EXTERNAL_PORTS);
    }
    if (hasHyperedges) {
        graphProperties.add(GraphProperties.HYPEREDGES);
    }
}
Also used : PortLabelPlacement(org.eclipse.elk.core.options.PortLabelPlacement) ElkPort(org.eclipse.elk.graph.ElkPort) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Aggregations

ElkPort (org.eclipse.elk.graph.ElkPort)76 ElkNode (org.eclipse.elk.graph.ElkNode)55 ElkEdge (org.eclipse.elk.graph.ElkEdge)32 ElkLabel (org.eclipse.elk.graph.ElkLabel)29 KVector (org.eclipse.elk.core.math.KVector)19 ElkConnectableShape (org.eclipse.elk.graph.ElkConnectableShape)15 ElkGraphElement (org.eclipse.elk.graph.ElkGraphElement)13 Test (org.junit.Test)13 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)12 PortSide (org.eclipse.elk.core.options.PortSide)11 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)11 List (java.util.List)10 ElkPadding (org.eclipse.elk.core.math.ElkPadding)8 ElkGraphUtil (org.eclipse.elk.graph.util.ElkGraphUtil)8 Lists (com.google.common.collect.Lists)7 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 Collectors (java.util.stream.Collectors)7 Direction (org.eclipse.elk.core.options.Direction)7 PortConstraints (org.eclipse.elk.core.options.PortConstraints)7