Search in sources :

Example 51 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class DotExporter method transform.

/**
 * Transforms the KGraph instance to a Dot instance using the given command.
 *
 * @param transData the transformation data instance
 */
public void transform(final IDotTransformationData<ElkNode, GraphvizModel> transData) {
    BiMap<String, ElkGraphElement> graphElems = HashBiMap.create();
    transData.setProperty(GRAPH_ELEMS, graphElems);
    ElkNode elkgraph = transData.getSourceGraph();
    GraphvizModel graphvizModel = DotFactory.eINSTANCE.createGraphvizModel();
    Graph graph = DotFactory.eINSTANCE.createGraph();
    graph.setType(GraphType.DIGRAPH);
    graphvizModel.getGraphs().add(graph);
    transformNodes(elkgraph, graph.getStatements(), new KVector(), transData);
    transformEdges(elkgraph, graph.getStatements(), transData);
    transData.getTargetGraphs().add(graphvizModel);
}
Also used : GraphvizModel(org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel) Graph(org.eclipse.elk.alg.graphviz.dot.dot.Graph) ElkNode(org.eclipse.elk.graph.ElkNode) KVector(org.eclipse.elk.core.math.KVector) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement)

Example 52 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class DotExporter method transformNodes.

/**
 * Transform the child nodes of the given parent node.
 *
 * @param parent a parent node
 * @param statements the list to which new statements are added
 * @param offset offset of the parent node in the whole graph
 * @param transData transformation data
 */
private void transformNodes(final ElkNode parent, final List<Statement> statements, final KVector offset, final IDotTransformationData<ElkNode, GraphvizModel> transData) {
    // set attributes for the whole graph
    setGraphAttributes(statements, parent, transData);
    // create nodes and subgraphs
    boolean hierarchy = transData.getProperty(HIERARCHY);
    boolean transformNodeLayout = transData.getProperty(TRANSFORM_NODE_LAYOUT);
    boolean transformNodeLabels = transData.getProperty(TRANSFORM_NODE_LABELS);
    for (ElkNode childNode : parent.getChildren()) {
        NodeStatement nodeStatement = DotFactory.eINSTANCE.createNodeStatement();
        List<Attribute> attributes = nodeStatement.getAttributes();
        String nodeID;
        // if hierarchy mode is active, create a subgraph, else a regular node
        if (hierarchy && !childNode.getChildren().isEmpty()) {
            String clusterNodeID = getNodeID(childNode, NodeType.CLUSTER, transData);
            Subgraph subgraph = DotFactory.eINSTANCE.createSubgraph();
            subgraph.setName(clusterNodeID);
            statements.add(subgraph);
            // transform child nodes recursively
            ElkPadding padding = childNode.getProperty(CoreOptions.PADDING);
            double subgraphx = childNode.getX() + padding.getLeft();
            double subgraphy = childNode.getY() + padding.getTop();
            transformNodes(childNode, subgraph.getStatements(), new KVector(offset).add(subgraphx, subgraphy), transData);
            // create a dummy node for compound edges
            nodeID = getNodeID(childNode, NodeType.DUMMY, transData);
            attributes.add(createAttribute(Attributes.STYLE, "invis"));
            attributes.add(createAttribute(Attributes.WIDTH, 0));
            attributes.add(createAttribute(Attributes.HEIGHT, 0));
            subgraph.getStatements().add(nodeStatement);
        } else {
            nodeID = getNodeID(childNode, NodeType.NODE, transData);
            // set width and height
            ElkUtil.resizeNode(childNode);
            if (childNode.getWidth() > 0) {
                attributes.add(createAttribute(Attributes.WIDTH, childNode.getWidth() / DPI));
            }
            if (childNode.getHeight() > 0) {
                attributes.add(createAttribute(Attributes.HEIGHT, childNode.getHeight() / DPI));
            }
            if (transformNodeLabels && !childNode.getLabels().isEmpty() && childNode.getLabels().get(0).getText().length() > 0) {
                attributes.add(createAttribute(Attributes.LABEL, createString(childNode.getLabels().get(0).getText())));
            }
            // add node position if interactive layout is chosen
            if (transformNodeLayout && (childNode.getX() != 0 || childNode.getY() != 0)) {
                double xpos = (childNode.getX() + childNode.getWidth() / 2 + offset.x);
                double ypos = (childNode.getY() + childNode.getHeight() / 2 + offset.y);
                String posString = "\"" + Double.toString(xpos) + "," + Double.toString(ypos) + "\"";
                attributes.add(createAttribute(Attributes.POS, posString));
            }
            statements.add(nodeStatement);
        }
        Node node = DotFactory.eINSTANCE.createNode();
        node.setName(nodeID);
        nodeStatement.setNode(node);
    }
}
Also used : ElkNode(org.eclipse.elk.graph.ElkNode) Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) 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) ElkPadding(org.eclipse.elk.core.math.ElkPadding)

