Commit 2b4f0611 authored by Manuel Schrick's avatar Manuel Schrick
Browse files

Temoved Code Smells: BusHandler, SplitHandler, ConnectorCoordinatesHandler

parent 040cb90f
......@@ -71,7 +71,9 @@ public class RoutesLayoutCalculator implements DrawingConstants {
setDetourIndices(bestOrder);
ComponentCoordinatesHandler.assignComponentCoordinates(inst, bestOrder);
PortCoordinatesHandler.assignPortCoordinates(layoutState);
ConnectorCoordinatesHandler.assignConnectorCoordinates(inst, layoutState);
ConnectorCoordinatesHandler connectorCoordinatesHandler = new ConnectorCoordinatesHandler(layoutState);
connectorCoordinatesHandler.assignConnectorCoordinates(inst);
System.out.println("\n\nUndrawn stuff:");
Log.printUndrawnComponents(layoutState);
......
......@@ -108,13 +108,8 @@ public class BusHandler {
int indexTempNodeTarget = layoutState.nodeOrder.indexOf(tempNodeTarget);
for (Route r : routes) {
if (indexTempNodeSource < indexTempNodeTarget) {
r.expandAt(indexTempNodeSource);
r.expandAt(indexTempNodeTarget);
} else {
r.expandAt(indexTempNodeTarget);
r.expandAt(indexTempNodeSource);
}
r.expandAt(Math.min(indexTempNodeSource, indexTempNodeTarget));
r.expandAt(Math.max(indexTempNodeSource, indexTempNodeTarget));
}
PortSymbol sourceOutputPort = source.getFirstPortForOutput(target);
PortSymbol targetInputPort = target.getFirstPortForInput(source);
......@@ -141,15 +136,7 @@ public class BusHandler {
int targetIndex = layoutState.nodeOrder.indexOf(target);
for (int i = 1; i < bus.portConnections.size(); i++) {
sourceOutputPort = bus.portConnections.get(i).getKey();
targetInputPort = bus.portConnections.get(i).getValue();
source.addOutput(tempNodeSource, sourceOutputPort);
tempNodeSource.addInput(source, DummyPort.create());
tempNodeSource.addOutput(tempNodeTarget, null);
tempNodeTarget.addInput(tempNodeSource, null);
tempNodeTarget.addOutput(target, DummyPort.create());
target.addInput(tempNodeTarget, targetInputPort);
insertBusNodesInputAndOutputForConnection(bus, bus.portConnections.get(i));
Route routeAbove = routeIndexAbove >= 0 ? routes.get(routeIndexAbove) : null;
Route routeBelow = routeIndexBelow < routes.size() ? routes.get(routeIndexBelow) : null;
......@@ -179,7 +166,6 @@ public class BusHandler {
temp.set(layoutState.nodeOrder.indexOf(tempNodeTarget), tempNodeTarget, null, DummyPort.create());
temp.set(layoutState.nodeOrder.indexOf(target), target, targetInputPort, temp.getNodeOutputPort(target));
portPair = new Pair<>(sourceOutputPort, targetInputPort);
detourHolder = layoutState.detourHolders.getOrDefault(portPair, new ConnectorDetourHolder(connection, portPair));
......@@ -192,6 +178,18 @@ public class BusHandler {
}
}
private static void insertBusNodesInputAndOutputForConnection(Bus bus, Pair<PortSymbol, PortSymbol> ports) {
PortSymbol sourceOutputPort = ports.getKey();
PortSymbol targetInputPort = ports.getValue();
bus.source.addOutput(bus.sourceBusNode, sourceOutputPort);
bus.sourceBusNode.addInput(bus.source, DummyPort.create());
bus.sourceBusNode.addOutput(bus.targetBusNode, null);
bus.targetBusNode.addInput(bus.sourceBusNode, null);
bus.targetBusNode.addOutput(bus.target, DummyPort.create());
bus.target.addInput(bus.targetBusNode, targetInputPort);
}
private static void insertBusWithBackConnector(Bus bus, BackConnector backConnector, RoutesLayoutState layoutState) {
List<Route> routes = layoutState.routes;
Node source = backConnector.source;
......
......@@ -23,11 +23,49 @@ import java.util.Optional;
*/
public class ConnectorCoordinatesHandler implements DrawingConstants {
public static void assignConnectorCoordinates(ExpandedComponentInstanceSymbol enclosingComponent, RoutesLayoutState layoutState) {
private RoutesLayoutState layoutState;
HashMap<Pair<Node, Node>, List<ConnectorSymbol>> backAndBusConnectors = new HashMap<>();
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<>();
List<Pair<Node, Node>> assignedPairs = new ArrayList<>();
for (Pair<Node, Node> pair : layoutState.busses.keySet()) {
Bus bus = layoutState.busses.get(pair);
List<ConnectorSymbol> connectors = backAndBusConnectors.getOrDefault(pair, new ArrayList<>());
if (layoutState.backConnectors.containsKey(pair)) {
BackConnector backConnector = layoutState.backConnectors.get(pair);
assignBusAndBackConnectorCoordinates(connectors, backConnector, routes);
} else {
assignBusConnectorCoordinates(connectors, bus, routes);
}
assignedPairs.add(pair);
}
for (Pair<Node, Node> pair : layoutState.backConnectors.keySet()) {
if (!assignedPairs.contains(pair)) {
List<ConnectorSymbol> connectors = backAndBusConnectors.getOrDefault(pair, new ArrayList<>());
BackConnector backConnector = layoutState.backConnectors.get(pair);
assignBackConnectorCoordinates(connectors, backConnector, routes);
}
}
for (Pair<Node, Node> pair : splitConnectors.keySet()) {
assignSplitConnectorCoordinates(splitConnectors.get(pair), layoutState.splitConnectors.get(pair), routes);
}
}
private void findNonnormalConnectors(ExpandedComponentInstanceSymbol enclosingComponent) {
enclosingComponent.getConnectors().stream()
.filter(connector -> !connector.getSourcePort().getTags(PortLayoutSymbol.KIND).isEmpty() || !connector.getSourcePort().getTags(CanvasPortLayoutSymbol.KIND).isEmpty())
.filter(connector -> !connector.getTargetPort().getTags(PortLayoutSymbol.KIND).isEmpty() || !connector.getTargetPort().getTags(CanvasPortLayoutSymbol.KIND).isEmpty())
......@@ -75,37 +113,9 @@ public class ConnectorCoordinatesHandler implements DrawingConstants {
assignNormalConnectorCoordinates(connector, enclosingComponent);
}
});
List<Pair<Node, Node>> assignedPairs = new ArrayList<>();
for (Pair<Node, Node> pair : layoutState.busses.keySet()) {
Bus bus = layoutState.busses.get(pair);
List<ConnectorSymbol> connectors = backAndBusConnectors.getOrDefault(pair, new ArrayList<>());
if (layoutState.backConnectors.containsKey(pair)) {
BackConnector backConnector = layoutState.backConnectors.get(pair);
assignBusAndBackConnectorCoordinates(connectors, bus, backConnector, routes);
} else {
assignBusConnectorCoordinates(connectors, bus, routes);
}
assignedPairs.add(pair);
}
for (Pair<Node, Node> pair : layoutState.backConnectors.keySet()) {
if (!assignedPairs.contains(pair)) {
List<ConnectorSymbol> connectors = backAndBusConnectors.getOrDefault(pair, new ArrayList<>());
BackConnector backConnector = layoutState.backConnectors.get(pair);
assignBackConnectorCoordinates(connectors, backConnector, routes);
}
}
for (Pair<Node, Node> pair : splitConnectors.keySet()) {
assignSplitConnectorCoordinates(splitConnectors.get(pair), layoutState.splitConnectors.get(pair), routes);
}
}
private static void assignNormalConnectorCoordinates(ConnectorSymbol connector, ExpandedComponentInstanceSymbol enclosingComponent) {
private void assignNormalConnectorCoordinates(ConnectorSymbol connector, ExpandedComponentInstanceSymbol enclosingComponent) {
int startX;
int endX;
int startY;
......@@ -161,7 +171,7 @@ public class ConnectorCoordinatesHandler implements DrawingConstants {
connector.addTag(new ConnectorLayoutSymbol(IdGenerator.getUniqueId(), points, portSpacingBypassPoint));
}
private static void assignBusAndBackConnectorCoordinates(List<ConnectorSymbol> connectors, Bus bus, BackConnector backConnector, List<Route> routes) {
private void assignBusAndBackConnectorCoordinates(List<ConnectorSymbol> connectors, BackConnector backConnector, List<Route> routes) {
int sourceBackConTempNodeX =RoutesUtils.getColumnLeft(backConnector.tempSourceColumn, routes);
int targetBackConTempNodeX = RoutesUtils.getColumnRight(backConnector.tempTargetColumn, routes);
int backConMiddleY = RoutesUtils.getRowCenter(backConnector.row, routes);
......@@ -233,7 +243,7 @@ public class ConnectorCoordinatesHandler implements DrawingConstants {
}
}
private static void assignBusConnectorCoordinates(List<ConnectorSymbol> connectors, Bus bus, List<Route> routes) {
private void assignBusConnectorCoordinates(List<ConnectorSymbol> connectors, Bus bus, List<Route> routes) {
int sourceBusX = RoutesUtils.getColumnLeft(bus.sourceBusColumn, routes);
int targetBusX = RoutesUtils.getColumnRight(bus.targetBusColumn, routes);
int centerY = RoutesUtils.getRowCenter(bus.sourceBusCenterRow, routes);
......@@ -287,7 +297,7 @@ public class ConnectorCoordinatesHandler implements DrawingConstants {
}
}
private static void assignSplitConnectorCoordinates(ConnectorSymbol connector, SplitConnector splitCon, List<Route> routes) {
private void assignSplitConnectorCoordinates(ConnectorSymbol connector, SplitConnector splitCon, List<Route> routes) {
PortSymbol sourcePort = connector.getSourcePort();
PortSymbol targetPort = connector.getTargetPort();
......
......@@ -234,39 +234,53 @@ public class SplitHandler {
}
}
addedSplitConnectors.addAll(createSplitConnectorsIfNeeded(nodeLeft, nodeRight, node, newRoute, oldRoute, layoutState));
oldRoute.clear(columnIndex);
return addedSplitConnectors;
}
private static List<SplitConnector> createSplitConnectorsIfNeeded(Node nodeLeft, Node centerNode, Node nodeRight, Route newRoute, Route oldRoute, RoutesLayoutState layoutState) {
int centerColumnIndex = oldRoute.columnIndexOf(centerNode);
int leftColumnIndex = oldRoute.columnIndexOf(nodeLeft);
int rightColumnIndex = oldRoute.columnIndexOf(nodeRight);
boolean isFreeLeft = newRoute.isFreeBetweenNodes(leftColumnIndex, centerColumnIndex, true);
boolean isFreeRight = newRoute.isFreeBetweenNodes(centerColumnIndex, rightColumnIndex, true);
List<SplitConnector> addedSplitConnectors = new ArrayList<>();
if (nodeLeft != null) {
Pair<Node, Node> pair = new Pair<>(nodeLeft, node);
Pair<Node, Node> pair = new Pair<>(nodeLeft, centerNode);
if (layoutState.splitConnectors.containsKey(pair)) {
SplitConnector splitCon = layoutState.splitConnectors.get(pair);
newRoute.set(columnIndex - 1, splitCon.tempNode, null, DummyPort.create());
oldRoute.set(columnIndex - 1, splitCon.tempNode, null, null);
newRoute.set(centerColumnIndex - 1, splitCon.tempNode, null, DummyPort.create());
oldRoute.set(centerColumnIndex - 1, splitCon.tempNode, null, null);
} else if (isFreeLeft) {
newRoute.set(leftNodeColumnIndex, nodeLeft, null, oldRoute.getNodeOutputPort(nodeLeft));
oldRoute.set(leftNodeColumnIndex, nodeLeft, oldRoute.getNodeInputPort(nodeLeft), null);
newRoute.set(leftColumnIndex, nodeLeft, null, oldRoute.getNodeOutputPort(nodeLeft));
oldRoute.set(leftColumnIndex, nodeLeft, oldRoute.getNodeInputPort(nodeLeft), null);
for (int i = leftNodeColumnIndex + 1; i < columnIndex; i++) {
for (int i = leftColumnIndex + 1; i < centerColumnIndex; i++) {
oldRoute.set(i, RouteState.EMPTY);
newRoute.set(i, RouteState.ROUTE);
}
} else {
SplitConnector splitConLeft = new SplitConnector();
splitConLeft.source = nodeLeft;
splitConLeft.target = node;
splitConLeft.tempNode = new Node(null, NodeNameGenerator.nextTempNodesName(node), true);
splitConLeft.target = centerNode;
splitConLeft.tempNode = new Node(null, NodeNameGenerator.nextTempNodesName(centerNode), true);
splitConLeft.isIncoming = true;
splitConLeft.sourceOutputPort = oldRoute.getNodeOutputPort(splitConLeft.source);
splitConLeft.targetInputPort = oldRoute.getNodeInputPort(splitConLeft.target);
for (Route route: layoutState.routes) {
route.expandAt(columnIndex);
route.expandAt(centerColumnIndex);
}
newRoute.set(columnIndex, splitConLeft.tempNode, null, DummyPort.create());
oldRoute.set(columnIndex, splitConLeft.tempNode, DummyPort.create(), null);
newRoute.set(centerColumnIndex, splitConLeft.tempNode, null, DummyPort.create());
oldRoute.set(centerColumnIndex, splitConLeft.tempNode, DummyPort.create(), null);
columnIndex += 1;
rightNodeColumnIndex += 1;
centerColumnIndex += 1;
rightColumnIndex += 1;
addedSplitConnectors.add(splitConLeft);
Pair<PortSymbol, PortSymbol> portPair = new Pair<>(oldRoute.getNodeOutputPort(splitConLeft.source), oldRoute.getNodeInputPort(splitConLeft.target));
......@@ -281,35 +295,35 @@ public class SplitHandler {
}
if (nodeRight != null) {
Pair<Node, Node> pair = new Pair<>(node, nodeRight);
Pair<Node, Node> pair = new Pair<>(centerNode, nodeRight);
if (layoutState.splitConnectors.containsKey(pair)) {
SplitConnector splitConRight = layoutState.splitConnectors.get(pair);
newRoute.set(columnIndex + 1, splitConRight.tempNode, DummyPort.create(), null);
oldRoute.set(columnIndex + 1, splitConRight.tempNode, null, null);
newRoute.set(centerColumnIndex + 1, splitConRight.tempNode, DummyPort.create(), null);
oldRoute.set(centerColumnIndex + 1, splitConRight.tempNode, null, null);
} else if (isFreeRight) {
newRoute.set(rightNodeColumnIndex, nodeRight, oldRoute.getNodeInputPort(nodeRight), null);
oldRoute.set(rightNodeColumnIndex, nodeRight, null, oldRoute.getNodeOutputPort(nodeRight));
newRoute.set(rightColumnIndex, nodeRight, oldRoute.getNodeInputPort(nodeRight), null);
oldRoute.set(rightColumnIndex, nodeRight, null, oldRoute.getNodeOutputPort(nodeRight));
for (int i = columnIndex + 1; i < rightNodeColumnIndex; i++) {
for (int i = centerColumnIndex + 1; i < rightColumnIndex; i++) {
oldRoute.set(i, RouteState.EMPTY);
newRoute.set(i, RouteState.ROUTE);
}
} else {
SplitConnector splitConRight = new SplitConnector();
splitConRight.source = node;
splitConRight.source = centerNode;
splitConRight.target = nodeRight;
splitConRight.tempNode = new Node(null, NodeNameGenerator.nextTempNodesName(node), true);
splitConRight.tempNode = new Node(null, NodeNameGenerator.nextTempNodesName(centerNode), true);
splitConRight.isIncoming = false;
splitConRight.sourceOutputPort = oldRoute.getNodeOutputPort(splitConRight.source);
splitConRight.targetInputPort = oldRoute.getNodeInputPort(splitConRight.target);
for (Route route : layoutState.routes) {
route.expandAt(columnIndex + 1);
route.expandAt(centerColumnIndex + 1);
}
oldRoute.set(columnIndex + 1, splitConRight.tempNode, null, DummyPort.create());
newRoute.set(columnIndex + 1, splitConRight.tempNode, DummyPort.create(), null);
oldRoute.set(centerColumnIndex + 1, splitConRight.tempNode, null, DummyPort.create());
newRoute.set(centerColumnIndex + 1, splitConRight.tempNode, DummyPort.create(), null);
addedSplitConnectors.add(splitConRight);
......@@ -324,8 +338,6 @@ public class SplitHandler {
}
}
oldRoute.clear(columnIndex);
return addedSplitConnectors;
}
}
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