aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/rule/ReplacingProductionRule.java
blob: db0b9b8b004f1160ad6a5174b9f2fb5bc883cb79 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.semantics.rule;

import com.yahoo.prelude.semantics.engine.Match;
import com.yahoo.prelude.semantics.engine.RuleEvaluation;

/**
 * A production rule which <i>replaces</i> matched terms by the production
 *
 * @author bratseth
 */
public class ReplacingProductionRule extends ProductionRule {

    /** Carries out the production of this rule */
    public void produce(RuleEvaluation e) {
        removeNonreferencedMatches(e);
        if (e.getTraceLevel() >= 5) {
            e.trace(5,"Removed terms to get '" + e.getEvaluation().getQuery().getModel().getQueryTree().getRoot() + "', will add terms");
        }
        super.produce(e);
    }

    /** Remove items until there's only one item left */
    private void removeNonreferencedMatches(RuleEvaluation e) {
        int itemCount = e.getEvaluation().getQuerySize();

        // Remove items backwards to ease index handling
        for (int i = e.getNonreferencedMatchCount() - 1; i >= 0; i--) {
            // Ensure we don't produce an empty query
            if (getProduction().getTermCount() == 0 && itemCount == 1)
                break;
            itemCount--;

            Match match = e.getNonreferencedMatch(i);
            match.getItem().getParent().removeItem(match.getPosition());
        }
    }

    protected String getSymbol() { return "->"; }

}