use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.
the class LabelSideSelectorTest method configurator.
@Configurator
public void configurator(final ElkNode graph) {
Deque<ElkNode> nodeQueue = new LinkedList<>();
nodeQueue.add(graph);
Random rand = new Random(RANDOM_SEED);
while (!nodeQueue.isEmpty()) {
ElkNode node = nodeQueue.poll();
for (ElkEdge edge : node.getContainedEdges()) {
if (edge.getLabels().isEmpty()) {
ElkGraphUtil.createLabel(edge.getIdentifier(), edge);
}
ElkLabel label = edge.getLabels().get(0);
double r = rand.nextDouble();
if (r < 0.3) {
label.setProperty(LayeredOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.HEAD);
} else if (r < 0.7) {
label.setProperty(LayeredOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.CENTER);
} else {
label.setProperty(LayeredOptions.EDGE_LABELS_PLACEMENT, EdgeLabelPlacement.TAIL);
}
}
}
}
use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.
the class OverallLayoutTest method testEdgeCoordinates.
/**
* Tests if the source and target coordinates of all edges are > 0. Doesn't work for
* hierarchical graphs.
*/
@Test
public void testEdgeCoordinates() {
for (ElkEdge edge : simpleGraph.getContainedEdges()) {
for (ElkEdgeSection section : edge.getSections()) {
assertTrue(section.getStartX() > 0);
assertTrue(section.getStartY() > 0);
assertTrue(section.getEndX() > 0);
assertTrue(section.getEndY() > 0);
}
}
}
use of org.eclipse.elk.graph.ElkEdge in project elk by eclipse.
the class InteractiveLayeredGraphVisitor method shiftOtherNodes.
/**
* Shifts nodes to the right such that edges in the same layer do not exist.
*
* @param movedNode
* The node which connected nodes must be shifted .
* @param layer
* The layer {@code moveNode} is in.
* @param layerNodes
* All existing layers with the containing nodes.
* @param incoming
* Determines if incoming or outgoing edges should be considered. True: incoming edges.
*/
private void shiftOtherNodes(final ElkNode movedNode, final int layer, final List<List<ElkNode>> layerNodes, final boolean incoming) {
List<ElkNode> nodesOfLayer = layerNodes.get(layer);
// get edges
List<ElkEdge> edges = new ArrayList<>();
if (incoming) {
ElkNode root = movedNode.getParent();
for (ElkEdge edge : root.getContainedEdges()) {
for (ElkConnectableShape target : edge.getTargets()) {
if (target.equals(movedNode) || (target instanceof ElkPort && target.eContainer().equals(movedNode))) {
edges.add(edge);
}
}
}
} else {
ElkNode root = movedNode.getParent();
for (ElkEdge edge : root.getContainedEdges()) {
for (ElkConnectableShape target : edge.getSources()) {
if (target.equals(movedNode) || (target instanceof ElkPort && target.eContainer().equals(movedNode))) {
edges.add(edge);
}
}
}
}
for (ElkEdge edge : edges) {
// get connected node
ElkNode node = null;
if (incoming) {
if (edge.getSources().get(0) instanceof ElkPort) {
node = (ElkNode) edge.getSources().get(0).eContainer();
} else if (edge.getSources().get(0) instanceof ElkNode) {
node = (ElkNode) edge.getSources().get(0);
}
} else {
if (edge.getTargets().get(0) instanceof ElkPort) {
node = (ElkNode) edge.getTargets().get(0).eContainer();
} else if (edge.getTargets().get(0) instanceof ElkNode) {
node = (ElkNode) edge.getTargets().get(0);
}
}
// shift node to the next layer
if (nodesOfLayer.contains(node)) {
nodesOfLayer.remove(node);
List<ElkNode> newLayer;
if (layer + 1 < layerNodes.size()) {
newLayer = layerNodes.get(layer + 1);
newLayer.add(node);
// the connected nodes in the layer the node is shifted to must be shifted too
shiftOtherNodes(node, layer + 1, layerNodes, false);
shiftOtherNodes(node, layer + 1, layerNodes, true);
} else {
layerNodes.add(new ArrayList<>(Arrays.asList(node)));
}
}
}
}
use of org.eclipse.elk.graph.ElkEdge 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.graph.ElkEdge in project elk by eclipse.
the class ElkGraphImporter method importFlatGraph.
/**
* Imports the direct children of the given graph.
*
* @param elkgraph
* graph to import.
* @param lgraph
* graph to add the imported elements to.
*/
private void importFlatGraph(final ElkNode elkgraph, final LGraph lgraph) {
// Transform the node's children, unless we're told not to
int index = 0;
for (ElkNode child : elkgraph.getChildren()) {
if (!child.getProperty(LayeredOptions.NO_LAYOUT)) {
if (elkgraph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY) != OrderingStrategy.NONE || elkgraph.getProperty(LayeredOptions.CYCLE_BREAKING_STRATEGY) == CycleBreakingStrategy.MODEL_ORDER) {
child.setProperty(InternalProperties.MODEL_ORDER, index);
index++;
}
transformNode(child, lgraph);
}
}
// iterate the list of contained edges to preserve the 'input order' of the edges
// (this is not part of the previous loop since all children must have already been transformed)
index = 0;
for (ElkEdge elkedge : elkgraph.getContainedEdges()) {
if (elkgraph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY) != OrderingStrategy.NONE || elkgraph.getProperty(LayeredOptions.CYCLE_BREAKING_STRATEGY) == CycleBreakingStrategy.MODEL_ORDER) {
elkedge.setProperty(InternalProperties.MODEL_ORDER, index);
index++;
}
ElkNode source = ElkGraphUtil.getSourceNode(elkedge);
ElkNode target = ElkGraphUtil.getTargetNode(elkedge);
// Is inside self loop processing enabled for this node?
boolean enableInsideSelfLoops = source.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE);
// Find out basic information about the edge
boolean isToBeLaidOut = !elkedge.getProperty(LayeredOptions.NO_LAYOUT);
boolean isInsideSelfLoop = enableInsideSelfLoops && elkedge.isSelfloop() && elkedge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
boolean connectsSiblings = source.getParent() == elkgraph && source.getParent() == target.getParent();
boolean connectsToGraph = (source.getParent() == elkgraph && target == elkgraph) ^ (target.getParent() == elkgraph && source == elkgraph);
// level of hierarchy (which implies that here we don't transform inside self loops)
if (isToBeLaidOut && !isInsideSelfLoop && (connectsToGraph || connectsSiblings)) {
transformEdge(elkedge, elkgraph, lgraph);
}
}
// now collect inside self loops of 'elkgraph'
if (elkgraph.getParent() != null) {
for (ElkEdge elkedge : elkgraph.getParent().getContainedEdges()) {
ElkNode source = ElkGraphUtil.getSourceNode(elkedge);
if (source == elkgraph && elkedge.isSelfloop()) {
boolean isInsideSelfLoop = source.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE) && elkedge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
if (isInsideSelfLoop) {
transformEdge(elkedge, elkgraph, lgraph);
}
}
}
}
}
Aggregations