aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRules.java
blob: fdc93f22367ee2ea318aa2a030c79f5f98d3a1b1 (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
// Copyright Vespa.ai. 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 com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.semantics.RuleImporter;
import com.yahoo.prelude.semantics.SemanticRulesConfig;
import com.yahoo.prelude.semantics.parser.ParseException;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Returns the semantic rules config from a set of rule bases.
 * Owned by a container cluster
 *
 * @author bratseth
 */
public class SemanticRules implements Serializable, SemanticRulesConfig.Producer {

    private final List<RuleBase> ruleBases;

    public SemanticRules(List<RuleBase> ruleBases) {
        this.ruleBases = ruleBases;
    }

    @Override
    public void getConfig(SemanticRulesConfig.Builder builder) {
        for (RuleBase ruleBase : ruleBases)
            builder.rulebase(ruleBase.getConfig());
    }

    /** A config view of a rule base */
    public static class RuleBase {

        private final String name;
        private final boolean isDefault;
        private final String rules;

        public RuleBase(String name, boolean isDefault, String rules) {
            this.name = name;
            this.isDefault = isDefault;
            this.rules = rules;
        }

        private SemanticRulesConfig.Rulebase.Builder getConfig() {
            SemanticRulesConfig.Rulebase.Builder ruleBaseBuilder = new SemanticRulesConfig.Rulebase.Builder();
            ruleBaseBuilder.name(name);
            ruleBaseBuilder.isdefault(isDefault);
            ruleBaseBuilder.rules(rules);
            return ruleBaseBuilder;
        }

    }

    public static class SemanticRuleBuilder {

        /** Builds the semantic rules for an application package and validates them */
        public SemanticRules build(ApplicationPackage applicationPackage) {
            var ruleFiles = applicationPackage.getFiles(ApplicationPackage.RULES_DIR, "sr");
            var rules = new SemanticRules(ruleFiles.stream().map(this::toRuleBaseConfigView).toList());

            // Create config to make sure rules are valid, config is validated in call to toMap() below
            var builder = new SemanticRulesConfig.Builder();
            rules.getConfig(builder);
            SemanticRulesConfig config = builder.build();
            try {
                toMap(config);  // validates config
                ensureZeroOrOneDefaultRule(config);
            } catch (ParseException | IOException e) {
                throw new RuntimeException(e);
            }
            return rules;
        }

        private RuleBase toRuleBaseConfigView(NamedReader reader) {
            try {
                String ruleBaseString = IOUtils.readAll(reader.getReader());
                boolean isDefault = ruleBaseString.contains("@default");
                return new 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());
        }

        private void ensureZeroOrOneDefaultRule(SemanticRulesConfig config) {
            String defaultName = null;
            for (SemanticRulesConfig.Rulebase ruleBase : config.rulebase()) {
                if (defaultName != null && ruleBase.isdefault()) {
                    List<String> defaultRules = new ArrayList<>(List.of(defaultName, ruleBase.name()));
                    defaultRules.sort(String::compareTo);
                    throw new IllegalArgumentException("Rules " + defaultRules + " are both marked as the default rule, there can only be one");
                }
                if (ruleBase.isdefault())
                    defaultName = ruleBase.name();
            }
        }

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

    }

}