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.searchlib.treenet.rule;
import java.util.Map;
/**
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public class Tree {
private final String name;
// The parent tree net of this.
private TreeNet parent;
// Returns the id of the next tree to run after this.
private String next;
// The initial response value of this tree, may be null.
private final Double value;
// The id of the first condition or response to run in this tree.
private final String begin;
// All named nodes of this tree.
private final Map<String, TreeNode> nodes;
/**
* Constructs a new tree.
*
* @param name The name of this tree, used for error outputs.
* @param value The initial response value of this tree, may be null.
* @param begin The id of the first condition or response to run in this tree.
* @param nodes All named nodes of this tree.
*/
public Tree(String name, Double value, String begin, Map<String, TreeNode> nodes) {
this.name = name;
this.value = value;
this.begin = begin;
this.nodes = nodes;
this.next = null;
for (TreeNode node : this.nodes.values()) {
node.setParent(this);
if (node instanceof Response) {
String next = ((Response)node).getNext();
if (this.next == null) {
this.next = next;
} else if (!this.next.equals(next)) {
throw new IllegalStateException("Not all child nodes of tree '" + name + "' agree on the next " +
"tree to run. Initial name was '" + this.next + "', conflicting " +
"name is '" + next + "'.");
}
}
}
}
public String getName() { return name; }
/**
* Returns the parent tree net of this.
*/
public TreeNet getParent() { return parent; }
/**
* Sets the parent tree net of this.
*
* @param parent The parent tree net.
* @return This, to allow chaining.
*/
public Tree setParent(TreeNet parent) {
this.parent = parent;
return this;
}
/**
* Returns the id of the next tree to run after this.
*/
public String getNext() {
return next;
}
/**
* Returns the initial response value of this tree, may be null.
*/
public Double getValue() {
return value;
}
/**
* Returns the id of the first condition or response to run in this tree.
*/
public String getBegin() {
return begin;
}
/**
* Returns all named nodes of this tree.
*/
public Map<String, TreeNode> getNodes() {
return nodes;
}
/**
* Returns a ranking expression equivalent of this tree.
*/
public String toRankingExpression() {
return nodes.get(begin).toRankingExpression();
}
}
|