From 4df7dd824152480a40be2eda4271e1c9e3195b03 Mon Sep 17 00:00:00 2001 From: Ivan Veselov Date: Fri, 17 Aug 2018 16:22:32 +0300 Subject: [PATCH 1/3] Replace type from AlgorithmResult class by Algorithm instance. --- ...lgorithmsEvaluationApplicationStarter.java | 2 +- .../evaluation/EvaluationResultsWriter.java | 2 +- .../groups/ml_methods/algorithm/ARI.java | 3 +- .../algorithm/AbstractAlgorithm.java | 32 +++++-------- .../ml_methods/algorithm/Algorithm.java | 9 +--- .../ml_methods/algorithm/AlgorithmResult.java | 23 ++++++---- .../algorithm/AlgorithmsRepository.java | 12 ++--- .../groups/ml_methods/algorithm/CCDA.java | 3 +- .../groups/ml_methods/algorithm/HAC.java | 3 +- .../RefactoringExecutionContext.java | 9 ++-- .../algorithm/AlgorithmAbstractTest.java | 2 +- .../algorithm/TestCasesCheckers.java | 46 +++++++++---------- .../intention/RefactoringAnnotator.java | 4 +- .../plugin/AutomaticRefactoringAction.java | 22 ++++----- .../ui/AlgorithmsSelectionPanel.java | 2 +- .../ml_methods/ui/ExecutionInfoDialog.java | 2 +- .../ml_methods/ui/IntersectionDialog.java | 12 ++--- .../ml_methods/ui/RefactoringsToolWindow.java | 12 ++--- 18 files changed, 87 insertions(+), 113 deletions(-) diff --git a/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/AlgorithmsEvaluationApplicationStarter.java b/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/AlgorithmsEvaluationApplicationStarter.java index 607ffefc..06b96462 100644 --- a/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/AlgorithmsEvaluationApplicationStarter.java +++ b/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/AlgorithmsEvaluationApplicationStarter.java @@ -83,7 +83,7 @@ public void main(String[] args) { private void printEvaluationResult(EvaluationResult evaluationResult) { System.out.println("=================="); - System.out.println("EVALUATION RESULT FOR " + evaluationResult.getAlgorithm().getDescriptionString()); + System.out.println("EVALUATION RESULT FOR " + evaluationResult.getAlgorithm().getName()); System.out.println("Number of good: " + evaluationResult.getNumberOfGood()); System.out.println("Number of found good: " + evaluationResult.getNumberOfFoundGood()); System.out.println("Number of bad: " + evaluationResult.getNumberOfBad()); diff --git a/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/EvaluationResultsWriter.java b/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/EvaluationResultsWriter.java index a6ba01cd..4da2fe4d 100644 --- a/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/EvaluationResultsWriter.java +++ b/algorithms-evaluation/src/main/java/org/jetbrains/research/groups/ml_methods/evaluation/EvaluationResultsWriter.java @@ -32,7 +32,7 @@ static void writeTable(List evaluationResults, Path outPath) t lines.add(""); lines.addAll(Arrays.asList("", "", "")); for (EvaluationResult evaluationResult : evaluationResults) { - lines.addAll(Arrays.asList("", evaluationResult.getAlgorithm().getDescriptionString(), "")); + lines.addAll(Arrays.asList("", evaluationResult.getAlgorithm().getName(), "")); } lines.add(""); diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java index 201facee..a5bce404 100755 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java @@ -6,7 +6,6 @@ import com.sixrr.stockmetrics.classMetrics.NumMethodsClassMetric; import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.algorithm.attributes.*; import org.jetbrains.research.groups.ml_methods.algorithm.distance.DistanceCalculator; import org.jetbrains.research.groups.ml_methods.algorithm.distance.RelevanceBasedDistanceCalculator; @@ -28,7 +27,7 @@ public class ARI extends AbstractAlgorithm { private static final @NotNull DistanceCalculator distanceCalculator = RelevanceBasedDistanceCalculator.getInstance(); public ARI() { - super(AlgorithmType.ARI, true); + super("ARI", true); } @Override diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java index 5aec1bff..5a38f3a4 100644 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java @@ -7,7 +7,6 @@ import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.algorithm.attributes.AttributesStorage; import org.jetbrains.research.groups.ml_methods.logging.Logging; import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring; @@ -36,21 +35,12 @@ public abstract class AbstractAlgorithm implements Algorithm { private static final Logger LOGGER = Logging.getLogger(AbstractAlgorithm.class); - private final AlgorithmType algorithmType; + private final String name; private final boolean enableParallelExecution; - /** - * {@inheritDoc} - */ - @NotNull - @Override - public AlgorithmType getAlgorithmType() { - return algorithmType; - } - - public AbstractAlgorithm(AlgorithmType algorithmType, boolean enableParallelExecution) { - this.algorithmType = algorithmType; + public AbstractAlgorithm(final @NotNull String name, final boolean enableParallelExecution) { + this.name = name; this.enableParallelExecution = enableParallelExecution; } @@ -58,8 +48,8 @@ public AbstractAlgorithm(AlgorithmType algorithmType, boolean enableParallelExec * {@inheritDoc} */ @Override - public @NotNull String getDescriptionString() { - return algorithmType.toString(); + public @NotNull String getName() { + return name; } /** @@ -71,7 +61,7 @@ public AbstractAlgorithm(AlgorithmType algorithmType, boolean enableParallelExec final @Nullable ExecutorService service, final boolean enableFieldRefactorings ) { - LOGGER.info(algorithmType + " started"); + LOGGER.info(name + " started"); final long startTime = System.currentTimeMillis(); final ProgressIndicator indicator; @@ -82,7 +72,7 @@ public AbstractAlgorithm(AlgorithmType algorithmType, boolean enableParallelExec } indicator.pushState(); - indicator.setText("Running " + algorithmType + "..."); + indicator.setText("Running " + name + "..."); indicator.setFraction(0); final ExecutionContext context = new ExecutionContext( @@ -97,16 +87,16 @@ public AbstractAlgorithm(AlgorithmType algorithmType, boolean enableParallelExec } catch (ProcessCanceledException e) { throw e; } catch (Exception e) { - LOGGER.error(algorithmType + " finished with error: " + e); - return new AlgorithmResult(algorithmType, e); + LOGGER.error(name + " finished with error: " + e); + return new AlgorithmResult(this, e); } final long time = System.currentTimeMillis() - startTime; indicator.popState(); - final AlgorithmResult result = new AlgorithmResult(refactorings, algorithmType, time, context.usedThreads); + final AlgorithmResult result = new AlgorithmResult(refactorings, this, time, context.usedThreads); - LOGGER.info(algorithmType + " successfully finished"); + LOGGER.info(name + " successfully finished"); LOGGER.info(result.getReport()); return result; diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/Algorithm.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/Algorithm.java index 06ec3fd9..ee50c1f2 100644 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/Algorithm.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/Algorithm.java @@ -8,8 +8,6 @@ import java.util.List; import java.util.concurrent.ExecutorService; -import static org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; - /** * An algorithm that analyses given {@link AttributesStorage} and produces refactoring suggestions * as an {@link AlgorithmResult}. @@ -36,12 +34,7 @@ public interface Algorithm { /** * Returns a short textual description of this algorithm. */ - @NotNull String getDescriptionString(); - - /** - * Returns a type of algorithm. Should be used to identify algorithm and request it by enum type not by name. - */ - @NotNull AlgorithmType getAlgorithmType(); + @NotNull String getName(); /** * Returns an array of metrics from which a features vectors for this particular algorithm diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmResult.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmResult.java index 1d8785df..0e0aeb70 100644 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmResult.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmResult.java @@ -2,7 +2,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring; import java.util.Collections; @@ -10,23 +9,27 @@ public class AlgorithmResult { private final List refactorings; - private final AlgorithmType algorithmType; + private final Algorithm algorithm; private final long executionTime; private final int threadUsed; private final Exception exception; - AlgorithmResult(@NotNull List refactorings, AlgorithmType algorithmType, long executionTime, - int threadUsed) { + AlgorithmResult( + final @NotNull List refactorings, + final @NotNull Algorithm algorithm, + final long executionTime, + final int threadUsed + ) { this.refactorings = refactorings; - this.algorithmType = algorithmType; + this.algorithm = algorithm; this.executionTime = executionTime; this.threadUsed = threadUsed; this.exception = null; } - AlgorithmResult(AlgorithmType algorithmType, @NotNull Exception exception) { + AlgorithmResult(final @NotNull Algorithm algorithm, final @NotNull Exception exception) { this.refactorings = Collections.emptyList(); - this.algorithmType = algorithmType; + this.algorithm = algorithm; this.executionTime = 0; this.threadUsed = 0; this.exception = exception; @@ -36,8 +39,8 @@ public List getRefactorings() { return Collections.unmodifiableList(refactorings); } - public AlgorithmType getAlgorithmType() { - return algorithmType; + public Algorithm getAlgorithm() { + return algorithm; } public long getExecutionTime() { @@ -58,7 +61,7 @@ public boolean isSuccess() { } public String getReport() { - return "Results of " + algorithmType + " running" + System.lineSeparator() + + return "Results of " + algorithm.getName() + " running" + System.lineSeparator() + " Found " + refactorings.size() + " refactorings" + System.lineSeparator() + " Execution time: " + executionTime + System.lineSeparator() + " Threads used: " + threadUsed; diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmsRepository.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmsRepository.java index caead412..8bdb996b 100644 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmsRepository.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmsRepository.java @@ -8,17 +8,13 @@ public class AlgorithmsRepository { private static final List ALGORITHMS = Arrays.asList( - new ARI(), - new CCDA(), - new HAC() + new ARI(), + new CCDA(), + new HAC() ); public static Optional getAlgorithmByName(String algorithmName) { - return ALGORITHMS.stream().filter(algorithm -> algorithm.getDescriptionString().equals(algorithmName)).findAny(); - } - - public enum AlgorithmType { - ARI, CCDA, HAC + return ALGORITHMS.stream().filter(algorithm -> algorithm.getName().equals(algorithmName)).findAny(); } @Contract(pure = true) diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java index b7ce10d4..8aa65b4e 100755 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java @@ -5,7 +5,6 @@ import com.sixrr.stockmetrics.classMetrics.NumMethodsClassMetric; import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.algorithm.attributes.AttributesStorage; import org.jetbrains.research.groups.ml_methods.algorithm.attributes.ClassAttributes; import org.jetbrains.research.groups.ml_methods.algorithm.attributes.ClassInnerEntityAttributes; @@ -26,7 +25,7 @@ public class CCDA extends AbstractAlgorithm { private static final double ACCURACY = 1; public CCDA() { - super(AlgorithmType.CCDA, true); + super("CCDA", true); } diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java index ca969190..52bf201d 100755 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java @@ -7,7 +7,6 @@ import org.apache.log4j.Logger; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.algorithm.attributes.*; import org.jetbrains.research.groups.ml_methods.algorithm.distance.DistanceCalculator; import org.jetbrains.research.groups.ml_methods.algorithm.distance.RelevanceBasedDistanceCalculator; @@ -31,7 +30,7 @@ public class HAC extends AbstractAlgorithm { private static final @NotNull DistanceCalculator distanceCalculator = RelevanceBasedDistanceCalculator.getInstance(); public HAC() { - super(AlgorithmType.HAC, true); + super("HAC", true); } @Override diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/RefactoringExecutionContext.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/RefactoringExecutionContext.java index df04bc67..8ac61068 100644 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/RefactoringExecutionContext.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/RefactoringExecutionContext.java @@ -28,7 +28,6 @@ import java.util.concurrent.Executors; import java.util.function.Consumer; -import static org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import static org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.getAvailableAlgorithms; /** @@ -139,8 +138,8 @@ private void calculate(Algorithm algorithm) { ); } catch (NoRequestedMetricException e) { LOGGER.error( - "Error during attributes creation for '" + algorithm.getDescriptionString() + - "' algorithm: " + e.getMessage() + " - " + algorithm.getDescriptionString() + + "Error during attributes creation for '" + algorithm.getName() + + "' algorithm: " + e.getMessage() + " - " + algorithm.getName() + "is aborted" ); @@ -157,9 +156,9 @@ public List getAlgorithmResults() { return new ArrayList<>(algorithmsResults); } - public AlgorithmResult getResultForType(AlgorithmType algorithmType) { + public AlgorithmResult getResultForAlgorithm(Algorithm algorithm) { return algorithmsResults.stream() - .filter(result -> algorithmType.equals(result.getAlgorithmType())) + .filter(result -> algorithm.equals(result.getAlgorithm())) .findAny().orElse(null); } diff --git a/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmAbstractTest.java b/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmAbstractTest.java index 65ba3612..93ddbbe2 100644 --- a/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmAbstractTest.java +++ b/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/AlgorithmAbstractTest.java @@ -11,7 +11,7 @@ @SuppressWarnings("WeakerAccess") public abstract class AlgorithmAbstractTest extends ScopeAbstractTest { protected final TestCasesCheckers testCasesChecker = - new TestCasesCheckers(getAlgorithm().getAlgorithmType()); + new TestCasesCheckers(getAlgorithm()); protected RefactoringExecutionContext createContext( AnalysisScope scope, diff --git a/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java b/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java index 7b8086f3..f5c7d962 100644 --- a/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java +++ b/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java @@ -15,10 +15,10 @@ class TestCasesCheckers { private static final String CHECK_METHODS_PREFIX = "check"; - private final AlgorithmType algorithmType; + private final Algorithm algorithm; - TestCasesCheckers(AlgorithmType algorithmType) { - this.algorithmType = algorithmType; + TestCasesCheckers(Algorithm algorithm) { + this.algorithm = algorithm; } private static Map toMap(List refactorings) { @@ -45,14 +45,14 @@ void checkMoveMethod(@NotNull RefactoringExecutionContext context) { final Map expected = new HashMap<>(); expected.put(getPackageName() + ".ClassB.methodB1()", getPackageName() + ".ClassA"); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(expected, refactorings); } void checkCallFromNested(@NotNull RefactoringExecutionContext context) { checkStructure(context, 3, 2, 1); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(1, refactorings.size()); assertContainsElements(refactorings.keySet(), getPackageName() + ".ClassB.methodB1()"); assertOneOf(refactorings.get(getPackageName() + ".ClassB.methodB1()"), @@ -62,7 +62,7 @@ void checkCallFromNested(@NotNull RefactoringExecutionContext context) { void checkCircularDependency(@NotNull RefactoringExecutionContext context) { checkStructure(context, 3, 3, 0); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Set> possibleRefactorings = new HashSet<>(); final Map possibleRefactoring1 = new HashMap<>(); possibleRefactoring1.put(getPackageName() + ".ClassB.fooB()", getPackageName() + ".ClassA"); @@ -82,7 +82,7 @@ void checkCircularDependency(@NotNull RefactoringExecutionContext context) { void checkCrossReferencesMethods(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 2, 0); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Set> possibleRefactorings = new HashSet<>(); final Map possibleRefactoring1 = new HashMap<>(); possibleRefactoring1.put(getPackageName() + ".ClassB.methodB1()", getPackageName() + ".ClassA"); @@ -97,28 +97,28 @@ void checkCrossReferencesMethods(@NotNull RefactoringExecutionContext context) { void checkDontMoveAbstract(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 2, 0); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(0, refactorings.size()); } void checkDontMoveConstructor(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 2, 1); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(0, refactorings.size()); } void checkDontMoveOverridden(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 3, 1); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(0, refactorings.size()); } void checkMoveField(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 11, 2); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Map expected = new HashMap<>(); expected.put(getPackageName() + ".ClassA.attributeA1", getPackageName() + ".ClassB"); expected.put(getPackageName() + ".ClassA.attributeA2", getPackageName() + ".ClassB"); @@ -128,7 +128,7 @@ void checkMoveField(@NotNull RefactoringExecutionContext context) { void checkMoveTogether(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 8, 4); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Map expected = new HashMap<>(); expected.put(getPackageName() + ".ClassB.methodB1()", getPackageName() + ".ClassA"); expected.put(getPackageName() + ".ClassB.methodB2()", getPackageName() + ".ClassA"); @@ -138,7 +138,7 @@ void checkMoveTogether(@NotNull RefactoringExecutionContext context) { void checkPriority(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 9, 0); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Map expected = new HashMap<>(); expected.put(getPackageName() + ".ClassA.methodA1()", getPackageName() + ".ClassB"); assertEquals(expected, refactorings); @@ -147,7 +147,7 @@ void checkPriority(@NotNull RefactoringExecutionContext context) { void checkRecursiveMethod(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 5, 2); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Map expected = new HashMap<>(); expected.put(getPackageName() + ".ClassA.methodA1()", getPackageName() + ".ClassB"); assertEquals(expected, refactorings); @@ -156,7 +156,7 @@ void checkRecursiveMethod(@NotNull RefactoringExecutionContext context) { void checkReferencesOnly(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 4, 0); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Set> possibleRefactorings = new HashSet<>(); final Map possibleRefactoring1 = new HashMap<>(); possibleRefactoring1.put(getPackageName() + ".ClassA.doSomething1()", getPackageName() + ".ClassB"); @@ -171,7 +171,7 @@ void checkReferencesOnly(@NotNull RefactoringExecutionContext context) { void checkTriangularDependence(@NotNull RefactoringExecutionContext context) { checkStructure(context, 3, 8, 0); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Map expected = new HashMap<>(); expected.put(getPackageName() + ".ClassB.methodToMove()", getPackageName() + ".ClassA"); expected.put(getPackageName() + ".ClassC.methodToMove()", getPackageName() + ".ClassA"); @@ -181,14 +181,14 @@ void checkTriangularDependence(@NotNull RefactoringExecutionContext context) { void checkMobilePhoneNoFeatureEnvy(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 5, 2); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(0, refactorings.size()); } void checkMobilePhoneWithFeatureEnvy(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 4, 2); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Map expected = new HashMap<>(); expected.put(getPackageName() + ".Customer.getMobilePhoneNumber()", getPackageName() + ".Phone"); assertEquals(expected, refactorings); @@ -197,14 +197,14 @@ void checkMobilePhoneWithFeatureEnvy(@NotNull RefactoringExecutionContext contex void checkMovieRentalStoreNoFeatureEnvy(@NotNull RefactoringExecutionContext context) { checkStructure(context, 3, 7, 9); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(0, refactorings.size()); } void checkMovieRentalStoreWithFeatureEnvy(@NotNull RefactoringExecutionContext context) { checkStructure(context, 3, 7, 9); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Map expected = new HashMap<>(); expected.put(getPackageName() + ".Customer.getMovie(Movie)", getPackageName() + ".Rental"); assertEquals(expected, refactorings); @@ -213,7 +213,7 @@ void checkMovieRentalStoreWithFeatureEnvy(@NotNull RefactoringExecutionContext c void checkCallFromLambda(@NotNull RefactoringExecutionContext context) { checkStructure(context, 2, 4, 1); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); final Set> possibleRefactorings = new HashSet<>(); final Map possibleRefactoring1 = new HashMap<>(); possibleRefactoring1.put(getPackageName() + ".ClassA.doSomething1()", getPackageName() + ".ClassB"); @@ -228,14 +228,14 @@ void checkCallFromLambda(@NotNull RefactoringExecutionContext context) { void checkStaticFactoryMethods(@NotNull RefactoringExecutionContext context) { checkStructure(context, 3, 7, 5); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(0, refactorings.size()); } void checkStaticFactoryMethodsWeak(@NotNull RefactoringExecutionContext context) { checkStructure(context, 3, 7, 5); - final Map refactorings = toMap(context.getResultForType(algorithmType).getRefactorings()); + final Map refactorings = toMap(context.getResultForAlgorithm(algorithm).getRefactorings()); assertEquals(0, refactorings.size()); } } diff --git a/src/main/java/org/jetbrains/research/groups/ml_methods/intention/RefactoringAnnotator.java b/src/main/java/org/jetbrains/research/groups/ml_methods/intention/RefactoringAnnotator.java index 13e880d3..d73578e5 100644 --- a/src/main/java/org/jetbrains/research/groups/ml_methods/intention/RefactoringAnnotator.java +++ b/src/main/java/org/jetbrains/research/groups/ml_methods/intention/RefactoringAnnotator.java @@ -32,8 +32,8 @@ public synchronized void annotate(@NotNull PsiElement psiElement, @NotNull Annot for (Algorithm algorithm : AlgorithmsRepository.getAvailableAlgorithms()) { try { setAnnotations(psiMember, - algorithm.getDescriptionString(), - AutomaticRefactoringAction.getInstance(project).getRefactoringsForType(algorithm.getAlgorithmType()), + algorithm.getName(), + AutomaticRefactoringAction.getInstance(project).getRefactoringsForAlgorithm(algorithm), annotationHolder, scope); } catch (IllegalArgumentException e) { //ignore diff --git a/src/main/java/org/jetbrains/research/groups/ml_methods/plugin/AutomaticRefactoringAction.java b/src/main/java/org/jetbrains/research/groups/ml_methods/plugin/AutomaticRefactoringAction.java index f3f9fe08..36031b79 100644 --- a/src/main/java/org/jetbrains/research/groups/ml_methods/plugin/AutomaticRefactoringAction.java +++ b/src/main/java/org/jetbrains/research/groups/ml_methods/plugin/AutomaticRefactoringAction.java @@ -21,7 +21,6 @@ import org.jetbrains.research.groups.ml_methods.algorithm.Algorithm; import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmResult; import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.algorithm.RefactoringExecutionContext; import org.jetbrains.research.groups.ml_methods.config.ArchitectureReloadedConfig; import org.jetbrains.research.groups.ml_methods.logging.Logging; @@ -49,7 +48,7 @@ public class AutomaticRefactoringAction extends BaseAnalysisAction { private static final String REFACTORING_PROFILE_KEY = "refactoring.metrics.profile.name"; private static final Map processes = new ConcurrentHashMap<>(); - private EnumMap> results = new EnumMap<>(AlgorithmType.class); + private Map> results = new HashMap<>(); private static final Map factory = new HashMap<>(); @@ -177,23 +176,20 @@ public void onFinished() { private void updateResults(@NotNull RefactoringExecutionContext context) { for (AlgorithmResult result : context.getAlgorithmResults()) { - results.put(result.getAlgorithmType(), result.getRefactorings()); + results.put(result.getAlgorithm(), result.getRefactorings()); } } private void showDialogs(@NotNull RefactoringExecutionContext context) { updateResults(context); - final Set selectedAlgorithms = - ArchitectureReloadedConfig.getInstance() - .getSelectedAlgorithms() - .stream() - .map(Algorithm::getAlgorithmType) - .collect(Collectors.toSet()); + final Set selectedAlgorithms = + new HashSet<>(ArchitectureReloadedConfig.getInstance() + .getSelectedAlgorithms()); final List algorithmsResults = context.getAlgorithmResults() .stream() - .filter(result -> selectedAlgorithms.contains(result.getAlgorithmType())) + .filter(result -> selectedAlgorithms.contains(result.getAlgorithm())) .collect(Collectors.toList()); boolean notEmptyResult = algorithmsResults.stream().flatMap(result -> result.getRefactorings() @@ -237,14 +233,14 @@ private static MetricsProfile getMetricsProfile( } @NotNull - public Set calculatedAlgorithms() { + public Set calculatedAlgorithms() { return results.keySet(); } @NotNull - public List getRefactoringsForType(AlgorithmType algorithm) { + public List getRefactoringsForAlgorithm(Algorithm algorithm) { if (!results.containsKey(algorithm)) { - throw new IllegalArgumentException("Uncalculated algorithm requested: " + algorithm); + throw new IllegalArgumentException("Uncalculated algorithm requested: " + algorithm.getName()); } return results.get(algorithm); } diff --git a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/AlgorithmsSelectionPanel.java b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/AlgorithmsSelectionPanel.java index c4db0191..5aa23bc3 100644 --- a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/AlgorithmsSelectionPanel.java +++ b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/AlgorithmsSelectionPanel.java @@ -107,7 +107,7 @@ private static Tab createAlgorithmsTab() { final Algorithm algorithm = algorithms[i]; final JCheckBox checkBox = - new JBCheckBox(algorithm.getDescriptionString(), currentSelection.contains(algorithm)); + new JBCheckBox(algorithm.getName(), currentSelection.contains(algorithm)); checkBox.addActionListener(e -> config.setSelected(algorithm, checkBox.isSelected())); constraints.gridy = i; diff --git a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/ExecutionInfoDialog.java b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/ExecutionInfoDialog.java index bd5c2b07..ab9c1a96 100644 --- a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/ExecutionInfoDialog.java +++ b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/ExecutionInfoDialog.java @@ -54,7 +54,7 @@ private JPanel createInfoPanel(AlgorithmResult result) { constraints.fill = GridBagConstraints.HORIZONTAL; constraints.anchor = GridBagConstraints.NORTHWEST; - panel.add(new TitledSeparator(result.getAlgorithmType() + " execution"), constraints); + panel.add(new TitledSeparator(result.getAlgorithm().getName() + " execution"), constraints); constraints.insets.left = 12; constraints.gridy++; diff --git a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/IntersectionDialog.java b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/IntersectionDialog.java index b6189beb..eea36833 100644 --- a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/IntersectionDialog.java +++ b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/IntersectionDialog.java @@ -5,7 +5,7 @@ import com.intellij.ui.TitledSeparator; import com.intellij.ui.components.JBCheckBox; import org.jetbrains.annotations.Nullable; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; +import org.jetbrains.research.groups.ml_methods.algorithm.Algorithm; import org.jetbrains.research.groups.ml_methods.utils.ArchitectureReloadedBundle; import javax.swing.*; @@ -16,9 +16,9 @@ import java.util.stream.Collectors; public class IntersectionDialog extends DialogWrapper { - private final Map selection = new HashMap<>(); + private final Map selection = new HashMap<>(); - IntersectionDialog(Project project, Set algorithms) { + IntersectionDialog(Project project, Set algorithms) { super(project, false); algorithms.forEach(algorithm -> selection.put(algorithm, false)); setModal(true); @@ -46,16 +46,16 @@ protected JComponent createCenterPanel() { content.add(separator, constraints); constraints.insets.left = 12; - for (AlgorithmType algorithm : selection.keySet()) { + for (Algorithm algorithm : selection.keySet()) { constraints.gridy++; - final JCheckBox checkBox = new JBCheckBox(algorithm.toString(), false); + final JCheckBox checkBox = new JBCheckBox(algorithm.getName(), false); checkBox.addActionListener(e -> selection.put(algorithm, checkBox.isSelected())); content.add(checkBox, constraints); } return content; } - Set getSelected() { + Set getSelected() { return selection.entrySet().stream() .filter(Map.Entry::getValue) .map(Map.Entry::getKey) diff --git a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/RefactoringsToolWindow.java b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/RefactoringsToolWindow.java index 72067827..ae9f34b0 100755 --- a/src/main/java/org/jetbrains/research/groups/ml_methods/ui/RefactoringsToolWindow.java +++ b/src/main/java/org/jetbrains/research/groups/ml_methods/ui/RefactoringsToolWindow.java @@ -13,8 +13,8 @@ import com.intellij.ui.content.Content; import com.sixrr.metrics.metricModel.MetricsRun; import org.jetbrains.annotations.NotNull; +import org.jetbrains.research.groups.ml_methods.algorithm.Algorithm; import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmResult; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.algorithm.entity.EntitiesStorage; import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring; import org.jetbrains.research.groups.ml_methods.utils.ArchitectureReloadedBundle; @@ -112,16 +112,16 @@ public void dispose() { entitiesStorage = null; } - private void intersect(Set algorithms) { + private void intersect(Set algorithms) { final List> refactorings = results.stream() - .filter(result -> algorithms.contains(result.getAlgorithmType())) + .filter(result -> algorithms.contains(result.getAlgorithm())) .map(AlgorithmResult::getRefactorings) .collect(Collectors.toList()); // todo may be should use combine instead of intersect final List intersection = RefactoringUtil.intersect(refactorings); if (!algorithms.isEmpty()) { final String tabName = algorithms.stream() - .map(Enum::toString) + .map(Algorithm::getName) .collect(Collectors.joining(" & ")); addTab(tabName, intersection, true); } @@ -137,8 +137,8 @@ private class IntersectAction extends AnAction { @Override public void actionPerformed(AnActionEvent e) { if (results != null) { - final Set algorithms = results.stream() - .map(AlgorithmResult::getAlgorithmType) + final Set algorithms = results.stream() + .map(AlgorithmResult::getAlgorithm) .collect(Collectors.toSet()); final IntersectionDialog dialog = new IntersectionDialog(project, algorithms); dialog.show(); From 892f1da7032832c1425e9b51b0a37a17adc43fe2 Mon Sep 17 00:00:00 2001 From: Ivan Veselov Date: Fri, 17 Aug 2018 16:27:33 +0300 Subject: [PATCH 2/3] Fix compilation error. --- .../research/groups/ml_methods/algorithm/TestCasesCheckers.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java b/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java index f5c7d962..80cc44b8 100644 --- a/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java +++ b/core/src/test/java/org/jetbrains/research/groups/ml_methods/algorithm/TestCasesCheckers.java @@ -1,7 +1,6 @@ package org.jetbrains.research.groups.ml_methods.algorithm; import org.jetbrains.annotations.NotNull; -import org.jetbrains.research.groups.ml_methods.algorithm.AlgorithmsRepository.AlgorithmType; import org.jetbrains.research.groups.ml_methods.refactoring.CalculatedRefactoring; import java.util.*; From e369ea1d92188aa6eead3ed9128d05c2cecfa348 Mon Sep 17 00:00:00 2001 From: Ivan Veselov Date: Mon, 20 Aug 2018 12:22:16 +0300 Subject: [PATCH 3/3] Remove field which stored algorithm name. --- .../groups/ml_methods/algorithm/ARI.java | 10 ++++++++- .../algorithm/AbstractAlgorithm.java | 21 +++++-------------- .../groups/ml_methods/algorithm/CCDA.java | 10 ++++++++- .../groups/ml_methods/algorithm/HAC.java | 10 ++++++++- 4 files changed, 32 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java index a5bce404..3397e52d 100755 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/ARI.java @@ -27,7 +27,15 @@ public class ARI extends AbstractAlgorithm { private static final @NotNull DistanceCalculator distanceCalculator = RelevanceBasedDistanceCalculator.getInstance(); public ARI() { - super("ARI", true); + super(true); + } + + /** + * {@inheritDoc} + */ + @Override + public @NotNull String getName() { + return "ARI"; } @Override diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java index 5a38f3a4..d924e919 100644 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/AbstractAlgorithm.java @@ -35,23 +35,12 @@ public abstract class AbstractAlgorithm implements Algorithm { private static final Logger LOGGER = Logging.getLogger(AbstractAlgorithm.class); - private final String name; - private final boolean enableParallelExecution; - public AbstractAlgorithm(final @NotNull String name, final boolean enableParallelExecution) { - this.name = name; + public AbstractAlgorithm(final boolean enableParallelExecution) { this.enableParallelExecution = enableParallelExecution; } - /** - * {@inheritDoc} - */ - @Override - public @NotNull String getName() { - return name; - } - /** * {@inheritDoc} */ @@ -61,7 +50,7 @@ public AbstractAlgorithm(final @NotNull String name, final boolean enableParalle final @Nullable ExecutorService service, final boolean enableFieldRefactorings ) { - LOGGER.info(name + " started"); + LOGGER.info(getName() + " started"); final long startTime = System.currentTimeMillis(); final ProgressIndicator indicator; @@ -72,7 +61,7 @@ public AbstractAlgorithm(final @NotNull String name, final boolean enableParalle } indicator.pushState(); - indicator.setText("Running " + name + "..."); + indicator.setText("Running " + getName() + "..."); indicator.setFraction(0); final ExecutionContext context = new ExecutionContext( @@ -87,7 +76,7 @@ public AbstractAlgorithm(final @NotNull String name, final boolean enableParalle } catch (ProcessCanceledException e) { throw e; } catch (Exception e) { - LOGGER.error(name + " finished with error: " + e); + LOGGER.error(getName() + " finished with error: " + e); return new AlgorithmResult(this, e); } @@ -96,7 +85,7 @@ public AbstractAlgorithm(final @NotNull String name, final boolean enableParalle final AlgorithmResult result = new AlgorithmResult(refactorings, this, time, context.usedThreads); - LOGGER.info(name + " successfully finished"); + LOGGER.info(getName() + " successfully finished"); LOGGER.info(result.getReport()); return result; diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java index 8aa65b4e..daa191f6 100755 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/CCDA.java @@ -25,10 +25,18 @@ public class CCDA extends AbstractAlgorithm { private static final double ACCURACY = 1; public CCDA() { - super("CCDA", true); + super(true); } + /** + * {@inheritDoc} + */ + @Override + public @NotNull String getName() { + return "CCDA"; + } + @Override public @NotNull List requiredMetrics() { return Arrays.asList(new NumMethodsClassMetric(), new NumAttributesAddedMetric()); diff --git a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java index 52bf201d..e9473acb 100755 --- a/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java +++ b/core/src/main/java/org/jetbrains/research/groups/ml_methods/algorithm/HAC.java @@ -30,7 +30,15 @@ public class HAC extends AbstractAlgorithm { private static final @NotNull DistanceCalculator distanceCalculator = RelevanceBasedDistanceCalculator.getInstance(); public HAC() { - super("HAC", true); + super(true); + } + + /** + * {@inheritDoc} + */ + @Override + public @NotNull String getName() { + return "HAC"; } @Override