Implemented: Clustering of MW comps and non MW comps

parent 4ec28211
......@@ -6,6 +6,7 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymb
import de.monticore.lang.embeddedmontiarc.tagging.MiddlewareSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosConnectionSymbol;
import de.monticore.lang.monticar.generator.FileContent;
import de.monticore.lang.monticar.generator.middleware.helpers.ClusterHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.FileHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.TemplateHelper;
import de.monticore.lang.monticar.generator.middleware.impls.GeneratorImpl;
......@@ -45,16 +46,11 @@ public class DistributedTargetGenerator extends CMakeGenerator {
fixComponentInstance(componentInstanceSymbol);
Collection<ExpandedComponentInstanceSymbol> subComps = getSubComponentInstances(componentInstanceSymbol);
//TODO: cluster non mw subcomps together with mw subcomps
boolean allSubsMwOnly = subComps.stream()
.flatMap(comp -> comp.getPorts().stream())
.allMatch(p -> p.getMiddlewareSymbol().isPresent());
if (subComps.size() > 0 && allSubsMwOnly) {
subComps.forEach(comp ->
generatorMap.put(comp, createFullGenerator(comp.getFullName().replace(".", "_")))
);
List<ExpandedComponentInstanceSymbol> clusterSubcomponents = ClusterHelper.getClusterSubcomponents(componentInstanceSymbol);
if (clusterSubcomponents.size() > 0) {
clusterSubcomponents.forEach(clusterECIS -> {
generatorMap.put(clusterECIS, createFullGenerator(clusterECIS.getFullName().replace(".", "_")));
});
} else {
generatorMap.put(componentInstanceSymbol, createFullGenerator(componentInstanceSymbol.getFullName().replace(".", "_")));
}
......
package de.monticore.lang.monticar.generator.middleware.helpers;
import com.google.common.collect.Sets;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceBuilder;
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.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 org.jgrapht.Graph;
import org.jgrapht.alg.ConnectivityInspector;
......@@ -51,12 +51,13 @@ public class ClusterHelper {
int[] i = {0};
clusters.forEach(c -> {
if (c.size() == 1) {
res.add(ExpandedComponentInstanceBuilder.clone(c.iterator().next()));
res.add(c.iterator().next());
} else {
//TODO: can lead to naming conflicts: subcomp that is named <componentName>Cluster<i[0]>
String clusterName = componentInstanceSymbol.getName() + "Cluster" + i[0];
i[0]++;
res.add(createECISFromCluster(componentInstanceSymbol, c, clusterName));
String clusterPrefix = componentInstanceSymbol.getName() + "_cluster";
while (componentInstanceSymbol.getSubComponent(clusterPrefix + i[0]).isPresent()) {
i[0]++;
}
res.add(createECISFromCluster(componentInstanceSymbol, c, clusterPrefix + i[0]));
}
});
return res;
......@@ -74,13 +75,12 @@ public class ClusterHelper {
// Sub Ros -> Sub Ros: do nothing, wrapper and therefore the mw does not affect this level
// Sub Ros -> Sub normal: handled(let comp connect)
private static ExpandedComponentInstanceSymbol createECISFromCluster(ExpandedComponentInstanceSymbol inst, Set<ExpandedComponentInstanceSymbol> cluster, String clusterName) {
//TODO: implement
Set<PortSymbol> curClusterPorts = cluster.stream().flatMap(ecis -> ecis.getPorts().stream()).collect(Collectors.toSet());
Set<PortSymbol> combinedPorts = new HashSet<>();
combinedPorts.addAll(curClusterPorts);
combinedPorts.addAll(inst.getPorts());
Set<ConnectorSymbol> validConnectors = inst.getConnectors().stream()
Set<ConnectorSymbol> tmpConnectiors = inst.getConnectors().stream()
//remove all connections that use subcomponents not in cluster
.filter(c -> combinedPorts.contains(c.getSourcePort()) && combinedPorts.contains(c.getTargetPort()))
//remove all connections from super -> super and warn
......@@ -91,21 +91,51 @@ public class ClusterHelper {
}
return true;
})
//remove all connections super <-> cluster
.filter(c -> !(inst.getPorts().contains(c.getSourcePort()) || (inst.getPorts().contains(c.getTargetPort()))))
.collect(Collectors.toSet());
Collection<PortSymbol> validPorts = validConnectors.stream()
.flatMap(c -> Sets.newHashSet(c.getTargetPort(), c.getSourcePort()).stream())
.filter(p -> !inst.containsPort(p))
Collection<PortSymbol> mwPorts = curClusterPorts.stream()
.filter(p -> p.getMiddlewareSymbol().isPresent())
.collect(Collectors.toSet());
return new ExpandedComponentInstanceBuilder()
List<PortSymbol> tmpPorts = mwPorts.stream()
.filter(p -> p.getMiddlewareSymbol().isPresent())
.map(p -> {
String sourcePortName;
String targetPortName;
String subName = p.getComponentInstance().get().getName();
if (p.isIncoming()) {
sourcePortName = p.getName();
targetPortName = subName + "." + p.getName();
} else {
sourcePortName = subName + "." + p.getName();
targetPortName = p.getName();
}
ConnectorSymbol tmpConnector = ConnectorSymbol.builder()
.setSource(sourcePortName)
.setTarget(targetPortName)
.build();
tmpConnectiors.add(tmpConnector);
return EMAPortBuilder.clone(p);
})
.collect(Collectors.toList());
Set<ResolvingFilter<? extends Symbol>> resolvingFilters = inst.getSpannedScope().getResolvingFilters();
List<ExpandedComponentInstanceSymbol> tmpSubcomps = cluster.stream().map(ExpandedComponentInstanceBuilder::clone).collect(Collectors.toList());
tmpSubcomps.forEach(sc -> ((CommonScope) sc.getSpannedScope()).setResolvingFilters(resolvingFilters));
ExpandedComponentInstanceSymbol res = new ExpandedComponentInstanceBuilder()
.setName(clusterName)
.setSymbolReference(inst.getComponentType())
.addPorts(validPorts)
.addConnectors(validConnectors)
.addSubComponents(cluster.stream().map(ExpandedComponentInstanceBuilder::clone).collect(Collectors.toList()))
.addPorts(tmpPorts)
.addConnectors(tmpConnectiors)
.addSubComponents(tmpSubcomps)
.addResolutionDeclarationSymbols(inst.getResolutionDeclarationSymbols())
.build();
((CommonScope) res.getSpannedScope()).setResolvingFilters(resolvingFilters);
res.setEnclosingScope((MutableScope) inst.getEnclosingScope());
return res;
}
......
package de.monticore.lang.monticar.generator.middleware;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosToEmamTagSchema;
import de.monticore.lang.monticar.generator.middleware.helpers.ClusterHelper;
import de.monticore.lang.monticar.generator.middleware.impls.CPPGenImpl;
import de.monticore.lang.monticar.generator.middleware.impls.RosCppGenImpl;
import de.monticore.lang.monticar.generator.roscpp.helper.TagHelper;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.se_rwth.commons.logging.Finding;
import de.se_rwth.commons.logging.Log;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
public class ClusterTest extends AbstractSymtabTest {
......@@ -61,16 +67,51 @@ public class ClusterTest extends AbstractSymtabTest {
assertTrue(clusters.contains(cluster1));
assertTrue(clusters.contains(cluster2));
// List<ExpandedComponentInstanceSymbol> clusterComps = ClusterHelper.getClusterSubcomponents(componentInstanceSymbol);
// assertTrue(clusterComps.size() == 2);
//
// ExpandedComponentInstanceSymbol clusterComp1 = clusterComps.get(0);
// ExpandedComponentInstanceSymbol clusterComp2 = clusterComps.get(1);
//
// assertTrue(clusterComp1.getPort("rosOut").isPresent());
List<ExpandedComponentInstanceSymbol> clusterComps = ClusterHelper.getClusterSubcomponents(componentInstanceSymbol);
assertTrue(clusterComps.size() == 2);
//TODO: copy ansatz funktioniert nicht gut: ports müssen hinzugefügt werden(nicht jeder port einer inneren comp ist umbedingt mit der äußeren verbunden)
ExpandedComponentInstanceSymbol clusterComp1 = clusterComps.get(0);
ExpandedComponentInstanceSymbol clusterComp2 = clusterComps.get(1);
assertTrue(clusterComp1.getName().equals("sub1"));
assertTrue(clusterComp1.getPort("rosOut").isPresent());
PortSymbol rosIn = clusterComp2.getPort("rosIn").orElse(null);
ExpandedComponentInstanceSymbol sub2 = clusterComp2.getSubComponent("sub2").orElse(null);
ExpandedComponentInstanceSymbol sub3 = clusterComp2.getSubComponent("sub3").orElse(null);
assertNotNull(rosIn);
assertNotNull(sub2);
assertNotNull(sub3);
PortSymbol sub2NoRosOut = sub2.getPort("noRosOut").orElse(null);
PortSymbol sub3NoRosIn = sub3.getPort("noRosIn").orElse(null);
PortSymbol sub3RosIn = sub3.getPort("rosIn").orElse(null);
assertNotNull(sub2NoRosOut);
assertNotNull(sub3NoRosIn);
assertNotNull(sub3RosIn);
assertFalse(sub2NoRosOut.getMiddlewareSymbol().isPresent());
assertFalse(sub3NoRosIn.getMiddlewareSymbol().isPresent());
assertTrue(sub3RosIn.getMiddlewareSymbol().isPresent());
}
@Test
public void generateMultiComponentCluster() throws IOException {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
RosToEmamTagSchema.registerTagTypes(taggingResolver);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("tests.dist.twoCompCluster", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
DistributedTargetGenerator distributedTargetGenerator = new DistributedTargetGenerator();
distributedTargetGenerator.setGenerationTargetPath("./target/generated-sources-cmake/multiCompCluster/src/");
distributedTargetGenerator.add(new CPPGenImpl(), "cpp");
distributedTargetGenerator.add(new RosCppGenImpl(), "roscpp");
List<File> files = distributedTargetGenerator.generate(componentInstanceSymbol, taggingResolver);
}
@Test
......
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