/
ElkGraphImporter.java
1211 lines (1028 loc) · 56.7 KB
/
ElkGraphImporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2015, 2020 Kiel University and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.elk.alg.layered.graph.transform;
import java.util.EnumSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import org.eclipse.elk.alg.common.nodespacing.NodeLabelAndSizeCalculator;
import org.eclipse.elk.alg.layered.graph.LEdge;
import org.eclipse.elk.alg.layered.graph.LGraph;
import org.eclipse.elk.alg.layered.graph.LGraphElement;
import org.eclipse.elk.alg.layered.graph.LGraphUtil;
import org.eclipse.elk.alg.layered.graph.LLabel;
import org.eclipse.elk.alg.layered.graph.LNode;
import org.eclipse.elk.alg.layered.graph.LPadding;
import org.eclipse.elk.alg.layered.graph.LPort;
import org.eclipse.elk.alg.layered.options.CrossingMinimizationStrategy;
import org.eclipse.elk.alg.layered.options.CycleBreakingStrategy;
import org.eclipse.elk.alg.layered.options.GraphProperties;
import org.eclipse.elk.alg.layered.options.InternalProperties;
import org.eclipse.elk.alg.layered.options.LayeredOptions;
import org.eclipse.elk.alg.layered.options.LayeredSpacings;
import org.eclipse.elk.alg.layered.options.NodePlacementStrategy;
import org.eclipse.elk.alg.layered.options.OrderingStrategy;
import org.eclipse.elk.alg.layered.options.PortType;
import org.eclipse.elk.core.UnsupportedGraphException;
import org.eclipse.elk.core.labels.LabelManagementOptions;
import org.eclipse.elk.core.math.ElkPadding;
import org.eclipse.elk.core.math.KVector;
import org.eclipse.elk.core.math.KVectorChain;
import org.eclipse.elk.core.options.CoreOptions;
import org.eclipse.elk.core.options.Direction;
import org.eclipse.elk.core.options.EdgeLabelPlacement;
import org.eclipse.elk.core.options.HierarchyHandling;
import org.eclipse.elk.core.options.PortConstraints;
import org.eclipse.elk.core.options.PortLabelPlacement;
import org.eclipse.elk.core.options.PortSide;
import org.eclipse.elk.core.options.SizeConstraint;
import org.eclipse.elk.core.util.ElkUtil;
import org.eclipse.elk.core.util.adapters.ElkGraphAdapters;
import org.eclipse.elk.core.util.adapters.GraphAdapters.GraphAdapter;
import org.eclipse.elk.core.util.adapters.GraphAdapters.NodeAdapter;
import org.eclipse.elk.graph.ElkConnectableShape;
import org.eclipse.elk.graph.ElkEdge;
import org.eclipse.elk.graph.ElkEdgeSection;
import org.eclipse.elk.graph.ElkGraphElement;
import org.eclipse.elk.graph.ElkLabel;
import org.eclipse.elk.graph.ElkNode;
import org.eclipse.elk.graph.ElkPort;
import org.eclipse.elk.graph.util.ElkGraphUtil;
import org.eclipse.emf.ecore.util.EcoreUtil;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
/**
* Implements the graph import aspect of {@link ElkGraphTransformer}.
*/
class ElkGraphImporter {
/** map between ElkGraph nodes / ports and the LGraph nodes / ports created for them. */
private final Map<ElkGraphElement, LGraphElement> nodeAndPortMap = Maps.newHashMap();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Import Entry Points
/**
* Imports the given graph.
*
* @param elkgraph
* the graph to import.
* @return the transformed graph.
*/
public LGraph importGraph(final ElkNode elkgraph) {
// Create the layered graph
final LGraph topLevelGraph = createLGraph(elkgraph);
// Assign defined port sides to all external ports
elkgraph.getPorts().stream().forEach(elkport -> ensureDefinedPortSide(topLevelGraph, elkport));
// Transform the external ports, if any
Set<GraphProperties> graphProperties = topLevelGraph.getProperty(InternalProperties.GRAPH_PROPERTIES);
checkExternalPorts(elkgraph, graphProperties);
if (graphProperties.contains(GraphProperties.EXTERNAL_PORTS)) {
for (ElkPort elkport : elkgraph.getPorts()) {
transformExternalPort(elkgraph, topLevelGraph, elkport);
}
}
// Calculate the graph's minimum size
if (shouldCalculateMinimumGraphSize(elkgraph)) {
calculateMinimumGraphSize(elkgraph, topLevelGraph);
}
// Remember things
if (topLevelGraph.getProperty(LayeredOptions.PARTITIONING_ACTIVATE)) {
graphProperties.add(GraphProperties.PARTITIONS);
}
// Apply a spacing configuration based on a base value (if it has been requested)
// Note that the computed spacing values are set on the lgraph and not the elkgraph to avoid polluting
// the input graph. If the spacing values were set on the input graph, a second layout run of the same
// input graph - with a different base value - would yield an unexpected result as the computed spacing
// values of the first layout run would be used (explicitly set spacing values are not overwritten).
if (topLevelGraph.hasProperty(LayeredOptions.SPACING_BASE_VALUE)) {
LayeredSpacings.withBaseValue(topLevelGraph.getProperty(LayeredOptions.SPACING_BASE_VALUE))
.apply(topLevelGraph);
}
// Import the graph either with or without multiple nested levels of hierarchy
if (elkgraph.getProperty(LayeredOptions.HIERARCHY_HANDLING) == HierarchyHandling.INCLUDE_CHILDREN) {
importHierarchicalGraph(elkgraph, topLevelGraph);
} else {
importFlatGraph(elkgraph, topLevelGraph);
}
return topLevelGraph;
}
/**
* Ensures that the given port has a defined port side.
*/
private void ensureDefinedPortSide(final LGraph lgraph, final ElkPort elkport) {
Direction layoutDirection = lgraph.getProperty(LayeredOptions.DIRECTION);
PortSide portSide = elkport.getProperty(LayeredOptions.PORT_SIDE);
PortConstraints portConstraints = lgraph.getProperty(LayeredOptions.PORT_CONSTRAINTS);
if (!portConstraints.isSideFixed()) {
// We are free to assign ports to sides, so the port side will depend on the layout direction and the
// port's net flow
int netFlow = calculateNetFlow(elkport);
if (netFlow > 0) {
portSide = PortSide.fromDirection(layoutDirection);
} else {
portSide = PortSide.fromDirection(layoutDirection).opposed();
}
} else {
// We are not free to assign port sides. If none is set, try inferring it from the port's position
if (portSide == PortSide.UNDEFINED) {
portSide = ElkUtil.calcPortSide(elkport, layoutDirection);
// There are cases where ELK may have failed to infer the port side
if (portSide == PortSide.UNDEFINED) {
portSide = PortSide.fromDirection(layoutDirection);
}
}
}
elkport.setProperty(LayeredOptions.PORT_SIDE, portSide);
}
/**
* Checks whether {@link #calculateMinimumGraphSize(ElkNode, LGraph)} should be called on the given graph.
*/
private boolean shouldCalculateMinimumGraphSize(final ElkNode elkgraph) {
return !elkgraph.getProperty(LayeredOptions.NODE_SIZE_CONSTRAINTS).isEmpty();
}
/**
* Asks the label and node size thing to calculate the minimum size necessary for the graph to be large enough for
* its ports and stuff (if it's not the top level graph).
*
* @param elkgraph
* the original ELK graph.
* @param lgraph
* the imported LGraph. Its properties may be updated over the course of this method.
*/
private void calculateMinimumGraphSize(final ElkNode elkgraph, final LGraph lgraph) {
// If the graph is on the top level, don't bother
if (elkgraph.getParent() == null) {
return;
}
// If the graph has no size constraints, don't bother either
EnumSet<SizeConstraint> sizeConstraints = lgraph.getProperty(LayeredOptions.NODE_SIZE_CONSTRAINTS);
// The method should only be called if shouldCalculateMinimumGraphSize(...) returns true
assert !sizeConstraints.isEmpty();
// Ensure that the port constraints are not UNDEFINED
if (elkgraph.getProperty(LayeredOptions.PORT_CONSTRAINTS) == PortConstraints.UNDEFINED) {
elkgraph.setProperty(LayeredOptions.PORT_CONSTRAINTS, PortConstraints.FREE);
}
// Size constraints are not empty, so calculate the size the node and label placement code thing would like to
// give the graph
GraphAdapter<?> graphAdapter = ElkGraphAdapters.adapt(elkgraph.getParent());
NodeAdapter<?> nodeAdapter = ElkGraphAdapters.adaptSingleNode(elkgraph);
KVector minSize = NodeLabelAndSizeCalculator.process(graphAdapter, nodeAdapter, false, true);
// Apply the minimum size a sa property and make sure the minimum size is respected by ELK Layered by making
// sure the necessary size constraint exists
sizeConstraints.add(SizeConstraint.MINIMUM_SIZE);
KVector configuredMinSize = lgraph.getProperty(LayeredOptions.NODE_SIZE_MINIMUM);
configuredMinSize.x = Math.max(minSize.x, configuredMinSize.x);
configuredMinSize.y = Math.max(minSize.y, configuredMinSize.y);
}
/**
* 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);
// Only transform the edge if we are to layout the edge and if it stays in the current
// 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);
}
}
}
}
}
/**
* Imports the graph hierarchy rooted at the given graph.
*
* @param elkgraph
* graph to import.
* @param lgraph
* graph to add the direct children of the current hierarchy level to.
*/
private void importHierarchicalGraph(final ElkNode elkgraph, final LGraph lgraph) {
final Queue<ElkNode> elkGraphQueue = Lists.newLinkedList();
Direction parentGraphDirection = lgraph.getProperty(LayeredOptions.DIRECTION);
// Model order index for nodes
int index = 0;
// Transform the node's children
elkGraphQueue.addAll(elkgraph.getChildren());
while (!elkGraphQueue.isEmpty()) {
ElkNode elknode = elkGraphQueue.poll();
if (elkgraph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY) != OrderingStrategy.NONE
|| elkgraph.getProperty(LayeredOptions.CYCLE_BREAKING_STRATEGY)
== CycleBreakingStrategy.MODEL_ORDER) {
// Assign a model order to the nodes as they are read
elknode.setProperty(InternalProperties.MODEL_ORDER, index++);
}
// Check if the current node is to be laid out in the first place
boolean isNodeToBeLaidOut = !elknode.getProperty(LayeredOptions.NO_LAYOUT);
if (isNodeToBeLaidOut) {
// Check if there has to be an LGraph for this node (which is the case if it has children or inside
// self-loops, and if it does not have another layout algorithm configured)
boolean hasChildren = !elknode.getChildren().isEmpty();
boolean hasInsideSelfLoops = hasInsideSelfLoops(elknode);
boolean hasHierarchyHandlingEnabled = elknode.getProperty(LayeredOptions.HIERARCHY_HANDLING)
== HierarchyHandling.INCLUDE_CHILDREN;
boolean usesElkLayered = !elknode.hasProperty(CoreOptions.ALGORITHM)
|| elknode.getProperty(CoreOptions.ALGORITHM).equals(LayeredOptions.ALGORITHM_ID);
LGraph nestedGraph = null;
if (usesElkLayered && hasHierarchyHandlingEnabled && (hasChildren || hasInsideSelfLoops)) {
nestedGraph = createLGraph(elknode);
nestedGraph.setProperty(LayeredOptions.DIRECTION, parentGraphDirection);
// Apply a spacing configuration, for details see comment int #importGraph(...)
if (nestedGraph.hasProperty(LayeredOptions.SPACING_BASE_VALUE)) {
LayeredSpacings.withBaseValue(nestedGraph.getProperty(LayeredOptions.SPACING_BASE_VALUE))
.apply(nestedGraph);
}
// We need to make sure that we make the graph large enough for any ports, node labels, etc.
// if the size constraints are not empty
if (shouldCalculateMinimumGraphSize(elknode)) {
final LGraph finalNestedGraph = nestedGraph;
elknode.getPorts().stream()
.forEach(elkport -> ensureDefinedPortSide(finalNestedGraph, elkport));
calculateMinimumGraphSize(elknode, nestedGraph);
}
}
// Transform da node!!!
LGraph parentLGraph = lgraph;
LNode parentLNode = (LNode) nodeAndPortMap.get(elknode.getParent());
if (parentLNode != null) {
parentLGraph = parentLNode.getNestedGraph();
}
LNode lnode = transformNode(elknode, parentLGraph);
// Setup hierarchical relationships
if (nestedGraph != null) {
lnode.setNestedGraph(nestedGraph);
nestedGraph.setParentNode(lnode);
elkGraphQueue.addAll(elknode.getChildren());
}
}
}
// Model order index for edges.
index = 0;
// Transform the edges
elkGraphQueue.add(elkgraph);
while (!elkGraphQueue.isEmpty()) {
ElkNode elkGraphNode = elkGraphQueue.poll();
for (ElkEdge elkedge : elkGraphNode.getContainedEdges()) {
// We don't support hyperedges
checkEdgeValidity(elkedge);
if (elkgraph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY) != OrderingStrategy.NONE
|| elkgraph.getProperty(LayeredOptions.CYCLE_BREAKING_STRATEGY)
== CycleBreakingStrategy.MODEL_ORDER) {
// Assign a model order to the edges as they are read
elkedge.setProperty(InternalProperties.MODEL_ORDER, index++);
}
ElkNode sourceNode = ElkGraphUtil.connectableShapeToNode(elkedge.getSources().get(0));
ElkNode targetNode = ElkGraphUtil.connectableShapeToNode(elkedge.getTargets().get(0));
// Don't bother if either the edge or at least one of its end points are excluded from layout
if (elkedge.getProperty(LayeredOptions.NO_LAYOUT)
|| sourceNode.getProperty(LayeredOptions.NO_LAYOUT)
|| targetNode.getProperty(LayeredOptions.NO_LAYOUT)) {
continue;
}
// Check if this edge is an inside self-loop
boolean isInsideSelfLoop = elkedge.isSelfloop()
&& sourceNode.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE)
&& elkedge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
// Find the graph the edge will be placed in. Basically, if the edge is an inside
// self loop or connects one of its end points to a descendant, the edge will be
// placed in the graph that represents that end point's insides. Otherwise, it will
// be placed in the current graph.
ElkNode parentElkGraph = elkGraphNode;
if (isInsideSelfLoop || ElkGraphUtil.isDescendant(targetNode, sourceNode)) {
parentElkGraph = sourceNode;
} else if (ElkGraphUtil.isDescendant(sourceNode, targetNode)) {
parentElkGraph = targetNode;
}
LGraph parentLGraph = lgraph;
LNode parentLNode = (LNode) nodeAndPortMap.get(parentElkGraph);
if (parentLNode != null) {
parentLGraph = parentLNode.getNestedGraph();
}
// Transform the edge, finally...
LEdge ledge = transformEdge(elkedge, parentElkGraph, parentLGraph);
// Find the graph the edge's coordinates will have to be made relative to during export. This will only
// do something if the edge containment inside ELK Layered differs from the edge containment in the
// ELK graph
ledge.setProperty(InternalProperties.COORDINATE_SYSTEM_ORIGIN,
findCoordinateSystemOrigin(elkedge, elkgraph, lgraph));
}
// We may need to look at edges contained in the current graph node's children as well.
// this is true unless either the current graph node does not have hierarchy handling
// enabled, or a child has another layout algorithm configured
boolean hasHierarchyHandlingEnabled = elkGraphNode.getProperty(LayeredOptions.HIERARCHY_HANDLING)
== HierarchyHandling.INCLUDE_CHILDREN;
if (hasHierarchyHandlingEnabled) {
for (ElkNode elkChildGraphNode : elkGraphNode.getChildren()) {
boolean usesElkLayered = !elkChildGraphNode.hasProperty(CoreOptions.ALGORITHM)
|| elkChildGraphNode.getProperty(CoreOptions.ALGORITHM).equals(LayeredOptions.ALGORITHM_ID);
boolean partOfSameLayoutRun = elkChildGraphNode.getProperty(LayeredOptions.HIERARCHY_HANDLING)
== HierarchyHandling.INCLUDE_CHILDREN;
if (usesElkLayered && partOfSameLayoutRun) {
elkGraphQueue.add(elkChildGraphNode);
}
}
}
}
}
/**
* Checks if the given node has any inside self loops.
*
* @param elknode the node to check for inside self loops.
* @return {@code true} if the node has inside self loops, {@code false} otherwise.
*/
private boolean hasInsideSelfLoops(final ElkNode elknode) {
if (elknode.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE)) {
for (ElkEdge edge : ElkGraphUtil.allOutgoingEdges(elknode)) {
if (edge.isSelfloop()) {
if (edge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO)) {
return true;
}
}
}
}
return false;
}
/**
* Finds the LGraph the edge's coordinates should be relative to when the layout results are applied back. This
* is only relevant if this differs from the graph the coordinates are relative to inside ELK Layered. In fact,
* this method only returns something for edges that connect nodes that are not in an anscestor-descendant
* relationship.
*/
private LGraph findCoordinateSystemOrigin(final ElkEdge elkedge, final ElkNode topLevelElkGraph,
final LGraph topLevelLGraph) {
ElkNode source = ElkGraphUtil.connectableShapeToNode(elkedge.getSources().get(0));
ElkNode target = ElkGraphUtil.connectableShapeToNode(elkedge.getTargets().get(0));
// We're going to rule out one case after the other. If the source and the target are siblings, we're good
// (this also includes self-loops)
if (source.getParent() == target.getParent()) {
return null;
}
// If the target is a descendant of the source, ELK Layered uses the source's top left corner as the origin
// of the coordinate system, which matches how ELK graph should be constructed
if (ElkGraphUtil.isDescendant(target, source)) {
return null;
}
// If the source is a descendant of the target, ELK Layered uses the source's parent graph as the origin of
// the coordinate system, while ELK will expect the first common ancestor (the target) to be the origin.
//
// If source and target have no relationship to each other, ELK Layered again uses the source's parent graph
// as the origin of the coordinate system, while ELK will expect the first common ancestor to be the origin
ElkNode origin = elkedge.getContainingNode();
// The origin must always be an ancestor of both, source and parent
assert source == origin || ElkGraphUtil.isDescendant(source, origin);
assert target == origin || ElkGraphUtil.isDescendant(target, origin);
// Find the associated LGraph
if (origin == topLevelElkGraph) {
return topLevelLGraph;
} else {
LNode lnode = (LNode) nodeAndPortMap.get(origin);
if (lnode != null) {
// Find the graph that represents the node's insides
LGraph lgraph = lnode.getNestedGraph();
if (lgraph != null) {
return lgraph;
}
}
}
return null;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Graph Transformation
/**
* Create an LGraph from the given node.
*
* @param elkgraph
* the parent node from which to create the LGraph
* @return a new LGraph instance
*/
private LGraph createLGraph(final ElkNode elkgraph) {
LGraph lgraph = new LGraph();
// Copy the properties of the KGraph to the layered graph
lgraph.copyProperties(elkgraph);
if (lgraph.getProperty(LayeredOptions.DIRECTION) == Direction.UNDEFINED) {
lgraph.setProperty(LayeredOptions.DIRECTION, LGraphUtil.getDirection(lgraph));
}
// The root may have a label manager installed
if (lgraph.getProperty(LabelManagementOptions.LABEL_MANAGER) == null) {
ElkGraphElement root = (ElkGraphElement) EcoreUtil.getRootContainer(elkgraph);
lgraph.setProperty(LabelManagementOptions.LABEL_MANAGER,
root.getProperty(LabelManagementOptions.LABEL_MANAGER));
}
// Remember the KGraph parent the LGraph was created from
lgraph.setProperty(InternalProperties.ORIGIN, elkgraph);
// Initialize the graph properties discovered during the transformations
lgraph.setProperty(InternalProperties.GRAPH_PROPERTIES,
EnumSet.noneOf(GraphProperties.class));
// Adjust the padding to respect inside labels (if the graph has a parent, we need to supply that as well
// since size information stored there may apply to the current graph node)
ElkPadding nodeLabelpadding = NodeLabelAndSizeCalculator.computeInsideNodeLabelPadding(
elkgraph.getParent() == null ? null : ElkGraphAdapters.adapt(elkgraph.getParent()),
ElkGraphAdapters.adaptSingleNode(elkgraph),
Direction.RIGHT);
ElkPadding nodePadding = lgraph.getProperty(LayeredOptions.PADDING);
// Setup the graph's padding
LPadding lPadding = lgraph.getPadding();
lPadding.add(nodePadding);
lPadding.add(nodeLabelpadding);
return lgraph;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// External Port Transformation
/**
* Checks if external ports processing should be active. This is the case if the parent node has
* ports and at least one of the following conditions is true:
* <ul>
* <li>
* Port label placement is set to {@code INSIDE} and at least one of the ports has a label.
* </li>
* <li>
* At least one of the ports has an edge that connects to the insides of the parent node.
* </li>
* <li>
* There is a self-loop that should be routed inside the node.
* </li>
* </ul>
*
* @param elkgraph
* a KGraph we want to check for external ports.
* @param graphProperties
* the set of graph properties to store our results in.
*/
private void checkExternalPorts(final ElkNode elkgraph, final Set<GraphProperties> graphProperties) {
final boolean enableSelfLoops = elkgraph.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE);
final Set<PortLabelPlacement> portLabelPlacement = elkgraph.getProperty(LayeredOptions.PORT_LABELS_PLACEMENT);
// We're iterating over the ports until we've determined that we have both external ports and
// hyperedges, or if there are no more ports left
boolean hasExternalPorts = false;
boolean hasHyperedges = false;
final Iterator<ElkPort> portIterator = elkgraph.getPorts().iterator();
while (portIterator.hasNext() && (!hasExternalPorts || !hasHyperedges)) {
final ElkPort elkport = portIterator.next();
// Find out if there are edges connected to external ports of the graph (this is the case
// for inside self loops as well as for edges connected to children)
int externalPortEdges = 0;
for (ElkEdge elkedge : ElkGraphUtil.allIncidentEdges(elkport)) {
boolean isInsideSelfLoop = enableSelfLoops && elkedge.isSelfloop()
&& elkedge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
boolean connectsToChild = elkedge.getSources().contains(elkport)
? elkgraph == ElkGraphUtil.connectableShapeToNode(elkedge.getTargets().get(0)).getParent()
: elkgraph == ElkGraphUtil.connectableShapeToNode(elkedge.getSources().get(0)).getParent();
if (isInsideSelfLoop || connectsToChild) {
externalPortEdges++;
if (externalPortEdges > 1) {
break;
}
}
}
// External ports?
if (externalPortEdges > 0) {
hasExternalPorts = true;
} else if (portLabelPlacement.contains(PortLabelPlacement.INSIDE) && elkport.getLabels().size() > 0) {
hasExternalPorts = true;
}
// Hyperedges, even?
if (externalPortEdges > 1) {
hasHyperedges = true;
}
}
// Update graph properties
if (hasExternalPorts) {
graphProperties.add(GraphProperties.EXTERNAL_PORTS);
}
if (hasHyperedges) {
graphProperties.add(GraphProperties.HYPEREDGES);
}
}
/**
* Transforms the given external port into a dummy node.
*
* @param elkgraph
* the original KGraph
* @param lgraph
* the corresponding layered graph
* @param elkport
* the port to be transformed
*/
private void transformExternalPort(final ElkNode elkgraph, final LGraph lgraph, final ElkPort elkport) {
// We need some information about the port
KVector elkportPosition = new KVector(
elkport.getX() + elkport.getWidth() / 2.0,
elkport.getY() + elkport.getHeight() / 2.0);
int netFlow = calculateNetFlow(elkport);
PortConstraints portConstraints = elkgraph.getProperty(LayeredOptions.PORT_CONSTRAINTS);
// If we don't have a proper port side, calculate one
PortSide portSide = elkport.getProperty(LayeredOptions.PORT_SIDE);
assert portSide != PortSide.UNDEFINED;
// If we don't have a port offset, infer one
if (!elkport.getAllProperties().containsKey(LayeredOptions.PORT_BORDER_OFFSET)) {
double portOffset;
// if port coordinates are (0,0), we default to port offset 0 to make the common case frustration-free
if (elkport.getX() == 0.0 && elkport.getY() == 0.0) {
portOffset = 0.0;
} else {
portOffset = ElkUtil.calcPortOffset(elkport, portSide);
}
elkport.setProperty(LayeredOptions.PORT_BORDER_OFFSET, portOffset);
}
// Create the external port dummy node
KVector graphSize = new KVector(elkgraph.getWidth(), elkgraph.getHeight());
LNode dummy = LGraphUtil.createExternalPortDummy(
elkport, portConstraints, portSide, netFlow, graphSize,
elkportPosition, new KVector(elkport.getWidth(), elkport.getHeight()),
lgraph.getProperty(LayeredOptions.DIRECTION), lgraph);
dummy.setProperty(InternalProperties.ORIGIN, elkport);
// The dummy only has one port
LPort dummyPort = dummy.getPorts().get(0);
dummyPort.setConnectedToExternalNodes(isConnectedToExternalNodes(elkport));
dummy.setProperty(LayeredOptions.PORT_LABELS_PLACEMENT, PortLabelPlacement.outside());
// If the compound node wants to have its port labels placed on the inside, we need to leave
// enough space for them by creating an LLabel for the KLabels. If the compound node wants to
// have its port labels placed on the outside, we still need to leave enough space for them
// so the port placement does not cause problems on the outside, but we also don't want to waste
// space inside. Thus, for east and west ports, we reduce the label width to zero, otherwise
// we reduce the label height to zero
boolean insidePortLabels =
elkgraph.getProperty(LayeredOptions.PORT_LABELS_PLACEMENT).contains(PortLabelPlacement.INSIDE);
// Transform all of the port's labels
for (ElkLabel elklabel : elkport.getLabels()) {
if (!elklabel.getProperty(LayeredOptions.NO_LAYOUT) && !Strings.isNullOrEmpty(elklabel.getText())) {
LLabel llabel = transformLabel(elklabel);
dummyPort.getLabels().add(llabel);
// If port labels are placed outside, modify the size
// If the port labels are fixed, we should consider the part that is inside the node and not 0.
if (!insidePortLabels) {
double insidePart = 0;
if (PortLabelPlacement.isFixed(elkgraph.getProperty(LayeredOptions.PORT_LABELS_PLACEMENT))) {
// We use 0 as port border offset here, as we only want the label part that is
// inside the node "after" the port.
insidePart = ElkUtil.computeInsidePart(new KVector(elklabel.getX(), elklabel.getY()),
new KVector(elklabel.getWidth(), elklabel.getHeight()),
new KVector(elkport.getWidth(), elkport.getHeight()), 0, portSide);
}
switch (portSide) {
case EAST:
case WEST:
llabel.getSize().x = insidePart;
break;
case NORTH:
case SOUTH:
llabel.getSize().y = insidePart;
break;
}
}
}
}
// Remember the relevant spacings that will apply to the labels here. It's not the spacings in the graph, but
// in the parent
dummy.setProperty(LayeredOptions.SPACING_LABEL_PORT,
elkgraph.getParent().getProperty(LayeredOptions.SPACING_LABEL_PORT));
dummy.setProperty(LayeredOptions.SPACING_LABEL_LABEL,
elkgraph.getParent().getProperty(LayeredOptions.SPACING_LABEL_LABEL));
// Put the external port dummy into our graph and associate it with the original KPort
lgraph.getLayerlessNodes().add(dummy);
nodeAndPortMap.put(elkport, dummy);
}
/**
* Count how many edges want the port to be an output port of the parent and how many want it to
* be an input port. An edge coming into the port from the inside votes for the port to be an
* output port of the parent, as does an edge leaving the port for the outside. The result returned
* by this method is the so-called net flow as fed into {@code createExternalPort(..)}.
*
* @param elkport
* the port to look at.
* @return the port's net flow.
*/
private int calculateNetFlow(final ElkPort elkport) {
final ElkNode elkgraph = elkport.getParent();
final boolean insideSelfLoopsEnabled = elkgraph.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE);
int outputPortVote = 0, inputPortVote = 0;
// Iterate over outgoing edges
for (ElkEdge outgoingEdge : elkport.getOutgoingEdges()) {
final boolean isSelfLoop = outgoingEdge.isSelfloop();
final boolean isInsideSelfLoop = isSelfLoop && insideSelfLoopsEnabled
&& outgoingEdge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
final ElkNode targetNode = ElkGraphUtil.connectableShapeToNode(outgoingEdge.getTargets().get(0));
if (isSelfLoop && isInsideSelfLoop) {
inputPortVote++;
} else if (isSelfLoop && !isInsideSelfLoop) {
outputPortVote++;
} else if (targetNode.getParent() == elkgraph || targetNode == elkgraph) {
inputPortVote++;
} else {
outputPortVote++;
}
}
// Iterate over incoming edges
for (ElkEdge incomingEdge : elkport.getIncomingEdges()) {
final boolean isSelfLoop = incomingEdge.isSelfloop();
final boolean isInsideSelfLoop = isSelfLoop && insideSelfLoopsEnabled
&& incomingEdge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO);
final ElkNode sourceNode = ElkGraphUtil.connectableShapeToNode(incomingEdge.getSources().get(0));
if (isSelfLoop && isInsideSelfLoop) {
outputPortVote++;
} else if (isSelfLoop && !isInsideSelfLoop) {
inputPortVote++;
} else if (sourceNode.getParent() == elkgraph || sourceNode == elkgraph) {
outputPortVote++;
} else {
inputPortVote++;
}
}
return outputPortVote - inputPortVote;
}
/**
* Checks whether the given (external) port has connections to the outside (that is, to non-descendants).
*/
private boolean isConnectedToExternalNodes(final ElkPort elkport) {
ElkNode parent = elkport.getParent();
for (ElkEdge outEdge : elkport.getOutgoingEdges()) {
ElkNode targetNode = ElkGraphUtil.connectableShapeToNode(outEdge.getTargets().get(0));
if (!ElkGraphUtil.isDescendant(targetNode, parent)) {
return true;
}
}
for (ElkEdge inEdge : elkport.getIncomingEdges()) {
ElkNode sourceNode = ElkGraphUtil.connectableShapeToNode(inEdge.getSources().get(0));
if (!ElkGraphUtil.isDescendant(sourceNode, parent)) {
return true;
}
}
return false;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Node Transformation
/**
* Transforms the given node and its contained ports.
*
* @param elknode
* the node to transform
* @param lgraph
* the layered graph into which the transformed node is put
* @return the transformed node
*/
private LNode transformNode(final ElkNode elknode, final LGraph lgraph) {
// add a new node to the layered graph, copying its position
LNode lnode = new LNode(lgraph);
lnode.copyProperties(elknode);
lnode.setProperty(InternalProperties.ORIGIN, elknode);
lnode.getSize().x = elknode.getWidth();
lnode.getSize().y = elknode.getHeight();
lnode.getPosition().x = elknode.getX();
lnode.getPosition().y = elknode.getY();
lgraph.getLayerlessNodes().add(lnode);
nodeAndPortMap.put(elknode, lnode);
// check if the node is a compound node in the original graph
if (!elknode.getChildren().isEmpty() || elknode.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_ACTIVATE)) {
lnode.setProperty(InternalProperties.COMPOUND_NODE, true);
}
Set<GraphProperties> graphProperties = lgraph.getProperty(InternalProperties.GRAPH_PROPERTIES);
// port constraints and sides cannot be undefined
PortConstraints portConstraints = lnode.getProperty(LayeredOptions.PORT_CONSTRAINTS);
if (portConstraints == PortConstraints.UNDEFINED) {
lnode.setProperty(LayeredOptions.PORT_CONSTRAINTS, PortConstraints.FREE);
} else if (portConstraints != PortConstraints.FREE) {
// if the port constraints are not free, set the appropriate graph property
graphProperties.add(GraphProperties.NON_FREE_PORTS);
}
// transform the ports
Direction direction = lgraph.getProperty(LayeredOptions.DIRECTION);
for (ElkPort elkport : elknode.getPorts()) {
if (!elkport.getProperty(LayeredOptions.NO_LAYOUT)) {
transformPort(elkport, lnode, graphProperties, direction, portConstraints);
}
}
// add the node's labels
for (ElkLabel elklabel : elknode.getLabels()) {
if (!elklabel.getProperty(LayeredOptions.NO_LAYOUT) && !Strings.isNullOrEmpty(elklabel.getText())) {
lnode.getLabels().add(transformLabel(elklabel));
}
}
if (lnode.getProperty(LayeredOptions.COMMENT_BOX)) {
graphProperties.add(GraphProperties.COMMENTS);
}
// if we have a hypernode without ports, create a default input and output port
if (lnode.getProperty(LayeredOptions.HYPERNODE)) {
graphProperties.add(GraphProperties.HYPERNODES);
graphProperties.add(GraphProperties.HYPEREDGES);
lnode.setProperty(LayeredOptions.PORT_CONSTRAINTS, PortConstraints.FREE);
}
return lnode;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Port Transformation
/**
* Transforms the given port. The new port will be added to the given node and will be
* registered with the {@code transformMap}.
*
* @param elkport
* the port to transform.
* @param parentLNode
* the node the port should be added to.
* @param graphProperties
* the graph properties of the graph the transformed port will be part of. The graph
* properties are modified depending on the port's properties.
* @param layoutDirection
* the layout direction in the graph the port will be part of.
* @param portConstraints
* the port constraints of the port's node.
* @return the transformed port.
*/
private LPort transformPort(final ElkPort elkport, final LNode parentLNode,
final Set<GraphProperties> graphProperties, final Direction layoutDirection,
final PortConstraints portConstraints) {
// create layered port, copying its position
LPort lport = new LPort();
lport.copyProperties(elkport);
lport.setSide(elkport.getProperty(LayeredOptions.PORT_SIDE));
lport.setProperty(InternalProperties.ORIGIN, elkport);
lport.setNode(parentLNode);
KVector portSize = lport.getSize();
portSize.x = elkport.getWidth();
portSize.y = elkport.getHeight();
KVector portPos = lport.getPosition();
portPos.x = elkport.getX();
portPos.y = elkport.getY();
nodeAndPortMap.put(elkport, lport);
// check if the original port has any outgoing connections to descendants of its node
boolean connectionsToDescendants = elkport.getOutgoingEdges().stream()
// All targets of each edge
.flatMap(edge -> edge.getTargets().stream())
// Target connectable shapes to nodes
.map(ElkGraphUtil::connectableShapeToNode)
// Check if any target is a descendant of the port's parent node
.anyMatch(targetNode -> ElkGraphUtil.isDescendant(targetNode, elkport.getParent()));
// there could be yet incoming connections from descendants
if (!connectionsToDescendants) {
// check if the original port has any incoming connections from descendants of its node
connectionsToDescendants = elkport.getIncomingEdges().stream()
// All sources of each edge
.flatMap(edge -> edge.getSources().stream())
// Source connectable shapes to nodes
.map(ElkGraphUtil::connectableShapeToNode)
// Check if any source is a descendant of the port's parent node
.anyMatch(sourceNode -> ElkGraphUtil.isDescendant(sourceNode, elkport.getParent()));
}
// if there are still no connections to descendants, there might yet be inside self loops involved
if (!connectionsToDescendants) {
// check if the original port has any incoming connections from descendants of its node
connectionsToDescendants = elkport.getOutgoingEdges().stream()
// All targets of each edge
.anyMatch(edge -> edge.isSelfloop() && edge.getProperty(LayeredOptions.INSIDE_SELF_LOOPS_YO));
}
// if we have found connections to / from descendants, mark the port accordingly
lport.setProperty(InternalProperties.INSIDE_CONNECTIONS, connectionsToDescendants);
// initialize the port's side, offset, and anchor point
LGraphUtil.initializePort(lport, portConstraints, layoutDirection,
elkport.getProperty(LayeredOptions.PORT_ANCHOR));
// create the port's labels
for (ElkLabel elklabel : elkport.getLabels()) {
if (!elklabel.getProperty(LayeredOptions.NO_LAYOUT) && !Strings.isNullOrEmpty(elklabel.getText())) {
lport.getLabels().add(transformLabel(elklabel));
}
}
switch (layoutDirection) {
case LEFT:
case RIGHT:
if (lport.getSide() == PortSide.NORTH || lport.getSide() == PortSide.SOUTH) {
graphProperties.add(GraphProperties.NORTH_SOUTH_PORTS);
}
break;
case UP:
case DOWN:
if (lport.getSide() == PortSide.EAST || lport.getSide() == PortSide.WEST) {
graphProperties.add(GraphProperties.NORTH_SOUTH_PORTS);
}
break;
}
return lport;
}