Added basics for Clustering + tests

parent e3dae7d8
......@@ -52,6 +52,12 @@
</properties>
<dependencies>
<dependency>
<groupId>org.jgrapht</groupId>
<artifactId>jgrapht-core</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
......
package de.monticore.lang.monticar.generator.master;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.se_rwth.commons.logging.Log;
import org.jgrapht.Graph;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import java.util.List;
import java.util.Set;
public class ClusterHelper {
private ClusterHelper() {
}
public static List<Set<ExpandedComponentInstanceSymbol>> getClusters(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
Graph<ExpandedComponentInstanceSymbol, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class);
componentInstanceSymbol.getSubComponents().forEach(graph::addVertex);
graph.addVertex(componentInstanceSymbol);
componentInstanceSymbol.getConnectors().stream()
.filter(c -> !(c.getSourcePort().getMiddlewareSymbol().isPresent() && c.getTargetPort().getMiddlewareSymbol().isPresent()))
.forEach(c -> {
ExpandedComponentInstanceSymbol compSource = c.getSourcePort().getComponentInstance().orElse(null);
ExpandedComponentInstanceSymbol compTarget = c.getTargetPort().getComponentInstance().orElse(null);
if (compSource == null || compTarget == null)
Log.error("ComponentInstance of source or target not found!");
graph.addEdge(compSource, compTarget);
});
ConnectivityInspector<ExpandedComponentInstanceSymbol, DefaultEdge> connectivityInspector = new ConnectivityInspector<>(graph);
if (connectivityInspector.connectedSetOf(componentInstanceSymbol).size() != 1)
Log.error("0x8EFC3: The supercomponent can only be connected to subcomponents via middleware ports!");
graph.removeVertex(componentInstanceSymbol);
return connectivityInspector.connectedSets();
}
}
package de.monticore.lang.monticar.generator.master;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.RosToEmamTagSchema;
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.util.HashSet;
import java.util.List;
import java.util.Set;
import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertNotNull;
public class ClusterTest extends AbstractSymtabTest {
@Test
public void testSingleComponentClusters() {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
RosToEmamTagSchema.registerTagTypes(taggingResolver);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("tests.dist.distComp", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterHelper.getClusters(componentInstanceSymbol);
assertTrue(clusters.size() == 2);
Set<ExpandedComponentInstanceSymbol> cluster1 = new HashSet<>();
Set<ExpandedComponentInstanceSymbol> cluster2 = new HashSet<>();
cluster1.add(componentInstanceSymbol.getSubComponent("sub1").orElse(null));
cluster2.add(componentInstanceSymbol.getSubComponent("sub2").orElse(null));
assertTrue(clusters.contains(cluster1));
assertTrue(clusters.contains(cluster2));
}
@Test
public void testMultiComponentCluster() {
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);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterHelper.getClusters(componentInstanceSymbol);
Set<ExpandedComponentInstanceSymbol> cluster1 = new HashSet<>();
Set<ExpandedComponentInstanceSymbol> cluster2 = new HashSet<>();
cluster1.add(componentInstanceSymbol.getSubComponent("sub1").orElse(null));
cluster2.add(componentInstanceSymbol.getSubComponent("sub2").orElse(null));
cluster2.add(componentInstanceSymbol.getSubComponent("sub3").orElse(null));
assertTrue(clusters.contains(cluster1));
assertTrue(clusters.contains(cluster2));
}
@Test
public void testInvalidSuperConnection() {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
RosToEmamTagSchema.registerTagTypes(taggingResolver);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("tests.dist.invalidSuperConnection", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
Log.enableFailQuick(false);
Log.getFindings().clear();
ClusterHelper.getClusters(componentInstanceSymbol);
List<Finding> findings = Log.getFindings();
assertTrue(findings.size() == 1);
assertTrue(findings.get(0).getMsg().contains("0x8EFC3"));
findings.clear();
}
@Test
public void testValidSuperConnection() {
TaggingResolver taggingResolver = createSymTabAndTaggingResolver("src/test/resources/");
RosToEmamTagSchema.registerTagTypes(taggingResolver);
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("tests.dist.validSuperConnection", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterHelper.getClusters(componentInstanceSymbol);
assertTrue(clusters.size() == 1);
Set<ExpandedComponentInstanceSymbol> cluster1 = new HashSet<>();
cluster1.add(componentInstanceSymbol.getSubComponent("sub1").orElse(null));
assertTrue(clusters.contains(cluster1));
}
}
package tests.dist;
conforms to de.monticore.lang.monticar.generator.roscpp.RosToEmamTagSchema;
tags Cluster{
tag twoCompCluster.sub1.rosOut with RosConnection;
tag twoCompCluster.sub3.rosIn with RosConnection;
tag invalidSuperConnection.sub1.rosIn with RosConnection;
tag validSuperConnection.rosIn with RosConnection;
tag validSuperConnection.rosOut with RosConnection;
tag validSuperConnection.sub1.rosIn with RosConnection;
tag validSuperConnection.sub1.rosOut with RosConnection;
}
\ No newline at end of file
package tests.dist;
component invalidSuperConnection{
port in Q noRosIn;
component Sub1{
port in Q rosIn;
}
instance Sub1 sub1;
connect noRosIn -> sub1.rosIn;
}
\ No newline at end of file
package tests.dist;
component TwoCompCluster{
component Sub1{
port out Q rosOut;
}
component Sub2{
port out Q noRosOut;
}
component Sub3{
port in Q rosIn;
port in Q noRosIn;
}
instance Sub1 sub1;
instance Sub2 sub2;
instance Sub3 sub3;
connect sub1.rosOut -> sub3.rosIn;
connect sub2.noRosOut -> sub3.noRosIn;
}
\ No newline at end of file
package tests.dist;
component ValidSuperConnection{
port in Q rosIn;
port out Q rosOut;
component Sub1{
port in Q rosIn;
port out Q rosOut;
}
instance Sub1 sub1;
connect rosIn -> sub1.rosIn;
connect rosOut -> sub1.rosOut;
}
\ 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