Example 53 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class DotExporter method transferLayout.

/**
 * Applies the layout information attached to the given Dot instance to the KGraph instance
 * using the mapping created by a previous call to {@code transform}. Has to be called after a
 * call to {@code transform}.
 *
 * @param transData the transformation data instance
 */
public void transferLayout(final IDotTransformationData<ElkNode, GraphvizModel> transData) {
    ElkPadding padding = transData.getSourceGraph().getProperty(CoreOptions.PADDING);
    Graph graph = transData.getTargetGraphs().get(0).getGraphs().get(0);
    // process nodes and subgraphs
    KVector baseOffset = new KVector();
    applyLayout(transData.getSourceGraph(), graph.getStatements(), baseOffset, padding, transData);
    // finally process the edges
    LinkedList<Statement> statements = new LinkedList<Statement>(graph.getStatements());
    KVector edgeOffset = baseOffset.add(padding.getLeft(), padding.getTop());
    while (!statements.isEmpty()) {
        Statement statement = statements.removeFirst();
        if (statement instanceof EdgeStatement) {
            applyEdgeLayout((EdgeStatement) statement, edgeOffset, transData);
        } else if (statement instanceof Subgraph) {
            statements.addAll(((Subgraph) statement).getStatements());
        }
    }
}
Also used : Graph(org.eclipse.elk.alg.graphviz.dot.dot.Graph) 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) Subgraph(org.eclipse.elk.alg.graphviz.dot.dot.Subgraph) KVector(org.eclipse.elk.core.math.KVector) ElkPadding(org.eclipse.elk.core.math.ElkPadding) LinkedList(java.util.LinkedList) EdgeStatement(org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement)

Example 54 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class DotExporter method parseSplinePoints.

/**
 * Puts the points of a position string into a list of splines.
 *
 * @param posString string with spline points
 * @param splines list of splines
 * @param offset offset to add to coordinates
 * @return the source and the target point, if specified by the position string
 */
private static Pair<KVector, KVector> parseSplinePoints(final String posString, final List<KVectorChain> splines, final KVector offset) {
    KVector sourcePoint = null, targetPoint = null;
    StringTokenizer splinesTokenizer = new StringTokenizer(posString, "\";");
    while (splinesTokenizer.hasMoreTokens()) {
        KVectorChain pointList = new KVectorChain();
        StringTokenizer posTokenizer = new StringTokenizer(splinesTokenizer.nextToken(), " \t");
        while (posTokenizer.hasMoreTokens()) {
            String token = posTokenizer.nextToken();
            try {
                if (token.startsWith("s")) {
                    if (sourcePoint == null) {
                        sourcePoint = new KVector();
                        int commaIndex = token.indexOf(',');
                        sourcePoint.parse(token.substring(commaIndex + 1));
                        sourcePoint.add(offset);
                    }
                } else if (token.startsWith("e")) {
                    if (targetPoint == null) {
                        targetPoint = new KVector();
                        int commaIndex = token.indexOf(',');
                        targetPoint.parse(token.substring(commaIndex + 1));
                        targetPoint.add(offset);
                    }
                } else {
                    KVector point = new KVector();
                    point.parse(token);
                    pointList.add(point.add(offset));
                }
            } catch (IllegalArgumentException exception) {
            // ignore exception
            }
        }
        splines.add(pointList);
    }
    return new Pair<KVector, KVector>(sourcePoint, targetPoint);
}
Also used : StringTokenizer(java.util.StringTokenizer) KVectorChain(org.eclipse.elk.core.math.KVectorChain) KVector(org.eclipse.elk.core.math.KVector) SizeConstraint(org.eclipse.elk.core.options.SizeConstraint) Pair(org.eclipse.elk.core.util.Pair)

