summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionWithTensorTestCase.java
blob: 48f2bf43e81560d4038c9b2492d3c26e2f0f1863 (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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.processing;

import com.yahoo.searchdefinition.parser.ParseException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
 * @author geirst
 */
public class RankingExpressionWithTensorTestCase {

    @Test
    public void requireThatSingleLineConstantTensorAndTypeCanBeParsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1,y:2}:1, {x:2,y:1}:2 }\n" +
                "        type: tensor(x{},y{})\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1,y:2}:1.0,{x:2,y:1}:2.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{},y{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatMultiLineConstantTensorAndTypeCanBeParsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value {\n" +
                "          { {x:1,y:2}:1,\n" +
                "            {x:2,y:1}:2 }\n" +
                "        }\n" +
                "        type: tensor(x{},y{})\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1,y:2}:1.0,{x:2,y:1}:2.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{},y{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatConstantTensorsCanBeUsedInSecondPhaseExpression() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    second-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.compileRankProfile("my_profile");
        f.assertSecondPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatConstantTensorsCanBeUsedInInheritedRankProfile() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile parent {\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "  rank-profile my_profile inherits parent {\n" +
                "    first-phase {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("reduce(constant(my_tensor), sum)", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatConstantTensorsCanBeUsedInFunction() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    function my_macro() {\n" +
                "      expression: sum(my_tensor)\n" +
                "    }\n" +
                "    first-phase {\n" +
                "      expression: 5.0 + my_macro\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("5.0 + my_macro", "my_profile");
        f.assertFunction("reduce(constant(my_tensor), sum)", "my_macro", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Test
    public void requireThatCombinationOfConstantTensorsAndConstantValuesCanBeUsed() throws ParseException {
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    first-phase {\n" +
                "      expression: my_number_1 + sum(my_tensor) + my_number_2\n" +
                "    }\n" +
                "    constants {\n" +
                "      my_number_1: 3.0\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "      }\n" +
                "      my_number_2: 5.0\n" +
                "    }\n" +
                "  }");
        f.compileRankProfile("my_profile");
        f.assertFirstPhaseExpression("3.0 + reduce(constant(my_tensor), sum) + 5.0", "my_profile");
        f.assertRankProperty("{{x:1}:1.0}", "constant(my_tensor).value", "my_profile");
        f.assertRankProperty("tensor(x{})", "constant(my_tensor).type", "my_profile");
    }

    @Rule
    public ExpectedException exception = ExpectedException.none();

    @Test
    public void requireThatInvalidTensorTypeSpecThrowsException() throws ParseException {
        exception.expect(IllegalArgumentException.class);
        exception.expectMessage("For constant tensor 'my_tensor' in rank profile 'my_profile': Illegal tensor type spec: Failed parsing element 'x' in type spec 'tensor(x)'");
        RankProfileSearchFixture f = new RankProfileSearchFixture(
                "  rank-profile my_profile {\n" +
                "    constants {\n" +
                "      my_tensor {\n" +
                "        value: { {x:1}:1 }\n" +
                "        type: tensor(x)\n" +
                "      }\n" +
                "    }\n" +
                "  }");
        f.compileRankProfile("my_profile");
    }

}