aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/rule/TermCondition.java
blob: d4e77944efb50ec1682f2228840eba859f1bee0e (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright Vespa.ai. 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.NameSpace;
import com.yahoo.prelude.semantics.engine.RuleBaseLinguistics;
import com.yahoo.prelude.semantics.engine.RuleEvaluation;

/**
 * A term in a rule
 *
 * @author bratseth
 */
public class TermCondition extends Condition {

    private final RuleBaseLinguistics linguistics;
    private final String originalTerm;
    private final String term;

    public TermCondition(String term, RuleBaseLinguistics linguistics) {
        this(null, term, linguistics);
    }

    public TermCondition(String label, String term, RuleBaseLinguistics linguistics) {
        super(label);
        this.linguistics = linguistics;
        this.originalTerm = term;
        this.term = linguistics.process(term);
    }

    protected boolean doesMatch(RuleEvaluation e) {
        // TODO: Move this into the respective namespaces when query becomes one */
        if (getNameSpace() != null) {
            NameSpace nameSpace = e.getEvaluation().getNameSpace(getNameSpace());
            return nameSpace.matches(originalTerm, e); // No processing of terms in namespaces
        }
        else {
            if (e.currentItem() == null) return false;
            if ( ! labelMatches(e)) return false;

            boolean matches = labelMatches(e.currentItem().getItem(), e) &&
                              linguistics.process(e.currentItem().getItem().stringValue()).equals(term);
            if ((matches && !e.isInNegation() || (!matches && e.isInNegation()))) {
                e.addMatch(e.currentItem(), originalTerm);
                e.setValue(term);
                e.next();
            }
            return matches;
        }
    }

    public String term() { return term; }

    @Override
    public String toInnerString() {
        return getLabelString() + term;
    }

}