summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/rule/LiteralTermProduction.java
blob: 69a4a87c04ecff6629199ae1872fdb2fc40b1a4f (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2017 Yahoo Holdings. 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.query.TermType;
import com.yahoo.prelude.query.WordItem;
import com.yahoo.prelude.semantics.engine.Match;
import com.yahoo.prelude.semantics.engine.RuleEvaluation;
import com.yahoo.protect.Validator;

/**
 * A literal term produced by a production rule
 *
 * @author bratseth
 */
public class LiteralTermProduction extends TermProduction {

    private String literal;

    /**
     * Creates a new produced literal term
     *
     * @param literal the label of the condition this should take it's value from
     */
    public LiteralTermProduction(String literal) {
        super();
        setLiteral(literal);
    }

    /**
     * Creates a new produced literal term
     *
     * @param literal the label of the condition this should take it's value from
     * @param termType the type of term to produce
     */
    public LiteralTermProduction(String literal, TermType termType) {
        super(termType);
        setLiteral(literal);
    }

    /**
     * Creates a new produced literal term
     *
     * @param label the label of the produced term
     * @param literal this term word
     * @param termType the type of term to produce
     */
    public LiteralTermProduction(String label,String literal, TermType termType) {
        super(label,termType);
        setLiteral(literal);
    }

    /** The literal term value, never null */
    public void setLiteral(String literal) {
        Validator.ensureNotNull("A produced term",literal);
        this.literal=literal;
    }

    /** Returns the term word produced, never null */
    public String getLiteral() { return literal; }

    public void produce(RuleEvaluation e,int offset) {
        WordItem newItem=new WordItem(literal,getLabel());
        if (replacing) {
            Match matched=e.getNonreferencedMatch(0);
            insertMatch(e,matched,newItem,offset);
        }
        else {
            newItem.setWeight(getWeight());
            if (e.getTraceLevel()>=6)
                e.trace(6,"Adding '" + newItem + "'");
            e.addItem(newItem,getTermType());
        }
    }

    public String toInnerTermString() {
        return getLabelString() + literal;
    }

}