Commit 4044d4d4 authored by Manuel Schrick's avatar Manuel Schrick
Browse files

Removed Code Smells: BusHandler, ConnectorCalculator

parent 947c6209
......@@ -5,7 +5,6 @@ import de.monticore.lang.montiarc.svggenerator.calculators.RoutesLayoutState;
import de.monticore.lang.montiarc.svggenerator.calculators.detour.BusDetour;
import de.monticore.lang.montiarc.svggenerator.calculators.detour.ConnectorDetourHolder;
import de.monticore.lang.montiarc.svggenerator.calculators.routes.*;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.LayoutSymbol;
import javafx.util.Pair;
import java.util.ArrayList;
......@@ -17,7 +16,6 @@ import java.util.List;
public class BusHandler {
public static void insertBuses(RoutesLayoutState layoutState) {
List<Route> routes = layoutState.routes;
List<Pair<Node, Node>> decomposedBusses = new ArrayList<>();
for (Pair<Node, Node> connection : layoutState.busses.keySet()) {
Node source = connection.getKey();
......@@ -27,126 +25,14 @@ public class BusHandler {
Route route = RoutesUtils.getRouteForConnection(source, target, layoutState);
BackConnector backConnector = layoutState.backConnectors.get(connection);
if (route != null && backConnector == null) {
if (bus.sourceBusColumn == bus.targetBusColumn - 1) {
decomposeBus(bus, layoutState);
decomposedBusses.add(connection);
continue;
}
boolean shouldDecomposeBus = true;
if (bus.getSourceOutputPorts().size() > 2 || bus.getTargetInputPorts().size() > 2) {
for (int i = layoutState.nodeOrder.indexOf(source) + 1; i < layoutState.nodeOrder.indexOf(target); i++) {
if (!layoutState.nodeOrder.get(i).isTemporary) {
shouldDecomposeBus = false;
break;
}
}
}
if (shouldDecomposeBus) {
decomposeBus(bus, layoutState);
decomposedBusses.add(connection);
continue;
}
if (route != null && backConnector == null && shouldDecomposeBus(bus, layoutState)) {
decomposeBus(bus, layoutState);
decomposedBusses.add(connection);
continue;
}
if (route != null && backConnector == null) {
System.out.println("\ninsert bus routes: " + source.compName + " -> " + target.compName);
Node tempNodeSource = new Node(null, NodeNameGenerator.nextTempNodesName(source), true);
Node tempNodeTarget = new Node(null, NodeNameGenerator.nextTempNodesName(target), true);
bus.sourceBusNode = tempNodeSource;
bus.targetBusNode = tempNodeTarget;
layoutState.nodeOrder.add(layoutState.nodeOrder.indexOf(source) + 1, tempNodeSource);
layoutState.nodeOrder.add(layoutState.nodeOrder.indexOf(target), tempNodeTarget);
int indexTempNodeSource = layoutState.nodeOrder.indexOf(tempNodeSource);
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);
}
}
PortSymbol sourceOutputPort = source.getFirstPortForOutput(target);
PortSymbol targetInputPort = target.getFirstPortForInput(source);
route.set(layoutState.nodeOrder.indexOf(source), source, route.getNodeInputPort(source), sourceOutputPort);
route.set(layoutState.nodeOrder.indexOf(tempNodeSource), tempNodeSource, DummyPort.create(), DummyPort.create());
route.set(layoutState.nodeOrder.indexOf(tempNodeTarget), tempNodeTarget, DummyPort.create(), DummyPort.create());
route.set(layoutState.nodeOrder.indexOf(target), target, targetInputPort, route.getNodeOutputPort(target));
Pair<PortSymbol, PortSymbol> portPair = new Pair<>(sourceOutputPort, targetInputPort);
ConnectorDetourHolder detourHolder = layoutState.detourHolders.getOrDefault(portPair, new ConnectorDetourHolder(connection, portPair));
BusDetour busDetour = detourHolder.getBusDetour();
busDetour.setTempSource(tempNodeSource);
busDetour.setTempTarget(tempNodeTarget);
detourHolder.addDetour(busDetour);
layoutState.detourHolders.put(portPair, detourHolder);
int routeIndexAbove = routes.indexOf(route) - 1;
int routeIndexBelow = routes.indexOf(route) + 1;
int sourceIndex = layoutState.nodeOrder.indexOf(source);
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);
Route routeAbove = routeIndexAbove >= 0 ? routes.get(routeIndexAbove) : null;
Route routeBelow = routeIndexBelow < routes.size() ? routes.get(routeIndexBelow) : null;
Route temp;
boolean routeAboveIsFree = routeAbove != null && routeAbove.isFreeBetweenNodes(sourceIndex, targetIndex);
boolean routeBelowIsFree = routeBelow != null && routeBelow.isFreeBetweenNodes(sourceIndex, targetIndex);
boolean routeAboveContainsNodes = routeAbove != null && routeAbove.columnIndexOf(source) >= 0 && routeAbove.columnIndexOf(target) >= 0;
if (routeAboveIsFree && routeAboveContainsNodes || routeAboveIsFree && !routeBelowIsFree) {
System.out.println("route " + routeAbove.id + " is free");
temp = routeAbove;
routeIndexAbove -= 1;
} else if (routeBelowIsFree) {
System.out.println("route " + routeBelow.id + " is free");
temp = routeBelow;
routeIndexBelow += 1;
} else {
System.out.println("new route above " + route.id);
temp = RoutesUtils.addRowAt(routes.indexOf(route), layoutState);
routeIndexAbove += 1;
}
temp.set(layoutState.nodeOrder.indexOf(source), source, temp.getNodeInputPort(source), sourceOutputPort);
temp.set(layoutState.nodeOrder.indexOf(tempNodeSource), tempNodeSource, DummyPort.create(), null);
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));
busDetour = detourHolder.getBusDetour(new BusDetour());
busDetour.setTempSource(tempNodeSource);
busDetour.setTempTarget(tempNodeTarget);
detourHolder.addDetour(busDetour);
layoutState.detourHolders.put(portPair, detourHolder);
}
insertBusRoutes(bus, route, layoutState);
} else if (backConnector != null) {
insertBusWithBackConnector(bus, backConnector, layoutState);
} else {
......@@ -205,6 +91,107 @@ public class BusHandler {
}
}
private static void insertBusRoutes(Bus bus, Route initialRoute, RoutesLayoutState layoutState) {
Node source = bus.source;
Node target = bus.target;
List<Route> routes = layoutState.routes;
Node tempNodeSource = new Node(null, NodeNameGenerator.nextTempNodesName(source), true);
Node tempNodeTarget = new Node(null, NodeNameGenerator.nextTempNodesName(target), true);
bus.sourceBusNode = tempNodeSource;
bus.targetBusNode = tempNodeTarget;
layoutState.nodeOrder.add(layoutState.nodeOrder.indexOf(source) + 1, tempNodeSource);
layoutState.nodeOrder.add(layoutState.nodeOrder.indexOf(target), tempNodeTarget);
int indexTempNodeSource = layoutState.nodeOrder.indexOf(tempNodeSource);
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);
}
}
PortSymbol sourceOutputPort = source.getFirstPortForOutput(target);
PortSymbol targetInputPort = target.getFirstPortForInput(source);
initialRoute.set(layoutState.nodeOrder.indexOf(source), source, initialRoute.getNodeInputPort(source), sourceOutputPort);
initialRoute.set(layoutState.nodeOrder.indexOf(tempNodeSource), tempNodeSource, DummyPort.create(), DummyPort.create());
initialRoute.set(layoutState.nodeOrder.indexOf(tempNodeTarget), tempNodeTarget, DummyPort.create(), DummyPort.create());
initialRoute.set(layoutState.nodeOrder.indexOf(target), target, targetInputPort, initialRoute.getNodeOutputPort(target));
Pair<PortSymbol, PortSymbol> portPair = new Pair<>(sourceOutputPort, targetInputPort);
Pair<Node, Node> connection = new Pair<>(source, target);
ConnectorDetourHolder detourHolder = layoutState.detourHolders.getOrDefault(portPair, new ConnectorDetourHolder(connection, portPair));
BusDetour busDetour = detourHolder.getBusDetour();
busDetour.setTempSource(tempNodeSource);
busDetour.setTempTarget(tempNodeTarget);
detourHolder.addDetour(busDetour);
layoutState.detourHolders.put(portPair, detourHolder);
int routeIndexAbove = routes.indexOf(initialRoute) - 1;
int routeIndexBelow = routes.indexOf(initialRoute) + 1;
int sourceIndex = layoutState.nodeOrder.indexOf(source);
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);
Route routeAbove = routeIndexAbove >= 0 ? routes.get(routeIndexAbove) : null;
Route routeBelow = routeIndexBelow < routes.size() ? routes.get(routeIndexBelow) : null;
Route temp;
boolean routeAboveIsFree = routeAbove != null && routeAbove.isFreeBetweenNodes(sourceIndex, targetIndex);
boolean routeBelowIsFree = routeBelow != null && routeBelow.isFreeBetweenNodes(sourceIndex, targetIndex);
boolean routeAboveContainsNodes = routeAbove != null && routeAbove.columnIndexOf(source) >= 0 && routeAbove.columnIndexOf(target) >= 0;
if (routeAboveIsFree && routeAboveContainsNodes || routeAboveIsFree && !routeBelowIsFree) {
System.out.println("route " + routeAbove.id + " is free");
temp = routeAbove;
routeIndexAbove -= 1;
} else if (routeBelowIsFree) {
System.out.println("route " + routeBelow.id + " is free");
temp = routeBelow;
routeIndexBelow += 1;
} else {
System.out.println("new route above " + initialRoute.id);
temp = RoutesUtils.addRowAt(routes.indexOf(initialRoute), layoutState);
routeIndexAbove += 1;
}
temp.set(layoutState.nodeOrder.indexOf(source), source, temp.getNodeInputPort(source), sourceOutputPort);
temp.set(layoutState.nodeOrder.indexOf(tempNodeSource), tempNodeSource, DummyPort.create(), null);
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));
busDetour = detourHolder.getBusDetour(new BusDetour());
busDetour.setTempSource(tempNodeSource);
busDetour.setTempTarget(tempNodeTarget);
detourHolder.addDetour(busDetour);
layoutState.detourHolders.put(portPair, detourHolder);
}
}
private static void insertBusWithBackConnector(Bus bus, BackConnector backConnector, RoutesLayoutState layoutState) {
List<Route> routes = layoutState.routes;
Node source = backConnector.source;
......@@ -238,7 +225,7 @@ public class BusHandler {
bus.targetBusNode = new Node(null, "unused");
Pair<Node, Node> nodePair = new Pair<>(source, target);
for (Pair<PortSymbol, PortSymbol> portPair: bus.portConnections) {
for (Pair<PortSymbol, PortSymbol> portPair : bus.portConnections) {
ConnectorDetourHolder detourHolder = layoutState.detourHolders.getOrDefault(portPair, new ConnectorDetourHolder(nodePair, portPair));
BusDetour busDetour = detourHolder.getBusDetour(new BusDetour());
......@@ -247,4 +234,20 @@ public class BusHandler {
detourHolder.addDetour(busDetour);
}
}
private static boolean shouldDecomposeBus(Bus bus, RoutesLayoutState layoutState) {
if (bus.sourceBusColumn == bus.targetBusColumn - 1) {
return true;
}
if (bus.getSourceOutputPorts().size() > 2 || bus.getTargetInputPorts().size() > 2) {
for (int i = layoutState.nodeOrder.indexOf(bus.source) + 1; i < layoutState.nodeOrder.indexOf(bus.target); i++) {
if (!layoutState.nodeOrder.get(i).isTemporary) {
return false;
}
}
}
return true;
}
}
package de.monticore.lang.montiarc.svggenerator.calculators;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.montiarc.svggenerator.LogConfig;
import de.monticore.lang.montiarc.svggenerator.TestingUtilities;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.ComponentLayoutSymbol;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.ConnectorLayoutSymbol;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.PortLayoutSymbol;
import de.monticore.lang.montiarc.tagging._symboltable.TagSymbol;
import de.monticore.symboltable.Scope;
import org.junit.*;
/**
* Created by RobertReineke on 08.11.2016.
*/
public class CalculatorTaggingTest {
CanvasCalculator canCal;
ComponentCalculator compCal;
ConnectorCalculator conCal;
PortCalculator portCal;
ExpandedComponentInstanceSymbol inst;
@BeforeClass
public static void init(){
LogConfig.init();
}
//Initialize variables and objects
@Before
public void setUp(){
compCal = new ComponentCalculator();
conCal = new ConnectorCalculator();
portCal = new PortCalculator();
canCal = new CanvasCalculator();
Scope symTab = TestingUtilities.createSymTab("src/test/resources/tags");
inst = symTab.<ExpandedComponentInstanceSymbol>resolve("industry4.turbineController",
ExpandedComponentInstanceSymbol.KIND).orElse(null);
}
//Test if the calculators add the layout to the sub-instances
@Ignore
@Test
public void test() throws NoHorizontalLayoutInformationException {
Assert.assertNotNull(inst);
for(ExpandedComponentInstanceSymbol subComp : inst.getSubComponents()){
boolean componentLayoutFound = false;
boolean connectorLayoutFound = false;
boolean portLayoutFound = false;
for(TagSymbol t : subComp.getTags()){
if(t instanceof ComponentLayoutSymbol){
componentLayoutFound = true;
}
if(t instanceof ConnectorLayoutSymbol){
connectorLayoutFound = true;
}
if(t instanceof PortLayoutSymbol){
portLayoutFound = true;
}
}
// Test that no layout is found before calculating the layout
Assert.assertFalse(componentLayoutFound);
Assert.assertFalse(connectorLayoutFound);
Assert.assertFalse(portLayoutFound);
}
//Calculate the layout for the instance
compCal.calculateLayout(inst);
canCal.calculateLayout(inst);
portCal.calculateLayout(inst);
conCal.calculateLayout(inst);
for(ExpandedComponentInstanceSymbol subComp : inst.getSubComponents()){
boolean componentLayoutFound = false;
//Test if after calculating a layout is found
for(TagSymbol t : subComp.getTags()){
if(t instanceof ComponentLayoutSymbol){
componentLayoutFound = true;
}
}
Assert.assertTrue(componentLayoutFound);
for(ConnectorSymbol c : subComp.getConnectors()){
Assert.assertTrue(hasConnectorLayout(c));
}
for(PortSymbol p : subComp.getPorts())
{
Assert.assertTrue(hasPortLayout(p));
}
}
}
private boolean hasConnectorLayout(ConnectorSymbol c) {
for(TagSymbol t : c.getTags()){
if(t instanceof ConnectorLayoutSymbol){
return true;
}
}
return false;
}
private boolean hasPortLayout(PortSymbol p) {
for(TagSymbol t : p.getTags()){
if(t instanceof PortLayoutSymbol){
return true;
}
}
return false;
}
}
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