diff options
author | Harald Musum <musum@yahooinc.com> | 2023-06-12 10:57:46 +0200 |
---|---|---|
committer | Harald Musum <musum@yahooinc.com> | 2023-06-12 10:57:46 +0200 |
commit | 06213d2c019d05bcbe6ee7d2b4c3e82f8425961c (patch) | |
tree | f4efc499e26d0041c4c3ba5d0de8d622ae631b7d /config-model/src | |
parent | a6d524a04c36132e848454f7b60585fd3aed7da1 (diff) |
Move builder into SemanticRules
Diffstat (limited to 'config-model/src')
6 files changed, 79 insertions, 94 deletions
diff --git a/config-model/src/main/java/com/yahoo/config/model/deploy/DeployState.java b/config-model/src/main/java/com/yahoo/config/model/deploy/DeployState.java index b8d63ba3778..4df7a76031a 100644 --- a/config-model/src/main/java/com/yahoo/config/model/deploy/DeployState.java +++ b/config-model/src/main/java/com/yahoo/config/model/deploy/DeployState.java @@ -24,7 +24,6 @@ import com.yahoo.config.model.api.ValidationParameters; import com.yahoo.config.model.application.provider.BaseDeployLogger; import com.yahoo.config.model.application.provider.MockFileRegistry; import com.yahoo.config.model.provision.HostsXmlProvisioner; -import com.yahoo.config.model.provision.InMemoryProvisioner; import com.yahoo.config.model.provision.SingleNodeProvisioner; import com.yahoo.config.model.test.MockApplicationPackage; import com.yahoo.config.provision.DockerImage; @@ -41,7 +40,6 @@ import com.yahoo.vespa.config.ConfigDefinitionKey; import com.yahoo.vespa.documentmodel.DocumentModel; import com.yahoo.vespa.model.container.search.QueryProfiles; import com.yahoo.vespa.model.container.search.QueryProfilesBuilder; -import com.yahoo.vespa.model.container.search.SemanticRuleBuilder; import com.yahoo.vespa.model.container.search.SemanticRules; import java.io.File; @@ -60,6 +58,8 @@ import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutorService; +import static com.yahoo.vespa.model.container.search.SemanticRules.SemanticRuleBuilder; + /** * Contains various state during deploy that should be available in all builders of a {@link com.yahoo.config.model.ConfigModel} * diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/search/PageTemplates.java b/config-model/src/main/java/com/yahoo/vespa/model/container/search/PageTemplates.java index dc9a9f127ed..d186008b998 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/search/PageTemplates.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/container/search/PageTemplates.java @@ -20,11 +20,8 @@ import java.util.List; */ public class PageTemplates implements Serializable, PageTemplatesConfig.Producer { - private List<String> pages = new ArrayList<>(); + private final List<String> pages = new ArrayList<>(); - /** The number of pages in this, for reporting */ - //private int pages=0; - /** Validates page templates in an application package. The passed readers will be closed. */ public static void validate(ApplicationPackage applicationPackage) { List<NamedReader> pageTemplateFiles=null; diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/search/RankProfilesEvaluatorComponent.java b/config-model/src/main/java/com/yahoo/vespa/model/container/search/RankProfilesEvaluatorComponent.java index 75a2802ee53..47ed2ea99f7 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/search/RankProfilesEvaluatorComponent.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/container/search/RankProfilesEvaluatorComponent.java @@ -8,7 +8,6 @@ import com.yahoo.vespa.config.search.RankProfilesConfig; import com.yahoo.vespa.config.search.core.OnnxModelsConfig; import com.yahoo.vespa.config.search.core.RankingConstantsConfig; import com.yahoo.vespa.config.search.core.RankingExpressionsConfig; -import com.yahoo.vespa.model.container.ContainerModelEvaluation; import com.yahoo.vespa.model.container.PlatformBundles; import com.yahoo.vespa.model.container.component.Component; import com.yahoo.vespa.model.search.DocumentDatabase; 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 deleted file mode 100644 index 4923c41224e..00000000000 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/search/SemanticRuleBuilder.java +++ /dev/null @@ -1,87 +0,0 @@ -// 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 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; - -/** - * Reads the semantic rules from the application package by delegating to SemanticRules. - * - * @author bratseth - */ -// TODO: Move into SemanticRules -public 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 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()); - } - - 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, true, 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 0083fc6b886..a77c2c98380 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 @@ -1,9 +1,21 @@ // 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 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. @@ -48,4 +60,67 @@ public class SemanticRules implements Serializable, SemanticRulesConfig.Producer } + 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; + } + + } + } diff --git a/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java b/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java index da6e055724a..544acc81b8d 100644 --- a/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java +++ b/config-model/src/test/java/com/yahoo/vespa/model/container/search/SemanticRulesTest.java @@ -11,6 +11,7 @@ import java.io.File; import java.io.IOException; import java.util.Map; +import static com.yahoo.vespa.model.container.search.SemanticRules.SemanticRuleBuilder; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; |