Search in sources :

Example 11 with Attribute

use of org.eclipse.elk.alg.graphviz.dot.dot.Attribute in project elk by eclipse.

the class DotExporter method setGeneralNodeAttributes.

/**
 * Sets general attributes for nodes.
 *
 * @param statements
 *            the statement list for adding attributes
 * @return the list of node attributes
 */
protected List<Attribute> setGeneralNodeAttributes(final List<Statement> statements) {
    AttributeStatement nodeAttrStatement = DotFactory.eINSTANCE.createAttributeStatement();
    nodeAttrStatement.setType(AttributeType.NODE);
    List<Attribute> nodeAttrs = nodeAttrStatement.getAttributes();
    statements.add(nodeAttrStatement);
    nodeAttrs.add(createAttribute(Attributes.SHAPE, "box"));
    nodeAttrs.add(createAttribute(Attributes.FIXEDSIZE, "true"));
    return nodeAttrs;
}
Also used : Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) AttributeStatement(org.eclipse.elk.alg.graphviz.dot.dot.AttributeStatement)

Example 12 with Attribute

use of org.eclipse.elk.alg.graphviz.dot.dot.Attribute in project elk by eclipse.

the class DotExporter method createAttribute.

/**
 * Create an attribute with given name and float value for the Dot graph.
 *
 * @param name name of the attribute
 * @param value value of the attribute
 * @return instance of a Dot attribute
 */
public static Attribute createAttribute(final String name, final double value) {
    Attribute attribute = DotFactory.eINSTANCE.createAttribute();
    attribute.setName(name);
    attribute.setValue("\"" + value + "\"");
    return attribute;
}
Also used : Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute)

Example 13 with Attribute

use of org.eclipse.elk.alg.graphviz.dot.dot.Attribute in project elk by eclipse.

the class DotExporter method applyLayout.

/**
 * Applies layout information from a Graphviz model to the original graph.
 * Note that GraphViz uses cubic B-Splines for edge routing, some generalization of Bezier curves.
 * Edge offsets are given separately, since on some platforms edges seem to have a different
 * reference point than nodes.
 *
 * @param parentNode parent node of the original graph
 * @param statements list of statements to process
 * @param baseOffset offset to be added to edge and subgraph coordinates
 * @param transData transformation data
 */
private void applyLayout(final ElkNode parentNode, final List<Statement> statements, final KVector baseOffset, final ElkPadding outerPadding, final IDotTransformationData<ElkNode, GraphvizModel> transData) {
    // process attributes: determine bounding box of the parent node
    ElkPadding padding = outerPadding;
    KVector nodeOffset = new KVector();
    attr_loop: for (Statement statement : statements) {
        if (statement instanceof AttributeStatement) {
            AttributeStatement attributeStatement = (AttributeStatement) statement;
            if (attributeStatement.getType() == AttributeType.GRAPH) {
                for (Attribute attribute : attributeStatement.getAttributes()) {
                    if (attribute.getName().equals(Attributes.BOUNDINGBOX)) {
                        try {
                            StringTokenizer tokenizer = new StringTokenizer(attribute.getValue(), ATTRIBUTE_DELIM);
                            double leftx = Double.parseDouble(tokenizer.nextToken());
                            double bottomy = Double.parseDouble(tokenizer.nextToken());
                            double rightx = Double.parseDouble(tokenizer.nextToken());
                            double topy = Double.parseDouble(tokenizer.nextToken());
                            // set parent node attributes
                            double width = rightx - leftx;
                            double height = bottomy - topy;
                            if (parentNode == transData.getSourceGraph()) {
                                // the root node, representing the drawing frame
                                width += padding.getHorizontal();
                                height += padding.getVertical();
                                baseOffset.add(-leftx, -topy);
                                nodeOffset.add(-leftx, -topy);
                            } else {
                                // a child or descendant of the root node
                                parentNode.setX(baseOffset.x + leftx + padding.getLeft());
                                parentNode.setY(baseOffset.y + topy + padding.getTop());
                                // since dot uses a 'compound' layout instead of laying out a hierarchical graph,
                                // no padding is supported for children
                                padding = new ElkPadding();
                                // ... and the children's offset must be somewhat adjusted
                                nodeOffset.x = -(baseOffset.x + leftx);
                                nodeOffset.y = -(baseOffset.y + topy);
                            }
                            ElkUtil.resizeNode(parentNode, width, height, false, true);
                            parentNode.setProperty(CoreOptions.NODE_SIZE_CONSTRAINTS, SizeConstraint.fixed());
                            break attr_loop;
                        } catch (NumberFormatException exception) {
                        // ignore exception
                        } catch (NoSuchElementException exception) {
                        // ignore exception
                        }
                    }
                }
            }
        }
    }
    // process nodes and subgraphs to collect all offset data
    for (Statement statement : statements) {
        if (statement instanceof NodeStatement) {
            applyNodeLayout((NodeStatement) statement, nodeOffset, padding, transData);
        } else if (statement instanceof Subgraph) {
            Subgraph subgraph = (Subgraph) statement;
            ElkNode elknode = (ElkNode) transData.getProperty(GRAPH_ELEMS).get(subgraph.getName());
            applyLayout(elknode, subgraph.getStatements(), baseOffset, padding, transData);
            elknode.setX(elknode.getX() + nodeOffset.x);
            elknode.setY(elknode.getY() + nodeOffset.y);
        }
    }
}
Also used : StringTokenizer(java.util.StringTokenizer) 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) AttributeStatement(org.eclipse.elk.alg.graphviz.dot.dot.AttributeStatement) NodeStatement(org.eclipse.elk.alg.graphviz.dot.dot.NodeStatement) Subgraph(org.eclipse.elk.alg.graphviz.dot.dot.Subgraph) KVector(org.eclipse.elk.core.math.KVector) ElkPadding(org.eclipse.elk.core.math.ElkPadding) NoSuchElementException(java.util.NoSuchElementException)

