use of org.apache.commons.lang3.mutable.MutableDouble in project gerrit by GerritCodeReview.
the class ReviewerRecommender method baseRanking.
/**
* @param baseWeight The weight applied to the ordering of the reviewers.
* @param query Query to match. For example, it can try to match all users that start with "Ab".
* @param candidateList The list of candidates based on the query. If query is empty, this list is
* also empty.
* @return Map of account ids that match the query and their appropriate ranking (the better the
* ranking, the better it is to suggest them as reviewers).
* @throws IOException Can't find owner="self" account.
* @throws ConfigInvalidException Can't find owner="self" account.
*/
private Map<Account.Id, MutableDouble> baseRanking(double baseWeight, String query, List<Account.Id> candidateList) throws IOException, ConfigInvalidException {
int numberOfRelevantChanges = config.getInt("suggest", "relevantChanges", 50);
// Get the user's last numberOfRelevantChanges changes, check reviewers
try {
List<ChangeData> result = queryProvider.get().setLimit(numberOfRelevantChanges).setRequestedFields(ChangeField.REVIEWER).query(changeQueryBuilder.owner("self"));
Map<Account.Id, MutableDouble> suggestions = new LinkedHashMap<>();
// Put those candidates at the bottom of the list
candidateList.stream().forEach(id -> suggestions.put(id, new MutableDouble(0)));
for (ChangeData cd : result) {
for (Account.Id reviewer : cd.reviewers().all()) {
if (accountMatchesQuery(reviewer, query)) {
suggestions.computeIfAbsent(reviewer, (ignored) -> new MutableDouble(0)).add(baseWeight);
}
}
}
return suggestions;
} catch (QueryParseException e) {
// Unhandled, because owner:self will never provoke a QueryParseException
logger.atSevere().withCause(e).log("Exception while suggesting reviewers");
return new HashMap<>();
}
}
use of org.apache.commons.lang3.mutable.MutableDouble in project gerrit by GerritCodeReview.
the class ReviewerRecommender method suggestReviewers.
public List<Account.Id> suggestReviewers(ReviewerState reviewerState, @Nullable ChangeNotes changeNotes, SuggestReviewers suggestReviewers, ProjectState projectState, List<Account.Id> candidateList) throws IOException, ConfigInvalidException {
logger.atFine().log("Candidates %s", candidateList);
String query = suggestReviewers.getQuery();
logger.atFine().log("query: %s", query);
double baseWeight = config.getInt("addReviewer", "baseWeight", 1);
logger.atFine().log("base weight: %s", baseWeight);
Map<Account.Id, MutableDouble> reviewerScores = baseRanking(baseWeight, query, candidateList);
logger.atFine().log("Base reviewer scores: %s", reviewerScores);
// Send the query along with a candidate list to all plugins and merge the
// results. Plugins don't necessarily need to use the candidates list, they
// can also return non-candidate account ids.
List<Callable<Set<SuggestedReviewer>>> tasks = new ArrayList<>(reviewerSuggestionPluginMap.plugins().size());
List<Double> weights = new ArrayList<>(reviewerSuggestionPluginMap.plugins().size());
reviewerSuggestionPluginMap.runEach(extension -> {
tasks.add(() -> extension.get().suggestReviewers(projectState.getNameKey(), changeNotes != null ? changeNotes.getChangeId() : null, query, reviewerScores.keySet()));
String key = extension.getPluginName() + "-" + extension.getExportName();
String pluginWeight = config.getString("addReviewer", key, "weight");
if (Strings.isNullOrEmpty(pluginWeight)) {
pluginWeight = "1";
}
logger.atFine().log("weight for %s: %s", key, pluginWeight);
try {
weights.add(Double.parseDouble(pluginWeight));
} catch (NumberFormatException e) {
logger.atSevere().withCause(e).log("Exception while parsing weight for %s", key);
weights.add(1d);
}
});
try {
List<Future<Set<SuggestedReviewer>>> futures = executor.invokeAll(tasks, PLUGIN_QUERY_TIMEOUT, TimeUnit.MILLISECONDS);
Iterator<Double> weightIterator = weights.iterator();
for (Future<Set<SuggestedReviewer>> f : futures) {
double weight = weightIterator.next();
for (SuggestedReviewer s : f.get()) {
if (reviewerScores.containsKey(s.account)) {
reviewerScores.get(s.account).add(s.score * weight);
} else {
reviewerScores.put(s.account, new MutableDouble(s.score * weight));
}
}
}
logger.atFine().log("Reviewer scores: %s", reviewerScores);
} catch (ExecutionException | InterruptedException e) {
logger.atSevere().withCause(e).log("Exception while suggesting reviewers");
return ImmutableList.of();
}
if (changeNotes != null) {
// Remove change owner
if (reviewerScores.remove(changeNotes.getChange().getOwner()) != null) {
logger.atFine().log("Remove change owner %s", changeNotes.getChange().getOwner());
}
// Remove existing reviewers
approvalsUtil.getReviewers(changeNotes).byState(ReviewerStateInternal.fromReviewerState(reviewerState)).forEach(r -> {
if (reviewerScores.remove(r) != null) {
logger.atFine().log("Remove existing reviewer %s", r);
}
});
}
// Sort results
Stream<Map.Entry<Account.Id, MutableDouble>> sorted = reviewerScores.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder()));
List<Account.Id> sortedSuggestions = sorted.map(Map.Entry::getKey).collect(toList());
logger.atFine().log("Sorted suggestions: %s", sortedSuggestions);
return sortedSuggestions;
}
use of org.apache.commons.lang3.mutable.MutableDouble in project neo4j by neo4j.
the class Dijkstra method traverser.
private Traverser traverser(Node start, final Node end, PathInterest<Double> interest) {
MutableDouble shortestSoFar = new MutableDouble(Double.MAX_VALUE);
PathExpander<Double> dijkstraExpander = new DijkstraPathExpander(expander, shortestSoFar, epsilon, interest.stopAfterLowestCost());
PathEvaluator<Double> dijkstraEvaluator = new DijkstraEvaluator(shortestSoFar, end, costEvaluator);
lastTraverser = new MonoDirectionalTraversalDescription().uniqueness(Uniqueness.NODE_PATH).expand(dijkstraExpander, stateFactory).order(new DijkstraSelectorFactory(interest, costEvaluator)).evaluator(dijkstraEvaluator).traverse(start);
return lastTraverser;
}
Aggregations