aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/semantics/rule/ProductionList.java
blob: 5d20075cd97bf74a8cbe576b5bf59c3b9feec297 (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
// 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 java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

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

/**
 * A list of the productions of a rule
 *
 * @author bratseth
 */
public class ProductionList {

    private final List<Production> productions = new java.util.ArrayList<>();

    /** True to replace by the production, false to add it */
    private boolean replacing = true;

    public void addProduction(Production term) {
        term.setReplacing(replacing);
        term.setPosition(productions.size());
        productions.add(term);
    }

    /** True to replace, false to add, default true */
    void setReplacing(boolean replacing) {
        for (Iterator<Production> i = productions.iterator(); i.hasNext(); ) {
            Production production = i.next();
            production.setReplacing(replacing);
        }

        this.replacing = replacing;
    }

    /** Returns an unmodifiable view of the productions in this */
    public List<Production> productionList() { return Collections.unmodifiableList(productions); }

    public int getTermCount() { return productions.size(); }

    void addMatchReferences(Set<String> matchReferences) {
        for (Iterator<Production> i = productions.iterator(); i.hasNext(); ) {
            Production term = i.next();
            term.addMatchReferences(matchReferences);
        }
    }

    public void produce(RuleEvaluation e) {
        for (int i = 0; i < productions.size(); i++) {
            productions.get(i).produce(e, i);
        }
    }

    @Override
    public String toString() {
        StringBuilder buffer = new StringBuilder();
        for (Iterator<Production> i = productions.iterator(); i.hasNext(); ) {
            buffer.append(i.next());
            if (i.hasNext())
                buffer.append(" ");
        }
        return buffer.toString();
    }

}