Split ClusterHelper into ClusterFromTagsHelper, AutomaticClusteringHelper, and ComponentHelper

parent f8222dda
Pipeline #83153 failed with stage
in 52 seconds
......@@ -40,7 +40,7 @@ public class DistributedTargetGenerator extends CMakeGenerator {
fixComponentInstance(componentInstanceSymbol);
List<ExpandedComponentInstanceSymbol> clusterSubcomponents = ClusterHelper.getClusterSubcomponents(componentInstanceSymbol);
List<ExpandedComponentInstanceSymbol> clusterSubcomponents = ClusterFromTagsHelper.getClusterSubcomponents(componentInstanceSymbol);
if (clusterSubcomponents.size() > 0) {
clusterSubcomponents.forEach(clusterECIS -> {
String nameTargetLanguage = NameHelper.getNameTargetLanguage(clusterECIS.getFullName());
......
package de.monticore.lang.monticar.generator.middleware.helpers;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosConnectionSymbol;
import de.se_rwth.commons.logging.Log;
import smile.clustering.SpectralClustering;
import java.util.*;
public class AutomaticClusteringHelper {
public static double[][] createAdjacencyMatrix(List<ExpandedComponentInstanceSymbol> subcomps, Collection<ConnectorSymbol> connectors, Map<String, Integer> subcompLabels) {
// Nodes = subcomponents
// Verts = connectors between subcomponents
double[][] res = new double[subcomps.size()][subcomps.size()];
connectors.forEach(con -> {
Optional<ExpandedComponentInstanceSymbol> sourceCompOpt = con.getSourcePort().getComponentInstance();
Optional<ExpandedComponentInstanceSymbol> targetCompOpt = con.getTargetPort().getComponentInstance();
if (sourceCompOpt.isPresent() && targetCompOpt.isPresent()) {
int index1 = subcompLabels.get(sourceCompOpt.get().getFullName());
int index2 = subcompLabels.get(targetCompOpt.get().getFullName());
res[index1][index2] = 1.0d;
res[index2][index1] = 1.0d;
} else {
Log.error("0xADE65: Component of source or target not found!");
}
});
return res;
}
public static List<Set<ExpandedComponentInstanceSymbol>> createClusters(ExpandedComponentInstanceSymbol component, int numberOfClusters, ClustererKind clustererKind){
//TODO: create wrapper for clusterer for easy exchange
List<ExpandedComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName);
double[][] adjMatrix = createAdjacencyMatrix(subcompsOrderedByName,
ComponentHelper.getInnerConnectors(component),
labelsForSubcomps);
SpectralClustering clustering = new SpectralClustering(adjMatrix,numberOfClusters);
int[] labels = clustering.getClusterLabel();
List<Set<ExpandedComponentInstanceSymbol>> res = new ArrayList<>();
for(int i = 0; i < numberOfClusters; i++){
res.add(new HashSet<>());
}
subcompsOrderedByName.forEach(sc -> {
int curClusterLabel = labels[labelsForSubcomps.get(sc.getFullName())];
res.get(curClusterLabel).add(sc);
});
return res;
}
public static void annotateComponentWithRosTagsForClusters(ExpandedComponentInstanceSymbol componentInstanceSymbol, List<Set<ExpandedComponentInstanceSymbol>> clusters) {
Collection<ConnectorSymbol> connectors = componentInstanceSymbol.getConnectors();
connectors.forEach(con -> {
// -1 = super comp
int sourceClusterLabel = -1;
int targetClusterLabel = -1;
ExpandedComponentInstanceSymbol sourceComp = con.getSourcePort().getComponentInstance().get();
ExpandedComponentInstanceSymbol targetComp = con.getTargetPort().getComponentInstance().get();
for(int i = 0; i < clusters.size(); i++){
if(clusters.get(i).contains(sourceComp)){
sourceClusterLabel = i;
}
if(clusters.get(i).contains(targetComp)){
targetClusterLabel = i;
}
}
if(sourceClusterLabel != targetClusterLabel){
con.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol());
con.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol());
}
});
}
}
package de.monticore.lang.monticar.generator.middleware.helpers;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.*;
import de.monticore.lang.embeddedmontiarc.tagging.middleware.ros.RosConnectionSymbol;
import de.monticore.symboltable.CommonScope;
import de.monticore.symboltable.MutableScope;
import de.monticore.symboltable.Symbol;
......@@ -11,14 +10,13 @@ import org.jgrapht.Graph;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import smile.clustering.SpectralClustering;
import java.util.*;
import java.util.stream.Collectors;
public class ClusterHelper {
public class ClusterFromTagsHelper {
private ClusterHelper() {
private ClusterFromTagsHelper() {
}
public static List<Set<ExpandedComponentInstanceSymbol>> getClusters(ExpandedComponentInstanceSymbol componentInstanceSymbol) {
......@@ -148,109 +146,4 @@ public class ClusterHelper {
}
public static List<ExpandedComponentInstanceSymbol> getSubcompsOrderedByName(ExpandedComponentInstanceSymbol componentInstanceSymbol){
return componentInstanceSymbol.getSubComponents().stream()
.sorted(Comparator.comparing(ExpandedComponentInstanceSymbol::getFullName))
.collect(Collectors.toList());
}
public static Collection<ConnectorSymbol> getInnerConnectors(ExpandedComponentInstanceSymbol componentInstanceSymbol){
String superCompName = componentInstanceSymbol.getFullName();
return componentInstanceSymbol.getConnectors().stream()
//filter out all connectors to super component
.filter(con -> !con.getSourcePort().getComponentInstance().get().getFullName().equals(superCompName)
&& !con.getTargetPort().getComponentInstance().get().getFullName().equals(superCompName))
.collect(Collectors.toList());
}
public static Map<String, Integer> getLabelsForSubcomps(List<ExpandedComponentInstanceSymbol> subcomps) {
Map<String, Integer> componentIndecies = new HashMap<>();
int[] i = {0};
subcomps.forEach(sc -> componentIndecies.put(sc.getFullName(), i[0]++));
return componentIndecies;
}
public static double[][] createAdjacencyMatrix(List<ExpandedComponentInstanceSymbol> subcomps, Collection<ConnectorSymbol> connectors, Map<String, Integer> subcompLabels) {
// Nodes = subcomponents
// Verts = connectors between subcomponents
double[][] res = new double[subcomps.size()][subcomps.size()];
connectors.forEach(con -> {
Optional<ExpandedComponentInstanceSymbol> sourceCompOpt = con.getSourcePort().getComponentInstance();
Optional<ExpandedComponentInstanceSymbol> targetCompOpt = con.getTargetPort().getComponentInstance();
if (sourceCompOpt.isPresent() && targetCompOpt.isPresent()) {
int index1 = subcompLabels.get(sourceCompOpt.get().getFullName());
int index2 = subcompLabels.get(targetCompOpt.get().getFullName());
res[index1][index2] = 1.0d;
res[index2][index1] = 1.0d;
} else {
Log.error("0xADE65: Component of source or target not found!");
}
});
return res;
}
public static List<Set<ExpandedComponentInstanceSymbol>> createClusters(ExpandedComponentInstanceSymbol component,int numberOfClusters,ClustererKind clustererKind){
//TODO: create wrapper for clusterer for easy exchange
List<ExpandedComponentInstanceSymbol> subcompsOrderedByName = ClusterHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ClusterHelper.getLabelsForSubcomps(subcompsOrderedByName);
double[][] adjMatrix = ClusterHelper.createAdjacencyMatrix(subcompsOrderedByName,
ClusterHelper.getInnerConnectors(component),
labelsForSubcomps);
SpectralClustering clustering = new SpectralClustering(adjMatrix,numberOfClusters);
int[] labels = clustering.getClusterLabel();
List<Set<ExpandedComponentInstanceSymbol>> res = new ArrayList<>();
for(int i = 0; i < numberOfClusters; i++){
res.add(new HashSet<>());
}
subcompsOrderedByName.forEach(sc -> {
int curClusterLabel = labels[labelsForSubcomps.get(sc.getFullName())];
res.get(curClusterLabel).add(sc);
});
return res;
}
public static void annotateComponentWithRosTagsForClusters(ExpandedComponentInstanceSymbol componentInstanceSymbol, List<Set<ExpandedComponentInstanceSymbol>> clusters) {
Collection<ConnectorSymbol> connectors = componentInstanceSymbol.getConnectors();
connectors.forEach(con -> {
// -1 = super comp
int sourceClusterLabel = -1;
int targetClusterLabel = -1;
ExpandedComponentInstanceSymbol sourceComp = con.getSourcePort().getComponentInstance().get();
ExpandedComponentInstanceSymbol targetComp = con.getTargetPort().getComponentInstance().get();
for(int i = 0; i < clusters.size(); i++){
if(clusters.get(i).contains(sourceComp)){
sourceClusterLabel = i;
}
if(clusters.get(i).contains(targetComp)){
targetClusterLabel = i;
}
}
if(sourceClusterLabel != targetClusterLabel){
con.getSourcePort().setMiddlewareSymbol(new RosConnectionSymbol());
con.getTargetPort().setMiddlewareSymbol(new RosConnectionSymbol());
}
});
}
}
package de.monticore.lang.monticar.generator.middleware.helpers;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ConnectorSymbol;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import java.util.*;
import java.util.stream.Collectors;
public class ComponentHelper {
public static List<ExpandedComponentInstanceSymbol> getSubcompsOrderedByName(ExpandedComponentInstanceSymbol componentInstanceSymbol){
return componentInstanceSymbol.getSubComponents().stream()
.sorted(Comparator.comparing(ExpandedComponentInstanceSymbol::getFullName))
.collect(Collectors.toList());
}
public static Collection<ConnectorSymbol> getInnerConnectors(ExpandedComponentInstanceSymbol componentInstanceSymbol){
String superCompName = componentInstanceSymbol.getFullName();
return componentInstanceSymbol.getConnectors().stream()
//filter out all connectors to super component
.filter(con -> !con.getSourcePort().getComponentInstance().get().getFullName().equals(superCompName)
&& !con.getTargetPort().getComponentInstance().get().getFullName().equals(superCompName))
.collect(Collectors.toList());
}
public static Map<String, Integer> getLabelsForSubcomps(List<ExpandedComponentInstanceSymbol> subcomps) {
Map<String, Integer> componentIndecies = new HashMap<>();
int[] i = {0};
subcomps.forEach(sc -> componentIndecies.put(sc.getFullName(), i[0]++));
return componentIndecies;
}
}
......@@ -2,7 +2,8 @@ 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.monticar.generator.middleware.helpers.ClusterHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.AutomaticClusteringHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.ComponentHelper;
import de.monticore.lang.monticar.generator.middleware.impls.CPPGenImpl;
import de.monticore.lang.monticar.generator.middleware.impls.RosCppGenImpl;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
......@@ -33,10 +34,10 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("lab.system", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
List<ExpandedComponentInstanceSymbol> subcompsOrderedByName = ClusterHelper.getSubcompsOrderedByName(componentInstanceSymbol);
double[][] matrix = ClusterHelper.createAdjacencyMatrix(subcompsOrderedByName,
ClusterHelper.getInnerConnectors(componentInstanceSymbol),
ClusterHelper.getLabelsForSubcomps(subcompsOrderedByName));
List<ExpandedComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(componentInstanceSymbol);
double[][] matrix = AutomaticClusteringHelper.createAdjacencyMatrix(subcompsOrderedByName,
ComponentHelper.getInnerConnectors(componentInstanceSymbol),
ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName));
//sorted by full name: alex, combine, dinhAn, michael, philipp
......@@ -96,7 +97,7 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("clustering.unambiguousCluster", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterHelper.createClusters(componentInstanceSymbol, 2, null);
List<Set<ExpandedComponentInstanceSymbol>> clusters = AutomaticClusteringHelper.createClusters(componentInstanceSymbol, 2, null);
assertTrue(clusters.size() == 2);
......@@ -152,7 +153,7 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
clusters.add(cluster2);
ClusterHelper.annotateComponentWithRosTagsForClusters(componentInstanceSymbol, clusters);
AutomaticClusteringHelper.annotateComponentWithRosTagsForClusters(componentInstanceSymbol, clusters);
List<String> rosPortsSuper = componentInstanceSymbol.getPortsList().stream()
.filter(PortSymbol::isRosPort)
......
......@@ -3,7 +3,7 @@ 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.middleware.ros.RosToEmamTagSchema;
import de.monticore.lang.monticar.generator.middleware.helpers.ClusterHelper;
import de.monticore.lang.monticar.generator.middleware.helpers.ClusterFromTagsHelper;
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;
......@@ -33,7 +33,7 @@ public class ClusterTest extends AbstractSymtabTest {
assertNotNull(componentInstanceSymbol);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterHelper.getClusters(componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterFromTagsHelper.getClusters(componentInstanceSymbol);
assertTrue(clusters.size() == 2);
Set<ExpandedComponentInstanceSymbol> cluster1 = new HashSet<>();
......@@ -55,7 +55,7 @@ public class ClusterTest extends AbstractSymtabTest {
assertNotNull(componentInstanceSymbol);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterHelper.getClusters(componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterFromTagsHelper.getClusters(componentInstanceSymbol);
Set<ExpandedComponentInstanceSymbol> cluster1 = new HashSet<>();
Set<ExpandedComponentInstanceSymbol> cluster2 = new HashSet<>();
......@@ -67,7 +67,7 @@ public class ClusterTest extends AbstractSymtabTest {
assertTrue(clusters.contains(cluster1));
assertTrue(clusters.contains(cluster2));
List<ExpandedComponentInstanceSymbol> clusterComps = ClusterHelper.getClusterSubcomponents(componentInstanceSymbol);
List<ExpandedComponentInstanceSymbol> clusterComps = ClusterFromTagsHelper.getClusterSubcomponents(componentInstanceSymbol);
assertTrue(clusterComps.size() == 2);
ExpandedComponentInstanceSymbol clusterComp1 = clusterComps.get(0);
......@@ -126,7 +126,7 @@ public class ClusterTest extends AbstractSymtabTest {
Log.enableFailQuick(false);
Log.getFindings().clear();
ClusterHelper.getClusters(componentInstanceSymbol);
ClusterFromTagsHelper.getClusters(componentInstanceSymbol);
List<Finding> findings = Log.getFindings();
......@@ -145,7 +145,7 @@ public class ClusterTest extends AbstractSymtabTest {
assertNotNull(componentInstanceSymbol);
TagHelper.resolveTags(taggingResolver, componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterHelper.getClusters(componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = ClusterFromTagsHelper.getClusters(componentInstanceSymbol);
assertTrue(clusters.size() == 1);
Set<ExpandedComponentInstanceSymbol> cluster1 = new HashSet<>();
......
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