aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/rule/Production.java
blob: d9f0cf7735efdc20059bd29d530cd7fa040ef383 (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
// 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 java.util.Set;

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

/**
 * A new term produced by a production rule
 *
 * @author bratseth
 */
public abstract class Production {

    /** True to add, false to replace, default true */
    protected boolean replacing = true;

    /** The (0-base) position of this term in the productions of this rule */
    private int position = 0;

    /** The weight (strength) of this production as a percentage (default is 100) */
    private int weight = 100;

    /** Creates a produced template term with no label and the default type */
    public Production() {
    }

    /** True to replace, false to add, if this production can do both. Default true. */
    public void setReplacing(boolean replacing) { this.replacing = replacing; }

    public int getPosition() { return position; }

    public void setPosition(int position) { this.position = position; }

    /** Sets the weight of this production as a percentage (default is 100) */
    public void setWeight(int weight) { this.weight = weight; }

    /** Returns the weight of this production as a percentage (default is 100) */
    public int getWeight() { return weight; }

    /**
     * Produces this at the current match
     *
     * @param e the evaluation context containing the current match and the query
     * @param offset the offset position at which to produce this. Offsets are used to produce multiple items
     *        at one position, inserted in the right order.
     */
    public abstract void produce(RuleEvaluation e, int offset);

    /**
     * Called to add the references into the condition of this rule made by this production
     * into the given set. The default implementation is void, override for productions
     * which refers to the condition
     */
    void addMatchReferences(Set<String> matchReferences) { }

    /** All instances of this produces a parseable string output */
    @Override
    public final String toString() {
        return toInnerString() + (getWeight()!=100 ? ("!" + getWeight()) : "");
    }

    /** All instances of this produces a parseable string output */
    protected abstract String toInnerString();

}