Skip to content

Commit

Permalink
Fix TeaStore failure
Browse files Browse the repository at this point in the history
The Operation type was used as a key in a TreeSet without implementing
Comparable.
  • Loading branch information
FloBoJa committed Feb 24, 2024
1 parent 03f10a9 commit 1de6a38
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.palladiosimulator.retriever.extraction.commonalities;

import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Set;

import org.eclipse.jdt.core.dom.ITypeBinding;

Expand Down Expand Up @@ -32,8 +32,8 @@ public Name getName() {
}

@Override
public Map<OperationInterface, SortedSet<Operation>> simplified() {
return Map.of(this, new TreeSet<>());
public Map<OperationInterface, Set<Operation>> simplified() {
return Map.of(this, new HashSet<>());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

import org.eclipse.jdt.core.dom.IMethodBinding;

Expand All @@ -27,8 +25,8 @@ public OperationName getName() {
}

@Override
public Map<OperationInterface, SortedSet<Operation>> simplified() {
return Map.of(this, new TreeSet<>(Set.of(this)));
public Map<OperationInterface, Set<Operation>> simplified() {
return Map.of(this, Set.of(this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.palladiosimulator.retriever.extraction.commonalities;

import java.util.Map;
import java.util.SortedSet;
import java.util.Set;

public interface OperationInterface extends Comparable<OperationInterface> {
Name getName();

Map<OperationInterface, SortedSet<Operation>> simplified();
Map<OperationInterface, Set<Operation>> simplified();

/**
* @returns the most specific interface name (and the interface name for java)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.stream.Collectors;

import org.palladiosimulator.retriever.extraction.engine.MapMerger;

public class PCMDetectionResult {
private final Set<Component> components;
private final Set<Composite> composites;
private final Map<OperationInterface, SortedSet<Operation>> operationInterfaces;
private final Map<OperationInterface, Set<Operation>> operationInterfaces;

public PCMDetectionResult(final Map<CompUnitOrName, ComponentBuilder> components,
final Map<String, CompositeBuilder> composites, final ProvisionsBuilder compositeProvisions,
Expand Down Expand Up @@ -145,9 +144,8 @@ private static Set<OperationInterface> collectVisibleProvisions(final Set<Compon
return provisions;
}

private Map<OperationInterface, SortedSet<Operation>> createOperationInterfaces() {
// TODO: This has to include composite interfaces as well
final List<Map<OperationInterface, SortedSet<Operation>>> constructedOperationInterfaces = this.getComponents()
private Map<OperationInterface, Set<Operation>> createOperationInterfaces() {
final List<Map<OperationInterface, Set<Operation>>> constructedOperationInterfaces = this.getComponents()
.stream()
.map(x -> x.provisions()
.simplified())
Expand Down Expand Up @@ -178,7 +176,7 @@ public Set<Composite> getCompositeComponents() {
return this.composites;
}

public Map<OperationInterface, SortedSet<Operation>> getOperationInterfaces() {
public Map<OperationInterface, Set<Operation>> getOperationInterfaces() {
return this.operationInterfaces;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.palladiosimulator.retriever.extraction.engine.MapMerger;
Expand Down Expand Up @@ -49,8 +47,8 @@ public Iterator<OperationInterface> iterator() {
return this.provisions.iterator();
}

public Map<OperationInterface, SortedSet<Operation>> simplified() {
final List<Map<OperationInterface, SortedSet<Operation>>> simplifiedInterfaces = new LinkedList<>();
public Map<OperationInterface, Set<Operation>> simplified() {
final List<Map<OperationInterface, Set<Operation>>> simplifiedInterfaces = new LinkedList<>();
for (final OperationInterface root : this.groupedProvisions.keySet()) {
final List<Operation> simplifiedRoot = new ArrayList<>(root.simplified()
.values()
Expand All @@ -66,7 +64,7 @@ public Map<OperationInterface, SortedSet<Operation>> simplified() {
}
simplifiedInterfaces.add(Map.of(root, simplifiedRoot.stream()
.distinct()
.collect(Collectors.toCollection(TreeSet::new))));
.collect(Collectors.toCollection(HashSet::new))));
}
return MapMerger.merge(simplifiedInterfaces);
}
Expand All @@ -91,7 +89,7 @@ public boolean equals(final Object obj) {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
final Map<OperationInterface, SortedSet<Operation>> simplified = this.simplified();
final Map<OperationInterface, Set<Operation>> simplified = this.simplified();

for (final OperationInterface iface : simplified.keySet()) {
builder.append(iface.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;

import org.palladiosimulator.retriever.extraction.engine.MapMerger;
Expand Down Expand Up @@ -62,8 +60,8 @@ public Iterator<OperationInterface> iterator() {
.iterator();
}

public Map<OperationInterface, SortedSet<Operation>> simplified() {
final List<Map<OperationInterface, SortedSet<Operation>>> simplifiedInterfaces = new LinkedList<>();
public Map<OperationInterface, Set<Operation>> simplified() {
final List<Map<OperationInterface, Set<Operation>>> simplifiedInterfaces = new LinkedList<>();
for (final OperationInterface root : this.groupedRequirements.keySet()) {
final List<Operation> simplifiedRoot = new ArrayList<>(root.simplified()
.values()
Expand All @@ -79,7 +77,7 @@ public Map<OperationInterface, SortedSet<Operation>> simplified() {
}
simplifiedInterfaces.add(Map.of(root, simplifiedRoot.stream()
.distinct()
.collect(Collectors.toCollection(TreeSet::new))));
.collect(Collectors.toCollection(HashSet::new))));
}
return MapMerger.merge(simplifiedInterfaces);
}
Expand All @@ -104,7 +102,7 @@ public boolean equals(final Object obj) {
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
final Map<OperationInterface, SortedSet<Operation>> simplified = this.simplified();
final Map<OperationInterface, Set<Operation>> simplified = this.simplified();

for (final OperationInterface iface : simplified.keySet()) {
builder.append(iface.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;

import org.apache.log4j.Logger;
import org.eclipse.jdt.core.dom.ASTNode;
Expand Down Expand Up @@ -40,7 +39,6 @@
import org.palladiosimulator.retriever.extraction.commonalities.OperationInterface;
import org.palladiosimulator.retriever.extraction.commonalities.PCMDetectionResult;

// TODO Bug-fix, probably
// Class to create a pcm instance out of all results from the detector class
public class PCMInstanceCreator {
private static final Logger LOG = Logger.getLogger(PCMInstanceCreator.class);
Expand Down Expand Up @@ -108,7 +106,7 @@ public Repository createPCM(final Map<String, Set<CompilationUnit>> mapping) {
final PCMDetectionResult detectionResult = this.blackboard.getPCMDetector()
.getResult();
final Set<Component> components = detectionResult.getComponents();
final Map<OperationInterface, SortedSet<Operation>> interfaces = detectionResult.getOperationInterfaces();
final Map<OperationInterface, Set<Operation>> interfaces = detectionResult.getOperationInterfaces();
final Set<Composite> composites = detectionResult.getCompositeComponents();

this.createPCMInterfaces(interfaces);
Expand Down Expand Up @@ -245,8 +243,9 @@ public Repository createPCM(final Map<String, Set<CompilationUnit>> mapping) {
return this.repository.createRepositoryNow();
}

private void createPCMInterfaces(final Map<OperationInterface, SortedSet<Operation>> interfaces) {
final Map<String, Integer> signatureNameCount = new HashMap<>();
private void createPCMInterfaces(final Map<OperationInterface, Set<Operation>> interfaces) {
final Map<String, Integer> signatureNameRegistry = new HashMap<>();

interfaces.forEach((inter, operations) -> {
final String interName = inter.getName()
.toString();
Expand All @@ -260,15 +259,8 @@ private void createPCMInterfaces(final Map<OperationInterface, SortedSet<Operati
String name = operation.getName()
.forInterface(interName)
.orElseThrow();
name = name.replace(".", "_");
final Integer oldCount = signatureNameCount.getOrDefault(name, 0);
signatureNameCount.put(name, oldCount + 1);
// Omit suffix for first occurrence.
if (oldCount > 0) {
name = name + "$" + signatureNameCount.get(name);
}
OperationSignatureCreator signature = this.create.newOperationSignature()
.withName(name);
.withName(prepareUniquePCMName(name, signatureNameRegistry));

final IMethodBinding method = operation.getBinding();

Expand Down Expand Up @@ -301,6 +293,20 @@ private void createPCMInterfaces(final Map<OperationInterface, SortedSet<Operati
});
}

private static String prepareUniquePCMName(final String name, final Map<String, Integer> registry) {
String pcmName = name.replace(".", "_");

Integer numberOfOccurences = registry.getOrDefault(pcmName, 0);
numberOfOccurences += 1;
registry.put(pcmName, numberOfOccurences);

if (numberOfOccurences == 1) {
// Omit suffix for first occurrence.
return pcmName;
}
return pcmName + "$" + registry.get(name);
}

private Optional<ASTNode> getDeclaration(final IMethodBinding binding) {
return this.blackboard.getDiscoveredFiles(JAVA_DISCOVERER_ID, CompilationUnit.class)
.values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.SortedSet;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -38,7 +37,7 @@ void singleJavaOperation() {
assertTrue(builtComponent.provisions()
.containsPartOf(expectedOperation));

final Map<OperationInterface, SortedSet<Operation>> simplifiedProvisions = builtComponent.provisions()
final Map<OperationInterface, Set<Operation>> simplifiedProvisions = builtComponent.provisions()
.simplified();

final Set<OperationInterface> interfaces = simplifiedProvisions.keySet();
Expand All @@ -49,7 +48,7 @@ void singleJavaOperation() {
.get();
assertEquals(expectedOperation, commonInterface, "operation does not have the correct interface");

final SortedSet<Operation> operations = simplifiedProvisions.get(expectedOperation);
final Set<Operation> operations = simplifiedProvisions.get(expectedOperation);
assertEquals(1, operations.size(), "more than one operation in the interface");

final List<Operation> firstMethodCandidates = operations.stream()
Expand Down Expand Up @@ -77,7 +76,7 @@ void singlePathOperation() {
assertTrue(builtComponent.provisions()
.containsPartOf(expectedOperation));

final Map<OperationInterface, SortedSet<Operation>> simplifiedProvisions = builtComponent.provisions()
final Map<OperationInterface, Set<Operation>> simplifiedProvisions = builtComponent.provisions()
.simplified();

final Set<OperationInterface> interfaces = simplifiedProvisions.keySet();
Expand All @@ -88,7 +87,7 @@ void singlePathOperation() {
.get();
assertEquals(expectedOperation, commonInterface, "operation does not have the correct interface");

final SortedSet<Operation> operations = simplifiedProvisions.get(commonInterface);
final Set<Operation> operations = simplifiedProvisions.get(commonInterface);
assertEquals(1, operations.size(), "more than one operation in the interface");

final List<Operation> firstMethodCandidates = operations.stream()
Expand Down Expand Up @@ -120,7 +119,7 @@ void entireJavaInterface() {
assertTrue(builtComponent.provisions()
.containsPartOf(expectedInterface));

final Map<OperationInterface, SortedSet<Operation>> simplifiedProvisions = builtComponent.provisions()
final Map<OperationInterface, Set<Operation>> simplifiedProvisions = builtComponent.provisions()
.simplified();

final Set<OperationInterface> interfaces = simplifiedProvisions.keySet();
Expand All @@ -131,7 +130,7 @@ void entireJavaInterface() {
.get();
assertEquals(expectedInterface, commonInterface, "common interface is not correct");

final SortedSet<Operation> operations = simplifiedProvisions.get(commonInterface);
final Set<Operation> operations = simplifiedProvisions.get(commonInterface);
assertEquals(2, operations.size(), "wrong number of operations in the interface");

final List<Operation> firstMethodCandidates = operations.stream()
Expand Down Expand Up @@ -176,7 +175,7 @@ void entirePathInterface() {
assertTrue(builtComponent.provisions()
.containsPartOf(expectedInterface));

final Map<OperationInterface, SortedSet<Operation>> simplifiedProvisions = builtComponent.provisions()
final Map<OperationInterface, Set<Operation>> simplifiedProvisions = builtComponent.provisions()
.simplified();

final Set<OperationInterface> interfaces = simplifiedProvisions.keySet();
Expand All @@ -187,7 +186,7 @@ void entirePathInterface() {
.get();
assertEquals(expectedInterface, commonInterface, "common interface is not correct");

final SortedSet<Operation> operations = simplifiedProvisions.get(commonInterface);
final Set<Operation> operations = simplifiedProvisions.get(commonInterface);
assertEquals(2, operations.size(), "wrong number of operations in the interface");

final List<Operation> firstMethodCandidates = operations.stream()
Expand Down

0 comments on commit 1de6a38

Please sign in to comment.