Search in sources :

Example 26 with ElkPort

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

the class RandomGraphGenerator method connect.

/**
 * Connects two nodes with an edge.
 *
 * @param source
 *            the source node
 * @param target
 *            the target node
 * @param directed
 *            whether the edge should be directed or undirected
 * @return the edge
 */
private ElkEdge connect(final ElkConnectableShape source, final ElkConnectableShape target) {
    ElkNode sourceNode = null;
    ElkNode targetNode = null;
    ElkPort sourcePort = null;
    ElkPort targetPort = null;
    if (source instanceof ElkNode) {
        sourceNode = (ElkNode) source;
    }
    if (source instanceof ElkPort) {
        sourcePort = (ElkPort) source;
        sourceNode = sourcePort.getParent();
    }
    if (target instanceof ElkPort) {
        targetPort = (ElkPort) target;
        targetNode = targetPort.getParent();
    }
    if (target instanceof ElkNode) {
        targetNode = (ElkNode) target;
    }
    ElkEdge edge = ElkGraphUtil.createEdge(null);
    if (get(GeneratorOptions.ENABLE_PORTS)) {
        if (source instanceof ElkNode) {
            sourcePort = retrievePort(sourceNode, true);
        }
        if (!isHypernode(sourceNode)) {
            // remove node
            if (edge.getSources().contains(sourceNode)) {
                edge.getSources().remove(sourceNode);
            }
            edge.getSources().add(sourcePort);
        // edge.setSourcePort(sourcePort);
        }
        if (!isHypernode(targetNode)) {
            if (targetPort == null) {
                targetPort = retrievePort(targetNode, false);
            }
            if (edge.getTargets().contains(targetNode)) {
                edge.getTargets().remove(targetNode);
            }
            edge.getTargets().remove(target);
            edge.getTargets().add(targetPort);
        // edge.setTargetPort(targetPort);
        }
    } else {
        edge.getSources().add(sourceNode);
        edge.getTargets().add(targetNode);
    }
    if (get(GeneratorOptions.EDGE_LABELS)) {
        addEdgeLabel(edge);
    }
    ElkGraphUtil.updateContainment(edge);
    return edge;
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 27 with ElkPort

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

the class RandomGraphGenerator method retrievePort.

/**
 * Retrieves a port for a new edge to connect to the given node through. This can either be a
 * newly created port, or an existing one. Which one it is depends on the chance of ports to be
 * reused.
 *
 * <p>
 * An outgoing edge will only ever try to reuse ports that only have outgoing edges connected to
 * them. The same is true for incoming edges and ports with only incoming edges.
 * </p>
 *
 * @param node
 *            the node to add the port to.
 * @param source
 *            {@code true} if the port will be used as a source port, {@code false} if it will
 *            be used as a target port.
 * @return the new or existing port.
 */
private ElkPort retrievePort(final ElkNode node, final boolean source) {
    // We might want to reuse an existing port
    float reusePortsChance = get(GeneratorOptions.USE_EXISTING_PORTS_CHANCE).floatVal(random);
    if (reusePortsChance > 0.0f && random.nextFloat() < reusePortsChance) {
        // Collect candidate ports for reuse
        List<ElkPort> reuseCandidates = Lists.newLinkedList();
        for (ElkPort port : node.getPorts()) {
            // Two flags indicating whether the port already has edges pointing in the right
            // or wrong direction connected to it
            boolean connectedToDesiredEdges = false;
            boolean connectedToBadEdges = false;
            // for (ElkEdge edge : port.getEdges()) {
            // connectedToDesiredEdges = (source && edge.getSourcePort() == port)
            // || (!source && edge.getTargetPort() == port);
            // connectedToBadEdges = (source && edge.getTargetPort() == port)
            // || (!source && edge.getSourcePort() == port);
            // }
            // TODO -apo- not exactly the same, but makes more sense...
            connectedToDesiredEdges = (!port.getIncomingEdges().isEmpty() && !source) || (!port.getOutgoingEdges().isEmpty() && source);
            connectedToBadEdges = (!port.getIncomingEdges().isEmpty() && source) || (!port.getOutgoingEdges().isEmpty() && !source);
            // the port, it qualifies as a candidate for reuse
            if (connectedToDesiredEdges && !connectedToBadEdges) {
                reuseCandidates.add(port);
            }
        }
        // If there are candidates for reuse, choose one at random
        if (!reuseCandidates.isEmpty()) {
            return reuseCandidates.get(randomInt(0, reuseCandidates.size() - 1));
        }
    }
    // We were unable to reuse an existing port, so create a new one and return that
    return createPort(node, source);
}
Also used : ElkPort(org.eclipse.elk.graph.ElkPort)

Example 28 with ElkPort

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

the class RandomGraphGenerator method findNodeWithDFS.

/**
 * Returns whether a node can be reached from another node. PRECONDITION: the graph containing
 * the nodes has to be acyclic! If that condition is not met the method is likely to loop
 * infinitely!
 *
 * @param start
 *            the start node
 * @param end
 *            the end node
 * @return whether the end node can be reached from the start node
 */
private static boolean findNodeWithDFS(final ElkNode start, final ElkNode end) {
    Queue<ElkNode> nodes = new LinkedList<ElkNode>();
    nodes.add(start);
    do {
        ElkNode node = nodes.poll();
        if (node == end) {
            return true;
        }
        for (ElkEdge edge : node.getOutgoingEdges()) {
            for (ElkConnectableShape target : edge.getTargets()) {
                if (target instanceof ElkNode) {
                    nodes.add((ElkNode) target);
                } else if (target instanceof ElkPort) {
                    ElkPort port = (ElkPort) target;
                    nodes.add(port.getParent());
                }
            }
        }
    } while (!nodes.isEmpty());
    return false;
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) ElkPort(org.eclipse.elk.graph.ElkPort) ElkConnectableShape(org.eclipse.elk.graph.ElkConnectableShape) LinkedList(java.util.LinkedList) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 29 with ElkPort

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

the class RandomGraphGenerator method createPort.

/**
 * Creates a port.
 *
 * @param node
 *            the containing node
 * @param source
 *            {@code true} if the port will be used as a source port, {@code false} if it will
 *            be used as a target port.
 * @return the port
 */
private ElkPort createPort(final ElkNode node, final boolean source) {
    ElkPort port = ElkGraphUtil.createPort(null);
    node.getPorts().add(port);
    // create label and identifier
    String portId = String.valueOf(portLabelCounter++);
    if (get(GeneratorOptions.CREATE_PORT_LABELS)) {
        ElkGraphUtil.createLabel("P" + portId, port);
    }
    port.setIdentifier("p" + portId);
    // set size of the port
    if (get(GeneratorOptions.SET_PORT_SIZE)) {
        port.setWidth(get(GeneratorOptions.PORT_WIDTH).floatVal(random));
        port.setHeight(get(GeneratorOptions.PORT_HEIGHT).floatVal(random));
    }
    if (get(GeneratorOptions.PORT_CONSTRAINTS).isSideFixed()) {
        // determine a random node side
        int northProb, eastProb, southProb, westProb;
        if (source) {
            northProb = get(GeneratorOptions.OUTGOING_NORTH_SIDE);
            eastProb = get(GeneratorOptions.OUTGOING_EAST_SIDE);
            southProb = get(GeneratorOptions.OUTGOING_SOUTH_SIDE);
            westProb = get(GeneratorOptions.OUTGOING_WEST_SIDE);
        } else {
            northProb = get(GeneratorOptions.INCOMING_NORTH_SIDE);
            eastProb = get(GeneratorOptions.INCOMING_EAST_SIDE);
            southProb = get(GeneratorOptions.INCOMING_SOUTH_SIDE);
            westProb = get(GeneratorOptions.INCOMING_WEST_SIDE);
        }
        int p = random.nextInt(northProb + eastProb + southProb + westProb);
        PortSide portSide;
        if (p < northProb) {
            portSide = PortSide.NORTH;
        } else if (p < northProb + eastProb) {
            portSide = PortSide.EAST;
        } else if (p < northProb + eastProb + southProb) {
            portSide = PortSide.SOUTH;
        } else {
            portSide = PortSide.WEST;
        }
        port.setProperty(CoreOptions.PORT_SIDE, portSide);
    }
    return port;
}
Also used : ElkPort(org.eclipse.elk.graph.ElkPort) PortSide(org.eclipse.elk.core.options.PortSide)

Example 30 with ElkPort

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

the class AbstractElkGraphSemanticSequencer method sequence.

@Override
public void sequence(ISerializationContext context, EObject semanticObject) {
    EPackage epackage = semanticObject.eClass().getEPackage();
    ParserRule rule = context.getParserRule();
    Action action = context.getAssignedAction();
    Set<Parameter> parameters = context.getEnabledBooleanParameters();
    if (epackage == ElkGraphPackage.eINSTANCE)
        switch(semanticObject.eClass().getClassifierID()) {
            case ElkGraphPackage.ELK_BEND_POINT:
                sequence_ElkBendPoint(context, (ElkBendPoint) semanticObject);
                return;
            case ElkGraphPackage.ELK_EDGE:
                sequence_EdgeLayout_ElkEdge(context, (ElkEdge) semanticObject);
                return;
            case ElkGraphPackage.ELK_EDGE_SECTION:
                if (rule == grammarAccess.getElkEdgeSectionRule()) {
                    sequence_ElkEdgeSection(context, (ElkEdgeSection) semanticObject);
                    return;
                } else if (rule == grammarAccess.getElkSingleEdgeSectionRule()) {
                    sequence_ElkSingleEdgeSection(context, (ElkEdgeSection) semanticObject);
                    return;
                } else
                    break;
            case ElkGraphPackage.ELK_LABEL:
                sequence_ElkLabel_ShapeLayout(context, (ElkLabel) semanticObject);
                return;
            case ElkGraphPackage.ELK_NODE:
                if (rule == grammarAccess.getElkNodeRule()) {
                    sequence_ElkNode_ShapeLayout(context, (ElkNode) semanticObject);
                    return;
                } else if (rule == grammarAccess.getRootNodeRule()) {
                    sequence_RootNode_ShapeLayout(context, (ElkNode) semanticObject);
                    return;
                } else
                    break;
            case ElkGraphPackage.ELK_PORT:
                sequence_ElkPort_ShapeLayout(context, (ElkPort) semanticObject);
                return;
            case ElkGraphPackage.ELK_PROPERTY_TO_VALUE_MAP_ENTRY:
                sequence_Property(context, (Map.Entry) semanticObject);
                return;
        }
    if (errorAcceptor != null)
        errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Action(org.eclipse.xtext.Action) ElkNode(org.eclipse.elk.graph.ElkNode) ElkLabel(org.eclipse.elk.graph.ElkLabel) ElkPort(org.eclipse.elk.graph.ElkPort) Parameter(org.eclipse.xtext.Parameter) ElkBendPoint(org.eclipse.elk.graph.ElkBendPoint) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) EPackage(org.eclipse.emf.ecore.EPackage) 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