clusterer factory

parent 4a3c2345
Pipeline #84278 failed with stage
in 40 seconds
......@@ -4,7 +4,6 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.Connecto
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.*;
......@@ -34,33 +33,6 @@ public class AutomaticClusteringHelper {
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();
......
package de.monticore.lang.monticar.generator.middleware.helpers;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import java.util.List;
import java.util.Set;
// creator if for clusterer factory
public abstract class Clusterer {
public List<Set<ExpandedComponentInstanceSymbol>> createClusters(ExpandedComponentInstanceSymbol component, int numberOfClusters) {
ClusteringAlgorithm clusteringAlgorithm = getClusteringAlgorithm();
return clusteringAlgorithm.cluster(component, numberOfClusters);
}
public abstract ClusteringAlgorithm getClusteringAlgorithm();
}
package de.monticore.lang.monticar.generator.middleware.helpers;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import java.util.List;
import java.util.Set;
// product if for clustering factory
public interface ClusteringAlgorithm {
public List<Set<ExpandedComponentInstanceSymbol>> cluster(ExpandedComponentInstanceSymbol component, int numberOfClusters);
}
package de.monticore.lang.monticar.generator.middleware.helpers;
// creator for spectral clusterer
public class SpectralClusterer extends Clusterer {
@Override
public ClusteringAlgorithm getClusteringAlgorithm() {
return new SpectralClusteringAlgorithm();
}
}
package de.monticore.lang.monticar.generator.middleware.helpers;
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.ExpandedComponentInstanceSymbol;
import smile.clustering.SpectralClustering;
import java.util.*;
import static de.monticore.lang.monticar.generator.middleware.helpers.AutomaticClusteringHelper.createAdjacencyMatrix;
// spectral clusterer product implementation
public class SpectralClusteringAlgorithm implements ClusteringAlgorithm {
@Override
public List<Set<ExpandedComponentInstanceSymbol>> cluster(ExpandedComponentInstanceSymbol component, int numberOfClusters) {
List<ExpandedComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName);
double[][] adjMatrix = AutomaticClusteringHelper.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;
}
}
......@@ -4,6 +4,7 @@ import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.Expanded
import de.monticore.lang.embeddedmontiarc.embeddedmontiarc._symboltable.PortSymbol;
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.helpers.SpectralClusterer;
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;
......@@ -97,7 +98,8 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("clustering.unambiguousCluster", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
List<Set<ExpandedComponentInstanceSymbol>> clusters = AutomaticClusteringHelper.createClusters(componentInstanceSymbol, 2, null);
SpectralClusterer spectralClusterer = new SpectralClusterer();
List<Set<ExpandedComponentInstanceSymbol>> clusters = spectralClusterer.createClusters(componentInstanceSymbol, 2);
assertTrue(clusters.size() == 2);
......
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