Skip to content

Hac is slow fix #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jetbrains.research.groups.ml_methods.algorithm;

class AlgorithmFailedException extends RuntimeException {
AlgorithmFailedException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;

public class AlgorithmResult {
private final List<CalculatedRefactoring> refactorings;
Expand All @@ -25,26 +26,29 @@ public class AlgorithmResult {
}

AlgorithmResult(AlgorithmType algorithmType, @NotNull Exception exception) {
this.refactorings = Collections.emptyList();
this.refactorings = null;
this.algorithmType = algorithmType;
this.executionTime = 0;
this.threadUsed = 0;
this.exception = exception;
}

public List<CalculatedRefactoring> getRefactorings() {
return Collections.unmodifiableList(refactorings);
throwIfNotSuccess();
return Collections.unmodifiableList(Objects.requireNonNull(refactorings));
}

public AlgorithmType getAlgorithmType() {
return algorithmType;
}

public long getExecutionTime() {
throwIfNotSuccess();
return executionTime;
}

public int getThreadUsed() {
throwIfNotSuccess();
return threadUsed;
}

Expand All @@ -53,14 +57,21 @@ public Exception getException() {
return exception;
}

public boolean isSuccess() {
boolean isSuccess() {
return exception == null;
}

public String getReport() {
return "Results of " + algorithmType + " running" + System.lineSeparator() +
" Found " + refactorings.size() + " refactorings" + System.lineSeparator() +
private void throwIfNotSuccess() {
if (!isSuccess()) {
throw new AlgorithmFailedException("Algorithm ended with exception. Requested data cannot be retrieved.");
}
}

String getReport() {
return isSuccess() ? "Results of " + algorithmType + " running" + System.lineSeparator() +
" Found " + Objects.requireNonNull(refactorings).size() + " refactorings" + System.lineSeparator() +
" Execution time: " + executionTime + System.lineSeparator() +
" Threads used: " + threadUsed;
" Threads used: " + threadUsed :
algorithmType + " failed with exception: " + Objects.requireNonNull(exception).getMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@

import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import static org.jetbrains.research.groups.ml_methods.utils.AlgorithmsUtil.getDensityBasedAccuracyRating;

public class HAC extends AbstractAlgorithm {
private static final Logger LOGGER = Logging.getLogger(HAC.class);
private static final int MAX_NUMBER_OF_CLASSES = 1000;
private static final int MAX_NUMBER_OF_METHODS = 3000;
private static final int MAX_NUMBER_OF_FIELDS = 1500;
private static final double ACCURACY = 1;

private static final @NotNull DistanceCalculator distanceCalculator = RelevanceBasedDistanceCalculator.getInstance();
Expand All @@ -44,6 +48,23 @@ public HAC() {
return new HACExecutor();
}

@NotNull
@Override
public AlgorithmResult execute(@NotNull AttributesStorage attributes, @Nullable ExecutorService service, boolean enableFieldRefactorings) {
if (willHaveLongTimeExecution(attributes)) {
LOGGER.warn("HAC haven't been executed because project is too large and HAC will work too long");
return new AlgorithmResult(AlgorithmType.HAC,
new TooLargeProjectException("HAC execution will be too long on this project"));
}
return super.execute(attributes, service, enableFieldRefactorings);
}

private boolean willHaveLongTimeExecution(@NotNull AttributesStorage attributes) {
return attributes.getClassesAttributes().size() > MAX_NUMBER_OF_CLASSES ||
attributes.getMethodsAttributes().size() > MAX_NUMBER_OF_METHODS ||
attributes.getFieldsAttributes().size() > MAX_NUMBER_OF_FIELDS;
}

private static class HACExecutor implements Executor {
private final SortedSet<Triple> heap = new TreeSet<>();
private final Map<Long, Triple> triples = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ private void calculate(Algorithm algorithm) {
final AlgorithmResult result =
algorithm.execute(attributes, executorService, enableFieldRefactoring);

algorithmsResults.add(result);
if (result.isSuccess()) {
algorithmsResults.add(result);
}
}

public List<AlgorithmResult> getAlgorithmResults() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.jetbrains.research.groups.ml_methods.algorithm;

class TooLargeProjectException extends Exception {
TooLargeProjectException(String message) {
super(message);
}
}