diff options
author | Jon Bratseth <bratseth@gmail.com> | 2023-06-10 15:15:32 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-10 15:15:32 +0200 |
commit | 548ab2e52ca577e1929897c19300f6d2ed9aaf44 (patch) | |
tree | d6fede974e64deb6af8444019e0fa9c6d5176438 /config-model/src/main | |
parent | e9ccfa72400a8d6d83f64cb1045b6c64cdb88760 (diff) | |
parent | 4abbbe29cf9c38104be31ff6820f0118fddb3557 (diff) |
Merge pull request #27367 from vespa-engine/hmusum/revert-validate-semantic-rules
Hmusum/revert validate semantic rules
Diffstat (limited to 'config-model/src/main')
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java | 56 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRules.java | 5 |
2 files changed, 12 insertions, 49 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 4a295d49a32..ce999603439 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,18 +4,11 @@ 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.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; +import java.util.stream.Collectors; /** * Reads the semantic rules from the application package by delegating to SemanticRules. @@ -25,22 +18,16 @@ import java.util.Map; // TODO: Move into SemanticRules public class SemanticRuleBuilder { - /** Build the set of semantic rules for an application package and validates them */ + /** Build the set of semantic rules for an application package */ 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(); + List<NamedReader> ruleBaseFiles = null; try { - toMap(config); // validates config - ensureZeroOrOneDefaultRule(config); - } catch (ParseException | IOException e) { - throw new RuntimeException(e); + ruleBaseFiles = applicationPackage.getFiles(ApplicationPackage.RULES_DIR, "sr"); + return new SemanticRules(ruleBaseFiles.stream().map(this::toRuleBaseConfigView).toList()); + } + finally { + NamedReader.closeAll(ruleBaseFiles); } - return rules; } private SemanticRules.RuleBase toRuleBaseConfigView(NamedReader reader) { @@ -56,32 +43,7 @@ public class SemanticRuleBuilder { 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, 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; + return shortName.substring(0, shortName.length()-".sr".length()); } } 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 0083fc6b886..46c8577c6e8 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 from a set of rule bases. + * Returns the semantic rules config form a set of rule bases. * Owned by a container cluster * * @author bratseth */ public class SemanticRules implements Serializable, SemanticRulesConfig.Producer { - private final List<RuleBase> ruleBases; + private List<RuleBase> ruleBases; public SemanticRules(List<RuleBase> ruleBases) { this.ruleBases = ruleBases; @@ -46,6 +46,7 @@ public class SemanticRules implements Serializable, SemanticRulesConfig.Producer return ruleBaseBuilder; } + } } |