aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/test/java/com/yahoo/searchlib/rankingexpression/evaluation/gbdtoptimization/ContextReuseTestCase.java
blob: 3d5710b81e00a0ca381a85f6e0cb20b28c7c6559 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.evaluation.gbdtoptimization;

import com.yahoo.io.IOUtils;
import com.yahoo.searchlib.rankingexpression.RankingExpression;
import com.yahoo.searchlib.rankingexpression.evaluation.ArrayContext;
import com.yahoo.searchlib.rankingexpression.evaluation.ExpressionOptimizer;
import com.yahoo.searchlib.rankingexpression.parser.ParseException;

import java.io.File;
import java.io.IOException;

/**
 * This tests reuse of a optimized context which is not initialized with
 * all values referenced in the expression.
 *
 * @author bratseth
 */
public class ContextReuseTestCase extends junit.framework.TestCase  {

    private String contextString=
            "CONCEPTTYPE = 0.0\n" +
            "REGEXTYPE = 0.0\n" +
            "POS_18 = 0.0\n" +
            "POS_19 = 0.0\n" +
            "ORDER_IN_CLUSTER = 2.0\n" +
            "GOOD_SYNTAX = 1.0\n" +
            "POS_20 = 0.0\n" +
            "POS_11 = 0.0\n" +
            "POS_10 = 0.0\n" +
            "CHUNKTYPE = 0.0\n" +
            "POS_13 = 0.0\n" +
            "STOP_WORD_1 = 0.0\n" +
            "TERM_CASE_2 = 0.0\n" +
            "TERM_CASE_3 = 0.0\n" +
            "STOP_WORD_3 = 0.0\n" +
            "POS_15 = 0.0\n" +
            "TERM_CASE_1 = 0.0\n" +
            "STOP_WORD_2 = 0.0\n" +
            "POS_1 = 0.0\n" +
            "TERM_CASE_4 = 1.0\n" +
            "LENGTH = 6.0\n" +
            "EXTENDEDTYPE = 0.0\n" +
            "ENTITYPLACETYPE = 0.0\n";

    public void testIt() throws ParseException, IOException {
        // Prepare
        RankingExpression expression=new RankingExpression(IOUtils.readFile(new File("src/test/files/s-expression.vre")));
        ArrayContext contextPrototype=new ArrayContext(expression);
        new ExpressionOptimizer().optimize(expression,contextPrototype);

        // Execute
        ArrayContext context=contextPrototype.clone();
        for (String contextValueString : contextString.split("\n")) {
            String[] contextValueParts = contextValueString.split("=");
            context.put(contextValueParts[0].trim(), Double.valueOf(contextValueParts[1].trim()));
        }
        assertEquals(-2.3450294999999994, expression.evaluate(context).asDouble());
    }

}