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
111
112
113
114
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.models.evaluation;
import com.yahoo.config.subscription.ConfigGetter;
import com.yahoo.config.subscription.FileSource;
import com.yahoo.filedistribution.fileacquirer.MockFileAcquirer;
import com.yahoo.path.Path;
import com.yahoo.searchlib.rankingexpression.ExpressionFunction;
import com.yahoo.searchlib.rankingexpression.RankingExpression;
import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.vespa.config.search.RankProfilesConfig;
import com.yahoo.vespa.config.search.core.RankingConstantsConfig;
import com.yahoo.yolean.Exceptions;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
/**
* @author bratseth
*/
public class ModelsEvaluatorTest {
private static final double delta = 0.00000000001;
@Test
public void testEvaluationDependingFunctionTakingArguments() {
ModelsEvaluator models = createModels("src/test/resources/config/rankexpression/");
FunctionEvaluator function = models.evaluatorOf("macros", "secondphase");
function.bind("match", 3);
function.bind("rankBoost", 5);
assertEquals(32.0, function.evaluate().asDouble(), delta);
}
@Test
public void testBindingValidation() {
List<ExpressionFunction> functions = new ArrayList<>();
ExpressionFunction function = new ExpressionFunction("test", RankingExpression.from("sum(arg1 * arg2)"));
function = function.withArgument("arg1", TensorType.fromSpec("tensor(d0[1])"));
function = function.withArgument("arg2", TensorType.fromSpec("tensor(d1{})"));
functions.add(function);
Model model = new Model("test-model", functions);
try { // No bindings
FunctionEvaluator evaluator = model.evaluatorOf("test");
evaluator.evaluate();
}
catch (IllegalStateException e) {
assertEquals("Missing argument 'arg2': Must be bound to a value of type tensor(d1{})",
Exceptions.toMessageString(e));
}
try { // Just one binding
FunctionEvaluator evaluator = model.evaluatorOf("test");
evaluator.bind("arg2", Tensor.from(TensorType.fromSpec("tensor(d1{})"), "{{d1:foo}:0.1}"));
evaluator.evaluate();
}
catch (IllegalStateException e) {
assertEquals("Missing argument 'arg1': Must be bound to a value of type tensor(d0[1])",
Exceptions.toMessageString(e));
}
try { // Wrong binding argument
FunctionEvaluator evaluator = model.evaluatorOf("test");
evaluator.bind("argNone", Tensor.from(TensorType.fromSpec("tensor(d1{})"), "{{d1:foo}:0.1}"));
evaluator.evaluate();
}
catch (IllegalArgumentException e) {
assertEquals("'argNone' is not a valid argument in function 'test'. Expected arguments: arg2: tensor(d1{}), arg1: tensor(d0[1])",
Exceptions.toMessageString(e));
}
try { // Wrong binding type
FunctionEvaluator evaluator = model.evaluatorOf("test");
evaluator.bind("arg1", Tensor.from(TensorType.fromSpec("tensor(d3{})"), "{{d3:foo}:0.1}"));
evaluator.evaluate();
}
catch (IllegalArgumentException e) {
assertEquals("'arg1' must be of type tensor(d0[1]), not tensor(d3{})",
Exceptions.toMessageString(e));
}
try { // Attempt to reuse evaluator
FunctionEvaluator evaluator = model.evaluatorOf("test");
evaluator.bind("arg1", Tensor.from(TensorType.fromSpec("tensor(d0[1])"), "{{d0:0}:0.1}"));
evaluator.bind("arg2", Tensor.from(TensorType.fromSpec("tensor(d1{})"), "{{d1:foo}:0.1}"));
evaluator.evaluate();
evaluator.bind("arg1", Tensor.from(TensorType.fromSpec("tensor(d0[1])"), "{{d0:0}:0.1}"));
}
catch (IllegalStateException e) {
assertEquals("Cannot bind a new value in a used evaluator",
Exceptions.toMessageString(e));
}
}
// TODO: Test argument-less function
// TODO: Test that rebinding doesn't work
// TODO: Test with nested functions
private ModelsEvaluator createModels(String path) {
Path configDir = Path.fromString(path);
RankProfilesConfig config = new ConfigGetter<>(new FileSource(configDir.append("rank-profiles.cfg").toFile()),
RankProfilesConfig.class).getConfig("");
RankingConstantsConfig constantsConfig = new ConfigGetter<>(new FileSource(configDir.append("ranking-constants.cfg").toFile()),
RankingConstantsConfig.class).getConfig("");
return new ModelsEvaluator(config, constantsConfig, MockFileAcquirer.returnFile(null));
}
}
|