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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.rule;
import com.yahoo.searchlib.rankingexpression.Reference;
import com.yahoo.searchlib.rankingexpression.evaluation.BooleanValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.TypeContext;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
import java.util.Objects;
import java.util.function.Predicate;
/**
* A node which returns true or false depending on a set membership test
*
* @author bratseth
*/
public class SetMembershipNode extends BooleanNode {
private final ExpressionNode testValue;
private final List<ExpressionNode> setValues;
public SetMembershipNode(ExpressionNode testValue, List<ExpressionNode> setValues) {
this.testValue = testValue;
this.setValues = List.copyOf(setValues);
}
/** The value to check for membership in the set */
public ExpressionNode getTestValue() { return testValue; }
/** Returns an immutable list of the values of the set */
public List<ExpressionNode> getSetValues() { return setValues; }
@Override
public List<ExpressionNode> children() {
ArrayList<ExpressionNode> children = new ArrayList<>();
children.add(testValue);
children.addAll(setValues);
return children;
}
@Override
public StringBuilder toString(StringBuilder string, SerializationContext context, Deque<String> path, CompositeNode parent) {
testValue.toString(string, context, path, this);
string.append(" in [");
for (int i = 0, len = setValues.size(); i < len; ++i) {
setValues.get(i).toString(string, context, path, this);
if (i < len - 1) {
string.append(", ");
}
}
string.append("]");
return string;
}
@Override
public TensorType type(TypeContext<Reference> context) {
return TensorType.empty;
}
@Override
public Value evaluate(Context context) {
Value value = testValue.evaluate(context);
if (value instanceof TensorValue) {
return evaluateTensor(((TensorValue) value).asTensor(), context);
}
return evaluateValue(value, context);
}
private Value evaluateValue(Value value, Context context) {
return new BooleanValue(testMembership(value::equals, context));
}
private Value evaluateTensor(Tensor tensor, Context context) {
return new TensorValue(tensor.map((value) -> contains(value, context) ? 1.0 : 0.0));
}
private boolean contains(double value, Context context) {
return testMembership((setValue) -> setValue.asDouble() == value, context);
}
private boolean testMembership(Predicate<Value> test, Context context) {
for (ExpressionNode setValue : setValues) {
if (test.test(setValue.evaluate(context)))
return true;
}
return false;
}
@Override
public SetMembershipNode setChildren(List<ExpressionNode> children) {
if (children.size()<1) throw new IllegalArgumentException("A set membership test must have at least 1 child");
return new SetMembershipNode(children.get(0), children.subList(1, children.size()));
}
@Override
public int hashCode() { return Objects.hash("setMembership", testValue, setValues); }
}
|