Example 14 with Attribute

use of org.eclipse.elk.alg.graphviz.dot.dot.Attribute in project elk by eclipse.

the class DotExporter method transformEdges.

/**
 * Transform the edges of the given parent node.
 *
 * @param parent a parent node
 * @param statements the list to which new statements are added
 * @param transData transformation data
 */
private void transformEdges(final ElkNode parent, final List<Statement> statements, final IDotTransformationData<ElkNode, GraphvizModel> transData) {
    boolean hierarchy = transData.getProperty(HIERARCHY);
    boolean transformEdgeLayout = transData.getProperty(TRANSFORM_EDGE_LAYOUT);
    Direction direction = parent.getProperty(CoreOptions.DIRECTION);
    boolean vertical = direction == Direction.DOWN || direction == Direction.UP || direction == Direction.UNDEFINED;
    LinkedList<ElkNode> nodes = new LinkedList<>(parent.getChildren());
    BiMap<ElkGraphElement, String> nodeIds = transData.getProperty(GRAPH_ELEMS).inverse();
    while (!nodes.isEmpty()) {
        ElkNode source = nodes.removeFirst();
        for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(source)) {
            // We don't support hyperedges
            if (edge.isHyperedge()) {
                throw new UnsupportedGraphException("Hyperedges are not supported.");
            }
            ElkNode target = ElkGraphUtil.connectableShapeToNode(edge.getTargets().get(0));
            // cross-hierarchy edges are considered only if hierarchy mode is active
            if (source.getParent() == target.getParent() || hierarchy && isInsideGraph(target, transData.getSourceGraph())) {
                EdgeStatement edgeStatement = DotFactory.eINSTANCE.createEdgeStatement();
                List<Attribute> attributes = edgeStatement.getAttributes();
                // set source node or cluster
                Node sourceNode = DotFactory.eINSTANCE.createNode();
                if (hierarchy && !source.getChildren().isEmpty()) {
                    sourceNode.setName(source.getProperty(CLUSTER_DUMMY));
                    attributes.add(createAttribute(Attributes.LTAIL, nodeIds.get(source)));
                } else {
                    sourceNode.setName(nodeIds.get(source));
                }
                edgeStatement.setSourceNode(sourceNode);
                // set target node or cluster
                EdgeTarget edgeTarget = DotFactory.eINSTANCE.createEdgeTarget();
                Node targetNode = DotFactory.eINSTANCE.createNode();
                if (hierarchy && !target.getChildren().isEmpty()) {
                    targetNode.setName(target.getProperty(CLUSTER_DUMMY));
                    attributes.add(createAttribute(Attributes.LHEAD, nodeIds.get(target)));
                } else {
                    targetNode.setName(nodeIds.get(target));
                }
                edgeTarget.setTargetnode(targetNode);
                edgeStatement.getEdgeTargets().add(edgeTarget);
                // add edge labels at head, tail, and middle position
                setEdgeLabels(edge, attributes, vertical);
                if (transData.getProperty(USE_EDGE_IDS)) {
                    // add comment with edge identifier
                    String edgeID = getEdgeID(edge, transData);
                    attributes.add(createAttribute(Attributes.COMMENT, "\"" + edgeID + "\""));
                }
                // include edge routing for full export, if there is one
                if (!edge.getSections().isEmpty()) {
                    ElkEdgeSection edgeSection = edge.getSections().get(0);
                    if (transformEdgeLayout && (edgeSection.getBendPoints().size() > 0 || edgeSection.getStartX() != 0 || edgeSection.getStartY() != 0 || edgeSection.getEndX() != 0 || edgeSection.getEndY() != 0)) {
                        StringBuilder pos = new StringBuilder();
                        Iterator<KVector> pointIter = ElkUtil.createVectorChain(edgeSection).iterator();
                        while (pointIter.hasNext()) {
                            KVector point = pointIter.next();
                            ElkUtil.toAbsolute(point, edge.getContainingNode());
                            pos.append(point.x);
                            pos.append(",");
                            pos.append(point.y);
                            if (pointIter.hasNext()) {
                                pos.append(" ");
                            }
                        }
                        attributes.add(createAttribute(Attributes.POS, "\"" + pos + "\""));
                    }
                }
                statements.add(edgeStatement);
            }
        }
        if (hierarchy) {
            nodes.addAll(source.getChildren());
        }
    }
}
Also used : UnsupportedGraphException(org.eclipse.elk.core.UnsupportedGraphException) ElkNode(org.eclipse.elk.graph.ElkNode) Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute) ElkNode(org.eclipse.elk.graph.ElkNode) Node(org.eclipse.elk.alg.graphviz.dot.dot.Node) Direction(org.eclipse.elk.core.options.Direction) LinkedList(java.util.LinkedList) EdgeStatement(org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement) ElkEdgeSection(org.eclipse.elk.graph.ElkEdgeSection) EdgeTarget(org.eclipse.elk.alg.graphviz.dot.dot.EdgeTarget) KVector(org.eclipse.elk.core.math.KVector) ElkGraphElement(org.eclipse.elk.graph.ElkGraphElement) ElkEdge(org.eclipse.elk.graph.ElkEdge)

Example 15 with Attribute

use of org.eclipse.elk.alg.graphviz.dot.dot.Attribute in project elk by eclipse.

the class DotExporter method createAttribute.

/**
 * Create an attribute with given name and float value for the Dot graph.
 *
 * @param name name of the attribute
 * @param value value of the attribute
 * @return instance of a Dot attribute
 */
public static Attribute createAttribute(final String name, final float value) {
    Attribute attribute = DotFactory.eINSTANCE.createAttribute();
    attribute.setName(name);
    attribute.setValue("\"" + value + "\"");
    return attribute;
}
Also used : Attribute(org.eclipse.elk.alg.graphviz.dot.dot.Attribute)

Aggregations

Attribute (org.eclipse.elk.alg.graphviz.dot.dot.Attribute)17 KVector (org.eclipse.elk.core.math.KVector)8 ElkNode (org.eclipse.elk.graph.ElkNode)8 AttributeStatement (org.eclipse.elk.alg.graphviz.dot.dot.AttributeStatement)7 EdgeStatement (org.eclipse.elk.alg.graphviz.dot.dot.EdgeStatement)7 Node (org.eclipse.elk.alg.graphviz.dot.dot.Node)5 NodeStatement (org.eclipse.elk.alg.graphviz.dot.dot.NodeStatement)5 Subgraph (org.eclipse.elk.alg.graphviz.dot.dot.Subgraph)5 EdgeTarget (org.eclipse.elk.alg.graphviz.dot.dot.EdgeTarget)3 Statement (org.eclipse.elk.alg.graphviz.dot.dot.Statement)3 ElkPadding (org.eclipse.elk.core.math.ElkPadding)3 ElkEdge (org.eclipse.elk.graph.ElkEdge)3 NoSuchElementException (java.util.NoSuchElementException)2 StringTokenizer (java.util.StringTokenizer)2 ElkLabel (org.eclipse.elk.graph.ElkLabel)2 LinkedList (java.util.LinkedList)1 Graph (org.eclipse.elk.alg.graphviz.dot.dot.Graph)1 GraphvizModel (org.eclipse.elk.alg.graphviz.dot.dot.GraphvizModel)1 Port (org.eclipse.elk.alg.graphviz.dot.dot.Port)1 DotSwitch (org.eclipse.elk.alg.graphviz.dot.dot.util.DotSwitch)1