summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java
blob: ce999603439600fa19abcdbaca58d38a38fd16dc (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
// 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.application.api.ApplicationPackage;
import com.yahoo.io.IOUtils;
import com.yahoo.io.reader.NamedReader;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Reads the semantic rules from the application package by delegating to SemanticRules.
 *
 * @author bratseth
 */
// TODO: Move into SemanticRules
public class SemanticRuleBuilder {

    /** Build the set of semantic rules for an application package */
    public SemanticRules build(ApplicationPackage applicationPackage) {
        List<NamedReader> ruleBaseFiles = null;
        try {
            ruleBaseFiles = applicationPackage.getFiles(ApplicationPackage.RULES_DIR, "sr");
            return new SemanticRules(ruleBaseFiles.stream().map(this::toRuleBaseConfigView).toList());
        }
        finally {
            NamedReader.closeAll(ruleBaseFiles);
        }
    }

    private SemanticRules.RuleBase toRuleBaseConfigView(NamedReader reader) {
        try {
            String ruleBaseString = IOUtils.readAll(reader.getReader());
            boolean isDefault = ruleBaseString.contains("@default");
            return new SemanticRules.RuleBase(toName(reader.getName()), isDefault, ruleBaseString);
        }
        catch (IOException e) {
            throw new IllegalArgumentException("Could not load rules bases", e);
        }
    }

    private String toName(String fileName) {
        String shortName = new File(fileName).getName();
        return shortName.substring(0, shortName.length()-".sr".length());
    }

}