summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java
blob: d9e2ae59ef6f9d41d3b30cce1dd693176091e1a6 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.search;

import com.yahoo.config.model.application.provider.FilesApplicationPackage;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.semantics.RuleBase;
import com.yahoo.prelude.semantics.RuleImporter;
import com.yahoo.prelude.semantics.SemanticRulesConfig;
import com.yahoo.prelude.semantics.parser.ParseException;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author bratseth
 */
public class SemanticRulesTest {

    private final static String root = "src/test/java/com/yahoo/vespa/model/container/search/semanticrules";

    @Test
    void semanticRulesTest()  throws ParseException, IOException {
        SemanticRuleBuilder ruleBuilder = new SemanticRuleBuilder();
        SemanticRules rules = ruleBuilder.build(FilesApplicationPackage.fromFile(new File(root)));
        SemanticRulesConfig.Builder configBuilder = new SemanticRulesConfig.Builder();
        rules.getConfig(configBuilder);
        SemanticRulesConfig config = new SemanticRulesConfig(configBuilder);
        Map<String, RuleBase> ruleBases = toMap(config);
        assertEquals(2, ruleBases.size());
        assertTrue(ruleBases.containsKey("common"));
        assertTrue(ruleBases.containsKey("other"));
        assertFalse(ruleBases.get("common").isDefault());
        assertTrue(ruleBases.get("other").isDefault());
    }

    private static Map<String, RuleBase> toMap(SemanticRulesConfig config) throws ParseException, IOException {
        RuleImporter ruleImporter = new RuleImporter(config, new SimpleLinguistics());
        Map<String, RuleBase> ruleBaseMap = new HashMap<>();
        for (SemanticRulesConfig.Rulebase ruleBaseConfig : config.rulebase()) {
            RuleBase ruleBase = ruleImporter.importConfig(ruleBaseConfig);
            if (ruleBaseConfig.isdefault())
                ruleBase.setDefault(true);
            ruleBaseMap.put(ruleBase.getName(), ruleBase);
        }
        return ruleBaseMap;
    }

}