Commit 4f69d770 authored by Manuel Schrick's avatar Manuel Schrick
Browse files

Removed Code smells: TableSizeOptimzer.moveNodes()

parent 4044d4d4
......@@ -135,4 +135,8 @@ public class RoutesUtils implements DrawingConstants {
public static int getTableHeight(List<Route> routes) {
return getRowBottom(routes.size() - 1, routes) + ROW_SPACING;
}
public static int getColumnCount(RoutesLayoutState layoutState) {
return layoutState.routes.stream().mapToInt(Route::size).max().orElse(0);
}
}
......@@ -87,29 +87,15 @@ public class TableSizeOptimizer {
}
private static void moveTemporaryNodesRight(RoutesLayoutState layoutState) {
int length = layoutState.routes.stream().mapToInt(Route::size).max().orElse(0);
int length = RoutesUtils.getColumnCount(layoutState);
if (length <= 3) {
return;
}
List<Node> nodesToBeMoved = new ArrayList<>();
for (Bus bus : layoutState.busses.values()) {
nodesToBeMoved.add(bus.targetBusNode);
}
for (BackConnector backConnector : layoutState.backConnectors.values()) {
nodesToBeMoved.add(backConnector.tempTarget);
}
for (SplitConnector splitCon : layoutState.splitConnectors.values()) {
if (splitCon.isIncoming) {
nodesToBeMoved.add(splitCon.tempNode);
}
}
moveNodes(layoutState.routes, Direction.RIGHT, nodesToBeMoved::contains);
moveNodes(layoutState.routes, Direction.RIGHT, node -> {
return node.getInputs().isEmpty() && !node.getOutputs().isEmpty();
});
}
private static void moveNodes(List<Route> routes, Direction direction, Predicate<Node> predicate) {
......@@ -133,7 +119,6 @@ public class TableSizeOptimizer {
while (columnIndexPredicate.test(columnIndex)) {
int tempColumnIndex = columnIndex;
boolean hasConflict = false;
Node node = null;
for (int i = rowIndex; i < routes.size(); i++) {
......@@ -166,69 +151,88 @@ public class TableSizeOptimizer {
tempColumnIndexPredicate = index -> index < length - 2;
}
while (tempColumnIndexPredicate.test(tempColumnIndex) && !hasConflict) {
hasConflict = false;
while (tempColumnIndexPredicate.test(tempColumnIndex)) {
int nextColumnIndex = (direction == Direction.LEFT) ? tempColumnIndex - 1 : tempColumnIndex + 1;
int previousColumnIndex = (direction == Direction.LEFT) ? tempColumnIndex + 1 : tempColumnIndex - 1;
List<Integer> occupiedIndices = getOccupiedRowIndicesInColumn(nextColumnIndex, routes);
boolean columnIsFree = isColumnFreeAt(nextColumnIndex, indexTop, indexBottom, routes);
if (!columnIsFree) {
break;
}
moveNode(indexTop, indexBottom, tempColumnIndex, direction, routes);
for (int routeIndex = indexTop; routeIndex <= indexBottom; routeIndex++) {
if (occupiedIndices.contains(routeIndex)) {
hasConflict = true;
break;
}
tempColumnIndex += direction == Direction.LEFT ? -1 : 1;
}
}
}
private static boolean isColumnFreeAt(int columnIndex, int rowIndexTop, int rowIndexBottom, List<Route> routes) {
List<Integer> occupiedIndices = getOccupiedRowIndicesInColumn(columnIndex, routes);
for (int routeIndex = rowIndexTop; routeIndex <= rowIndexBottom; routeIndex++) {
if (occupiedIndices.contains(routeIndex)) {
return false;
}
}
return true;
}
private static void moveNode(int rowIndexTop, int rowIndexBottom, int sourceColumnIndex, Direction direction, List<Route> routes) {
int targetColumnIndex;
int previousColumnIndex;
if (direction == Direction.LEFT) {
targetColumnIndex = sourceColumnIndex - 1;
previousColumnIndex = sourceColumnIndex + 1;
} else {
targetColumnIndex = sourceColumnIndex + 1;
previousColumnIndex = sourceColumnIndex - 1;
}
for (int routeIndex = rowIndexTop; routeIndex <= rowIndexBottom; routeIndex++) {
Route route = routes.get(routeIndex);
RouteState state = route.getState(sourceColumnIndex);
RouteState nextState = route.getState(targetColumnIndex);
if (nextState != RouteState.NODE) {
if (state == RouteState.NODE) {
route.set(targetColumnIndex, route.getNode(sourceColumnIndex));
} else if (state == RouteState.ROUTE) {
route.set(targetColumnIndex, route.getState(sourceColumnIndex));
}
}
if (!hasConflict) {
for (int routeIndex = indexTop; routeIndex <= indexBottom; routeIndex++) {
Route route = routes.get(routeIndex);
RouteState state = route.getState(tempColumnIndex);
RouteState nextState = route.getState(nextColumnIndex);
if (nextState != RouteState.NODE) {
if (state == RouteState.NODE) {
route.set(nextColumnIndex, route.getNode(tempColumnIndex));
} else if (state == RouteState.ROUTE) {
route.set(nextColumnIndex, route.getState(tempColumnIndex));
}
}
nextState = route.getState(nextColumnIndex);
RouteState newState;
if (route.size() > previousColumnIndex && previousColumnIndex > 0) {
RouteState previousState = route.getState(previousColumnIndex);
if (state == RouteState.ROUTE && previousState == RouteState.ROUTE) {
newState = RouteState.ROUTE;
} else if (previousState == RouteState.NODE) {
Node previousNode = route.getNode(previousColumnIndex);
PortSymbol portSymbol = (direction == Direction.LEFT) ? route.getNodeInputPort(previousNode) : route.getNodeOutputPort(previousNode);
if (portSymbol != null) {
newState = RouteState.ROUTE;
} else {
newState = RouteState.EMPTY;
}
} else if (nextState == RouteState.NODE) {
Node nextNode = route.getNode(nextColumnIndex);
PortSymbol portSymbol = (direction == Direction.LEFT) ? route.getNodeOutputPort(nextNode) : route.getNodeInputPort(nextNode);
if (portSymbol != null) {
newState = RouteState.ROUTE;
} else {
newState = RouteState.EMPTY;
}
} else {
newState = RouteState.EMPTY;
}
} else {
newState = RouteState.EMPTY;
}
route.set(tempColumnIndex, newState);
nextState = route.getState(targetColumnIndex);
RouteState newState;
if (route.size() > previousColumnIndex && previousColumnIndex > 0) {
RouteState previousState = route.getState(previousColumnIndex);
if (state == RouteState.ROUTE && previousState == RouteState.ROUTE) {
newState = RouteState.ROUTE;
} else if (previousState == RouteState.NODE) {
Node previousNode = route.getNode(previousColumnIndex);
PortSymbol portSymbol = (direction == Direction.LEFT) ? route.getNodeInputPort(previousNode) : route.getNodeOutputPort(previousNode);
if (portSymbol != null) {
newState = RouteState.ROUTE;
} else {
newState = RouteState.EMPTY;
}
tempColumnIndex += direction == Direction.LEFT ? -1 : 1;
} else if (nextState == RouteState.NODE) {
Node nextNode = route.getNode(targetColumnIndex);
PortSymbol portSymbol = (direction == Direction.LEFT) ? route.getNodeOutputPort(nextNode) : route.getNodeInputPort(nextNode);
if (portSymbol != null) {
newState = RouteState.ROUTE;
} else {
newState = RouteState.EMPTY;
}
} else {
newState = RouteState.EMPTY;
}
} else {
newState = RouteState.EMPTY;
}
route.set(sourceColumnIndex, newState);
}
}
......
Supports Markdown
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