use of org.eclipse.elk.alg.graphviz.dot.dot.Node 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);
}
}
use of org.eclipse.elk.alg.graphviz.dot.dot.Node 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));
}
use of org.eclipse.elk.alg.graphviz.dot.dot.Node in project elk by eclipse.
the class AbstractGraphvizDotSemanticSequencer 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 == DotPackage.eINSTANCE)
switch(semanticObject.eClass().getClassifierID()) {
case DotPackage.ATTRIBUTE:
if (rule == grammarAccess.getStatementRule() || rule == grammarAccess.getAttributeRule()) {
sequence_Attribute(context, (Attribute) semanticObject);
return;
} else if (rule == grammarAccess.getListAttributeRule()) {
sequence_ListAttribute(context, (Attribute) semanticObject);
return;
} else
break;
case DotPackage.ATTRIBUTE_STATEMENT:
sequence_AttributeStatement(context, (AttributeStatement) semanticObject);
return;
case DotPackage.EDGE_STATEMENT:
sequence_EdgeStatement(context, (EdgeStatement) semanticObject);
return;
case DotPackage.EDGE_TARGET:
sequence_EdgeTarget(context, (EdgeTarget) semanticObject);
return;
case DotPackage.GRAPH:
sequence_Graph(context, (Graph) semanticObject);
return;
case DotPackage.GRAPHVIZ_MODEL:
sequence_GraphvizModel(context, (GraphvizModel) semanticObject);
return;
case DotPackage.NODE:
sequence_Node(context, (Node) semanticObject);
return;
case DotPackage.NODE_STATEMENT:
sequence_NodeStatement(context, (NodeStatement) semanticObject);
return;
case DotPackage.PORT:
sequence_Port(context, (Port) semanticObject);
return;
case DotPackage.SUBGRAPH:
sequence_Subgraph(context, (Subgraph) semanticObject);
return;
}
if (errorAcceptor != null)
errorAcceptor.accept(diagnosticProvider.createInvalidContextOrTypeDiagnostic(semanticObject, context));
}
use of org.eclipse.elk.alg.graphviz.dot.dot.Node 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());
}
}
}
use of org.eclipse.elk.alg.graphviz.dot.dot.Node in project elk by eclipse.
the class DotImporter method transformEdge.
/**
* Transforms an edge.
*
* @param statement an edge statement
* @param parent the parent node
* @param transData the transformation data instance
* @param defaultProps default values for edge options
*/
private void transformEdge(final EdgeStatement statement, final ElkNode parent, final IDotTransformationData<GraphvizModel, ElkNode> transData, final IPropertyHolder defaultProps) {
String sourceName = statement.getSourceNode().getName();
ElkNode source = transformNode(sourceName, parent, transData);
ElkPort sourcePort = null;
if (statement.getSourceNode().getPort() != null) {
String portName = statement.getSourceNode().getPort().getCompass_pt();
if (portName == null) {
portName = statement.getSourceNode().getPort().getName();
}
sourcePort = transformPort(portName, source, transData);
}
ListIterator<EdgeTarget> targetIter = statement.getEdgeTargets().listIterator();
while (targetIter.hasNext()) {
EdgeTarget edgeTarget = targetIter.next();
ElkEdge elkedge = ElkGraphUtil.createEdge(null);
if (sourcePort != null) {
elkedge.getSources().add(sourcePort);
} else {
elkedge.getSources().add(source);
}
ElkNode target;
ElkPort targetPort = null;
Node edgeTargetNode = edgeTarget.getTargetnode();
String targetName;
if (edgeTargetNode == null) {
targetName = edgeTarget.getTargetSubgraph().getName();
target = transformNode(targetName, parent, transData);
} else {
targetName = edgeTargetNode.getName();
target = transformNode(targetName, parent, transData);
if (edgeTargetNode.getPort() != null) {
String portName = edgeTargetNode.getPort().getCompass_pt();
if (portName == null) {
portName = edgeTargetNode.getPort().getName();
}
targetPort = transformPort(portName, target, transData);
}
}
if (targetPort != null) {
elkedge.getTargets().add(targetPort);
} else {
elkedge.getTargets().add(target);
}
elkedge.copyProperties(defaultProps);
if (targetIter.previousIndex() == 0) {
// this is the first target - just store the edge statement
elkedge.setProperty(PROP_STATEMENT, statement);
} else {
// the edge statement has more that one target - create a copy
EdgeStatement newStatement = DotFactory.eINSTANCE.createEdgeStatement();
Node sourceNode = DotFactory.eINSTANCE.createNode();
sourceNode.setName(sourceName);
newStatement.setSourceNode(sourceNode);
targetIter.remove();
newStatement.getEdgeTargets().add(edgeTarget);
for (Attribute attr : statement.getAttributes()) {
newStatement.getAttributes().add(EcoreUtil.copy(attr));
}
elkedge.setProperty(PROP_STATEMENT, newStatement);
}
// evaluate attributes for the new edge
for (Attribute attr : statement.getAttributes()) {
String value = trimValue(attr);
if (Attributes.LABEL.equals(attr.getName())) {
ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.CENTER);
} else if (Attributes.HEADLABEL.equals(attr.getName())) {
ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.HEAD);
} else if (Attributes.TAILLABEL.equals(attr.getName())) {
ElkLabel label = ElkGraphUtil.createLabel(value, elkedge);
label.setProperty(CoreOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.TAIL);
} else {
transformAttribute(elkedge, attr, transData);
}
}
// the edge target is the source for the next edge target
source = target;
sourceName = targetName;
sourcePort = targetPort;
}
}
Aggregations