Search in sources :

Example 6 with MutableDouble

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<>();
    }
}
Also used : AccountCache(com.google.gerrit.server.account.AccountCache) FanOutExecutor(com.google.gerrit.server.FanOutExecutor) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Inject(com.google.inject.Inject) ReviewerState(com.google.gerrit.extensions.client.ReviewerState) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) Config(org.eclipse.jgit.lib.Config) Future(java.util.concurrent.Future) ImmutableList(com.google.common.collect.ImmutableList) QueryParseException(com.google.gerrit.index.query.QueryParseException) Map(java.util.Map) ExecutorService(java.util.concurrent.ExecutorService) SuggestedReviewer(com.google.gerrit.server.change.SuggestedReviewer) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) Iterator(java.util.Iterator) ReviewerStateInternal(com.google.gerrit.server.notedb.ReviewerStateInternal) ProjectState(com.google.gerrit.server.project.ProjectState) Account(com.google.gerrit.entities.Account) Set(java.util.Set) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) PluginMapContext(com.google.gerrit.server.plugincontext.PluginMapContext) ChangeField(com.google.gerrit.server.index.change.ChangeField) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ChangeData(com.google.gerrit.server.query.change.ChangeData) Provider(com.google.inject.Provider) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) Stream(java.util.stream.Stream) ChangeQueryBuilder(com.google.gerrit.server.query.change.ChangeQueryBuilder) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) Optional(java.util.Optional) AccountState(com.google.gerrit.server.account.AccountState) Collections(java.util.Collections) FluentLogger(com.google.common.flogger.FluentLogger) ReviewerSuggestion(com.google.gerrit.server.change.ReviewerSuggestion) Account(com.google.gerrit.entities.Account) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) ChangeData(com.google.gerrit.server.query.change.ChangeData) LinkedHashMap(java.util.LinkedHashMap) QueryParseException(com.google.gerrit.index.query.QueryParseException)

Example 7 with MutableDouble

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;
}
Also used : Account(com.google.gerrit.entities.Account) Set(java.util.Set) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) ArrayList(java.util.ArrayList) Callable(java.util.concurrent.Callable) ExecutionException(java.util.concurrent.ExecutionException) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) SuggestedReviewer(com.google.gerrit.server.change.SuggestedReviewer) Future(java.util.concurrent.Future) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with MutableDouble

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;
}
Also used : MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) MonoDirectionalTraversalDescription(org.neo4j.kernel.impl.traversal.MonoDirectionalTraversalDescription) DijkstraSelectorFactory(org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble)

Aggregations

MutableDouble (org.apache.commons.lang3.mutable.MutableDouble)8 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Account (com.google.gerrit.entities.Account)2 SuggestedReviewer (com.google.gerrit.server.change.SuggestedReviewer)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 Callable (java.util.concurrent.Callable)2 ExecutionException (java.util.concurrent.ExecutionException)2 Future (java.util.concurrent.Future)2 DijkstraBranchCollisionDetector (org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector)2 DijkstraSelectorFactory (org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory)2 BidirectionalTraversalDescription (org.neo4j.graphdb.traversal.BidirectionalTraversalDescription)2 TraversalDescription (org.neo4j.graphdb.traversal.TraversalDescription)2 Geometry (cbit.vcell.geometry.Geometry)1 MathDescription (cbit.vcell.math.MathDescription)1 MembraneSubDomain (cbit.vcell.math.MembraneSubDomain)1 ParticleProperties (cbit.vcell.math.ParticleProperties)1