Commit 6143d4b6 authored by Manuel Schrick's avatar Manuel Schrick
Browse files

Removed Code Smells: Route, SplitHandler, ComponentCoordinatesHandler, ConnectorCoordinatesHandler

parent 58c0c3c8
......@@ -18,11 +18,11 @@ public class OptimalRouteOrderCalculator implements RouteOrderCalculator {
List<List<Node>> openNodesStack = new ArrayList<>();
List<List<Route>> possibleRoutesStack = new ArrayList<>();
HashMap<Route, Integer> routePriorites = new HashMap<>();
HashMap<Route, Integer> routePriorities = new HashMap<>();
List<Route> nextRouteStack = new ArrayList<>();
openNodesStack.add(new ArrayList<>());
possibleRoutesStack.add(getNextPossibleRoutes(remainingRoutes, new ArrayList<>(), routePriorites));
possibleRoutesStack.add(getNextPossibleRoutes(remainingRoutes, new ArrayList<>(), routePriorities));
List<Route> possibleRoutes;
List<Node> openNodes;
......@@ -45,8 +45,8 @@ public class OptimalRouteOrderCalculator implements RouteOrderCalculator {
Route reverted = nextRouteStack.remove(0);
remainingRoutes.forEach(route -> {
int prio = routePriorites.getOrDefault(route, 0) + 1;
routePriorites.put(route, prio);
int priority = routePriorities.getOrDefault(route, 0) + 1;
routePriorities.put(route, priority);
});
remainingRoutes.add(reverted);
......@@ -67,7 +67,7 @@ public class OptimalRouteOrderCalculator implements RouteOrderCalculator {
openNodes.removeAll(nextRoute.closedNodes);
possibleRoutesStack.add(0, getNextPossibleRoutes(remainingRoutes, openNodes, routePriorites));
possibleRoutesStack.add(0, getNextPossibleRoutes(remainingRoutes, openNodes, routePriorities));
openNodesStack.add(0, openNodes);
nextRouteStack.add(0, nextRoute);
......@@ -114,10 +114,10 @@ public class OptimalRouteOrderCalculator implements RouteOrderCalculator {
}
nextPossibleRoutes.sort((lhs, rhs) -> {
int lhsPrio = routePriorities.getOrDefault(lhs, 0);
int rhsPrio = routePriorities.getOrDefault(rhs, 0);
int lhsPriority = routePriorities.getOrDefault(lhs, 0);
int rhsPriority = routePriorities.getOrDefault(rhs, 0);
return Integer.compare(lhsPrio, rhsPrio);
return Integer.compare(lhsPriority, rhsPriority);
});
return nextPossibleRoutes;
......
......@@ -25,60 +25,64 @@ public class ComponentCoordinatesHandler implements DrawingConstants {
enclosingComponent.addTag(new CanvasLayoutSymbol(IdGenerator.getUniqueId(), CANVAS_OUTER_MARGIN, CANVAS_OUTER_MARGIN, tableWidth, tableHeight));
for (int columnIndex = 0; columnIndex < length; columnIndex++) {
Node currentNode = null;
List<PortSymbol> ports = new ArrayList<>();
int startRowIndex = -1;
int endRowIndex = -1;
for (int rowIndex = 0; rowIndex < routes.size(); rowIndex++) {
Route route = routes.get(rowIndex);
if (route.getState(columnIndex) == RouteState.NODE) {
Node node = route.getNode(columnIndex);
PortSymbol inputPort = route.getNodeInputPort(node);
PortSymbol outputPort = route.getNodeOutputPort(node);
boolean hasNewPorts = inputPort != null && !ports.contains(inputPort) || outputPort != null && !ports.contains(outputPort);
if (!hasNewPorts) {
if (!node.isConnected()) {
assignComponentCoordinates(node, columnIndex, rowIndex, rowIndex, routes);
}
continue;
}
assignComponentCoordinatesForColumn(columnIndex, routes);
}
}
if (inputPort != null) {
ports.add(inputPort);
private static void assignComponentCoordinatesForColumn(int columnIndex, List<Route> routes) {
Node currentNode = null;
List<PortSymbol> ports = new ArrayList<>();
int startRowIndex = -1;
int endRowIndex = -1;
for (int rowIndex = 0; rowIndex < routes.size(); rowIndex++) {
Route route = routes.get(rowIndex);
if (route.getState(columnIndex) == RouteState.NODE) {
Node node = route.getNode(columnIndex);
PortSymbol inputPort = route.getNodeInputPort(node);
PortSymbol outputPort = route.getNodeOutputPort(node);
boolean hasNewPorts = inputPort != null && !ports.contains(inputPort) || outputPort != null && !ports.contains(outputPort);
if (!hasNewPorts) {
if (!node.isConnected()) {
assignComponentCoordinates(node, columnIndex, rowIndex, rowIndex, routes);
}
continue;
}
if (outputPort != null) {
ports.add(outputPort);
}
if (inputPort != null) {
ports.add(inputPort);
}
if (currentNode == null) {
currentNode = node;
startRowIndex = rowIndex;
} else if (!currentNode.equals(node)) {
assignComponentCoordinates(currentNode, columnIndex, startRowIndex, endRowIndex, routes);
if (outputPort != null) {
ports.add(outputPort);
}
currentNode = node;
startRowIndex = rowIndex;
}
if (currentNode == null) {
currentNode = node;
startRowIndex = rowIndex;
} else if (!currentNode.equals(node)) {
assignComponentCoordinates(currentNode, columnIndex, startRowIndex, endRowIndex, routes);
endRowIndex = rowIndex;
} else if (currentNode != null) {
if (!currentNode.compName.equals(NAME_ROOT_IN) && !currentNode.compName.equals(NAME_ROOT_OUT)) {
assignComponentCoordinates(currentNode, columnIndex, startRowIndex, endRowIndex, routes);
}
currentNode = node;
startRowIndex = rowIndex;
}
currentNode = null;
startRowIndex = -1;
endRowIndex = -1;
endRowIndex = rowIndex;
} else if (currentNode != null) {
if (!currentNode.compName.equals(NAME_ROOT_IN) && !currentNode.compName.equals(NAME_ROOT_OUT)) {
assignComponentCoordinates(currentNode, columnIndex, startRowIndex, endRowIndex, routes);
}
}
if (currentNode != null) {
assignComponentCoordinates(currentNode, columnIndex, startRowIndex, endRowIndex, routes);
currentNode = null;
startRowIndex = -1;
endRowIndex = -1;
}
}
if (currentNode != null) {
assignComponentCoordinates(currentNode, columnIndex, startRowIndex, endRowIndex, routes);
}
}
public static void assignComponentCoordinates(Node node, int column, int startRow, int endRow, List<Route> routes) {
......
......@@ -24,18 +24,17 @@ import java.util.Optional;
public class ConnectorCoordinatesHandler implements DrawingConstants {
private RoutesLayoutState layoutState;
HashMap<Pair<Node, Node>, List<ConnectorSymbol>> backAndBusConnectors = new HashMap<>();
HashMap<Pair<Node, Node>, ConnectorSymbol> splitConnectors = new HashMap<>();
private HashMap<Pair<Node, Node>, List<ConnectorSymbol>> backAndBusConnectors = new HashMap<>();
private HashMap<Pair<Node, Node>, ConnectorSymbol> splitConnectors = new HashMap<>();
public ConnectorCoordinatesHandler(RoutesLayoutState layoutState) {
this.layoutState = layoutState;
}
public void assignConnectorCoordinates(ExpandedComponentInstanceSymbol enclosingComponent) {
List<Route> routes = layoutState.routes;
HashMap<Pair<Node, Node>, List<ConnectorSymbol>> backAndBusConnectors = new HashMap<>();
HashMap<Pair<Node, Node>, ConnectorSymbol> splitConnectors = new HashMap<>();
findNonnormalConnectors(enclosingComponent);
List<Route> routes = layoutState.routes;
List<Pair<Node, Node>> assignedPairs = new ArrayList<>();
for (Pair<Node, Node> pair : layoutState.busses.keySet()) {
......
......@@ -5,7 +5,9 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.Expanded
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.montiarc.svggenerator.calculators.RoutesLayoutState;
import de.monticore.lang.montiarc.svggenerator.calculators.routes.Bus;
import de.monticore.lang.montiarc.svggenerator.calculators.routes.Node;
import de.monticore.lang.montiarc.svggenerator.calculators.routes.Route;
import de.monticore.lang.montiarc.svggenerator.calculators.routes.RouteState;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.*;
import java.util.ArrayList;
......@@ -35,7 +37,7 @@ public class Log {
public static void printRoutes(String title, List<Route> routes) {
System.out.println(title);
routes.forEach(route -> System.out.println(route.toIdString()));
routes.forEach(route -> System.out.println(getRouteIdString(route)));
System.out.println("");
}
......@@ -132,4 +134,65 @@ public class Log {
System.out.println("All Connectors drawn");
}
}
public static String getRouteIdString(Route route) {
int id = route.id;
String message;
if (id < 10) {
message = " " + String.valueOf(id) + ": ";
} else if (id < 100) {
message = " " + String.valueOf(id) + ": ";
} else if (id < 1000) {
message = " " + String.valueOf(id) + ": ";
} else {
message = String.valueOf(id) + ": ";
}
for (int i = 0; i < route.size(); i++) {
RouteState state = route.getState(i);
switch (state) {
case EMPTY:
message += " ";
break;
case NODE:
message += getNodeIdString(route, i);
break;
case ROUTE:
message += "---";
break;
}
}
return message;
}
private static String getNodeIdString(Route route, int columnIndex) {
Node node = route.getNode(columnIndex);
if (node == null) {
return "N--";
} else {
int index = node.indexInOrder;
String message = String.valueOf(index);
if (columnIndex >= route.size() - 1) {
return message;
}
RouteState nextState = route.getState(columnIndex + 1);
if (nextState == RouteState.EMPTY) {
message += index < 10 ? " " : " ";
} else if (nextState == RouteState.ROUTE) {
message += index < 10 ? "--" : "-";
} else if (route.getNodeOutputPort(node) != null) {
message += index < 10 ? "--" : "-";
} else {
message += index < 10 ? " " : " ";
}
return message;
}
}
}
......@@ -16,7 +16,7 @@ import java.util.List;
public class SplitHandler {
public static void removeSplits(RoutesLayoutState layoutState) {
int length = layoutState.routes.stream().mapToInt(Route::size).max().orElse(0);
int length = RoutesUtils.getColumnCount(layoutState);
int columnIndex = 1;
while (columnIndex < length) {
List<List<Integer>> nodeParts = new ArrayList<>();
......
......@@ -88,17 +88,6 @@ public class Route {
}
}
public boolean isFreeAtInterval(int start, int end) {
for (int i = start; i <= end; i++) {
RouteState state = getState(i);
if (state == RouteState.NODE || state == RouteState.ROUTE) {
return false;
}
}
return true;
}
public boolean isFreeBetweenNodes(int start, int end) {
return isFreeBetweenNodes(start, end, false);
}
......@@ -153,10 +142,6 @@ public class Route {
return nodes.get(index);
}
public Node firstNode() {
return nodes.stream().filter(Objects::nonNull).findFirst().orElseGet(null);
}
public Node lastNode() {
for (int i = nodes.size() - 1; i >= 0; i--) {
if (nodes.get(i) != null) {
......@@ -224,17 +209,6 @@ public class Route {
Node node = nodes.get(index);
if (node != null) {
Node successor = getSuccessor(node);
Node predecessor = getPredecessor(node);
// if (successor != null) {
// inputPorts.remove(successor);
// }
//
// if (predecessor != null) {
// outputPorts.remove(predecessor);
// }
inputPorts.remove(node);
outputPorts.remove(node);
nodes.set(index, null);
......@@ -318,80 +292,6 @@ public class Route {
outputPorts.putAll(other.outputPorts);
}
public void moveConnection(int startIndex, int endIndex, Route destRoute) {
for (int i = startIndex; i <= endIndex; i++) {
RouteState temp = destRoute.getState(i);
Node otherNode = destRoute.getNode(i);
PortSymbol otherInputPort = null;
PortSymbol otherOutputPort = null;
if (otherNode != null) {
otherInputPort = destRoute.getNodeInputPort(otherNode);
otherOutputPort = destRoute.getNodeOutputPort(otherNode);
}
if (getState(i) == RouteState.NODE && temp == RouteState.NODE) {
Node node = getNode(i);
if (i == startIndex) {
destRoute.set(i, node, destRoute.getNodeInputPort(node), getNodeOutputPort(node));
set(i, otherNode, getNodeInputPort(otherNode), otherOutputPort);
} else if (i == endIndex) {
destRoute.set(i, node, getNodeInputPort(node), destRoute.getNodeOutputPort(node));
set(i, otherNode, otherInputPort, getNodeOutputPort(otherNode));
} else {
destRoute.set(i, node, getNodeInputPort(node), getNodeOutputPort(node));
set(i, otherNode, otherInputPort, otherOutputPort);
}
} else if (getState(i) == RouteState.NODE) {
Node node = getNode(i);
if (i == startIndex) {
destRoute.set(i, node, null, getNodeOutputPort(node));
if (getNodeInputPort(node) != null) {
set(i, node, getNodeInputPort(node), null);
} else {
set(i, RouteState.EMPTY);
}
} else if (i == endIndex) {
destRoute.set(i, node, getNodeInputPort(node), null);
if (getNodeOutputPort(node) != null) {
set(i, node, null, getNodeOutputPort(node));
} else {
set(i, RouteState.EMPTY);
}
} else {
destRoute.set(i, node, getNodeInputPort(node), getNodeOutputPort(node));
set(i, RouteState.EMPTY);
}
} else if (temp == RouteState.NODE) {
if (i == startIndex) {
set(i, otherNode, null, destRoute.getNodeOutputPort(otherNode));
if (destRoute.getNodeInputPort(otherNode) != null) {
destRoute.set(i, otherNode, destRoute.getNodeInputPort(otherNode), null);
} else {
destRoute.set(i, RouteState.EMPTY);
}
} else if (i == endIndex) {
set(i, otherNode, destRoute.getNodeInputPort(otherNode), null);
if (destRoute.getNodeOutputPort(otherNode) != null) {
destRoute.set(i, otherNode, null, destRoute.getNodeOutputPort(otherNode));
} else {
destRoute.set(i, RouteState.EMPTY);
}
} else {
set(i, otherNode, destRoute.getNodeInputPort(otherNode), destRoute.getNodeOutputPort(otherNode));
destRoute.set(i, RouteState.ROUTE);
}
} else {
destRoute.set(i, getState(i));
set(i, temp);
}
}
}
public boolean isNodeConnected(Node node) {
return nodes.contains(node) && (inputPorts.get(node) != null || outputPorts.get(node) != null);
}
......@@ -428,145 +328,6 @@ public class Route {
return message;
}
public String toIdString() {
String message;
if (id < 10) {
message = " " + String.valueOf(id) + ": ";
} else if (id < 100) {
message = " " + String.valueOf(id) + ": ";
} else if (id < 1000) {
message = " " + String.valueOf(id) + ": ";
} else {
message = String.valueOf(id) + ": ";
}
for (int i = 0; i < size(); i++) {
RouteState state = getState(i);
switch (state) {
case EMPTY:
message += " ";
break;
case NODE:
Node node = getNode(i);
if (node == null) {
message += "N--";
} else {
int index = node.indexInOrder;
message += index;
if (i < size() - 1) {
RouteState nextState = getState(i + 1);
if (nextState == RouteState.EMPTY) {
message += index < 10 ? " " : " ";
} else if (nextState == RouteState.ROUTE) {
message += index < 10 ? "--" : "-";
} else {
if (getNodeOutputPort(node) != null) {
message += index < 10 ? "--" : "-";
} else {
message += index < 10 ? " " : " ";
}
}
}
}
break;
case ROUTE:
message += "---";
break;
}
}
return message;
}
public String toVerticalIdString() {
String message;
if (id < 10) {
message = " " + String.valueOf(id) + ": ";
} else if (id < 100) {
message = " " + String.valueOf(id) + ": ";
} else if (id < 1000) {
message = " " + String.valueOf(id) + ": ";
} else {
message = String.valueOf(id) + ": ";
}
for (int i = 0; i < size(); i++) {
RouteState state = getState(i);
message += "\n " + i + ": ";
switch (state) {
case EMPTY:
message += "EMPTY";
break;
case NODE:
message += "NODE: " + getNode(i).indexInOrder;
break;
case ROUTE:
message += "ROUTE";
break;
}
}
return message;
}
public void printInputPorts() {
System.out.print("inputPorts: ");
inputPorts.keySet().forEach(n -> {
System.out.print(n.compName + "(" + n.indexInOrder + "): ");
PortSymbol port = inputPorts.get(n);
if (port != null) {
System.out.print(port.getName() + ", ");
} else {
System.out.print("null, ");
}
});
System.out.println("");
}
public void printOutputPorts() {
System.out.print("outputPorts: ");
outputPorts.keySet().forEach(n -> {
System.out.print(n.compName + "(" + n.indexInOrder + "): ");
PortSymbol port = outputPorts.get(n);
if (port != null) {
System.out.print(port.getName() + ", ");
} else {
System.out.print("null, ");
}
});
System.out.println("");
}
public List<Integer[]> getConnectionIndices() {
List<Integer[]> indices = new ArrayList<>();
int startIndex = -1;
for (int i = 0; i < size(); i++) {
if (getState(i) == RouteState.NODE) {
if (startIndex < 0) {
startIndex = i;
} else {
indices.add(new Integer[]{startIndex, i});
startIndex = i;
}
} else if (getState(i) == RouteState.EMPTY) {
startIndex = -1;
}
}
return indices;
}
@Override
public boolean equals(Object other) {
if (other instanceof Route) {
......