summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/rule/ReferenceTermProduction.java
blob: 319e1969174af9e3761f89f5b78afbe8da839d6d (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2016 Yahoo Inc. 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.query.Item;
import com.yahoo.prelude.query.TermType;
import com.yahoo.prelude.semantics.engine.EvaluationException;
import com.yahoo.prelude.semantics.engine.Match;
import com.yahoo.prelude.semantics.engine.ReferencedMatches;
import com.yahoo.prelude.semantics.engine.RuleEvaluation;
import com.yahoo.protect.Validator;

/**
 * A term produced by a production rule which takes it's actual term value
 * from one or more terms matched in the condition
 *
 * @author <a href="mailto:bratseth@yahoo-inc.com">Jon S Bratseth</a>
 */
public class ReferenceTermProduction extends TermProduction {

    private String reference;

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

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

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

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

    /** The label of the condition this should take its value from, never null */
    public void setReference(String reference) {
        Validator.ensureNotNull("reference name of a produced reference term",reference);
        this.reference =reference;
    }

    /** Returns the label of the condition this should take its value from, never null */
    public String getReference() { return reference; }

    void addMatchReferences(Set<String> matchReferences) {
        matchReferences.add(reference);
    }

    public void produce(RuleEvaluation e,int offset) {
        ReferencedMatches referencedMatches=e.getReferencedMatches(reference);
        if (referencedMatches==null)
            throw new EvaluationException("Referred match '" + reference + "' not found");
        if (replacing) {
            replaceMatches(e,referencedMatches);
        }
        else {
            addMatches(e,referencedMatches);
        }
    }

    public void replaceMatches(RuleEvaluation e,ReferencedMatches referencedMatches) {
        Item referencedItem=referencedMatches.toItem(getLabel());
        if (referencedItem==null) return;
        e.removeMatches(referencedMatches);
        insertMatch(e, referencedMatches.matchIterator().next(),referencedItem,0);
    }

    private void addMatches(RuleEvaluation e,ReferencedMatches referencedMatches) {
        Item referencedItem=referencedMatches.toItem(getLabel());
        if (referencedItem==null) return;
        e.addItem(referencedItem,getTermType());
    }

    public String toInnerTermString() {
        return getLabelString() + "[" + reference + "]";
    }

}