clustering factory: optional parameter handling

parent 27298b7a
......@@ -7,5 +7,5 @@ import java.util.Set;
// product if for clustering factory
public interface ClusteringAlgorithm {
public List<Set<ExpandedComponentInstanceSymbol>> cluster(ExpandedComponentInstanceSymbol component, int numberOfClusters);
public List<Set<ExpandedComponentInstanceSymbol>> cluster(ExpandedComponentInstanceSymbol component, int numClusters, Object... args);
}
......@@ -11,7 +11,7 @@ import java.util.*;
// spectral clusterer product implementation
public class SpectralClusteringAlgorithm implements ClusteringAlgorithm {
@Override
public List<Set<ExpandedComponentInstanceSymbol>> cluster(ExpandedComponentInstanceSymbol component, int numberOfClusters) {
public List<Set<ExpandedComponentInstanceSymbol>> cluster(ExpandedComponentInstanceSymbol component, int numClusters, Object... args) {
List<ExpandedComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName);
......@@ -19,13 +19,45 @@ public class SpectralClusteringAlgorithm implements ClusteringAlgorithm {
ComponentHelper.getInnerConnectors(component),
labelsForSubcomps);
SpectralClustering clustering = new SpectralClustering(adjMatrix,numberOfClusters);
SpectralClustering clustering;
SpectralClusteringBuilder builder = new SpectralClusteringBuilder(adjMatrix, numClusters);
// Handle optional additional params for SpectralClustering.
// Additional params come as one or multiple key-value-pairs in the optional varargs array for this method,
// with key as a string (containing the name of the parameter to pass thru to the spectral clusterer) followed by its value as an object
String key;
Object value;
int v = 0;
while (v < args.length) {
if (args[v] instanceof String) {
key = (String)args[v];
if (v+1 < args.length) {
value = args[v + 1];
switch (key) {
case "l":
if (value instanceof Integer) {
builder.setL((Integer) value);
}
break;
case "sigma":
if (value instanceof Double) {
builder.setSigma((Double) value);
}
break;
}
}
}
v = v + 2;
}
clustering = builder.build();
//SpectralClustering clustering = new SpectralClustering(adjMatrix,numberOfClusters);
int[] labels = clustering.getClusterLabel();
List<Set<ExpandedComponentInstanceSymbol>> res = new ArrayList<>();
for(int i = 0; i < numberOfClusters; i++){
for(int i = 0; i < clustering.getNumClusters(); i++){
res.add(new HashSet<>());
}
......
......@@ -12,6 +12,7 @@ import de.monticore.lang.monticar.generator.middleware.impls.RosCppGenImpl;
import de.monticore.lang.tagging._symboltable.TaggingResolver;
import de.monticore.symboltable.CommonSymbol;
import org.junit.Test;
import smile.clustering.KMeans;
import smile.clustering.SpectralClustering;
import java.io.IOException;
......@@ -102,6 +103,8 @@ public class AutomaticClusteringTest extends AbstractSymtabTest{
ExpandedComponentInstanceSymbol componentInstanceSymbol = taggingResolver.<ExpandedComponentInstanceSymbol>resolve("clustering.unambiguousCluster", ExpandedComponentInstanceSymbol.KIND).orElse(null);
assertNotNull(componentInstanceSymbol);
System.out.println(algorithm);
List<Set<ExpandedComponentInstanceSymbol>> clusters = algorithm.cluster(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