Example 55 with KVector

use of org.eclipse.elk.core.math.KVector in project elk by eclipse.

the class ComponentsToCGraphTransformer method transform.

/* -----------------------------------------------------------
     *                    Graph Transformation
     * ----------------------------------------------------------- */
@Override
public CGraph transform(final IConnectedComponents<N, E> ccs) {
    cGraph = new CGraph(EnumSet.allOf(Direction.class));
    for (IComponent<N, E> comp : ccs.getComponents()) {
        CGroup group = new CGroup();
        cGraph.cGroups.add(group);
        // convert the hull of the graph's elements without external edges
        for (ElkRectangle rect : comp.getHull()) {
            CRectNode rectNode = new CRectNode(rect);
            setLock(rectNode, comp.getExternalExtensionSides());
            if (!oldPosition.containsKey(comp)) {
                oldPosition.put(comp, new KVector(rect.x, rect.y));
                offsets.put(comp, rectNode);
            }
            cGraph.cNodes.add(rectNode);
            group.addCNode(rectNode);
        }
        // they can be added to the CGraph on demand later on
        for (IExternalExtension<E> ee : comp.getExternalExtensions()) {
            CRectNode rectNode = new CRectNode(ee.getRepresentor());
            externalExtensions.put(ee, Pair.of(group, rectNode));
            setLock(rectNode, comp.getExternalExtensionSides());
            // placeholder
            if (ee.getPlaceholder() != null) {
                CRectNode rectPlaceholder = new CRectNode(ee.getPlaceholder(), 1d);
                setLock(rectPlaceholder, comp.getExternalExtensionSides());
                CGroup dummyGroup = new CGroup();
                dummyGroup.addCNode(rectPlaceholder);
                externalPlaceholder.put(ee.getDirection(), Pair.of(group, rectPlaceholder));
            }
        }
    }
    return cGraph;
}
Also used : CGraph(org.eclipse.elk.alg.layered.compaction.oned.CGraph) ElkRectangle(org.eclipse.elk.core.math.ElkRectangle) KVector(org.eclipse.elk.core.math.KVector) CGroup(org.eclipse.elk.alg.layered.compaction.oned.CGroup)

Aggregations

KVector (org.eclipse.elk.core.math.KVector)292 KVectorChain (org.eclipse.elk.core.math.KVectorChain)52 ElkNode (org.eclipse.elk.graph.ElkNode)49 LNode (org.eclipse.elk.alg.layered.graph.LNode)39 LPort (org.eclipse.elk.alg.layered.graph.LPort)37 ElkRectangle (org.eclipse.elk.core.math.ElkRectangle)36 LEdge (org.eclipse.elk.alg.layered.graph.LEdge)35 Test (org.junit.Test)30 ElkEdge (org.eclipse.elk.graph.ElkEdge)28 ElkLabel (org.eclipse.elk.graph.ElkLabel)27 ElkEdgeSection (org.eclipse.elk.graph.ElkEdgeSection)26 ElkPadding (org.eclipse.elk.core.math.ElkPadding)25 LLabel (org.eclipse.elk.alg.layered.graph.LLabel)20 PortSide (org.eclipse.elk.core.options.PortSide)19 ElkPort (org.eclipse.elk.graph.ElkPort)18 SizeConstraint (org.eclipse.elk.core.options.SizeConstraint)17 LGraph (org.eclipse.elk.alg.layered.graph.LGraph)15 ArrayList (java.util.ArrayList)13 Layer (org.eclipse.elk.alg.layered.graph.Layer)12 LMargin (org.eclipse.elk.alg.layered.graph.LMargin)11