Commit 8b30b74c authored by Philipp Görick's avatar Philipp Görick

Added algorithm to flatten architecture.

parent d976c589
Pipeline #94867 failed with stages
in 5 minutes and 29 seconds
package de.monticore.lang.monticar.generator.middleware.clustering;
import de.monticore.expressionsbasis._ast.ASTExpression;
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.embeddedmontiarc.embeddedmontiarc._symboltable.*;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosConnectionSymbol;
import de.monticore.lang.math._ast.ASTNumberExpression;
import de.monticore.lang.monticar.common2._ast.ASTCommonMatrixType;
import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference;
import de.monticore.lang.monticar.ts.references.MCTypeReference;
import de.monticore.symboltable.CommonScope;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Symbol;
import de.monticore.symboltable.resolving.ResolvingFilter;
import de.se_rwth.commons.logging.Log;
import java.util.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class AutomaticClusteringHelper {
public static double[][] createAdjacencyMatrix(List<ExpandedComponentInstanceSymbol> subcomps, Collection<ConnectorSymbol> connectors, Map<String, Integer> subcompLabels) {
......@@ -127,4 +131,159 @@ public class AutomaticClusteringHelper {
return 50;
}
public static ExpandedComponentInstanceSymbol flattenArchitecture(ExpandedComponentInstanceSymbol symbol){
if (symbol.getSubComponents().isEmpty()){
if (symbol.getEnclosingComponent().isPresent()){
return copySymbolWithSystemName(symbol);
}
return symbol;
}
for (ExpandedComponentInstanceSymbol sym : symbol.getSubComponents()){
symbol = flattenArchitecture(sym);
}
if (symbol.getEnclosingComponent().isPresent()){
ExpandedComponentInstanceSymbol enclosingComponent = copySymbolWithSystemName(symbol);
symbol = enclosingComponent.getSubComponent(symbol.getFullName().replace(".", "_")).get();
ExpandedComponentInstanceSymbol thisSymbol = symbol;
List<ExpandedComponentInstanceSymbol> newSubcomponents = enclosingComponent.getSubComponents().stream()
.filter(e -> !e.getFullName().equals(thisSymbol.getFullName())).collect(Collectors.toList());
newSubcomponents.addAll(newSubcomponents.size(), new ArrayList<>(symbol.getSubComponents()));
HashSet<String> incomingPorts = new HashSet<>(symbol.getIncomingPorts().stream().map(p ->{
return p.getFullName();
}).collect(Collectors.toList()));
HashSet<String> outgoingPorts = new HashSet<>(symbol.getOutgoingPorts().stream().map(p ->{
return p.getFullName();
}).collect(Collectors.toList()));
//only connectors from incoming ports
Set<ConnectorSymbol> incomingConnectors = symbol.getConnectors().stream()
.filter(c -> incomingPorts.contains(thisSymbol.getFullName() + "." + thisSymbol.getName() + "_"+ c.getSource()))
.collect(Collectors.toSet());
//only connectors going into symbol
Set<ConnectorSymbol> incomingParentConnectors = enclosingComponent.getConnectors().stream()
.filter(c -> c.getTargetPort().getComponentInstance().get().getFullName().equals(thisSymbol.getFullName()))
.collect(Collectors.toSet());
//only connectors from outgoing ports
Set<ConnectorSymbol> outgoingConnectors = symbol.getConnectors().stream()
.filter(c -> outgoingPorts.contains(thisSymbol.getFullName() + "." + thisSymbol.getName() + "_"+ c.getTarget()))
.collect(Collectors.toSet());
//only connectors going out of symbol
Set<ConnectorSymbol> outgoingParentConnectors = enclosingComponent.getConnectors().stream()
.filter(c -> c.getSourcePort().getComponentInstance().get().getFullName().equals(thisSymbol.getFullName()))
.collect(Collectors.toSet());
//untouched connectors of enclosing symbol
Set<ConnectorSymbol> newConnectors = enclosingComponent.getConnectors().stream()
.filter(c -> !(incomingParentConnectors.contains(c) || outgoingParentConnectors.contains(c)))
.collect(Collectors.toSet());
//untouched connectors of symbol with renamed ports
newConnectors.addAll(symbol.getConnectors().stream()
//.map(c -> {return mapToNewName(c);})
.filter(c -> !(incomingConnectors.contains(c) || outgoingConnectors.contains(c)))
.collect(Collectors.toSet()));
for (ConnectorSymbol con : incomingConnectors){
for (ConnectorSymbol connectorSymbol : incomingParentConnectors){
if (con.getSource().equals(connectorSymbol.getTarget().replaceFirst(".*_", ""))){
ConnectorSymbol tmpConnector = ConnectorSymbol.builder()
.setSource(connectorSymbol.getSource())
.setTarget(con.getTarget())
.build();
newConnectors.add(tmpConnector);
}
}
}
for (ConnectorSymbol con : outgoingConnectors){
for (ConnectorSymbol connectorSymbol : outgoingParentConnectors){
if (con.getTarget().equals(connectorSymbol.getSource().replaceFirst(".*_", ""))){
ConnectorSymbol tmpConnector = ConnectorSymbol.builder()
.setSource(con.getSource())
.setTarget(connectorSymbol.getTarget())
.build();
newConnectors.add(tmpConnector);
}
}
}
ExpandedComponentInstanceSymbol res = constructECIS(enclosingComponent, newSubcomponents, newConnectors,
enclosingComponent.getName(), new ArrayList<>(enclosingComponent.getPortsList()));
return res;
} else {
return symbol;
}
}
private static ExpandedComponentInstanceSymbol copySymbolWithSystemName(ExpandedComponentInstanceSymbol symbol) {
ExpandedComponentInstanceSymbol enclosingComponent = symbol.getEnclosingComponent().get();
ExpandedComponentInstanceSymbol thisSymbol = symbol;
List<ExpandedComponentInstanceSymbol> subcomponents = enclosingComponent.getSubComponents().stream()
.filter(e -> !e.getFullName().equals(thisSymbol.getFullName())).collect(Collectors.toList());
List<PortSymbol> ports = new ArrayList<>();
String newName = symbol.getFullName().replace(".", "_");
String newEnclosingName = enclosingComponent.getFullName().replace(".", "_");
for (PortSymbol port : symbol.getPortsList()) {
ports.add((PortSymbol)(port.isConstant() ?
(new EMAPortBuilder()).setName(newName + "_" + port.getName()).setDirection(port.isIncoming())
.setTypeReference(port.getTypeReference()).setConstantValue(((ConstantPortSymbol)port).getConstantValue())
.setASTNode(port.getAstNode()).buildConstantPort()
: (new EMAPortBuilder()).setName(newName + "_" + port.getName()).setDirection(port.isIncoming())
.setTypeReference(port.getTypeReference()).setASTNode(port.getAstNode()).setConfig(port.isConfig())
.setMiddlewareSymbol(port.getMiddlewareSymbol()).build()));
}
ExpandedComponentInstanceSymbol e = constructECIS(symbol, new ArrayList<>(symbol.getSubComponents()),
new HashSet<>(symbol.getConnectors()), newName, ports);
subcomponents.add(e);
HashSet<String> incomingPorts = new HashSet<>(symbol.getIncomingPorts().stream().map(p ->{
return p.getFullName();
}).collect(Collectors.toList()));
HashSet<String> outgoingPorts = new HashSet<>(symbol.getOutgoingPorts().stream().map(p ->{
return p.getFullName();
}).collect(Collectors.toList()));
Set<ConnectorSymbol> newConnectors = enclosingComponent.getConnectors().stream()
.map(c ->{
if (incomingPorts.contains(c.getComponentInstance().get().getFullName() + "." + c.getTarget().substring(0,1).toLowerCase()
+ c.getTarget().substring(1))){
c.setSource(c.getSource());
c.setTarget(c.getTarget().replaceFirst("[^.]*.",e.getName() + "." + newName + "_"));
} else if (outgoingPorts.contains(c.getComponentInstance().get().getFullName() + "." + c.getSource().substring(0,1).toLowerCase()
+ c.getSource().substring(1))){
c.setSource(c.getSource().replaceFirst("[^.]*.",e.getName() + "." + newName + "_"));
c.setTarget(c.getTarget());
}
return c;
})
.collect(Collectors.toSet());
return constructECIS(enclosingComponent, subcomponents, newConnectors, enclosingComponent.getName(),
new ArrayList<>(enclosingComponent.getPortsList()));
}
private static ExpandedComponentInstanceSymbol constructECIS(ExpandedComponentInstanceSymbol enclosingComponent,
List<ExpandedComponentInstanceSymbol> newSubcomponents,
Set<ConnectorSymbol> newConnectors, String name, List<PortSymbol> ports) {
Set<ResolvingFilter<? extends Symbol>> resolvingFilters = enclosingComponent.getSpannedScope().getResolvingFilters();
newSubcomponents.forEach(sc -> ((CommonScope) sc.getSpannedScope()).setResolvingFilters(resolvingFilters));
ExpandedComponentInstanceSymbol res = new ExpandedComponentInstanceBuilder()
.setName(name)
.setSymbolReference(enclosingComponent.getComponentType())
.addPorts(ports)
.addConnectors(newConnectors)
.addSubComponents(newSubcomponents)
.addResolutionDeclarationSymbols(enclosingComponent.getResolutionDeclarationSymbols())
.build();
((CommonScope) res.getSpannedScope()).setResolvingFilters(resolvingFilters);
res.setEnclosingScope((MutableScope) enclosingComponent.getEnclosingScope());
return res;
}
}
package de.monticore.lang.monticar.generator.middleware;
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.monticar.generator.middleware.clustering.AutomaticClusteringHelper;
......@@ -98,6 +99,36 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
}
@Test
public void testFlattenAlgorithm1(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("lab.overallSystem", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
ExpandedComponentInstanceSymbol newComponentInstanceSymbol = AutomaticClusteringHelper.flattenArchitecture(componentInstanceSymbol);
assertNotNull(newComponentInstanceSymbol);
Collection<ExpandedComponentInstanceSymbol> subComponents = newComponentInstanceSymbol.getSubComponents();
Collection<ConnectorSymbol> connectors = newComponentInstanceSymbol.getConnectors();
assertEquals(10, subComponents.size());
assertEquals(20, connectors.size());
}
@Test
public void testFlattenAlgorithm2(){
TaggingResolver taggingResolver = AbstractSymtabTest.createSymTabAndTaggingResolver(TEST_PATH);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("lab.spanningSystem", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
ExpandedComponentInstanceSymbol newComponentInstanceSymbol = AutomaticClusteringHelper.flattenArchitecture(componentInstanceSymbol);
assertNotNull(newComponentInstanceSymbol);
Collection<ExpandedComponentInstanceSymbol> subComponents = newComponentInstanceSymbol.getSubComponents();
Collection<ConnectorSymbol> connectors = newComponentInstanceSymbol.getConnectors();
assertEquals(20, subComponents.size());
assertEquals(40, connectors.size());
}
// todo: gotta move this thing later, just temporarily here for testing purposes
public static Dataset[] getClustering(Dataset data, SparseMatrix matrix) {
......
package lab;
component SpanningSystem{
ports in Q input,
out Q output;
instance OverallSystem overallSystem1;
instance OverallSystem overallSystem2;
connect input -> OverallSystem1.input[1];
connect input -> OverallSystem1.input[2];
connect input -> OverallSystem1.input[3];
connect input -> OverallSystem1.input[4];
connect OverallSystem1.output[4] -> output;
connect OverallSystem1.output[1] -> output;
connect OverallSystem1.output[2] -> output;
connect OverallSystem1.output[3] -> output;
connect input -> OverallSystem2.input[1];
connect input -> OverallSystem2.input[2];
connect input -> OverallSystem2.input[3];
connect input -> OverallSystem2.input[4];
connect OverallSystem2.output[4] -> output;
connect OverallSystem2.output[1] -> output;
connect OverallSystem2.output[2] -> output;
connect OverallSystem2.output[3] -> output;
}
\ No newline at end of file
Markdown is supported
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