Commit 99ccc825 authored by Alexander David Hellwig's avatar Alexander David Hellwig

Remove empty clusters from ClusteringResult

parent 52825f09
Pipeline #110665 canceled with stages
......@@ -2,6 +2,8 @@ package de.monticore.lang.monticar.generator.middleware.cli.algorithms;
import de.monticore.lang.monticar.generator.middleware.clustering.ClusteringAlgorithm;
import java.util.Optional;
public abstract class AlgorithmCliParameters {
public static final String TYPE_SPECTRAL_CLUSTERING = "SpectralClustering";
public static final String TYPE_UNKOWN = "Unkown";
......@@ -22,4 +24,8 @@ public abstract class AlgorithmCliParameters {
public abstract Object[] asAlgorithmArgs();
public abstract boolean isValid();
public Optional<Integer> expectedClusterCount(){
return Optional.empty();
}
}
......@@ -68,6 +68,11 @@ public class SpectralClusteringCliParameters extends AlgorithmCliParameters {
return numberOfClusters != null;
}
@Override
public Optional<Integer> expectedClusterCount() {
return Optional.of(getNumberOfClusters().get());
}
public Optional<Integer> getNumberOfClusters() {
return Optional.ofNullable(numberOfClusters);
}
......
......@@ -12,6 +12,7 @@ import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference;
import de.monticore.lang.monticar.ts.references.MCTypeReference;
import de.monticore.symboltable.CommonSymbol;
import de.se_rwth.commons.logging.Log;
import org.jgrapht.Graph;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultEdge;
......@@ -266,7 +267,12 @@ public class AutomaticClusteringHelper {
ClusteringResultList res = new ClusteringResultList();
for (int i = 0; i < algoParams.size(); i++) {
System.out.println("Clustering with algorithm " + (i+1) + "/" + algoParams.size() + ": " +algoParams.get(i).toString());
res.add(ClusteringResult.fromParameters(emaComponentInstance, algoParams.get(i)));
ClusteringResult result = ClusteringResult.fromParameters(emaComponentInstance, algoParams.get(i));
if(result.isValid()){
res.add(result);
}else{
Log.warn("Ignoring the result! It is invalid!");
}
}
return res;
}
......
......@@ -15,7 +15,9 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Set;
public class ClusteringResult {
......@@ -23,27 +25,51 @@ public class ClusteringResult {
private EMAComponentInstanceSymbol component;
private AlgorithmCliParameters parameters;
private List<Set<EMAComponentInstanceSymbol>> clustering;
private long durration;
private long duration;
private int componentNumber;
private boolean valid;
private ClusteringResult(EMAComponentInstanceSymbol component, AlgorithmCliParameters parameters,
List<Set<EMAComponentInstanceSymbol>> clustering, long durration, int componentNumber) {
List<Set<EMAComponentInstanceSymbol>> clustering, long duration, int componentNumber, boolean valid) {
this.component = component;
this.parameters = parameters;
this.clustering = clustering;
this.durration = durration;
this.duration = duration;
this.componentNumber = componentNumber;
this.valid = valid;
}
public static ClusteringResult fromParameters(EMAComponentInstanceSymbol component, AlgorithmCliParameters parameters){
public static ClusteringResult fromParameters(EMAComponentInstanceSymbol component, AlgorithmCliParameters parameters) {
List<Set<EMAComponentInstanceSymbol>> res;
long startTime = System.currentTimeMillis();
List<Set<EMAComponentInstanceSymbol>> res = parameters.asClusteringAlgorithm().clusterWithState(component);
try {
res = parameters.asClusteringAlgorithm().clusterWithState(component);
} catch (Exception e) {
Log.warn("Marking this result as invalid. Error clustering the component.", e);
return new ClusteringResult(component, parameters, new ArrayList<>(), -1, component.getSubComponents().size(), false);
}
long endTime = System.currentTimeMillis();
boolean curValid = true;
int clustersBefore = res.size();
res.removeIf(Set::isEmpty);
if (clustersBefore != res.size()) {
Log.warn("Removed " + (clustersBefore - res.size()) + " empty clusters for algorithm " + parameters.toString());
}
Optional<Integer> expClusters = parameters.expectedClusterCount();
if(expClusters.isPresent() && !expClusters.get().equals(res.size())){
curValid = false;
Log.warn("Marking this result as invalid. The actual number of clusters(" + res.size() + ") does not equal the expected number(" + expClusters.get() +")");
}
int componentNumber = 0;
for (Set<EMAComponentInstanceSymbol> cluster : res) {
componentNumber += cluster.size();
}
return new ClusteringResult(component, parameters, res, endTime - startTime, componentNumber);
return new ClusteringResult(component, parameters, res, endTime - startTime, componentNumber, curValid);
}
public double getScore(){
......@@ -69,8 +95,8 @@ public class ClusteringResult {
return clustering.size();
}
private long getDurration() {
return this.durration;
private long getDuration() {
return this.duration;
}
public int getComponentNumber() {
......@@ -87,7 +113,7 @@ public class ClusteringResult {
String prefix = "//Algorithm: " + this.getParameters().toString() + "\n" +
"//Number of clusters: " + this.getNumberOfClusters() + "\n" +
"//Score: " + this.getScore() + "\n" +
"//Durration in ms: " + this.getDurration() + "\n";
"//Durration in ms: " + this.getDuration() + "\n";
String content = MiddlewareTagGenImpl.getFileContent(component, this.clustering);
res.setFileContent(prefix + content);
......@@ -133,9 +159,16 @@ public class ClusteringResult {
result.addProperty("Algorithm", this.getParameters().toString());
result.addProperty("NumberOfClusters", this.getNumberOfClusters());
result.addProperty("Score", this.getScore());
result.addProperty("DurationInMs", this.getDurration());
result.addProperty("DurationInMs", this.getDuration());
result.addProperty("ComponentNumber", this.getComponentNumber());
return result;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean valid) {
this.valid = valid;
}
}
\ 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