Commit 040cb90f authored by Manuel Schrick's avatar Manuel Schrick
Browse files

Code Cleanup: removed old and unused calculator files

parent 4f69d770
package de.monticore.lang.montiarc.svggenerator.calculators;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
/**
* Created by Robert on 01.11.2016.
*/
public interface Calculator{
void calculateLayout(ExpandedComponentInstanceSymbol inst) throws NoHorizontalLayoutInformationException;
}
package de.monticore.lang.montiarc.svggenerator.calculators;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.ComponentLayoutSymbol;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.IdGenerator;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.LayoutPosition;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.SingletonComponentLayoutSymbol;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
// de.monticore.lang.montiarc.svggenerator.calculators
// montiarc-core-features
//
// Created by Sven Titgemeyer on 11/7/16.
// Copyright (c) 2016 Sven Titgemeyer. All rights reserved.
//
public class CanvasCalculator implements Calculator, DrawingConstants {
@Override
public void calculateLayout(ExpandedComponentInstanceSymbol inst) {
calculateStandardLayout(inst);
calculateSingletonLayout(inst);
}
private void calculateStandardLayout(ExpandedComponentInstanceSymbol inst) {
List<List<ExpandedComponentInstanceSymbol>> columns = DrawingHelper.getColumns(inst);
List<List<ExpandedComponentInstanceSymbol>> rows = DrawingHelper.getRows(inst);
List<Integer> connectorsPerColumn = DrawingHelper.getConnectorCountsPerColumn(inst);
List<Integer> connectorsPerRow = DrawingHelper.getConnectorCountsPerRow(inst);
List<Integer> rowHeights = new ArrayList<>();
List<Integer> columnWidths = new ArrayList<>();
for (int column = 0; column < columns.size(); column++) {
columnWidths.add(DrawingHelper.getColumnWidth(columns.get(column)));
}
for (int row = 0; row < rows.size(); row++) {
rowHeights.add(DrawingHelper.getRowHeight(rows.get(row)));
}
int totalWidth = 0;
int totalHeight = 0;
for (int i = 0; i <= columnWidths.size(); i++) {
if (i < connectorsPerColumn.size()) {
totalWidth += DrawingHelper.getHorizontalConnectorSpacing(connectorsPerColumn.get(i));
}
if (i < columnWidths.size()) {
totalWidth += columnWidths.get(i);
}
}
for (int i = 0; i <= rowHeights.size(); i++) {
if (i < connectorsPerRow.size()) {
totalHeight += DrawingHelper.getVerticalConnectorSpacing(connectorsPerRow.get(i));
}
if (i < rowHeights.size()) {
totalHeight += rowHeights.get(i);
}
}
totalHeight += COMPONENT_SPACING;
// Create a new layout symbol for the canvas
ComponentLayoutSymbol sym = new ComponentLayoutSymbol(IdGenerator.getUniqueId(), CANVAS_OUTER_MARGIN, CANVAS_OUTER_MARGIN, totalWidth, totalHeight, LayoutPosition.UNDEFINED, 10);
// Add layout symbol to the outer component
inst.addTag(sym);
}
private void calculateSingletonLayout(ExpandedComponentInstanceSymbol inst) {
List<List<ExpandedComponentInstanceSymbol>> columns = SingletonDrawingHelper.getColumns(inst);
List<List<ExpandedComponentInstanceSymbol>> rows = SingletonDrawingHelper.getRows(inst);
List<Integer> connectorsPerColumn = SingletonDrawingHelper.getConnectorCountsPerColumn(inst);
List<Integer> connectorsPerRow = SingletonDrawingHelper.getConnectorCountsPerRow(inst);
List<Integer> rowHeights = new ArrayList<>();
List<Integer> columnWidths = new ArrayList<>();
for (int column = 0; column < columns.size(); column++) {
columnWidths.add(SingletonDrawingHelper.getColumnWidth(columns.get(column)));
}
for (int row = 0; row < rows.size(); row++) {
rowHeights.add(SingletonDrawingHelper.getRowHeight(rows.get(row)));
}
int totalWidth = 0;
int totalHeight = 0;
for (int i = 0; i <= columnWidths.size(); i++) {
if (i < connectorsPerColumn.size()) {
totalWidth += SingletonDrawingHelper.getHorizontalConnectorSpacing(connectorsPerColumn.get(i));
}
if (i < columnWidths.size()) {
totalWidth += columnWidths.get(i);
}
}
for (int i = 0; i <= rowHeights.size(); i++) {
if (i < connectorsPerRow.size()) {
totalHeight += SingletonDrawingHelper.getVerticalConnectorSpacing(connectorsPerRow.get(i));
}
if (i < rowHeights.size()) {
totalHeight += rowHeights.get(i);
}
}
totalHeight += COMPONENT_SPACING;
// Create a new layout symbol for the canvas
SingletonComponentLayoutSymbol sym = new SingletonComponentLayoutSymbol(IdGenerator.getUniqueId(), CANVAS_OUTER_MARGIN, CANVAS_OUTER_MARGIN, totalWidth, totalHeight, LayoutPosition.UNDEFINED, 10);
// Add layout symbol to the outer component
inst.addTag(sym);
}
}
package de.monticore.lang.montiarc.svggenerator.calculators;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.montiarc.svggenerator.calculators.symbols.*;
import java.util.*;
/**
* Created by Robert on 01.11.2016.
*/
public class ComponentCalculator implements Calculator, DrawingConstants {
@Override
public void calculateLayout(ExpandedComponentInstanceSymbol inst) throws NoHorizontalLayoutInformationException {
calculateStandardLayout(inst);
calculateSingletonLayout(inst);
}
private void calculateStandardLayout(ExpandedComponentInstanceSymbol inst) throws NoHorizontalLayoutInformationException {
Collection<ExpandedComponentInstanceSymbol> subComponents = inst.getSubComponents();
List<List<ExpandedComponentInstanceSymbol>> columns = DrawingHelper.getColumns(inst);
List<List<ExpandedComponentInstanceSymbol>> rows = DrawingHelper.getRows(inst);
List<Integer> connectorsPerColumn = DrawingHelper.getConnectorCountsPerColumn(inst);
List<Integer> connectorsPerRow = DrawingHelper.getConnectorCountsPerRow(inst);
List<Integer> rowHeights = new ArrayList<>();
List<Integer> columnWidths = new ArrayList<>();
for (int i = 0; i < columns.size(); i++) {
columnWidths.add(DrawingHelper.getColumnWidth(columns.get(i)));
}
for (int i = 0; i < rows.size(); i++) {
rowHeights.add(DrawingHelper.getRowHeight(rows.get(i)));
}
for (ExpandedComponentInstanceSymbol comp : subComponents) {
if (comp.<LayoutSymbol>getTags(LayoutSymbol.KIND).isEmpty()) {
continue;
}
LayoutSymbol layoutSymbol = comp.<LayoutSymbol>getTags(LayoutSymbol.KIND).iterator().next();
int x = layoutSymbol.getColumn();
int y = layoutSymbol.getRow();
int compHeight = DrawingHelper.getComponentHeight(comp);
int compWidth = DrawingHelper.getComponentWidth(comp);
int rowHeight = rowHeights.get(y);
int top = CANVAS_OUTER_MARGIN + (int) (COMPONENT_SPACING / 2.0);
int left = CANVAS_OUTER_MARGIN;
for (int i = 0; i <= x; i++) {
if (i < connectorsPerColumn.size()) {
left += DrawingHelper.getHorizontalConnectorSpacing(connectorsPerColumn.get(i));
}
if (i < x) {
left += columnWidths.get(i);
}
}
for (int i = 0; i <= y; i++) {
if (i < connectorsPerRow.size()) {
top += DrawingHelper.getVerticalConnectorSpacing(connectorsPerRow.get(i));
}
if (i < y) {
top += rowHeights.get(i);
}
}
top += (int) ((rowHeight - compHeight) / 2.0);
addComponentLayoutSymbol(comp, left, top, compWidth, compHeight);
}
}
private void addComponentLayoutSymbol(ExpandedComponentInstanceSymbol comp, int left, int top, int width, int height) {
// Create a new layout symbol for each subcomponent
ComponentLayoutSymbol sym = new ComponentLayoutSymbol(
IdGenerator.getUniqueId(), left, top, width, height,
LayoutPosition.LEFT, 10);
// Add layout symbol to the subcomponent
comp.addTag(sym);
}
private void calculateSingletonLayout(ExpandedComponentInstanceSymbol inst) throws NoHorizontalLayoutInformationException {
Collection<ExpandedComponentInstanceSymbol> subComponents = inst.getSubComponents();
List<List<ExpandedComponentInstanceSymbol>> columns = SingletonDrawingHelper.getColumns(inst);
List<List<ExpandedComponentInstanceSymbol>> rows = SingletonDrawingHelper.getRows(inst);
List<Integer> connectorsPerColumn = SingletonDrawingHelper.getConnectorCountsPerColumn(inst);
List<Integer> connectorsPerRow = SingletonDrawingHelper.getConnectorCountsPerRow(inst);
List<Integer> rowHeights = new ArrayList<>();
List<Integer> columnWidths = new ArrayList<>();
for (List<ExpandedComponentInstanceSymbol> column : columns) {
columnWidths.add(SingletonDrawingHelper.getColumnWidth(column));
}
for (List<ExpandedComponentInstanceSymbol> row : rows) {
rowHeights.add(SingletonDrawingHelper.getRowHeight(row));
}
for (ExpandedComponentInstanceSymbol comp : subComponents) {
if (comp.<LayoutSymbol>getTags(LayoutSymbol.KIND).isEmpty()) {
continue;
}
LayoutSymbol layoutSymbol = comp.<LayoutSymbol>getTags(LayoutSymbol.KIND).iterator().next();
int x = layoutSymbol.getColumn();
int y = layoutSymbol.getRow();
int compHeight = SingletonDrawingHelper.getComponentHeight(comp);
int compWidth = SingletonDrawingHelper.getComponentWidth(comp);
int rowHeight = rowHeights.get(y);
int top = CANVAS_OUTER_MARGIN + (int) (COMPONENT_SPACING / 2.0);
int left = CANVAS_OUTER_MARGIN;
for (int i = 0; i <= x; i++) {
if (i < connectorsPerColumn.size()) {
left += SingletonDrawingHelper.getHorizontalConnectorSpacing(connectorsPerColumn.get(i));
}
if (i < x) {
left += columnWidths.get(i);
}
}
for (int i = 0; i <= y; i++) {
if (i < connectorsPerRow.size()) {
top += SingletonDrawingHelper.getVerticalConnectorSpacing(connectorsPerRow.get(i));
}
if (i < y) {
top += rowHeights.get(i);
}
}
top += (int) ((rowHeight - compHeight) / 2.0);
addSingletonComponentLayoutSymbol(comp, left, top, compWidth, compHeight);
}
}
private void addSingletonComponentLayoutSymbol(ExpandedComponentInstanceSymbol comp, int left, int top, int width, int height) {
// Create a new layout symbol for each subcomponent
SingletonComponentLayoutSymbol sym = new SingletonComponentLayoutSymbol(
IdGenerator.getUniqueId(), left, top, width, height,
LayoutPosition.LEFT, 10);
// Add layout symbol to the subcomponent
comp.addTag(sym);
}
}
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.calculators.symbols.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;
/**
* Created by Robert on 01.11.2016.
*/
public class PortCalculator implements Calculator, DrawingConstants {
@Override
public void calculateLayout(ExpandedComponentInstanceSymbol inst) {
HashMap<PortSymbol, ConnectorSymbol> incomingPortConnectors = new HashMap<>();
// Sort incoming canvas ports
for (PortSymbol port : inst.getIncomingPorts()) {
inst.getConnectors().stream()
.filter(c -> c.getSourcePort().equals(port))
.sorted(this::compareTargetPorts)
.findFirst()
.ifPresent(c -> incomingPortConnectors.put(port, c));
}
List<PortSymbol> sortedPorts = inst.getIncomingPorts().stream()
.sorted((lhs, rhs) -> compareTargetPorts(incomingPortConnectors.get(lhs), incomingPortConnectors.get(rhs)))
.collect(Collectors.toList());
ComponentLayoutSymbol compLayoutSymbol = inst.<ComponentLayoutSymbol>getTags(ComponentLayoutSymbol.KIND).iterator().next();
for (int i = 0; i < sortedPorts.size(); i++) {
PortSymbol port = sortedPorts.get(i);
int x = compLayoutSymbol.getX() - (int) (PORT_WIDTH / 2f);
int y = (i * (PORT_HEIGHT + PORT_SPACING)) + compLayoutSymbol.getY() + (int) ((compLayoutSymbol.getHeight() - sortedPorts.size() * (PORT_HEIGHT + PORT_SPACING)) / 2f);
PortLayoutSymbol portSym = new PortLayoutSymbol(IdGenerator.getUniqueId(), x, y, true);
port.addTag(portSym);
}
// Sort ports of subcomponents
inst.getSubComponents()
.stream()
.sorted(this::compareComponentPositions)
.forEach(this::calculateSubComponent);
// Sort outgoing canvas ports
HashMap<PortSymbol, ConnectorSymbol> outgoingPortConnectors = new HashMap<>();
for (PortSymbol port : inst.getOutgoingPorts()) {
inst.getConnectors().stream()
.filter(c -> c.getTargetPort().equals(port))
.sorted(this::compareSourcePorts)
.findFirst()
.ifPresent(c -> outgoingPortConnectors.put(port, c));
}
List<PortSymbol> sortedOutgoingPorts = inst.getOutgoingPorts().stream()
.sorted((lhs, rhs) -> compareSourcePorts(outgoingPortConnectors.get(lhs), outgoingPortConnectors.get(rhs)))
.collect(Collectors.toList());
for (int i = 0; i < sortedOutgoingPorts.size(); i++) {
PortSymbol port = sortedOutgoingPorts.get(i);
int x = compLayoutSymbol.getX() + compLayoutSymbol.getWidth() - (int) (PORT_WIDTH / 2f);
int y = (i * (PORT_HEIGHT + PORT_SPACING)) + compLayoutSymbol.getY() + (int) ((compLayoutSymbol.getHeight() - sortedOutgoingPorts.size() * (PORT_HEIGHT + PORT_SPACING)) / 2f);
port.addTag(new PortLayoutSymbol(IdGenerator.getUniqueId(), x, y, false));
}
HashMap<ExpandedComponentInstanceSymbol, List<PortSymbol>> singletonPorts = new HashMap<>();
inst.getConnectors().stream()
.filter(con -> con.getSourcePort().getComponentInstance().isPresent())
.filter(con -> con.getTargetPort().getComponentInstance().isPresent())
.sorted(this::compareConnectors)
.forEach(con -> {
PortSymbol sourcePort = con.getSourcePort();
PortSymbol targetPort = con.getTargetPort();
PortOrderSymbol sourceOrderSymbol = sourcePort.<PortOrderSymbol>getTags(PortOrderSymbol.KIND).iterator().next();
PortOrderSymbol targetOrderSymbol = targetPort.<PortOrderSymbol>getTags(PortOrderSymbol.KIND).iterator().next();
if (sourceOrderSymbol.isVisibleInSingletonMode()) {
ExpandedComponentInstanceSymbol sourceComp = sourcePort.getComponentInstance().get();
List<PortSymbol> sourceSingletonPorts = singletonPorts.getOrDefault(sourceComp, new ArrayList<>());
sourceSingletonPorts.add(sourcePort);
singletonPorts.put(sourceComp, sourceSingletonPorts);
}
if (targetOrderSymbol.isVisibleInSingletonMode()) {
ExpandedComponentInstanceSymbol targetComp = targetPort.getComponentInstance().get();
List<PortSymbol> targetSingletonPorts = singletonPorts.getOrDefault(targetComp, new ArrayList<>());
targetSingletonPorts.add(targetPort);
singletonPorts.put(targetComp, targetSingletonPorts);
}
});
for (ExpandedComponentInstanceSymbol comp : singletonPorts.keySet()) {
List<PortSymbol> compSingletonPorts = singletonPorts.get(comp);
List<PortSymbol> compSingletonIncomingPorts = compSingletonPorts.stream().filter(PortSymbol::isIncoming).collect(Collectors.toList());
List<PortSymbol> compSingletonOutgoingPorts = compSingletonPorts.stream().filter(PortSymbol::isOutgoing).collect(Collectors.toList());
ComponentLayoutSymbol layoutSymbol;
if (!comp.getTags(SingletonComponentLayoutSymbol.KIND).isEmpty()) {
layoutSymbol = comp.<SingletonComponentLayoutSymbol>getTags(SingletonComponentLayoutSymbol.KIND).iterator().next();
} else {
layoutSymbol = comp.<ComponentLayoutSymbol>getTags(ComponentLayoutSymbol.KIND).iterator().next();
}
for (int i = 0; i < compSingletonIncomingPorts.size(); i++) {
PortSymbol portSymbol = compSingletonIncomingPorts.get(i);
int x = layoutSymbol.getX() - (int) (PORT_WIDTH / 2f);
if (portSymbol.isOutgoing()) {
x += layoutSymbol.getWidth();
}
int y = (i * (PORT_HEIGHT + PORT_SPACING)) + layoutSymbol.getY() + (int) ((layoutSymbol.getHeight() - compSingletonIncomingPorts.size() * (PORT_HEIGHT + PORT_SPACING)) / 2f);
portSymbol.addTag(new SingletonPortLayoutSymbol(IdGenerator.getUniqueId(), x, y));
}
for (int i = 0; i < compSingletonOutgoingPorts.size(); i++) {
PortSymbol portSymbol = compSingletonOutgoingPorts.get(i);
int x = layoutSymbol.getX() - (int) (PORT_WIDTH / 2f);
if (portSymbol.isOutgoing()) {
x += layoutSymbol.getWidth();
}
int y = (i * (PORT_HEIGHT + PORT_SPACING)) + layoutSymbol.getY() + (int) ((layoutSymbol.getHeight() - compSingletonOutgoingPorts.size() * (PORT_HEIGHT + PORT_SPACING)) / 2f);
portSymbol.addTag(new SingletonPortLayoutSymbol(IdGenerator.getUniqueId(), x, y));
}
}
}
private void calculateSubComponent(ExpandedComponentInstanceSymbol comp) {
ComponentLayoutSymbol compLayoutSymbol = comp.<ComponentLayoutSymbol>getTags(ComponentLayoutSymbol.KIND).iterator().next();
HashMap<PortSymbol, ConnectorSymbol> incomingPortConnectors = new HashMap<>();
if (!comp.getEnclosingComponent().isPresent()) {
return;
}
ExpandedComponentInstanceSymbol parentComp = comp.getEnclosingComponent().get();
// Sort incoming ports
if (!comp.getIncomingPorts().isEmpty()) {
for (PortSymbol port : comp.getIncomingPorts()) {
parentComp.getConnectors().stream()
.filter(c -> c.getTargetPort().equals(port))
.sorted(this::compareSourcePorts)
.findFirst()
.ifPresent(c -> incomingPortConnectors.put(port, c));
}
List<PortSymbol> sortedPorts = comp.getIncomingPorts().stream()
.sorted((lhs, rhs) -> compareSourcePorts(incomingPortConnectors.get(lhs), incomingPortConnectors.get(rhs)))
.collect(Collectors.toList());
for (int i = 0; i < sortedPorts.size(); i++) {
PortSymbol port = sortedPorts.get(i);
int x = compLayoutSymbol.getX() - (int) (PORT_WIDTH / 2f);
int y = (i * (PORT_HEIGHT + PORT_SPACING)) + compLayoutSymbol.getY() + (int) ((compLayoutSymbol.getHeight() - sortedPorts.size() * (PORT_HEIGHT + PORT_SPACING)) / 2f);
PortLayoutSymbol portSym = new PortLayoutSymbol(IdGenerator.getUniqueId(), x, y, true);
port.addTag(portSym);
}
}
// Sort outgoing ports
if (!comp.getOutgoingPorts().isEmpty()) {
HashMap<PortSymbol, ConnectorSymbol> outgoingPortConnectors = new HashMap<>();
for (PortSymbol port : comp.getOutgoingPorts()) {
parentComp.getConnectors().stream()
.filter(c -> c.getSourcePort().equals(port))
.sorted(this::compareTargetPorts)
.findFirst()
.ifPresent(c -> outgoingPortConnectors.put(port, c));
}
List<PortSymbol> sortedOutgoingPorts = comp.getOutgoingPorts().stream()
.sorted((lhs, rhs) -> compareTargetPorts(outgoingPortConnectors.get(lhs), outgoingPortConnectors.get(rhs)))
.collect(Collectors.toList());
for (int i = 0; i < sortedOutgoingPorts.size(); i++) {
PortSymbol port = sortedOutgoingPorts.get(i);
int x = compLayoutSymbol.getX() + compLayoutSymbol.getWidth() - (int) (PORT_WIDTH / 2f);
int y = (i * (PORT_HEIGHT + PORT_SPACING)) + compLayoutSymbol.getY() + (int) ((compLayoutSymbol.getHeight() - sortedOutgoingPorts.size() * (PORT_HEIGHT + PORT_SPACING)) / 2f);
PortLayoutSymbol portSym = new PortLayoutSymbol(IdGenerator.getUniqueId(), x, y, false);
port.addTag(portSym);
}
}
}
private int compareConnectors(ConnectorSymbol lhs, ConnectorSymbol rhs) {
if (lhs == null) {
return -1;
} else if (rhs == null) {
return 1;
}
int result = compareSourcePorts(lhs, rhs);
if (result == 0) {
result = compareTargetPorts(lhs, rhs);
}
return result;
}
private int compareSourcePorts(ConnectorSymbol lhs, ConnectorSymbol rhs) {
if (lhs == null) {
return -1;
} else if (rhs == null) {
return 1;
}
PortSymbol lhsSourcePort = lhs.getSourcePort();
PortSymbol rhsSourcePort = rhs.getSourcePort();
ExpandedComponentInstanceSymbol lhsSourceComp = lhsSourcePort.getComponentInstance().get();
ExpandedComponentInstanceSymbol rhsSourceComp = rhsSourcePort.getComponentInstance().get();
if (!lhsSourceComp.equals(rhsSourceComp)) {
return compareComponentPositions(lhsSourceComp, rhsSourceComp);
}
if (lhsSourcePort.getTags(PortOrderSymbol.KIND).isEmpty()) {
return -1;
} else if(rhsSourcePort.getTags(PortOrderSymbol.KIND).isEmpty()) {
return 1;
}
PortOrderSymbol lhsOrder = lhsSourcePort.<PortOrderSymbol>getTags(PortOrderSymbol.KIND).iterator().next();
PortOrderSymbol rhsOrder = rhsSourcePort.<PortOrderSymbol>getTags(PortOrderSymbol.KIND).iterator().next();
return Integer.compare(lhsOrder.getIndexInOrder(), rhsOrder.getIndexInOrder());
}