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);
}
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);
}
}
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());
}
}
}
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);
}
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;
}
Aggregations