aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/test/java/com/yahoo/searchlib/treenet/TreeNetParserTestCase.java
blob: 166b19955bd944b319f3a864960a3199a16bb688 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.treenet;

import com.yahoo.searchlib.rankingexpression.RankingExpression;
import com.yahoo.searchlib.treenet.parser.ParseException;
import com.yahoo.searchlib.treenet.parser.TreeNetParser;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;

import static org.junit.Assert.assertEquals;

/**
 * @author Simon Thoresen Hult
 */
public class TreeNetParserTestCase {

    private static final boolean WRITE_FILES = false;

    @Test
    public void testRankingExpression() {
        for (int i = 1; i <= 8; ++i) {
            String inputFile  = String.format("src/test/files/treenet%02d.model", i);
            String outputFile = String.format("src/test/files/ranking%02d.expression", i);
            String input = readFile(inputFile);
            String expression = convertModel(inputFile, input);
            if (WRITE_FILES) {
                writeFile(outputFile, expression);
            }
            else {
                String output = readFile(outputFile);
                assertParseable(output, outputFile);
                assertEquals(output.trim(), expression);
            }
        }
    }

    private void assertParseable(String rankingExpressionString,String fileName) {
        try {
            new RankingExpression(rankingExpressionString);
        }
        catch (com.yahoo.searchlib.rankingexpression.parser.ParseException e) {
            throw new RuntimeException("Could not parse ranking expression in '" + fileName + "'",e);
        }
    }

    private String convertModel(String modelFile, String model) {
        try {
            TreeNetParser parser = new TreeNetParser(new StringReader(model));
            return parser.treeNet().toRankingExpression();
        } catch (ParseException e) {
            throw new AssertionError("In model " + modelFile + ": " + e.getMessage(), e);
        }
    }

    private String readFile(String file) {
        try {
            StringBuilder ret = new StringBuilder();
            BufferedReader in = new BufferedReader(new FileReader(file));
            while (true) {
                String str = in.readLine();
                if (str == null) {
                    break;
                }
                ret.append(str).append("\n");
            }
            return ret.toString();
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    }

    private void writeFile(String file, String content) {
        try {
            FileWriter out = new FileWriter(file);
            out.write(content);
            out.close();
        } catch (IOException e) {
            throw new AssertionError(e);
        }
    }

}