aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/TreeNet.java
blob: d5b9dc171e8e4ac6c4a78f245373cff67431b035 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.treenet.rule;

import java.util.Map;

/**
 * @author Simon Thoresen Hult
 */
public class TreeNet {

    // The id of the first tree to run in this net.
    private String begin;

    // All named trees of this net.
    private final Map<String, Tree> trees;

    /**
     * Constructs a new tree net.
     *
     * @param begin The id of the first tree to run in this net.
     * @param trees All named trees of this net.
     */
    public TreeNet(String begin, Map<String, Tree> trees) {
        this.begin = begin;
        this.trees = trees;
        for (Tree tree : this.trees.values()) {
            tree.setParent(this);
        }
    }

    /**
     * Returns the id of the first tree to run in this net.
     */
    public String getBegin() {
        return begin;
    }

    /**
     * Returns all named trees of this net.
     */
    public Map<String, Tree> getTrees() {
        return trees;
    }

    /**
     * Returns a ranking expression equivalent of this net.
     */
    public String toRankingExpression() {
        StringBuilder ret = new StringBuilder();
        String next = begin;
        while (next != null) {
            Tree tree = trees.get(next);
            if (tree.getBegin() != null) {
                if (ret.length() > 0) {
                    ret.append(" + \n");
                }
                ret.append(tree.toRankingExpression());
            }
            next = tree.getNext();
        }
        return ret.toString();
    }
}