Skip to content

Commit

Permalink
Merge branch 'sdo/fixModelOrderPortCalculation' into sdo/mwr-mt
Browse files Browse the repository at this point in the history
  • Loading branch information
soerendomroes committed Aug 16, 2024
2 parents 3a6de95 + 68691d8 commit 88712c7
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
package org.eclipse.elk.alg.layered.intermediate;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.elk.alg.layered.graph.LEdge;
Expand Down Expand Up @@ -68,8 +70,7 @@ public void process(final LGraph graph, final IElkProgressMonitor progressMonito
// Therefore all ports that connect to the same node should have the same
// (their minimal) model order.
// Get minimal model order for target node

Collections.sort(node.getPorts(),
insertionSortPorts(node.getPorts(),
new ModelOrderPortComparator(previousLayer,
graph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY),
longEdgeTargetNodePreprocessing(node),
Expand All @@ -78,10 +79,11 @@ public void process(final LGraph graph, final IElkProgressMonitor progressMonito
}
}
// Sort nodes.
Collections.sort(layer.getNodes(),
new ModelOrderNodeComparator(previousLayer,
graph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY),
graph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_LONG_EDGE_STRATEGY)));
ModelOrderNodeComparator comparator = new ModelOrderNodeComparator(previousLayer,
graph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_STRATEGY),
graph.getProperty(LayeredOptions.CONSIDER_MODEL_ORDER_LONG_EDGE_STRATEGY));
SortByInputModelProcessor.insertionSort(layer.getNodes(), comparator);

progressMonitor.log("Layer " + layerIndex + ": " + layer);
layerIndex++;
}
Expand Down Expand Up @@ -141,5 +143,35 @@ public static LNode getTargetNode(final LPort port) {
} while (node != null && node.getType() != NodeType.NORMAL);
return node;
}

public static void insertionSort(final List<LNode> layer,
final ModelOrderNodeComparator comparator) {
LNode temp;
for (int i = 1; i < layer.size(); i++) {
temp = layer.get(i);
int j = i;
while (j > 0 && comparator.compare(layer.get(j - 1), temp) > 0) {
layer.set(j, layer.get(j - 1));
j--;
}
layer.set(j, temp);
}
comparator.clearTransitiveOrdering();
}

public static void insertionSortPorts(final List<LPort> ports,
final ModelOrderPortComparator comparator) {
LPort temp;
for (int i = 1; i < ports.size(); i++) {
temp = ports.get(i);
int j = i;
while (j > 0 && comparator.compare(ports.get(j - 1), temp) > 0) {
ports.set(j, ports.get(j - 1));
j--;
}
ports.set(j, temp);
}
comparator.clearTransitiveOrdering();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public int compare(final LNode n1, final LNode n2) {
for (LPort p : n1.getPorts()) {
// Get the first port that actually connects to a previous layer.
if (!p.getIncomingEdges().isEmpty()) {
if (p.getIncomingEdges().get(0).getSource().getNode().getLayer() != n1.getLayer()) {
if (p.getIncomingEdges().get(0).getSource().getNode().getLayer().id == (n1.getLayer().id - 1)) {
p1SourcePort = p.getIncomingEdges().get(0).getSource();
break;
}
Expand All @@ -128,7 +128,7 @@ public int compare(final LNode n1, final LNode n2) {
for (LPort p : n2.getPorts()) {
// Get the first port that actually connects to a previous layer.
if (!p.getIncomingEdges().isEmpty()) {
if (p.getIncomingEdges().get(0).getSource().getNode().getLayer() != n2.getLayer()) {
if (p.getIncomingEdges().get(0).getSource().getNode().getLayer().id == (n2.getLayer().id - 1)) {
p2SourcePort = p.getIncomingEdges().get(0).getSource();
break;
}
Expand Down Expand Up @@ -182,6 +182,11 @@ public int compare(final LNode n1, final LNode n2) {
// Check whether one of them is a helper dummy node.
int comparedWithLongEdgeFeedback = handleHelperDummyNodes(n1, n2);
if (comparedWithLongEdgeFeedback != 0) {
if (comparedWithLongEdgeFeedback > 0) {
updateBiggerAndSmallerAssociations(n1, n2);
} else {
updateBiggerAndSmallerAssociations(n2, n1);
}
return comparedWithLongEdgeFeedback;
}

Expand All @@ -205,6 +210,11 @@ public int compare(final LNode n1, final LNode n2) {
// Check whether one of them is a helper dummy node.
int comparedWithLongEdgeFeedback = handleHelperDummyNodes(n1, n2);
if (comparedWithLongEdgeFeedback != 0) {
if (comparedWithLongEdgeFeedback > 0) {
updateBiggerAndSmallerAssociations(n1, n2);
} else {
updateBiggerAndSmallerAssociations(n2, n1);
}
return comparedWithLongEdgeFeedback;
}
}
Expand All @@ -220,12 +230,11 @@ public int compare(final LNode n1, final LNode n2) {
int n2ModelOrder = n2.getProperty(InternalProperties.MODEL_ORDER);
if (n1ModelOrder > n2ModelOrder) {
updateBiggerAndSmallerAssociations(n1, n2);
return 1;
} else {
updateBiggerAndSmallerAssociations(n2, n1);
return -1;
}
return Integer.compare(
n1ModelOrder,
n2ModelOrder);
} else {
return 0;
}
Expand Down Expand Up @@ -311,7 +320,7 @@ private int handleHelperDummyNodes(LNode n1, LNode n2) {
LNode dummyNodeTargetNode = dummyNodeTargetPort.getNode();
if (dummyNodeTargetNode.equals(n1)) {
updateBiggerAndSmallerAssociations(n2, n1);
return 1;
return -1;
}

// If the two nodes are not the same, order them based on the source model order.
Expand Down Expand Up @@ -374,4 +383,12 @@ private LPort getFirstOutgoingPortOfNode(LNode node) {
private LPort getFirstOutgoingSourcePortOfNode(LNode node) {
return getFirstOutgoingPortOfNode(node).getOutgoingEdges().get(0).getTarget();
}

/**
* Clears the transitive ordering.
*/
public void clearTransitiveOrdering() {
this.biggerThan = new HashMap<>();
this.smallerThan = new HashMap<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,32 @@ public int compare(final LPort originalP1, final LPort originalP2) {

// Sort incoming edges by sorting their ports by the order of the nodes they connect to.
if (!p1.getIncomingEdges().isEmpty() && !p2.getIncomingEdges().isEmpty()) {

LPort p1SourcePort = p1.getIncomingEdges().get(0).getSource();
LPort p2SourcePort = p2.getIncomingEdges().get(0).getSource();
LNode p1Node = p1SourcePort.getNode();
LNode p2Node = p2SourcePort.getNode();
if (p1.getSide() == PortSide.WEST && p2.getSide() == PortSide.WEST && p1SourcePort.getSide() != PortSide.SOUTH
&& p2SourcePort.getSide() != PortSide.SOUTH
|| p1.getSide() == PortSide.NORTH && p2.getSide() == PortSide.NORTH && p1SourcePort.getSide() != PortSide.SOUTH
&& p2SourcePort.getSide() != PortSide.SOUTH
|| p1.getSide() == PortSide.SOUTH && p2.getSide() == PortSide.SOUTH && p1SourcePort.getSide() != PortSide.SOUTH
&& p2SourcePort.getSide() != PortSide.SOUTH
|| p1.getSide() == PortSide.EAST && p2.getSide() == PortSide.EAST && p1SourcePort.getSide() != PortSide.SOUTH
&& p2SourcePort.getSide() != PortSide.SOUTH) {
if (p1.getSide() == PortSide.WEST && p2.getSide() == PortSide.WEST
|| p1.getSide() == PortSide.NORTH && p2.getSide() == PortSide.NORTH
|| p1.getSide() == PortSide.SOUTH && p2.getSide() == PortSide.SOUTH) {
// Some ports are ordered in the way around.
// Previously this did not matter, since the north south processor did override the ordering.
LPort temp = p1;
p1 = p2;
p2 = temp;
}

LPort p1SourcePort = p1.getIncomingEdges().get(0).getSource();
LPort p2SourcePort = p2.getIncomingEdges().get(0).getSource();
LNode p1Node = p1SourcePort.getNode();
LNode p2Node = p2SourcePort.getNode();
if (p1Node.equals(p2Node)) {

int p1MO = p1.getIncomingEdges().get(0).getProperty(InternalProperties.MODEL_ORDER);
int p2MO = p2.getIncomingEdges().get(0).getProperty(InternalProperties.MODEL_ORDER);
if (p1MO > p2MO) {
updateBiggerAndSmallerAssociations(p1, p2);
} else {
updateBiggerAndSmallerAssociations(p2, p1);
// If both connect to the same node, check their occurrence order since these ports have to be handled
// first and should hence be sorted.
for (LPort port : p1Node.getPorts()) {
if (p1SourcePort.equals(port)) {
updateBiggerAndSmallerAssociations(p2, p1);
return -1;
} else if (p2SourcePort.equals(port)) {
updateBiggerAndSmallerAssociations(p1, p2);
return 1;
}
}
// In this case both incoming edges must have a model order set. Check it.
return Integer.compare(p1MO, p2MO);
}
// If both ports connect to long edges in the same layer, reverse the order.
if (p1SourcePort.getNode().getType() == NodeType.LONG_EDGE
Expand All @@ -168,17 +164,20 @@ public int compare(final LPort originalP1, final LPort originalP2) {
// |__2__||
// ___1___|
//
// The difference to the other cases above is that a EAST port uses dummy nodes, while a
// NORTH or SOUTH port uses none since they are later created by the NorthSouthProcessor.
Layer previousLayer = p1Node.getLayer();
int inPreviousLayer = checkReferenceLayer(previousLayer, p1Node, p2Node, p1, p2);
if (inPreviousLayer != 0) {
if (inPreviousLayer > 0) {
updateBiggerAndSmallerAssociations(p1, p2);
} else {
updateBiggerAndSmallerAssociations(p1, p2);
}
return inPreviousLayer;
}
}

// Check which of the nodes connects first to the previous layer.
int inPreviousLayer = checkReferenceLayer(Arrays.stream(previousLayer).collect(Collectors.toList()), p2Node, p1Node, p2, p1);
int inPreviousLayer = checkReferenceLayer(Arrays.stream(previousLayer).collect(Collectors.toList()), p1Node, p2Node, p1, p2);
if (inPreviousLayer != 0) {
return inPreviousLayer;
}
Expand All @@ -188,9 +187,9 @@ public int compare(final LPort originalP1, final LPort originalP2) {
if (portModelOrder) {
int result = checkPortModelOrder(p1, p2);
if (result != 0) {
if (result == -1) {
if (result < 0) {
updateBiggerAndSmallerAssociations(p2, p1);
} else if (result == 1) {
} else if (result > 0) {
updateBiggerAndSmallerAssociations(p1, p2);
}
return result;
Expand Down Expand Up @@ -250,16 +249,6 @@ public int compare(final LPort originalP1, final LPort originalP2) {

// If both ports have the same target nodes, make sure that the backward edge is below the normal edge.
if (p1TargetNode != null && p1TargetNode.equals(p2TargetNode)) {
// // Backward edges below
// if (p1.getOutgoingEdges().get(0).getProperty(InternalProperties.REVERSED)
// && !p2.getOutgoingEdges().get(0).getProperty(InternalProperties.REVERSED)) {
// updateBiggerAndSmallerAssociations(p1, p2);
// return 1;
// } else if (!p1.getOutgoingEdges().get(0).getProperty(InternalProperties.REVERSED)
// && p2.getOutgoingEdges().get(0).getProperty(InternalProperties.REVERSED)) {
// updateBiggerAndSmallerAssociations(p2, p1);
// return -1;
// }
// If both edges are reversed or not reversed, just use their model order.
if (p1Order > p2Order) {
updateBiggerAndSmallerAssociations(p1, p2);
Expand Down Expand Up @@ -385,4 +374,12 @@ public int compare(final PortSide ps1, final PortSide ps2) {
}

}

/**
* Clears the transitive ordering.
*/
public void clearTransitiveOrdering() {
this.biggerThan = new HashMap<>();
this.smallerThan = new HashMap<>();
}
}

0 comments on commit 88712c7

Please sign in to comment.