Commit e876a5c2 authored by Sebastian Vollbrecht's avatar Sebastian Vollbrecht

Added two utility methods to the LayerStructure class.

Also inserted corresponding calls where possible.
parent d19096c1
......@@ -370,12 +370,12 @@ public final class GraphGenerator {
if (dst == SOURCE || dst == SINK)
continue;
int dstDepth = layers.getDepth(dst.getId());
int dstDepth = layers.getDepth(dst);
boolean hasMandatoryIncomingEdge = false;
for (Edge<ResourceNode> incomingEdge : edgeCreator.incomingEdgesOf(dst)) {
if (layers.getDepth(incomingEdge.getSrc().getId()) == dstDepth - 1) {
if (layers.getDepth(incomingEdge.getSrc()) == dstDepth - 1) {
hasMandatoryIncomingEdge = true;
break;
}
......@@ -482,7 +482,7 @@ public final class GraphGenerator {
out.println("\t\tTrying to generate an outgoing edge for node " + src);
}
int srcDepth = layers.getDepth(src.getId());
int srcDepth = layers.getDepth(src);
Set<ResourceNode> dstCandidates = new HashSet<>();
......@@ -576,7 +576,7 @@ public final class GraphGenerator {
if (src == SOURCE || src == SINK)
continue;
int srcLayer = layers.getDepth(src.getId());
int srcLayer = layers.getDepth(src);
List<ResourceNode> dstNodes = new ArrayList<>();
......
......@@ -206,7 +206,7 @@ public class EdgeCreator extends Observable<Edge<ResourceNode>> implements EdgeC
public boolean isEdgeValid(ResourceNode src, ResourceNode dst) {
if (src != GraphGenerator.SOURCE && dst != GraphGenerator.SINK) {
if (layers.getDepth(src.getId()) >= layers.getDepth(dst.getId())) {
if (layers.getDepth(src) >= layers.getDepth(dst)) {
return false;
}
}
......
......@@ -323,13 +323,13 @@ public class FeasibleMinIIProperty extends Property {
private void prependEdge(PlannedEdge edge) {
if (highestLayer == null)
highestLayer = layers.getDepth(edge.dst.getId());
highestLayer = layers.getDepth(edge.dst);
if (!isEndNodeImmutable && highestLayer == layers.getHeight() - 1)
isEndNodeImmutable = true;
if (lowestLayer == null)
lowestLayer = layers.getDepth(edge.src.getId());
lowestLayer = layers.getDepth(edge.src);
else
lowestLayer--;
......@@ -342,13 +342,13 @@ public class FeasibleMinIIProperty extends Property {
private void appendEdge(PlannedEdge edge) {
if (lowestLayer == null)
lowestLayer = layers.getDepth(edge.src.getId());
lowestLayer = layers.getDepth(edge.src);
if (!isStartNodeImmutable && lowestLayer == 0)
isStartNodeImmutable = true;
if (highestLayer == null)
highestLayer = layers.getDepth(edge.dst.getId());
highestLayer = layers.getDepth(edge.dst);
else
highestLayer++;
......@@ -506,8 +506,8 @@ public class FeasibleMinIIProperty extends Property {
private boolean moveNodesToNeighbouringLayersIfNeeded(ResourceNode n1, ResourceNode n2) {
int firstNodeDepth = layers.getDepth(n1.getId());
int secondNodeDepth = layers.getDepth(n2.getId());
int firstNodeDepth = layers.getDepth(n1);
int secondNodeDepth = layers.getDepth(n2);
/*
* Only move the nodes if they are not currently located in neighbouring layers
......@@ -538,7 +538,7 @@ public class FeasibleMinIIProperty extends Property {
layers.swapIDs(nodeToSwap.getId(), swapID);
}
return layers.getDepth(n1.getId()) < layers.getDepth(n2.getId());
return layers.getDepth(n1) < layers.getDepth(n2);
}
......@@ -608,7 +608,7 @@ public class FeasibleMinIIProperty extends Property {
candidates.add(candidate);
int candidateDepth = layers.getDepth(candidate.getId());
int candidateDepth = layers.getDepth(candidate);
canBeNewSucc &= candidateDepth == path.highestLayer + 1;
canBeNewPred &= candidateDepth == path.lowestLayer - 1;
......
......@@ -176,7 +176,7 @@ public class InfeasibleEdgePlanner {
private static Set<ResourceNode> getPossiblePredecessors(ResourceNode node, StartConfiguration startCfg,
InfeasibleConfiguration infeasibleCfg) {
int nodeDepth = infeasibleCfg.getLayers().getDepth(node.getId());
int nodeDepth = infeasibleCfg.getLayers().getDepth(node);
if (nodeDepth == startCfg.getDestinationNodeLayer() + 1) {
return JavaUtils.asSet(startCfg.getDestinationNode());
......@@ -233,8 +233,9 @@ public class InfeasibleEdgePlanner {
* successors. Otherwise it might be possible for the summed delay (delay to the
* zero node plus the delay to the successor) to exceed the required delay.
*/
for (ResourceNode node : JavaUtils.asSortedList(infeasibleCfg.getProblematicNodes(), Comparator
.comparingInt(n -> infeasibleCfg.getLayers().getDepth(n.getId())))) {
for (ResourceNode node : JavaUtils.asSortedList(infeasibleCfg.getProblematicNodes(),
Comparator.comparingInt(infeasibleCfg.getLayers()::getDepth)
)) {
makeNodeSlotProblematic(node, problematicEdgesDelay, incomingEdgesByNode, fixedEdges);
}
......@@ -330,11 +331,11 @@ public class InfeasibleEdgePlanner {
*/
if (startCfg.getProblematicResource().delay == 0) {
int sourceNodeDepth = startCfg.getLayers().getDepth(fixedNode.getId());
int sourceNodeDepth = startCfg.getLayers().getDepth(fixedNode);
for (ResourceNode dst : infeasibleCfg.getFixedNodes()) {
if (startCfg.getLayers().getDepth(dst.getId()) <= sourceNodeDepth) {
if (startCfg.getLayers().getDepth(dst) <= sourceNodeDepth) {
continue;
}
......
......@@ -295,8 +295,8 @@ public class InfeasibleMinIIProperty extends Property {
* could be replaced by zero nodes if a lower problematic node required one to
* be placed in the respective layer.
*/
for (ResourceNode problematicNode : JavaUtils.asSortedList(infeasibleCfg.getProblematicNodes(), Comparator
.comparingInt(n -> layers.getDepth(n.getId())))) {
for (ResourceNode problematicNode : JavaUtils
.asSortedList(infeasibleCfg.getProblematicNodes(), Comparator.comparingInt(layers::getDepth))) {
/*
* If a problematic node has been fixed already, we don't need to place it
......
......@@ -197,7 +197,7 @@ public class InfeasibleNodePlacer {
}
}
boolean useCurrentLayer = targetLayers.contains(infeasibleCfg.getLayers().getDepth(problematicNode.getId()));
boolean useCurrentLayer = targetLayers.contains(infeasibleCfg.getLayers().getDepth(problematicNode));
/*
* Determine the problematic node's target layer, based on our previous
......@@ -207,7 +207,7 @@ public class InfeasibleNodePlacer {
if (!useCurrentLayer) {
targetLayer = JavaUtils.pickRandomElement(targetLayers, rng);
} else {
targetLayer = infeasibleCfg.getLayers().getDepth(problematicNode.getId());
targetLayer = infeasibleCfg.getLayers().getDepth(problematicNode);
}
if (!useCurrentLayer) {
......
......@@ -94,7 +94,7 @@ public class InfeasibleConfiguration extends MinIICycleConfiguration {
throw new IllegalArgumentException("Node " + node + " has been fixed already.");
}
int layer = layers.getDepth(node.getId());
int layer = layers.getDepth(node);
if (problematicNodes.contains(node)) {
JavaUtils.insertOrIncValue(problematicNodesAtLayer, layer);
......@@ -224,7 +224,7 @@ public class InfeasibleConfiguration extends MinIICycleConfiguration {
*/
public Set<ResourceNode> getLowestProblematicNodes() {
int maxDepth = Collections.max(problematicNodesAtLayer.keySet());
return JavaUtils.asSet(problematicNodes.stream().filter(n -> layers.getDepth(n.getId()) == maxDepth));
return JavaUtils.asSet(problematicNodes.stream().filter(n -> layers.getDepth(n) == maxDepth));
}
}
......@@ -90,11 +90,11 @@ public final class LayerStructure implements Serializable, Iterable<Integer>, Ob
if (firstID == secondID)
return;
if (!containsID(firstID)) {
if (!contains(firstID)) {
throw new NodeDoesNotExistException(firstID, this);
}
if (!containsID(secondID)) {
if (!contains(secondID)) {
throw new NodeDoesNotExistException(secondID, this);
}
......@@ -120,10 +120,20 @@ public final class LayerStructure implements Serializable, Iterable<Integer>, Ob
* @param id the ID
* @return true if it is contained in the layer structure, otherwise false
*/
public boolean containsID(int id) {
public boolean contains(int id) {
return depthsByID.containsKey(id);
}
/**
* Checks whether the layer structure contains the provided node's ID.
*
* @param node the node
* @return true if its ID is contained in the layer structure, otherwise false
*/
public boolean contains(Node node) {
return contains(node.getId());
}
/**
* Returns an unmodifiable view of the layer at the specified depth.
*
......@@ -168,13 +178,24 @@ public final class LayerStructure implements Serializable, Iterable<Integer>, Ob
*/
public int getDepth(int id) {
if (!containsID(id)) {
if (!contains(id)) {
throw new NodeDoesNotExistException(id, this);
}
return depthsByID.get(id);
}
/**
* Returns the depth of the given node, i.e. which layer its ID is currently located in.
*
* @param node the provided node
* @return the depth of the node's ID
* @throws NodeDoesNotExistException if the layer structure does not contain the ID of the provided node
*/
public int getDepth(Node node) {
return getDepth(node.getId());
}
/**
* Returns the relative depth of the provided ID. The relative depth is always in the inclusive interval [0, 1].
*
......
......@@ -343,8 +343,8 @@ public class GraphUtils {
int minDepth = Integer.MAX_VALUE;
for (N src : sourceNodes) {
if (layers.containsID(src.getId())) {
minDepth = Math.min(minDepth, layers.getDepth(src.getId()));
if (layers.contains(src)) {
minDepth = Math.min(minDepth, layers.getDepth(src));
}
}
......@@ -352,7 +352,7 @@ public class GraphUtils {
for (N dst : destinationNodes) {
if (layers.containsID(dst.getId()) && layers.getDepth(dst.getId()) < minDepth)
if (layers.contains(dst) && layers.getDepth(dst) < minDepth)
continue;
worklist.offer(dst);
......@@ -370,7 +370,7 @@ public class GraphUtils {
N src = edge.getSrc();
if (layers.containsID(src.getId()) && layers.getDepth(src.getId()) < minDepth)
if (layers.contains(src) && layers.getDepth(src) < minDepth)
continue;
if (!reachableNodesByNode.containsKey(src))
......@@ -468,9 +468,9 @@ public class GraphUtils {
N src = backedge.getSrc();
N dst = backedge.getDst();
minDepth = Math.min(minDepth, Math.min(layers.getDepth(src.getId()), layers.getDepth(dst.getId())));
minDepth = Math.min(minDepth, Math.min(layers.getDepth(src), layers.getDepth(dst)));
if (layers.getDepth(src.getId()) > layers.getDepth(dst.getId())) {
if (layers.getDepth(src) > layers.getDepth(dst)) {
resultSet.add(new Edge<>(dst, src, 0, 0));
}
}
......@@ -541,7 +541,7 @@ public class GraphUtils {
N src = edge.getSrc();
if (layers.getDepth(src.getId()) < minDepth)
if (layers.getDepth(src) < minDepth)
continue;
delays.putIfAbsent(src, new HashMap<>());
......@@ -560,7 +560,7 @@ public class GraphUtils {
N src = edge.getSrc();
if (layers.getDepth(src.getId()) < minDepth)
if (layers.getDepth(src) < minDepth)
continue;
delays.putIfAbsent(src, new HashMap<>());
......@@ -620,13 +620,13 @@ public class GraphUtils {
delays.put(sourceNode, new HashMap<>());
delays.get(sourceNode).put(sourceNode, 0);
int depthLimit = layers.getDepth(sourceNode.getId());
int depthLimit = layers.getDepth(sourceNode);
Queue<N> worklist = new ArrayDeque<>();
for (N dstNode : destinationSet) {
if (layers.containsID(dstNode.getId()) && layers.getDepth(dstNode.getId()) <= depthLimit)
if (layers.contains(dstNode) && layers.getDepth(dstNode) <= depthLimit)
continue;
delays.putIfAbsent(dstNode, new HashMap<>());
......@@ -636,7 +636,7 @@ public class GraphUtils {
N src = edge.getSrc();
if (layers.containsID(src.getId()) && layers.getDepth(src.getId()) < depthLimit)
if (layers.contains(src) && layers.getDepth(src) < depthLimit)
continue;
delays.putIfAbsent(src, new HashMap<>());
......@@ -651,14 +651,14 @@ public class GraphUtils {
N dst = worklist.poll();
if (layers.containsID(dst.getId()) && layers.getDepth(dst.getId()) <= depthLimit)
if (layers.contains(dst) && layers.getDepth(dst) <= depthLimit)
continue;
for (Edge<N> edge : incomingEdges.get(dst)) {
N src = edge.getSrc();
if (layers.containsID(src.getId()) && layers.getDepth(src.getId()) < depthLimit)
if (layers.contains(src) && layers.getDepth(src) < depthLimit)
continue;
delays.putIfAbsent(src, new HashMap<>());
......@@ -720,9 +720,9 @@ public class GraphUtils {
if (startNode == endNode)
return delays;
int maxDepth = layers.getDepth(endNode.getId());
int maxDepth = layers.getDepth(endNode);
if (layers.containsID(startNode.getId()) && layers.getDepth(startNode.getId()) >= maxDepth)
if (layers.contains(startNode) && layers.getDepth(startNode) >= maxDepth)
return delays;
Queue<N> worklist = new ArrayDeque<>();
......@@ -732,7 +732,7 @@ public class GraphUtils {
N src = edge.getSrc();
N dst = edge.getDst();
if (layers.containsID(dst.getId()) && layers.getDepth(dst.getId()) >= maxDepth && dst != endNode)
if (layers.contains(dst) && layers.getDepth(dst) >= maxDepth && dst != endNode)
continue;
delays.put(dst, src.getDelay() + edge.getDelay());
......@@ -750,7 +750,7 @@ public class GraphUtils {
N dst = edge.getDst();
if (layers.containsID(dst.getId()) && layers.getDepth(dst.getId()) >= maxDepth && dst != endNode)
if (layers.contains(dst) && layers.getDepth(dst) >= maxDepth && dst != endNode)
continue;
boolean addSuccessors = false;
......@@ -810,9 +810,9 @@ public class GraphUtils {
if (startNode == endNode)
return delays;
int minDepth = layers.getDepth(startNode.getId());
int minDepth = layers.getDepth(startNode);
if (layers.containsID(endNode.getId()) && layers.getDepth(endNode.getId()) <= minDepth)
if (layers.contains(endNode) && layers.getDepth(endNode) <= minDepth)
return delays;
Queue<N> worklist = new ArrayDeque<>();
......@@ -826,7 +826,7 @@ public class GraphUtils {
N src = edge.getSrc();
if (layers.containsID(src.getId()) && layers.getDepth(src.getId()) <= minDepth && src != startNode)
if (layers.contains(src) && layers.getDepth(src) <= minDepth && src != startNode)
continue;
boolean addPredecessors = false;
......
......@@ -574,13 +574,13 @@ public class InfeasibleNodePlacerTest {
placer.placeNode(node6, JavaUtils.asSet(3));
assertEquals(3, layers.getDepth(node6.getId()));
assertEquals(3, layers.getDepth(node6));
assertEquals(JavaUtils.asSet(node6), infeasibleCfg.getZeroNodesByLayer().get(3));
assertEquals(new Integer(1), infeasibleCfg.getProblematicNodesAtLayer().get(3));
placer.placeNode(node8, JavaUtils.asSet(3));
assertEquals(3, layers.getDepth(node8.getId()));
assertEquals(3, layers.getDepth(node8));
assertEquals(JavaUtils.asSet(node6, node8), infeasibleCfg.getZeroNodesByLayer().get(3));
assertEquals(new Integer(2), infeasibleCfg.getProblematicNodesAtLayer().get(3));
......@@ -633,21 +633,21 @@ public class InfeasibleNodePlacerTest {
placer.placeNode(node4, JavaUtils.asSet(2));
assertEquals(2, layers.getDepth(node4.getId()));
assertEquals(2, layers.getDepth(node4));
assertEquals(new Integer(1), infeasibleCfg.getProblematicNodesAtLayer().get(2));
placer.placeNode(node6, JavaUtils.asSet(3));
assertEquals(3, layers.getDepth(node6.getId()));
assertEquals(3, layers.getDepth(node6));
assertEquals(JavaUtils.asSet(node5), infeasibleCfg.getZeroNodesByLayer().get(2));
assertEquals(2, layers.getDepth(node5.getId()));
assertEquals(2, layers.getDepth(node5));
assertEquals(new Integer(1), infeasibleCfg.getProblematicNodesAtLayer().get(3));
placer.placeNode(node8, JavaUtils.asSet(5));
assertEquals(5, layers.getDepth(node8.getId()));
assertEquals(5, layers.getDepth(node8));
assertEquals(JavaUtils.asSet(node7), infeasibleCfg.getZeroNodesByLayer().get(3));
assertEquals(3, layers.getDepth(node7.getId()));
assertEquals(3, layers.getDepth(node7));
assertTrue(infeasibleCfg.getZeroNodesByLayer().get(4).stream().anyMatch(n -> n != node5 && n != node7));
assertTrue(layers.getLayer(4).stream().map(nodeTable::get).allMatch(n -> n.getDelay() == 0));
assertEquals(new Integer(1), infeasibleCfg.getProblematicNodesAtLayer().get(5));
......@@ -702,19 +702,19 @@ public class InfeasibleNodePlacerTest {
placer.placeNode(node4, JavaUtils.asSet(2));
assertEquals(2, layers.getDepth(node4.getId()));
assertEquals(2, layers.getDepth(node4));
assertEquals(new Integer(1), infeasibleCfg.getProblematicNodesAtLayer().get(2));
placer.placeNode(node6, JavaUtils.asSet(3));
assertEquals(3, layers.getDepth(node6.getId()));
assertEquals(3, layers.getDepth(node6));
assertEquals(JavaUtils.asSet(node2), infeasibleCfg.getZeroNodesByLayer().get(2));
assertEquals(2, layers.getDepth(node2.getId()));
assertEquals(2, layers.getDepth(node2));
assertEquals(new Integer(1), infeasibleCfg.getProblematicNodesAtLayer().get(3));
placer.placeNode(node8, JavaUtils.asSet(3));
assertEquals(3, layers.getDepth(node8.getId()));
assertEquals(3, layers.getDepth(node8));
assertEquals(new Integer(2), infeasibleCfg.getProblematicNodesAtLayer().get(3));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment