|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2022 Kiel University and others. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License 2.0 which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + *******************************************************************************/ |
| 10 | +package org.eclipse.elk.alg.layered.components; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.Set; |
| 14 | + |
| 15 | +import org.eclipse.elk.alg.layered.graph.LGraph; |
| 16 | +import org.eclipse.elk.alg.layered.options.LayeredOptions; |
| 17 | +import org.eclipse.elk.core.math.KVector; |
| 18 | +import org.eclipse.elk.core.options.CoreOptions; |
| 19 | +import org.eclipse.elk.core.options.EdgeRouting; |
| 20 | +import org.eclipse.elk.core.options.PortSide; |
| 21 | + |
| 22 | +/** |
| 23 | + * A graph placer that tries to place the components of a graph with taking the model order and the |
| 24 | + * connections to external ports into account. This graph placer should only be used if the constraints applying to the |
| 25 | + * external ports are either {@code FREE} or {@code FIXED_SIDES}. |
| 26 | + */ |
| 27 | +public class ComponentGroupModelOrderGraphPlacer extends ComponentGroupGraphPlacer { |
| 28 | + |
| 29 | + /////////////////////////////////////////////////////////////////////////////// |
| 30 | + // AbstractGraphPlacer |
| 31 | + |
| 32 | + @Override |
| 33 | + public void combine(final List<LGraph> components, final LGraph target) { |
| 34 | + componentGroups.clear(); |
| 35 | + assert !components.contains(target); |
| 36 | + target.getLayerlessNodes().clear(); |
| 37 | + |
| 38 | + // Check if there are any components to be placed |
| 39 | + if (components.isEmpty()) { |
| 40 | + target.getSize().x = 0; |
| 41 | + target.getSize().y = 0; |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + // Set the graph properties |
| 46 | + LGraph firstComponent = components.get(0); |
| 47 | + target.copyProperties(firstComponent); |
| 48 | + |
| 49 | + // Construct component groups |
| 50 | + for (LGraph component : components) { |
| 51 | + addComponent(component); |
| 52 | + } |
| 53 | + |
| 54 | + // Place components in each group |
| 55 | + KVector spaceBlockedBySouthEdges = new KVector(); |
| 56 | + KVector spaceBlockedByComponents = new KVector(); |
| 57 | + KVector offset = new KVector(); |
| 58 | + KVector maxSize = new KVector(); |
| 59 | + double componentSpacing = firstComponent.getProperty(LayeredOptions.SPACING_COMPONENT_COMPONENT); |
| 60 | + |
| 61 | + // In horizontal mode the next component is placed below the last one. |
| 62 | + // If it has a connection to NORTH, it has to be placed at an x-coordinate to not hit the other components. |
| 63 | + // Otherwise will be placed as far WEST as possible without potentially hitting SOUTH edges. |
| 64 | + for (ComponentGroup group : componentGroups) { |
| 65 | + // Place the components |
| 66 | + if (target.getProperty(CoreOptions.DIRECTION).isHorizontal()) { |
| 67 | + offset.x = spaceBlockedBySouthEdges.x; |
| 68 | + for (Set<PortSide> side : group.getPortSides()) { |
| 69 | + if (side.contains(PortSide.NORTH)) { |
| 70 | + offset.x = spaceBlockedByComponents.x; |
| 71 | + break; |
| 72 | + } |
| 73 | + } |
| 74 | + } else if (target.getProperty(CoreOptions.DIRECTION).isVertical()) { |
| 75 | + offset.y = spaceBlockedBySouthEdges.y; |
| 76 | + for (Set<PortSide> side : group.getPortSides()) { |
| 77 | + if (side.contains(PortSide.WEST)) { |
| 78 | + offset.y = spaceBlockedByComponents.y; |
| 79 | + break; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + KVector groupSize = this.placeComponents((ModelOrderComponentGroup) group, componentSpacing); |
| 84 | + offsetGraphs(group.getComponents(), offset.x, offset.y); |
| 85 | + |
| 86 | + if (target.getProperty(CoreOptions.DIRECTION).isHorizontal()) { |
| 87 | + spaceBlockedByComponents.x = offset.x + groupSize.x; |
| 88 | + maxSize.x = Math.max(maxSize.x, spaceBlockedByComponents.x); |
| 89 | + for (Set<PortSide> side : group.getPortSides()) { |
| 90 | + if (side.contains(PortSide.SOUTH)) { |
| 91 | + spaceBlockedBySouthEdges.x = offset.x + groupSize.x; |
| 92 | + break; |
| 93 | + } |
| 94 | + } |
| 95 | + spaceBlockedByComponents.y = offset.y + groupSize.y; |
| 96 | + offset.y = spaceBlockedByComponents.y; |
| 97 | + maxSize.y = Math.max(maxSize.y, offset.y); |
| 98 | + } else if (target.getProperty(CoreOptions.DIRECTION).isVertical()) { |
| 99 | + spaceBlockedByComponents.y = offset.y + groupSize.y; |
| 100 | + maxSize.y = Math.max(maxSize.y, spaceBlockedByComponents.y); |
| 101 | + for (Set<PortSide> side : group.getPortSides()) { |
| 102 | + if (side.contains(PortSide.EAST)) { |
| 103 | + spaceBlockedBySouthEdges.y = offset.y + groupSize.y; |
| 104 | + break; |
| 105 | + } |
| 106 | + } |
| 107 | + spaceBlockedByComponents.x = offset.x + groupSize.x; |
| 108 | + offset.x = spaceBlockedByComponents.x; |
| 109 | + maxSize.x = Math.max(maxSize.x, offset.x); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Set the graph's new size (the component group sizes include additional spacing |
| 114 | + // on the right and bottom sides which we need to subtract at this point) |
| 115 | + target.getSize().x = maxSize.x - componentSpacing; |
| 116 | + target.getSize().y = maxSize.y - componentSpacing; |
| 117 | + |
| 118 | + // if compaction is desired, do so!cing; |
| 119 | + if (firstComponent.getProperty(LayeredOptions.COMPACTION_CONNECTED_COMPONENTS) |
| 120 | + // the compaction only supports orthogonally routed edges |
| 121 | + && firstComponent.getProperty(LayeredOptions.EDGE_ROUTING) == EdgeRouting.ORTHOGONAL) { |
| 122 | + |
| 123 | + // apply graph offsets (which we reset later on) |
| 124 | + // since the compaction works in a common coordinate system |
| 125 | + for (LGraph h : components) { |
| 126 | + offsetGraph(h, h.getOffset().x, h.getOffset().y); |
| 127 | + } |
| 128 | + |
| 129 | + ComponentsCompactor compactor = new ComponentsCompactor(); |
| 130 | + compactor.compact(components, target.getSize(), componentSpacing); |
| 131 | + |
| 132 | + // the compaction algorithm places components absolutely, |
| 133 | + // therefore we have to use the final drawing's offset |
| 134 | + for (LGraph h : components) { |
| 135 | + h.getOffset().reset().add(compactor.getOffset()); |
| 136 | + } |
| 137 | + |
| 138 | + // set the new (compacted) graph size |
| 139 | + target.getSize().reset().add(compactor.getGraphSize()); |
| 140 | + } |
| 141 | + |
| 142 | + // finally move the components to the combined graph |
| 143 | + for (ComponentGroup group : componentGroups) { |
| 144 | + moveGraphs(target, group.getComponents(), 0, 0); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + /////////////////////////////////////////////////////////////////////////////// |
| 150 | + // Component Group Building |
| 151 | + |
| 152 | + /** |
| 153 | + * Adds the given component to the first component group that has place for it. |
| 154 | + * |
| 155 | + * @param component the component to be placed. |
| 156 | + */ |
| 157 | + protected void addComponent(final LGraph component) { |
| 158 | + // Check if one of the existing component groups has some place left |
| 159 | + if (this.componentGroups.size() > 0) { |
| 160 | + ModelOrderComponentGroup group = |
| 161 | + (ModelOrderComponentGroup) this.componentGroups.get(componentGroups.size() - 1); |
| 162 | + if (group.add(component)) { |
| 163 | + return; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // Create a new component group for the component |
| 168 | + componentGroups.add(new ModelOrderComponentGroup(component)); |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments