Example usage for java.util PriorityQueue add

List of usage examples for java.util PriorityQueue add

Introduction

In this page you can find the example usage for java.util PriorityQueue add.

Prototype

public boolean add(E e) 

Source Link

Document

Inserts the specified element into this priority queue.

Usage

From source file:org.apache.hadoop.hdfs.qjournal.client.QuorumJournalManager.java

@Override
public void selectInputStreams(Collection<EditLogInputStream> streams, long fromTxnId, boolean inProgressOk)
        throws IOException {

    QuorumCall<AsyncLogger, RemoteEditLogManifest> q = loggers.getEditLogManifest(fromTxnId, inProgressOk);
    Map<AsyncLogger, RemoteEditLogManifest> resps = loggers.waitForWriteQuorum(q, selectInputStreamsTimeoutMs,
            "selectInputStreams");

    LOG.debug("selectInputStream manifests:\n" + Joiner.on("\n").withKeyValueSeparator(": ").join(resps));

    final PriorityQueue<EditLogInputStream> allStreams = new PriorityQueue<EditLogInputStream>(64,
            JournalSet.EDIT_LOG_INPUT_STREAM_COMPARATOR);
    for (Map.Entry<AsyncLogger, RemoteEditLogManifest> e : resps.entrySet()) {
        AsyncLogger logger = e.getKey();
        RemoteEditLogManifest manifest = e.getValue();

        for (RemoteEditLog remoteLog : manifest.getLogs()) {
            URL url = logger.buildURLToFetchLogs(remoteLog.getStartTxId());

            EditLogInputStream elis = EditLogFileInputStream.fromUrl(connectionFactory, url,
                    remoteLog.getStartTxId(), remoteLog.getEndTxId(), remoteLog.isInProgress());
            allStreams.add(elis);
        }/*from   w ww  .  j a v a2 s.  com*/
    }
    JournalSet.chainAndMakeRedundantStreams(streams, allStreams, fromTxnId);
}

From source file:com.joliciel.talismane.tokeniser.patterns.CompoundPatternTokeniser.java

@Override
public List<TokenisedAtomicTokenSequence> tokeniseWithDecisions(Sentence sentence) {
    MONITOR.startTask("tokeniseWithDecisions");
    try {/*from ww  w .  j a  v a 2 s.  c o m*/
        // apply any pre-tokenisation decisions via filters
        // we only want one placeholder per start index - the first one that gets added
        Map<Integer, TokenPlaceholder> placeholderMap = new HashMap<Integer, TokenPlaceholder>();
        for (TokenFilter tokenFilter : this.tokenFilters) {
            Set<TokenPlaceholder> myPlaceholders = tokenFilter.apply(sentence.getText());
            for (TokenPlaceholder placeholder : myPlaceholders) {
                if (!placeholderMap.containsKey(placeholder.getStartIndex())) {
                    placeholderMap.put(placeholder.getStartIndex(), placeholder);
                }
            }
            if (LOG.isTraceEnabled()) {
                if (myPlaceholders.size() > 0) {
                    LOG.trace("TokenFilter: " + tokenFilter);
                    LOG.trace("placeholders: " + myPlaceholders);
                }
            }
        }

        Set<TokenPlaceholder> placeholders = new HashSet<TokenPlaceholder>(placeholderMap.values());

        // Initially, separate the sentence into tokens using the separators provided
        TokenSequence tokenSequence = this.tokeniserService.getTokenSequence(sentence, Tokeniser.SEPARATORS,
                placeholders);

        // apply any pre-processing filters that have been added
        for (TokenSequenceFilter tokenSequenceFilter : this.tokenSequenceFilters) {
            tokenSequenceFilter.apply(tokenSequence);
        }

        // Assign each separator its default value
        List<TokeniserOutcome> defaultOutcomes = this.tokeniserPatternManager.getDefaultOutcomes(tokenSequence);
        List<Decision<TokeniserOutcome>> defaultDecisions = new ArrayList<Decision<TokeniserOutcome>>(
                defaultOutcomes.size());
        for (TokeniserOutcome outcome : defaultOutcomes) {
            Decision<TokeniserOutcome> tokeniserDecision = this.tokeniserDecisionFactory
                    .createDefaultDecision(outcome);
            tokeniserDecision.addAuthority("_" + this.getClass().getSimpleName());
            tokeniserDecision.addAuthority("_" + "DefaultDecision");
            defaultDecisions.add(tokeniserDecision);
        }

        List<TokenisedAtomicTokenSequence> sequences = null;

        // For each test pattern, see if anything in the sentence matches it
        if (this.decisionMaker != null) {
            List<TokenPatternMatchSequence> matchingSequences = new ArrayList<TokenPatternMatchSequence>();
            Map<Token, Set<TokenPatternMatchSequence>> tokenMatchSequenceMap = new HashMap<Token, Set<TokenPatternMatchSequence>>();
            Map<TokenPatternMatchSequence, TokenPatternMatch> primaryMatchMap = new HashMap<TokenPatternMatchSequence, TokenPatternMatch>();
            Set<Token> matchedTokens = new HashSet<Token>();

            MONITOR.startTask("pattern matching");
            try {
                for (TokenPattern parsedPattern : this.getTokeniserPatternManager().getParsedTestPatterns()) {
                    List<TokenPatternMatchSequence> matchesForThisPattern = parsedPattern.match(tokenSequence);
                    for (TokenPatternMatchSequence matchSequence : matchesForThisPattern) {
                        matchingSequences.add(matchSequence);
                        matchedTokens.addAll(matchSequence.getTokensToCheck());

                        TokenPatternMatch primaryMatch = null;
                        Token token = matchSequence.getTokensToCheck().get(0);

                        Set<TokenPatternMatchSequence> matchSequences = tokenMatchSequenceMap.get(token);
                        if (matchSequences == null) {
                            matchSequences = new TreeSet<TokenPatternMatchSequence>();
                            tokenMatchSequenceMap.put(token, matchSequences);
                        }
                        matchSequences.add(matchSequence);

                        for (TokenPatternMatch patternMatch : matchSequence.getTokenPatternMatches()) {
                            if (patternMatch.getToken().equals(token)) {
                                primaryMatch = patternMatch;
                                break;
                            }
                        }

                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Found match: " + primaryMatch);
                        }
                        primaryMatchMap.put(matchSequence, primaryMatch);
                    }
                }
            } finally {
                MONITOR.endTask("pattern matching");
            }

            // we want to create the n most likely token sequences
            // the sequence has to correspond to a token pattern
            Map<TokenPatternMatchSequence, List<Decision<TokeniserOutcome>>> matchSequenceDecisionMap = new HashMap<TokenPatternMatchSequence, List<Decision<TokeniserOutcome>>>();

            for (TokenPatternMatchSequence matchSequence : matchingSequences) {
                TokenPatternMatch match = primaryMatchMap.get(matchSequence);
                LOG.debug("next pattern match: " + match.toString());
                List<FeatureResult<?>> tokenFeatureResults = new ArrayList<FeatureResult<?>>();
                MONITOR.startTask("analyse features");
                try {
                    for (TokenPatternMatchFeature<?> feature : features) {
                        RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                        FeatureResult<?> featureResult = feature.check(match, env);
                        if (featureResult != null) {
                            tokenFeatureResults.add(featureResult);
                        }
                    }

                    if (LOG.isTraceEnabled()) {
                        for (FeatureResult<?> featureResult : tokenFeatureResults) {
                            LOG.trace(featureResult.toString());
                        }
                    }
                } finally {
                    MONITOR.endTask("analyse features");
                }

                List<Decision<TokeniserOutcome>> decisions = null;
                MONITOR.startTask("make decision");
                try {
                    decisions = this.decisionMaker.decide(tokenFeatureResults);

                    for (ClassificationObserver<TokeniserOutcome> observer : this.observers)
                        observer.onAnalyse(match.getToken(), tokenFeatureResults, decisions);

                    for (Decision<TokeniserOutcome> decision : decisions) {
                        decision.addAuthority("_" + this.getClass().getSimpleName());
                        decision.addAuthority("_" + "Patterns");
                        decision.addAuthority(match.getPattern().getName());
                    }
                } finally {
                    MONITOR.endTask("make decision");
                }

                matchSequenceDecisionMap.put(matchSequence, decisions);
            }

            // initially create a heap with a single, empty sequence
            PriorityQueue<TokenisedAtomicTokenSequence> heap = new PriorityQueue<TokenisedAtomicTokenSequence>();
            TokenisedAtomicTokenSequence emptySequence = this.getTokeniserService()
                    .getTokenisedAtomicTokenSequence(sentence, 0);
            heap.add(emptySequence);

            for (int i = 0; i < tokenSequence.listWithWhiteSpace().size(); i++) {
                Token token = tokenSequence.listWithWhiteSpace().get(i);
                if (LOG.isTraceEnabled()) {
                    LOG.trace("Token : \"" + token.getText() + "\"");
                }

                // build a new heap for this iteration
                PriorityQueue<TokenisedAtomicTokenSequence> previousHeap = heap;
                heap = new PriorityQueue<TokenisedAtomicTokenSequence>();

                if (i == 0) {
                    // first token is always "separate" from the outside world
                    Decision<TokeniserOutcome> decision = this.tokeniserDecisionFactory
                            .createDefaultDecision(TokeniserOutcome.SEPARATE);
                    decision.addAuthority("_" + this.getClass().getSimpleName());
                    decision.addAuthority("_" + "DefaultDecision");

                    TaggedToken<TokeniserOutcome> taggedToken = this.tokeniserService.getTaggedToken(token,
                            decision);

                    TokenisedAtomicTokenSequence newSequence = this.getTokeniserService()
                            .getTokenisedAtomicTokenSequence(emptySequence);
                    newSequence.add(taggedToken);
                    heap.add(newSequence);
                    continue;
                }

                // limit the heap breadth to K
                int maxSequences = previousHeap.size() > this.getBeamWidth() ? this.getBeamWidth()
                        : previousHeap.size();
                MONITOR.startTask("heap sort");
                try {
                    for (int j = 0; j < maxSequences; j++) {
                        TokenisedAtomicTokenSequence history = previousHeap.poll();

                        // Find the separating & non-separating decisions
                        if (history.size() > i) {
                            // token already added as part of a sequence introduced by another token
                            heap.add(history);
                        } else if (tokenMatchSequenceMap.containsKey(token)) {
                            // token begins one or more match sequences
                            // these are ordered from shortest to longest (via TreeSet)
                            List<TokenPatternMatchSequence> matchSequences = new ArrayList<TokenPatternMatchSequence>(
                                    tokenMatchSequenceMap.get(token));

                            // Since sequences P1..Pn contain each other,
                            // there can be exactly matchSequences.size() consistent solutions
                            // Assume the default is separate
                            // 0: all separate
                            // 1: join P1, separate rest
                            // 2: join P2, separate rest
                            // ...
                            // n: join Pn
                            // We need to add each of these to the heap
                            // by taking the product of all probabilities consistent with each solution
                            // The probabities for each solution are (j=join, s=separate)
                            // All separate: s1 x s2 x ... x sn
                            // P1: j1 x s2 x ... x sn
                            // P2: j1 x j2 x ... x sn
                            // ...
                            // Pn: j1 x j2 x ... x jn
                            // Any solution of the form s1 x j2 would be inconsistent, and is not considered
                            // If Pi and Pj start and end on the exact same token, then the solution for both is
                            // Pi: j1 x ... x ji x jj x sj+1 ... x sn
                            // Pj: j1 x ... x ji x jj x sj+1 ... x sn
                            // Note of course that we're never likely to have more than two Ps here,
                            // but we need a solution for more just to be sure to be sure
                            TokeniserOutcome defaultOutcome = defaultDecisions
                                    .get(token.getIndexWithWhiteSpace()).getOutcome();
                            TokeniserOutcome otherOutcome = null;
                            if (defaultOutcome == TokeniserOutcome.SEPARATE)
                                otherOutcome = TokeniserOutcome.JOIN;
                            else
                                otherOutcome = TokeniserOutcome.SEPARATE;

                            double[] decisionProbs = new double[matchSequences.size() + 1];
                            for (int k = 0; k < decisionProbs.length; k++)
                                decisionProbs[k] = 1;

                            // Note: k0 = default decision (e.g. separate all), k1=first pattern
                            // p1 = first pattern
                            int p = 1;
                            int prevEndIndex = -1;
                            for (TokenPatternMatchSequence matchSequence : matchSequences) {
                                int endIndex = matchSequence.getTokensToCheck()
                                        .get(matchSequence.getTokensToCheck().size() - 1).getEndIndex();
                                List<Decision<TokeniserOutcome>> decisions = matchSequenceDecisionMap
                                        .get(matchSequence);
                                for (Decision<TokeniserOutcome> decision : decisions) {
                                    for (int k = 0; k < decisionProbs.length; k++) {
                                        if (decision.getOutcome() == defaultOutcome) {
                                            // e.g. separate in most cases
                                            if (k < p && endIndex > prevEndIndex)
                                                decisionProbs[k] *= decision.getProbability();
                                            else if (k + 1 < p && endIndex <= prevEndIndex)
                                                decisionProbs[k] *= decision.getProbability();
                                        } else {
                                            // e.g. join in most cases
                                            if (k >= p && endIndex > prevEndIndex)
                                                decisionProbs[k] *= decision.getProbability();
                                            else if (k + 1 >= p && endIndex <= prevEndIndex)
                                                decisionProbs[k] *= decision.getProbability();
                                        }
                                    } // next k
                                } // next decision (only 2 of these)
                                prevEndIndex = endIndex;
                                p++;
                            }

                            // transform to probability distribution
                            double sumProbs = 0;
                            for (int k = 0; k < decisionProbs.length; k++)
                                sumProbs += decisionProbs[k];

                            if (sumProbs > 0)
                                for (int k = 0; k < decisionProbs.length; k++)
                                    decisionProbs[k] /= sumProbs;

                            // Apply default decision
                            // Since this is the default decision for all tokens in the sequence, we don't add the other tokens for now,
                            // so as to allow them
                            // to get examined one at a time, just in case one of them starts its own separate sequence
                            Decision<TokeniserOutcome> defaultDecision = this.tokeniserDecisionFactory
                                    .createDecision(defaultOutcome.getCode(), decisionProbs[0]);
                            defaultDecision.addAuthority("_" + this.getClass().getSimpleName());
                            defaultDecision.addAuthority("_" + "Patterns");
                            for (TokenPatternMatchSequence matchSequence : matchSequences) {
                                defaultDecision.addAuthority(matchSequence.getTokenPattern().getName());
                            }

                            TaggedToken<TokeniserOutcome> defaultTaggedToken = this.tokeniserService
                                    .getTaggedToken(token, defaultDecision);
                            TokenisedAtomicTokenSequence defaultSequence = this.getTokeniserService()
                                    .getTokenisedAtomicTokenSequence(history);
                            defaultSequence.add(defaultTaggedToken);
                            defaultSequence.addDecision(defaultDecision);
                            heap.add(defaultSequence);

                            // Apply one non-default decision per match sequence
                            for (int k = 0; k < matchSequences.size(); k++) {
                                TokenPatternMatchSequence matchSequence = matchSequences.get(k);
                                double prob = decisionProbs[k + 1];
                                Decision<TokeniserOutcome> decision = this.tokeniserDecisionFactory
                                        .createDecision(otherOutcome.getCode(), prob);
                                decision.addAuthority("_" + this.getClass().getSimpleName());
                                decision.addAuthority("_" + "Patterns");
                                decision.addAuthority(matchSequence.getTokenPattern().getName());

                                TaggedToken<TokeniserOutcome> taggedToken = this.tokeniserService
                                        .getTaggedToken(token, decision);

                                TokenisedAtomicTokenSequence newSequence = this.getTokeniserService()
                                        .getTokenisedAtomicTokenSequence(history);
                                newSequence.add(taggedToken);
                                newSequence.addDecision(decision);

                                // The decision is NOT the default decision for all tokens in the sequence, add all other tokens
                                // in this sequence to the solution
                                for (Token tokenInSequence : matchSequence.getTokensToCheck()) {
                                    if (tokenInSequence.equals(token)) {
                                        continue;
                                    }
                                    Decision<TokeniserOutcome> decisionInSequence = this.tokeniserDecisionFactory
                                            .createDefaultDecision(decision.getOutcome());
                                    decisionInSequence.addAuthority("_" + this.getClass().getSimpleName());
                                    decisionInSequence.addAuthority("_" + "DecisionInSequence");
                                    decisionInSequence.addAuthority("_" + "DecisionInSequence_non_default");
                                    decisionInSequence.addAuthority("_" + "Patterns");
                                    TaggedToken<TokeniserOutcome> taggedTokenInSequence = this.tokeniserService
                                            .getTaggedToken(tokenInSequence, decisionInSequence);
                                    newSequence.add(taggedTokenInSequence);
                                }

                                heap.add(newSequence);

                            } // next sequence
                        } else {
                            // token doesn't start match sequence, and hasn't already been added to the current sequence
                            Decision<TokeniserOutcome> decision = defaultDecisions.get(i);
                            if (matchedTokens.contains(token)) {
                                decision = this.tokeniserDecisionFactory
                                        .createDefaultDecision(decision.getOutcome());
                                decision.addAuthority("_" + this.getClass().getSimpleName());
                                decision.addAuthority("_" + "DecisionInSequence");
                                decision.addAuthority("_" + "DecisionInSequence_default");
                                decision.addAuthority("_" + "Patterns");
                            }
                            TaggedToken<TokeniserOutcome> taggedToken = this.tokeniserService
                                    .getTaggedToken(token, decision);

                            TokenisedAtomicTokenSequence newSequence = this.getTokeniserService()
                                    .getTokenisedAtomicTokenSequence(history);
                            newSequence.add(taggedToken);
                            heap.add(newSequence);
                        }

                    } // next sequence in the old heap
                } finally {
                    MONITOR.endTask("heap sort");
                }
            } // next token

            sequences = new ArrayList<TokenisedAtomicTokenSequence>();
            int k = 0;
            while (!heap.isEmpty()) {
                sequences.add(heap.poll());
                k++;
                if (k >= this.getBeamWidth())
                    break;
            }
        } else {
            sequences = new ArrayList<TokenisedAtomicTokenSequence>();
            TokenisedAtomicTokenSequence defaultSequence = this.getTokeniserService()
                    .getTokenisedAtomicTokenSequence(sentence, 0);
            int i = 0;
            for (Token token : tokenSequence.listWithWhiteSpace()) {
                TaggedToken<TokeniserOutcome> taggedToken = this.tokeniserService.getTaggedToken(token,
                        defaultDecisions.get(i++));
                defaultSequence.add(taggedToken);
            }
            sequences.add(defaultSequence);
        } // have decision maker?

        LOG.debug("####Final token sequences:");
        int j = 1;
        for (TokenisedAtomicTokenSequence sequence : sequences) {
            TokenSequence newTokenSequence = sequence.inferTokenSequence();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Token sequence " + (j++) + ", score=" + df.format(sequence.getScore()));
                LOG.debug("Atomic sequence: " + sequence);
                LOG.debug("Resulting sequence: " + newTokenSequence);
            }
            // need to re-apply the pre-processing filters, because the tokens are all new
            // Question: why can't we conserve the initial tokens when they haven't changed at all?
            // Answer: because the tokenSequence and index in the sequence is referenced by the token.
            // Question: should we create a separate class, Token and TokenInSequence,
            // one with index & sequence access & one without?
            for (TokenSequenceFilter tokenSequenceFilter : this.tokenSequenceFilters) {
                tokenSequenceFilter.apply(newTokenSequence);
            }
            if (LOG.isDebugEnabled()) {
                LOG.debug("After filters:      " + newTokenSequence);
            }
        }

        return sequences;
    } finally {
        MONITOR.endTask("tokeniseWithDecisions");
    }
}

From source file:com.datatorrent.contrib.hdht.HDHTWalManager.java

/**
 * Copy content from parent WAL files to new location ordered by WindowID.
 * @param parentWals//from  w  w w  . j a v a 2s .c om
 * @param walPositions
 */
public void mergeWalFiles(List<PreviousWALDetails> parentWals, HashMap<Long, WalPosition> walPositions) {
    Map<Long, Iterator<Map.Entry<Long, WalPosition>>> iteratorsMap = Maps.newHashMap();
    Map<Long, WalPosition> startPositionMap = Maps.newHashMap();

    for (PreviousWALDetails walDetails : parentWals) {
        Iterator<Map.Entry<Long, WalPosition>> it = walDetails.walPositions.entrySet().iterator();
        iteratorsMap.put(walDetails.getWalKey(), it);
        if (walDetails.getCommittedWalPosition() != null) {
            startPositionMap.put(walDetails.getWalKey(), walDetails.getCommittedWalPosition());
        } else {
            startPositionMap.put(walDetails.getWalKey(), new WalPosition(0, 0));
        }
    }

    PriorityQueue<WalWindowPosition> currentValues = new PriorityQueue<>(parentWals.size(),
            new Comparator<WalWindowPosition>() {
                @Override
                public int compare(WalWindowPosition o1, WalWindowPosition o2) {
                    return (int) (o1.windowId - o2.windowId);
                }
            });

    do {
        for (Map.Entry<Long, Iterator<Map.Entry<Long, WalPosition>>> entry : iteratorsMap.entrySet()) {
            if (entry.getValue().hasNext()) {
                Map.Entry<Long, WalPosition> windowWalPosition = entry.getValue().next();
                currentValues.add(new WalWindowPosition(entry.getKey(), windowWalPosition.getKey(),
                        windowWalPosition.getValue()));
            }
        }
        if (!currentValues.isEmpty()) {
            WalWindowPosition minWindowWalEntry = currentValues.remove();
            copyWALFiles(startPositionMap.get(minWindowWalEntry.walKey), minWindowWalEntry.walPosition,
                    minWindowWalEntry.walKey);
            // Set next start position for WAL key
            startPositionMap.put(minWindowWalEntry.walKey, minWindowWalEntry.walPosition);
            // Set end position for windowId for checkpointed positions
            walPositions.put(minWindowWalEntry.windowId, this.getCurrentPosition());
        }
    } while (!currentValues.isEmpty());
}

From source file:com.joliciel.talismane.parser.TransitionBasedParserImpl.java

@Override
public List<ParseConfiguration> parseSentence(List<PosTagSequence> posTagSequences) {
    MONITOR.startTask("parseSentence");
    try {/*from  w  ww. jav a2  s. co  m*/
        long startTime = (new Date()).getTime();
        int maxAnalysisTimeMilliseconds = maxAnalysisTimePerSentence * 1000;
        int minFreeMemoryBytes = minFreeMemory * KILOBYTE;

        TokenSequence tokenSequence = posTagSequences.get(0).getTokenSequence();

        TreeMap<Integer, PriorityQueue<ParseConfiguration>> heaps = new TreeMap<Integer, PriorityQueue<ParseConfiguration>>();

        PriorityQueue<ParseConfiguration> heap0 = new PriorityQueue<ParseConfiguration>();
        for (PosTagSequence posTagSequence : posTagSequences) {
            // add an initial ParseConfiguration for each postag sequence
            ParseConfiguration initialConfiguration = this.getParserServiceInternal()
                    .getInitialConfiguration(posTagSequence);
            initialConfiguration.setScoringStrategy(decisionMaker.getDefaultScoringStrategy());
            heap0.add(initialConfiguration);
            if (LOG.isDebugEnabled()) {
                LOG.debug("Adding initial posTagSequence: " + posTagSequence);
            }
        }
        heaps.put(0, heap0);
        PriorityQueue<ParseConfiguration> backupHeap = null;

        PriorityQueue<ParseConfiguration> finalHeap = null;
        PriorityQueue<ParseConfiguration> terminalHeap = new PriorityQueue<ParseConfiguration>();
        while (heaps.size() > 0) {
            Entry<Integer, PriorityQueue<ParseConfiguration>> heapEntry = heaps.pollFirstEntry();
            PriorityQueue<ParseConfiguration> currentHeap = heapEntry.getValue();
            int currentHeapIndex = heapEntry.getKey();
            if (LOG.isTraceEnabled()) {
                LOG.trace("##### Polling next heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
            }

            boolean finished = false;
            // systematically set the final heap here, just in case we exit "naturally" with no more heaps
            finalHeap = heapEntry.getValue();
            backupHeap = new PriorityQueue<ParseConfiguration>();

            // we jump out when either (a) all tokens have been attached or (b) we go over the max alloted time
            ParseConfiguration topConf = currentHeap.peek();
            if (topConf.isTerminal()) {
                LOG.trace("Exiting with terminal heap: " + heapEntry.getKey() + ", size: "
                        + heapEntry.getValue().size());
                finished = true;
            }

            if (earlyStop && terminalHeap.size() >= beamWidth) {
                LOG.debug(
                        "Early stop activated and terminal heap contains " + beamWidth + " entries. Exiting.");
                finalHeap = terminalHeap;
                finished = true;
            }

            long analysisTime = (new Date()).getTime() - startTime;
            if (maxAnalysisTimePerSentence > 0 && analysisTime > maxAnalysisTimeMilliseconds) {
                LOG.info("Parse tree analysis took too long for sentence: " + tokenSequence.getText());
                LOG.info("Breaking out after " + maxAnalysisTimePerSentence + " seconds.");
                finished = true;
            }

            if (minFreeMemory > 0) {
                long freeMemory = Runtime.getRuntime().freeMemory();
                if (freeMemory < minFreeMemoryBytes) {
                    LOG.info("Not enough memory left to parse sentence: " + tokenSequence.getText());
                    LOG.info("Min free memory (bytes):" + minFreeMemoryBytes);
                    LOG.info("Current free memory (bytes): " + freeMemory);
                    finished = true;
                }
            }

            if (finished) {
                break;
            }

            // limit the breadth to K
            int maxSequences = currentHeap.size() > this.beamWidth ? this.beamWidth : currentHeap.size();

            int j = 0;
            while (currentHeap.size() > 0) {
                ParseConfiguration history = currentHeap.poll();
                if (LOG.isTraceEnabled()) {
                    LOG.trace("### Next configuration on heap " + heapEntry.getKey() + ":");
                    LOG.trace(history.toString());
                    LOG.trace("Score: " + df.format(history.getScore()));
                    LOG.trace(history.getPosTagSequence());
                }

                List<Decision<Transition>> decisions = new ArrayList<Decision<Transition>>();

                // test the positive rules on the current configuration
                boolean ruleApplied = false;
                if (parserPositiveRules != null) {
                    MONITOR.startTask("check rules");
                    try {
                        for (ParserRule rule : parserPositiveRules) {
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Checking rule: " + rule.toString());
                            }
                            RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                            FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                            if (ruleResult != null && ruleResult.getOutcome()) {
                                Decision<Transition> positiveRuleDecision = TalismaneSession
                                        .getTransitionSystem().createDefaultDecision(rule.getTransition());
                                decisions.add(positiveRuleDecision);
                                positiveRuleDecision.addAuthority(rule.getCondition().getName());
                                ruleApplied = true;
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Rule applies. Setting transition to: "
                                            + rule.getTransition().getCode());
                                }
                                break;
                            }
                        }
                    } finally {
                        MONITOR.endTask("check rules");
                    }
                }

                if (!ruleApplied) {
                    // test the features on the current configuration
                    List<FeatureResult<?>> parseFeatureResults = new ArrayList<FeatureResult<?>>();
                    MONITOR.startTask("feature analyse");
                    try {
                        for (ParseConfigurationFeature<?> feature : this.parseFeatures) {
                            MONITOR.startTask(feature.getName());
                            try {
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<?> featureResult = feature.check(history, env);
                                if (featureResult != null)
                                    parseFeatureResults.add(featureResult);
                            } finally {
                                MONITOR.endTask(feature.getName());
                            }
                        }
                        if (LOG_FEATURES.isTraceEnabled()) {
                            for (FeatureResult<?> featureResult : parseFeatureResults) {
                                LOG_FEATURES.trace(featureResult.toString());
                            }
                        }
                    } finally {
                        MONITOR.endTask("feature analyse");
                    }

                    // evaluate the feature results using the decision maker
                    MONITOR.startTask("make decision");
                    try {
                        decisions = this.decisionMaker.decide(parseFeatureResults);

                        for (ClassificationObserver<Transition> observer : this.observers) {
                            observer.onAnalyse(history, parseFeatureResults, decisions);
                        }

                        List<Decision<Transition>> decisionShortList = new ArrayList<Decision<Transition>>(
                                decisions.size());
                        for (Decision<Transition> decision : decisions) {
                            if (decision.getProbability() > MIN_PROB_TO_STORE)
                                decisionShortList.add(decision);
                        }
                        decisions = decisionShortList;
                    } finally {
                        MONITOR.endTask("make decision");
                    }

                    // apply the negative rules
                    Set<Transition> eliminatedTransitions = new HashSet<Transition>();
                    if (parserNegativeRules != null) {
                        MONITOR.startTask("check negative rules");
                        try {
                            for (ParserRule rule : parserNegativeRules) {
                                if (LOG.isTraceEnabled()) {
                                    LOG.trace("Checking negative rule: " + rule.toString());
                                }
                                RuntimeEnvironment env = this.featureService.getRuntimeEnvironment();
                                FeatureResult<Boolean> ruleResult = rule.getCondition().check(history, env);
                                if (ruleResult != null && ruleResult.getOutcome()) {
                                    eliminatedTransitions.addAll(rule.getTransitions());
                                    if (LOG.isTraceEnabled()) {
                                        for (Transition eliminatedTransition : rule.getTransitions())
                                            LOG.trace("Rule applies. Eliminating transition: "
                                                    + eliminatedTransition.getCode());
                                    }
                                }
                            }

                            if (eliminatedTransitions.size() > 0) {
                                List<Decision<Transition>> decisionShortList = new ArrayList<Decision<Transition>>();
                                for (Decision<Transition> decision : decisions) {
                                    if (!eliminatedTransitions.contains(decision.getOutcome())) {
                                        decisionShortList.add(decision);
                                    } else {
                                        LOG.trace("Eliminating decision: " + decision.toString());
                                    }
                                }
                                if (decisionShortList.size() > 0) {
                                    decisions = decisionShortList;
                                } else {
                                    LOG.debug("All decisions eliminated! Restoring original decisions.");
                                }
                            }
                        } finally {
                            MONITOR.endTask("check negative rules");
                        }
                    }
                } // has a positive rule been applied?

                boolean transitionApplied = false;
                // add new configuration to the heap, one for each valid transition
                MONITOR.startTask("heap sort");
                try {
                    // Why apply all decisions here? Why not just the top N (where N = beamwidth)?
                    // Answer: because we're not always adding solutions to the same heap
                    // And yet: a decision here can only do one of two things: process a token (heap+1000), or add a non-processing transition (heap+1)
                    // So, if we've already applied N decisions of each type, we should be able to stop
                    for (Decision<Transition> decision : decisions) {
                        Transition transition = decision.getOutcome();
                        if (LOG.isTraceEnabled())
                            LOG.trace("Outcome: " + transition.getCode() + ", " + decision.getProbability());

                        if (transition.checkPreconditions(history)) {
                            transitionApplied = true;
                            ParseConfiguration configuration = this.parserServiceInternal
                                    .getConfiguration(history);
                            if (decision.isStatistical())
                                configuration.addDecision(decision);
                            transition.apply(configuration);

                            int nextHeapIndex = parseComparisonStrategy.getComparisonIndex(configuration)
                                    * 1000;
                            if (configuration.isTerminal()) {
                                nextHeapIndex = Integer.MAX_VALUE;
                            } else {
                                while (nextHeapIndex <= currentHeapIndex)
                                    nextHeapIndex++;
                            }

                            PriorityQueue<ParseConfiguration> nextHeap = heaps.get(nextHeapIndex);
                            if (nextHeap == null) {
                                if (configuration.isTerminal())
                                    nextHeap = terminalHeap;
                                else
                                    nextHeap = new PriorityQueue<ParseConfiguration>();
                                heaps.put(nextHeapIndex, nextHeap);
                                if (LOG.isTraceEnabled())
                                    LOG.trace("Created heap with index: " + nextHeapIndex);
                            }
                            nextHeap.add(configuration);
                            if (LOG.isTraceEnabled()) {
                                LOG.trace("Added configuration with score " + configuration.getScore()
                                        + " to heap: " + nextHeapIndex + ", total size: " + nextHeap.size());
                            }

                            configuration.clearMemory();
                        } else {
                            if (LOG.isTraceEnabled())
                                LOG.trace("Cannot apply transition: doesn't meet pre-conditions");
                            // just in case the we run out of both heaps and analyses, we build this backup heap
                            backupHeap.add(history);
                        } // does transition meet pre-conditions?
                    } // next transition
                } finally {
                    MONITOR.endTask("heap sort");
                }

                if (transitionApplied) {
                    j++;
                } else {
                    LOG.trace("No transitions could be applied: not counting this history as part of the beam");
                }

                // beam width test
                if (j == maxSequences)
                    break;
            } // next history   
        } // next atomic index

        // return the best sequences on the heap
        List<ParseConfiguration> bestConfigurations = new ArrayList<ParseConfiguration>();
        int i = 0;

        if (finalHeap.isEmpty())
            finalHeap = backupHeap;

        while (!finalHeap.isEmpty()) {
            bestConfigurations.add(finalHeap.poll());
            i++;
            if (i >= this.getBeamWidth())
                break;
        }
        if (LOG.isDebugEnabled()) {
            for (ParseConfiguration finalConfiguration : bestConfigurations) {
                LOG.debug(df.format(finalConfiguration.getScore()) + ": " + finalConfiguration.toString());
                LOG.debug("Pos tag sequence: " + finalConfiguration.getPosTagSequence());
                LOG.debug("Transitions: " + finalConfiguration.getTransitions());
                LOG.debug("Decisions: " + finalConfiguration.getDecisions());
                if (LOG.isTraceEnabled()) {
                    StringBuilder sb = new StringBuilder();
                    for (Decision<Transition> decision : finalConfiguration.getDecisions()) {
                        sb.append(" * ");
                        sb.append(df.format(decision.getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getTransitions().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * PosTag sequence score ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getScore()));
                    sb.append(" = ");
                    for (PosTaggedToken posTaggedToken : finalConfiguration.getPosTagSequence()) {
                        sb.append(" * ");
                        sb.append(df.format(posTaggedToken.getDecision().getProbability()));
                    }
                    sb.append(" root ");
                    sb.append(finalConfiguration.getPosTagSequence().size());
                    LOG.trace(sb.toString());

                    sb = new StringBuilder();
                    sb.append(" * Token sequence score = ");
                    sb.append(df.format(finalConfiguration.getPosTagSequence().getTokenSequence().getScore()));
                    LOG.trace(sb.toString());

                }
            }
        }
        return bestConfigurations;
    } finally {
        MONITOR.endTask("parseSentence");
    }
}

From source file:org.apache.hadoop.hbase.io.hfile.bucket.BucketCache.java

/**
 * Free the space if the used size reaches acceptableSize() or one size block
 * couldn't be allocated. When freeing the space, we use the LRU algorithm and
 * ensure there must be some blocks evicted
 *//*from   ww w  .  j  a  va  2 s  .  co  m*/
private void freeSpace() {
    // Ensure only one freeSpace progress at a time
    if (!freeSpaceLock.tryLock())
        return;
    try {
        freeInProgress = true;
        long bytesToFreeWithoutExtra = 0;
        /*
         * Calculate free byte for each bucketSizeinfo
         */
        StringBuffer msgBuffer = new StringBuffer();
        BucketAllocator.IndexStatistics[] stats = bucketAllocator.getIndexStatistics();
        long[] bytesToFreeForBucket = new long[stats.length];
        for (int i = 0; i < stats.length; i++) {
            bytesToFreeForBucket[i] = 0;
            long freeGoal = (long) Math.floor(stats[i].totalCount() * (1 - DEFAULT_MIN_FACTOR));
            freeGoal = Math.max(freeGoal, 1);
            if (stats[i].freeCount() < freeGoal) {
                bytesToFreeForBucket[i] = stats[i].itemSize() * (freeGoal - stats[i].freeCount());
                bytesToFreeWithoutExtra += bytesToFreeForBucket[i];
                msgBuffer.append("Free for bucketSize(" + stats[i].itemSize() + ")="
                        + StringUtils.byteDesc(bytesToFreeForBucket[i]) + ", ");
            }
        }
        msgBuffer.append("Free for total=" + StringUtils.byteDesc(bytesToFreeWithoutExtra) + ", ");

        if (bytesToFreeWithoutExtra <= 0) {
            return;
        }
        long currentSize = bucketAllocator.getUsedSize();
        long totalSize = bucketAllocator.getTotalSize();
        LOG.debug("Bucket cache free space started; Attempting to  " + msgBuffer.toString()
                + " of current used=" + StringUtils.byteDesc(currentSize) + ",actual cacheSize="
                + StringUtils.byteDesc(realCacheSize.get()) + ",total=" + StringUtils.byteDesc(totalSize));

        long bytesToFreeWithExtra = (long) Math
                .floor(bytesToFreeWithoutExtra * (1 + DEFAULT_EXTRA_FREE_FACTOR));

        // Instantiate priority buckets
        BucketEntryGroup bucketSingle = new BucketEntryGroup(bytesToFreeWithExtra, blockSize, singleSize());
        BucketEntryGroup bucketMulti = new BucketEntryGroup(bytesToFreeWithExtra, blockSize, multiSize());
        BucketEntryGroup bucketMemory = new BucketEntryGroup(bytesToFreeWithExtra, blockSize, memorySize());

        // Scan entire map putting bucket entry into appropriate bucket entry
        // group
        for (Map.Entry<BlockCacheKey, BucketEntry> bucketEntryWithKey : backingMap.entrySet()) {
            switch (bucketEntryWithKey.getValue().getPriority()) {
            case SINGLE: {
                bucketSingle.add(bucketEntryWithKey);
                break;
            }
            case MULTI: {
                bucketMulti.add(bucketEntryWithKey);
                break;
            }
            case MEMORY: {
                bucketMemory.add(bucketEntryWithKey);
                break;
            }
            }
        }

        PriorityQueue<BucketEntryGroup> bucketQueue = new PriorityQueue<BucketEntryGroup>(3);

        bucketQueue.add(bucketSingle);
        bucketQueue.add(bucketMulti);
        bucketQueue.add(bucketMemory);

        int remainingBuckets = 3;
        long bytesFreed = 0;

        BucketEntryGroup bucketGroup;
        while ((bucketGroup = bucketQueue.poll()) != null) {
            long overflow = bucketGroup.overflow();
            if (overflow > 0) {
                long bucketBytesToFree = Math.min(overflow,
                        (bytesToFreeWithoutExtra - bytesFreed) / remainingBuckets);
                bytesFreed += bucketGroup.free(bucketBytesToFree);
            }
            remainingBuckets--;
        }

        /**
         * Check whether need extra free because some bucketSizeinfo still needs
         * free space
         */
        stats = bucketAllocator.getIndexStatistics();
        boolean needFreeForExtra = false;
        for (int i = 0; i < stats.length; i++) {
            long freeGoal = (long) Math.floor(stats[i].totalCount() * (1 - DEFAULT_MIN_FACTOR));
            freeGoal = Math.max(freeGoal, 1);
            if (stats[i].freeCount() < freeGoal) {
                needFreeForExtra = true;
                break;
            }
        }

        if (needFreeForExtra) {
            bucketQueue.clear();
            remainingBuckets = 2;

            bucketQueue.add(bucketSingle);
            bucketQueue.add(bucketMulti);

            while ((bucketGroup = bucketQueue.poll()) != null) {
                long bucketBytesToFree = (bytesToFreeWithExtra - bytesFreed) / remainingBuckets;
                bytesFreed += bucketGroup.free(bucketBytesToFree);
                remainingBuckets--;
            }
        }

        if (LOG.isDebugEnabled()) {
            long single = bucketSingle.totalSize();
            long multi = bucketMulti.totalSize();
            long memory = bucketMemory.totalSize();
            LOG.debug("Bucket cache free space completed; " + "freed=" + StringUtils.byteDesc(bytesFreed) + ", "
                    + "total=" + StringUtils.byteDesc(totalSize) + ", " + "single="
                    + StringUtils.byteDesc(single) + ", " + "multi=" + StringUtils.byteDesc(multi) + ", "
                    + "memory=" + StringUtils.byteDesc(memory));
        }

    } finally {
        cacheStats.evict();
        freeInProgress = false;
        freeSpaceLock.unlock();
    }
}

From source file:io.anserini.rerank.lib.AxiomReranker.java

/**
 * Calculate the scores (weights) of each term that occured in the reranking pool.
 * The Process://  w  w w .j a v a 2  s .  c  om
 * 1. For each query term, calculate its score for each term in the reranking pool. the score
 * is calcuated as
 * <pre>
 * P(both occurs)*log{P(both occurs)/P(t1 occurs)/P(t2 occurs)}
 * + P(both not occurs)*log{P(both not occurs)/P(t1 not occurs)/P(t2 not occurs)}
 * + P(t1 occurs t2 not occurs)*log{P(t1 occurs t2 not occurs)/P(t1 occurs)/P(t2 not occurs)}
 * + P(t1 not occurs t2 occurs)*log{P(t1 not occurs t2 occurs)/P(t1 not occurs)/P(t2 occurs)}
 * </pre>
 * 2. For each query term the scores of every other term in the reranking pool are stored in a
 * PriorityQueue, only the top {@code K} are kept.
 * 3. Add the scores of the same term together and pick the top {@code M} ones.
 *
 * @param termInvertedList A Map of <term -> Set<docId>> where the Set of docIds is where the term occurs
 * @param context An instance of RerankerContext
 * @return Map<String, Double> Top terms and their weight scores in a HashMap
 */
private Map<String, Double> computeTermScore(Map<String, Set<Integer>> termInvertedList,
        RerankerContext<T> context) throws IOException {
    class ScoreComparator implements Comparator<Pair<String, Double>> {
        public int compare(Pair<String, Double> a, Pair<String, Double> b) {
            int cmp = Double.compare(b.getRight(), a.getRight());
            if (cmp == 0) {
                return a.getLeft().compareToIgnoreCase(b.getLeft());
            } else {
                return cmp;
            }
        }
    }

    // get collection statistics so that we can get idf later on.
    IndexReader reader;
    if (this.externalIndexPath != null) {
        Path indexPath = Paths.get(this.externalIndexPath);
        if (!Files.exists(indexPath) || !Files.isDirectory(indexPath) || !Files.isReadable(indexPath)) {
            throw new IllegalArgumentException(
                    this.externalIndexPath + " does not exist or is not a directory.");
        }
        reader = DirectoryReader.open(FSDirectory.open(indexPath));
    } else {
        IndexSearcher searcher = context.getIndexSearcher();
        reader = searcher.getIndexReader();
    }
    final long docCount = reader.numDocs() == -1 ? reader.maxDoc() : reader.numDocs();

    //calculate the Mutual Information between term with each query term
    List<String> queryTerms = context.getQueryTokens();
    Map<String, Integer> queryTermsCounts = new HashMap<>();
    for (String qt : queryTerms) {
        queryTermsCounts.put(qt, queryTermsCounts.getOrDefault(qt, 0) + 1);
    }

    Set<Integer> allDocIds = new HashSet<>();
    for (Set<Integer> s : termInvertedList.values()) {
        allDocIds.addAll(s);
    }
    int docIdsCount = allDocIds.size();

    // Each priority queue corresponds to a query term: The p-queue itself stores all terms
    // in the reranking pool and their reranking scores to the query term.
    List<PriorityQueue<Pair<String, Double>>> allTermScoresPQ = new ArrayList<>();
    for (Map.Entry<String, Integer> q : queryTermsCounts.entrySet()) {
        String queryTerm = q.getKey();
        long df = reader.docFreq(new Term(LuceneDocumentGenerator.FIELD_BODY, queryTerm));
        if (df == 0L) {
            continue;
        }
        float idf = (float) Math.log((1 + docCount) / df);
        int qtf = q.getValue();
        if (termInvertedList.containsKey(queryTerm)) {
            PriorityQueue<Pair<String, Double>> termScorePQ = new PriorityQueue<>(new ScoreComparator());
            double selfMI = computeMutualInformation(termInvertedList.get(queryTerm),
                    termInvertedList.get(queryTerm), docIdsCount);
            for (Map.Entry<String, Set<Integer>> termEntry : termInvertedList.entrySet()) {
                double score;
                if (termEntry.getKey().equals(queryTerm)) { // The mutual information to itself will always be 1
                    score = idf * qtf;
                } else {
                    double crossMI = computeMutualInformation(termInvertedList.get(queryTerm),
                            termEntry.getValue(), docIdsCount);
                    score = idf * beta * qtf * crossMI / selfMI;
                }
                termScorePQ.add(Pair.of(termEntry.getKey(), score));
            }
            allTermScoresPQ.add(termScorePQ);
        }
    }

    Map<String, Double> aggTermScores = new HashMap<>();
    for (PriorityQueue<Pair<String, Double>> termScores : allTermScoresPQ) {
        for (int i = 0; i < Math.min(termScores.size(), this.K); i++) {
            Pair<String, Double> termScore = termScores.poll();
            String term = termScore.getLeft();
            Double score = termScore.getRight();
            if (score - 0.0 > 1e-8) {
                aggTermScores.put(term, aggTermScores.getOrDefault(term, 0.0) + score);
            }
        }
    }
    PriorityQueue<Pair<String, Double>> termScoresPQ = new PriorityQueue<>(new ScoreComparator());
    for (Map.Entry<String, Double> termScore : aggTermScores.entrySet()) {
        termScoresPQ.add(Pair.of(termScore.getKey(), termScore.getValue() / queryTerms.size()));
    }
    Map<String, Double> resultTermScores = new HashMap<>();
    for (int i = 0; i < Math.min(termScoresPQ.size(), this.M); i++) {
        Pair<String, Double> termScore = termScoresPQ.poll();
        String term = termScore.getKey();
        double score = termScore.getValue();
        resultTermScores.put(term, score);
    }

    return resultTermScores;
}

From source file:org.kuali.rice.krms.framework.engine.TermResolutionEngineImpl.java

/**
 *
 * @param termName//from   w w w.  ja  v a 2 s.co  m
 * @return List<{@link TermResolverKey}>
 */
protected List<TermResolverKey> buildTermResolutionPlan(String termName) {
    // our result
    List<TermResolverKey> resolutionPlan = null;

    // Holds the resolvers we've visited, along with the needed metadata for generating our final plan
    Map<TermResolverKey, Visited> visitedByKey = new HashMap<TermResolverKey, Visited>();

    // this holds a least cost first list of nodes remaining to be explored
    PriorityQueue<ToVisit> toVisits = new PriorityQueue<ToVisit>(); // nice grammar there cowboy

    // dummy resolver to be the root of this tree
    // Do I really need this?  Yes, because there may be more than one resolver that resolves to the desired termName,
    // so this destination unifies the trees of those candidate resolvers
    TermResolver destination = createDestination(termName); // problem is we can't get this one out of the registry
    TermResolverKey destinationKey = new TermResolverKey(destination);

    LOG.debug("Beginning resolution tree search for " + termName);

    // seed our queue of resolvers to visit
    // need to be aware of null parent for root ToVisit
    toVisits.add(new ToVisit(0, destination, null));

    // there may not be a viable plan
    boolean plannedToDestination = false;

    // We'll do a modified Dijkstra's shortest path algorithm, where at each leaf we see if we've planned out
    // termName resolution all the way up to the root, our destination.  If so, we just reconstruct our plan.
    while (!plannedToDestination && toVisits.size() > 0) {
        // visit least cost node remaining
        ToVisit visiting = toVisits.poll();

        LOG.debug("visiting " + visiting.getTermResolverKey());

        // the resolver is the edge in our tree -- we don't get it directly from the termResolversByKey Map, because it could be our destination
        TermResolver resolver = getResolver(visiting.getTermResolverKey(), destination, destinationKey);
        TermResolver parent = getResolver(visiting.getParentKey(), destination, destinationKey);

        if (visitedByKey.containsKey(visiting.getTermResolverKey())) {
            continue; // We've already visited this one
        }

        Visited parentVisited = visitedByKey.get(visiting.getParentKey());

        if (resolver == null)
            throw new RuntimeException("Unable to get TermResolver by its key");
        Set<String> prereqs = resolver.getPrerequisites();
        // keep track of any prereqs that we already have handy
        List<String> metPrereqs = new LinkedList<String>();

        // see what prereqs we have already, and which we'll need to visit
        if (prereqs != null)
            for (String prereq : prereqs) {
                if (!termCache.containsKey(new Term(prereq, null))) {
                    // enqueue all resolvers in toVisits
                    List<TermResolver<?>> prereqResolvers = termResolversByOutput.get(prereq);
                    if (prereqResolvers != null)
                        for (TermResolver prereqResolver : prereqResolvers) {
                            // Only TermResolvers that don't take paramaterized terms can be chained, so:
                            // if the TermResolver doesn't take parameters, or it resolves the output termName
                            if (CollectionUtils.isEmpty(prereqResolver.getParameterNames())
                                    || termName.equals(prereqResolver.getOutput())) {
                                // queue it up for visiting
                                toVisits.add(new ToVisit(visiting.getCost() /* cost to get to this resolver */,
                                        prereqResolver, resolver));
                            }
                        }
                } else {
                    metPrereqs.add(prereq);
                }
            }

        // Build visited info
        Visited visited = buildVisited(resolver, parentVisited, metPrereqs);
        visitedByKey.put(visited.getResolverKey(), visited);

        plannedToDestination = isPlannedBackToDestination(visited, destinationKey, visitedByKey);
    }

    if (plannedToDestination) {
        // build result from Visited tree.
        resolutionPlan = new LinkedList<TermResolverKey>();

        assembleLinearResolutionPlan(visitedByKey.get(destinationKey), visitedByKey, resolutionPlan);
    }
    return resolutionPlan;
}

From source file:$.HyperGraphBuilder$.java

@Override
    public V_GenericGraph makeGraphResponse(final V_GraphQuery graphQuery) throws Exception {
        nodeList = new HashMap<String, V_GenericNode>();
        // edgeMap = new HashMap<String, V_GenericEdge>();
        edgeList = new HashMap<String, V_GenericEdge>();
        scannedQueries = new HashSet<String>();

        final PriorityQueue<G_EntityQuery> queriesToRun = new PriorityQueue<G_EntityQuery>(10,
                new ScoreComparator());
        Map<String, V_GenericNode> nodesFromPreviousDegree = new HashMap<String, V_GenericNode>();
        Map<String, V_GenericEdge> edgesFromPreviousDegree = new HashMap<String, V_GenericEdge>();

        if (graphQuery.getMaxHops() <= 0) {
            return new V_GenericGraph();
        } else {//  w  ww  .j a v  a 2  s  .co  m
            logger.debug("Attempting a graph for query " + graphQuery.toString());
        }

        int intStatus = 0;
        String strStatus = "Graph Loaded";

        final G_PropertyMatchDescriptor identifierList = G_PropertyMatchDescriptor.newBuilder().setKey("_all")
                .setListRange(new ListRangeHelper(G_PropertyType.STRING, graphQuery.getSearchIds()))
                .setConstraint(G_Constraint.EQUALS).build();
        final QueryHelper qh = new QueryHelper(identifierList);
        qh.setTargetSchema(index);
        queriesToRun.add(qh);

        int currentDegree = 0;
        for (currentDegree = 0; (currentDegree < graphQuery.getMaxHops())
                && (nodeList.size() < graphQuery.getMaxNodes()); currentDegree++) {
            G_EntityQuery eq = null;
            logger.debug("${symbol_dollar}${symbol_dollar}${symbol_dollar}${symbol_dollar}There are "
                    + queriesToRun.size() + " queries to run in the current degree.");
            while ((queriesToRun.size() > 0) && ((eq = queriesToRun.poll()) != null)
                    && (nodeList.size() < graphQuery.getMaxNodes())) {

                if (ValidationUtils.isValid(eq.getPropertyMatchDescriptors())) {
                    nodesFromPreviousDegree = new HashMap<String, V_GenericNode>(nodeList);
                    edgesFromPreviousDegree = new HashMap<String, V_GenericEdge>(edgeList);
                    logger.debug("Processing degree " + currentDegree);

                    /**
                     * This will end up building nodes and edges, and creating
                     * new queries for the queue
                     */
                    logger.debug("1111=====Running query " + eq.toString());
                    getDAO().performCallback(0, eq.getMaxResult(), this, eq);
                    logger.debug("3333====After running " + eq.toString() + ", there are "
                            + queriesToRunNextDegree.size() + " queries to run in the next degree.");
                }
            } // end while loop

            // very important!!
            // unscannedNodeList.clear();
            // ////////////////////////////////////////////////
            logger.debug("4444==== At the end of degree " + currentDegree + ", there are " + nodeList.size()
                    + " nodes and " + edgeList.size() + " edges");

            logger.debug(
                    "5555====There are " + queriesToRunNextDegree.size() + " queries to run in the next degree.");
            queriesToRun.addAll(queriesToRunNextDegree);
            queriesToRunNextDegree.clear();
        }

        // All hops have been done
        // Check to see if we have too many nodes.
        if (nodeList.size() > graphQuery.getMaxNodes()) {
            nodeList = nodesFromPreviousDegree;
            edgeList = edgesFromPreviousDegree;
            intStatus = 1; // will trigger the message.
            strStatus = "Returning only " + currentDegree
                    + " hops, as maximum nodes you requested would be exceeded";
        } else {
            intStatus = 1; // will trigger the message.
            strStatus = "Returning " + nodeList.size() + " nodes and " + edgeList.size() + " edges.";
        }

        // NOW finally add in all those unique edges.

        performPostProcess(graphQuery);
        final V_GenericGraph g = new V_GenericGraph(nodeList, edgeList);
        g.setIntStatus(intStatus);
        g.setStrStatus(strStatus);
        logger.debug("Graph status: " + g.getStrStatus());
        for (final V_LegendItem li : legendItems) {
            g.addLegendItem(li);
        }

        return g;
    }

From source file:sg.atom.utils._commons.lang.metadata.ScoringClassMapBuilder.java

public ClassMapBuilder<A, B> byDefault(MappingDirection direction, DefaultFieldMapper... withDefaults) {

    DefaultFieldMapper[] defaults;//from   w  ww  .jav  a 2s.  com
    if (withDefaults.length == 0) {
        defaults = getDefaultFieldMappers();
    } else {
        defaults = withDefaults;
    }
    /*
     * For our custom 'byDefault' method, we're going to try and match
     * fields by their Levenshtein distance
     */
    PriorityQueue<FieldMatchScore> matchScores = new PriorityQueue<FieldMatchScore>();

    Map<String, Property> propertiesForA = getPropertyExpressions(getAType());
    Map<String, Property> propertiesForB = getPropertyExpressions(getBType());

    for (final Entry<String, Property> propertyA : propertiesForA.entrySet()) {
        if (!propertyA.getValue().getName().equals("class")) {
            for (final Entry<String, Property> propertyB : propertiesForB.entrySet()) {
                if (!propertyB.getValue().getName().equals("class")) {
                    FieldMatchScore matchScore = new FieldMatchScore(propertyA.getValue(), propertyB.getValue(),
                            matchingWeights);
                    matchScores.add(matchScore);
                }
            }
        }
    }

    Set<String> unmatchedFields = new LinkedHashSet<String>(this.getPropertiesForTypeA());
    unmatchedFields.remove("class");

    for (FieldMatchScore score : matchScores) {

        if (!this.getMappedPropertiesForTypeA().contains(score.propertyA.getExpression())
                && !this.getMappedPropertiesForTypeB().contains(score.propertyB.getExpression())) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("\n" + score.toString());
            }
            if (score.meetsMinimumScore()) {
                fieldMap(score.propertyA.getExpression(), score.propertyB.getExpression()).direction(direction)
                        .add();
                unmatchedFields.remove(score.propertyA.getExpression());
            }
        }
    }

    /*
     * Apply any default field mappers to the unmapped fields
     */
    for (String propertyNameA : unmatchedFields) {
        Property prop = resolvePropertyForA(propertyNameA);
        for (DefaultFieldMapper defaulter : defaults) {
            String suggestion = defaulter.suggestMappedField(propertyNameA, prop.getType());
            if (suggestion != null && getPropertiesForTypeB().contains(suggestion)) {
                if (!getMappedPropertiesForTypeB().contains(suggestion)) {
                    fieldMap(propertyNameA, suggestion).direction(direction).add();
                }
            }
        }
    }

    return this;
}

From source file:edu.snu.leader.hidden.SpatialIndividual.java

/**
 * Finds the nearest neighbors for this individual
 *
 * @param simState/*  w  w w . j a v a  2 s  . c o  m*/
 */
public void findNearestNeighbors(SimulationState simState) {
    _LOG.trace("Entering findNearestNeighbors( simState )");

    // Get the number of nearest neighbors
    _nearestNeighborCount = simState.getNearestNeighborCount();

    // Build a priority queue to sort things for us
    PriorityQueue<Neighbor> sortedNeighbors = new PriorityQueue<Neighbor>();

    // Iterate through all the individuals
    Iterator<SpatialIndividual> indIter = simState.getAllIndividuals().iterator();
    while (indIter.hasNext()) {
        // Get the individual
        SpatialIndividual ind = indIter.next();

        // If it is us, continue on
        if (_id.equals(ind._id)) {
            continue;
        }

        // Build a neighbor out of it and put it in the queue
        Neighbor neighbor = new Neighbor((float) _location.distance(ind._location), ind);
        sortedNeighbors.add(neighbor);
    }

    // Get the "nearest" neighbors
    int count = Math.min(sortedNeighbors.size(), _nearestNeighborCount);
    for (int i = 0; i < count; i++) {
        Neighbor neighbor = sortedNeighbors.poll();
        _nearestNeighbors.add(neighbor);
        neighbor.getIndividual().signalNearestNeighborStatus(this);
        //            _LOG.debug( "Nearest neighbor: id=["
        //                    + getID()
        //                    + "] neighbor=["
        //                    + neighbor.getIndividual().getID()
        //                    + "]" );
    }

    _LOG.trace("Leaving findNearestNeighbors( simState )");
}