diff options
author | Harald Musum <musum@yahooinc.com> | 2023-06-08 23:38:27 +0200 |
---|---|---|
committer | Harald Musum <musum@yahooinc.com> | 2023-06-08 23:38:27 +0200 |
commit | fa0044d92067bfe91a104d0a6bb6b085c0b9439e (patch) | |
tree | afed4f72ba4271b76ef8867c2917e21a54196bb8 /config-model/src/main | |
parent | e3bda63e89a8b75edba41ec589de5b7ce1c934ba (diff) |
Validate semantic rules when building config model
Diffstat (limited to 'config-model/src/main')
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java | 54 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRules.java | 5 |
2 files changed, 46 insertions, 13 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java b/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java index ce999603439..b184861c102 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java @@ -4,11 +4,16 @@ 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.RuleBase; +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.util.List; -import java.util.stream.Collectors; +import java.util.HashMap; +import java.util.Map; /** * Reads the semantic rules from the application package by delegating to SemanticRules. @@ -18,16 +23,22 @@ import java.util.stream.Collectors; // TODO: Move into SemanticRules public class SemanticRuleBuilder { - /** Build the set of semantic rules for an application package */ + /** Build the set of semantic rules for an application package and validates them */ public SemanticRules build(ApplicationPackage applicationPackage) { - List<NamedReader> ruleBaseFiles = null; + 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 { - ruleBaseFiles = applicationPackage.getFiles(ApplicationPackage.RULES_DIR, "sr"); - return new SemanticRules(ruleBaseFiles.stream().map(this::toRuleBaseConfigView).toList()); - } - finally { - NamedReader.closeAll(ruleBaseFiles); + toMap(config); // validates config + ensureZeroOrOneDefaultRule(config); + } catch (ParseException | IOException e) { + throw new RuntimeException(e); } + return rules; } private SemanticRules.RuleBase toRuleBaseConfigView(NamedReader reader) { @@ -43,7 +54,30 @@ public class SemanticRuleBuilder { private String toName(String fileName) { String shortName = new File(fileName).getName(); - return shortName.substring(0, shortName.length()-".sr".length()); + 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()) + throw new IllegalArgumentException("Both '" + defaultName + "' and '" + ruleBase.name() + + "' is marked as default rule, there can only be one"); + if (ruleBase.isdefault()) + defaultName = ruleBase.name(); + } + } + + 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; } } diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRules.java b/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRules.java index 46c8577c6e8..0083fc6b886 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRules.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRules.java @@ -6,14 +6,14 @@ import java.io.Serializable; import java.util.List; /** - * Returns the semantic rules config form a set of rule bases. + * 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 List<RuleBase> ruleBases; + private final List<RuleBase> ruleBases; public SemanticRules(List<RuleBase> ruleBases) { this.ruleBases = ruleBases; @@ -46,7 +46,6 @@ public class SemanticRules implements Serializable, SemanticRulesConfig.Producer return ruleBaseBuilder; } - } } |