Commit d69df847 authored by Alexander David Hellwig's avatar Alexander David Hellwig

Added and used guaranteedConnectedAdjacencyMatrix

parent 1fb59c30
...@@ -12,6 +12,10 @@ import de.monticore.lang.monticar.ts.MCTypeSymbol; ...@@ -12,6 +12,10 @@ import de.monticore.lang.monticar.ts.MCTypeSymbol;
import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference; import de.monticore.lang.monticar.ts.references.MCASTTypeSymbolReference;
import de.monticore.lang.monticar.ts.references.MCTypeReference; import de.monticore.lang.monticar.ts.references.MCTypeReference;
import de.monticore.symboltable.CommonSymbol; import de.monticore.symboltable.CommonSymbol;
import org.jgrapht.Graph;
import org.jgrapht.alg.ConnectivityInspector;
import org.jgrapht.graph.DefaultEdge;
import org.jgrapht.graph.SimpleGraph;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
...@@ -43,6 +47,52 @@ public class AutomaticClusteringHelper { ...@@ -43,6 +47,52 @@ public class AutomaticClusteringHelper {
return res; return res;
} }
public static double[][] guaranteedConnectedAdjacencyMatrix(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors, Map<String, Integer> subcompLabels){
double[][] res = createAdjacencyMatrix(subcomps, connectors, subcompLabels);
List<Set<EMAComponentInstanceSymbol>> connectedSubcomponentSets = getConnectedSubcomponentSets(subcomps, connectors);
double max = 0;
for (double[] doubles : res) {
for (double adj : doubles) {
if(adj > max){
max = adj;
}
}
}
double unconnectedCost = Math.max(1000 * max, 1000);
List<Integer> representativeLabels = connectedSubcomponentSets.stream()
.map(s -> subcompLabels.get(s.iterator().next().getFullName()))
.collect(Collectors.toList());
for (Integer a : representativeLabels) {
for (Integer b : representativeLabels) {
if(!a.equals(b)){
res[a][b] = unconnectedCost;
}
}
}
return res;
}
public static List<Set<EMAComponentInstanceSymbol>> getConnectedSubcomponentSets(List<EMAComponentInstanceSymbol> subcomps, Collection<EMAConnectorInstanceSymbol> connectors){
Graph<EMAComponentInstanceSymbol, DefaultEdge> graph = new SimpleGraph<>(DefaultEdge.class);
subcomps.forEach(graph::addVertex);
connectors.stream()
.filter(c -> subcomps.contains(c.getSourcePort().getComponentInstance()))
.filter(c -> subcomps.contains(c.getTargetPort().getComponentInstance()))
.forEach(c -> graph.addEdge(c.getSourcePort().getComponentInstance(), c.getTargetPort().getComponentInstance()));
ConnectivityInspector<EMAComponentInstanceSymbol, DefaultEdge> connectivityInspector = new ConnectivityInspector<>(graph);
return connectivityInspector.connectedSets();
}
public static double[][] adjacencyMatrix2transitionMatrix(double[][] adjacencyMatrix) { public static double[][] adjacencyMatrix2transitionMatrix(double[][] adjacencyMatrix) {
double[][] transitionMatrix= adjacencyMatrix; double[][] transitionMatrix= adjacencyMatrix;
......
...@@ -29,7 +29,7 @@ public class AffinityPropagationAlgorithm implements ClusteringAlgorithm { ...@@ -29,7 +29,7 @@ public class AffinityPropagationAlgorithm implements ClusteringAlgorithm {
List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component); List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName); Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName);
double[][] adjMatrix = AutomaticClusteringHelper.createAdjacencyMatrix(subcompsOrderedByName, double[][] adjMatrix = AutomaticClusteringHelper.guaranteedConnectedAdjacencyMatrix(subcompsOrderedByName,
ComponentHelper.getInnerConnectors(component), ComponentHelper.getInnerConnectors(component),
labelsForSubcomps); labelsForSubcomps);
......
...@@ -82,7 +82,7 @@ public class DBSCANClusteringAlgorithm implements ClusteringAlgorithm { ...@@ -82,7 +82,7 @@ public class DBSCANClusteringAlgorithm implements ClusteringAlgorithm {
} else { } else {
List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component); List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName); Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName);
double[][] adjMatrix = AutomaticClusteringHelper.createAdjacencyMatrix(subcompsOrderedByName, double[][] adjMatrix = AutomaticClusteringHelper.guaranteedConnectedAdjacencyMatrix(subcompsOrderedByName,
ComponentHelper.getInnerConnectors(component), ComponentHelper.getInnerConnectors(component),
labelsForSubcomps); labelsForSubcomps);
......
...@@ -149,7 +149,7 @@ public class MarkovClusteringAlgorithm implements ClusteringAlgorithm { ...@@ -149,7 +149,7 @@ public class MarkovClusteringAlgorithm implements ClusteringAlgorithm {
} else { } else {
List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component); List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName); Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName);
double[][] adjMatrix = AutomaticClusteringHelper.createAdjacencyMatrix(subcompsOrderedByName, double[][] adjMatrix = AutomaticClusteringHelper.guaranteedConnectedAdjacencyMatrix(subcompsOrderedByName,
ComponentHelper.getInnerConnectors(component), ComponentHelper.getInnerConnectors(component),
labelsForSubcomps); labelsForSubcomps);
......
...@@ -88,7 +88,7 @@ public class SpectralClusteringAlgorithm implements ClusteringAlgorithm { ...@@ -88,7 +88,7 @@ public class SpectralClusteringAlgorithm implements ClusteringAlgorithm {
} else { } else {
List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component); List<EMAComponentInstanceSymbol> subcompsOrderedByName = ComponentHelper.getSubcompsOrderedByName(component);
Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName); Map<String, Integer> labelsForSubcomps = ComponentHelper.getLabelsForSubcomps(subcompsOrderedByName);
double[][] adjMatrix = AutomaticClusteringHelper.createAdjacencyMatrix(subcompsOrderedByName, double[][] adjMatrix = AutomaticClusteringHelper.guaranteedConnectedAdjacencyMatrix(subcompsOrderedByName,
ComponentHelper.getInnerConnectors(component), ComponentHelper.getInnerConnectors(component),
labelsForSubcomps); labelsForSubcomps);
......
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