diff options
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java | 60 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/derived/RawRankProfile.java | 4 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroInliner.java | 6 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroShadower.java | 6 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/RankProfileTransformContext.java | 6 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java | 18 | ||||
-rw-r--r-- | config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionsTestCase.java | 2 | ||||
-rw-r--r-- | config-model/src/test/java/com/yahoo/searchdefinition/processing/ReservedRankingExpressionFunctionNamesTestCase.java (renamed from config-model/src/test/java/com/yahoo/searchdefinition/processing/ReservedMacroNamesTestCase.java) | 2 |
8 files changed, 51 insertions, 53 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java index 200eac0cdad..509fa243bc3 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java @@ -101,7 +101,7 @@ public class RankProfile implements Serializable, Cloneable { private String firstPhaseRankingString = null; - private Map<String, Macro> macros= new LinkedHashMap<>(); + private Map<String, RankingExpressionFunction> macros= new LinkedHashMap<>(); private Set<String> filterFields = new HashSet<>(); @@ -549,7 +549,7 @@ public class RankProfile implements Serializable, Cloneable { } /** Adds a new macro and returns it */ - public Macro addMacro(String name, List<String> arguments, String expression, boolean inline) { + public RankingExpressionFunction addMacro(String name, List<String> arguments, String expression, boolean inline) { try { return addMacro(new ExpressionFunction(name, arguments, parseRankingExpression(name, expression)), inline); } @@ -559,20 +559,20 @@ public class RankProfile implements Serializable, Cloneable { } /** Adds a new macro and returns it */ - public Macro addMacro(ExpressionFunction function, boolean inline) { - Macro macro = new Macro(function, inline); - macros.put(function.getName(), macro); - return macro; + public RankingExpressionFunction addMacro(ExpressionFunction function, boolean inline) { + RankingExpressionFunction rankingExpressionFunction = new RankingExpressionFunction(function, inline); + macros.put(function.getName(), rankingExpressionFunction); + return rankingExpressionFunction; } /** Returns an unmodifiable view of the macros in this */ - public Map<String, Macro> getMacros() { + public Map<String, RankingExpressionFunction> getMacros() { if (macros.size() == 0 && getInherited()==null) return Collections.emptyMap(); if (macros.size() == 0) return getInherited().getMacros(); if (getInherited() == null) return Collections.unmodifiableMap(macros); // Neither is null - Map<String, Macro> allMacros = new LinkedHashMap<>(getInherited().getMacros()); + Map<String, RankingExpressionFunction> allMacros = new LinkedHashMap<>(getInherited().getMacros()); allMacros.putAll(macros); return Collections.unmodifiableMap(allMacros); @@ -717,7 +717,7 @@ public class RankProfile implements Serializable, Cloneable { ExpressionTransforms expressionTransforms = new ExpressionTransforms(); // Macro compiling first pass: compile inline macros without resolving other macros - Map<String, Macro> inlineMacros = compileMacros(getInlineMacros(), queryProfiles, importedModels, Collections.emptyMap(), expressionTransforms); + Map<String, RankingExpressionFunction> inlineMacros = compileMacros(getInlineMacros(), queryProfiles, importedModels, Collections.emptyMap(), expressionTransforms); // Macro compiling second pass: compile all macros and insert previously compiled inline macros macros = compileMacros(getMacros(), queryProfiles, importedModels, inlineMacros, expressionTransforms); @@ -726,29 +726,29 @@ public class RankProfile implements Serializable, Cloneable { secondPhaseRanking = compile(this.getSecondPhaseRanking(), queryProfiles, importedModels, getConstants(), inlineMacros, expressionTransforms); } - private void checkNameCollisions(Map<String, Macro> macros, Map<String, Value> constants) { - for (Map.Entry<String, Macro> macroEntry : macros.entrySet()) { + private void checkNameCollisions(Map<String, RankingExpressionFunction> macros, Map<String, Value> constants) { + for (Map.Entry<String, RankingExpressionFunction> macroEntry : macros.entrySet()) { if (constants.get(macroEntry.getKey()) != null) throw new IllegalArgumentException("Cannot have both a constant and macro named '" + macroEntry.getKey() + "'"); } } - private Map<String, Macro> getInlineMacros() { + private Map<String, RankingExpressionFunction> getInlineMacros() { return getMacros().entrySet().stream().filter(x -> x.getValue().inline()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } - private Map<String, Macro> compileMacros(Map<String, Macro> macros, - QueryProfileRegistry queryProfiles, - ImportedModels importedModels, - Map<String, Macro> inlineMacros, - ExpressionTransforms expressionTransforms) { - Map<String, Macro> compiledMacros = new LinkedHashMap<>(); - for (Map.Entry<String, Macro> entry : macros.entrySet()) { - Macro macro = entry.getValue(); - RankingExpression compiled = compile(macro.function().getBody(), queryProfiles, importedModels, getConstants(), inlineMacros, expressionTransforms); - compiledMacros.put(entry.getKey(), macro.withBody(compiled)); + private Map<String, RankingExpressionFunction> compileMacros(Map<String, RankingExpressionFunction> macros, + QueryProfileRegistry queryProfiles, + ImportedModels importedModels, + Map<String, RankingExpressionFunction> inlineMacros, + ExpressionTransforms expressionTransforms) { + Map<String, RankingExpressionFunction> compiledMacros = new LinkedHashMap<>(); + for (Map.Entry<String, RankingExpressionFunction> entry : macros.entrySet()) { + RankingExpressionFunction rankingExpressionFunction = entry.getValue(); + RankingExpression compiled = compile(rankingExpressionFunction.function().getBody(), queryProfiles, importedModels, getConstants(), inlineMacros, expressionTransforms); + compiledMacros.put(entry.getKey(), rankingExpressionFunction.withBody(compiled)); } return compiledMacros; } @@ -757,7 +757,7 @@ public class RankProfile implements Serializable, Cloneable { QueryProfileRegistry queryProfiles, ImportedModels importedModels, Map<String, Value> constants, - Map<String, Macro> inlineMacros, + Map<String, RankingExpressionFunction> inlineMacros, ExpressionTransforms expressionTransforms) { if (expression == null) return null; RankProfileTransformContext context = new RankProfileTransformContext(this, @@ -778,7 +778,7 @@ public class RankProfile implements Serializable, Cloneable { */ public TypeContext<Reference> typeContext(QueryProfileRegistry queryProfiles) { MapEvaluationTypeContext context = new MapEvaluationTypeContext(getMacros().values().stream() - .map(Macro::function) + .map(RankingExpressionFunction::function) .collect(Collectors.toList())); // Add small and large constants, respectively @@ -945,17 +945,15 @@ public class RankProfile implements Serializable, Cloneable { } - /** - * A function in a rank profile - */ - public static class Macro { + /** A function in a rank profile */ + public static class RankingExpressionFunction { private final ExpressionFunction function; /** True if this should be inlined into calling expressions. Useful for very cheap macros. */ private final boolean inline; - public Macro(ExpressionFunction function, boolean inline) { + public RankingExpressionFunction(ExpressionFunction function, boolean inline) { this.function = function; this.inline = inline; } @@ -966,8 +964,8 @@ public class RankProfile implements Serializable, Cloneable { return inline && function.arguments().isEmpty(); // only inline no-arg macros; } - public Macro withBody(RankingExpression expression) { - return new Macro(function.withBody(expression), inline); + public RankingExpressionFunction withBody(RankingExpression expression) { + return new RankingExpressionFunction(function.withBody(expression), inline); } @Override diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/derived/RawRankProfile.java b/config-model/src/main/java/com/yahoo/searchdefinition/derived/RawRankProfile.java index d3ba7d4b613..98d147aca79 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/derived/RawRankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/derived/RawRankProfile.java @@ -178,10 +178,10 @@ public class RawRankProfile implements RankProfilesConfig.Producer { derivePropertiesAndSummaryFeaturesFromMacros(rankProfile.getMacros()); } - private void derivePropertiesAndSummaryFeaturesFromMacros(Map<String, RankProfile.Macro> macros) { + private void derivePropertiesAndSummaryFeaturesFromMacros(Map<String, RankProfile.RankingExpressionFunction> macros) { if (macros.isEmpty()) return; Map<String, ExpressionFunction> expressionMacros = new LinkedHashMap<>(); - for (Map.Entry<String, RankProfile.Macro> macro : macros.entrySet()) { + for (Map.Entry<String, RankProfile.RankingExpressionFunction> macro : macros.entrySet()) { expressionMacros.put(macro.getKey(), macro.getValue().function()); } diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroInliner.java b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroInliner.java index b5642a5426f..155568825e0 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroInliner.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroInliner.java @@ -24,9 +24,9 @@ public class MacroInliner extends ExpressionTransformer<RankProfileTransformCont } private ExpressionNode transformFeatureNode(ReferenceNode feature, RankProfileTransformContext context) { - RankProfile.Macro macro = context.inlineMacros().get(feature.getName()); - if (macro == null) return feature; - return transform(macro.function().getBody().getRoot(), context); // inline recursively and return + RankProfile.RankingExpressionFunction rankingExpressionFunction = context.inlineMacros().get(feature.getName()); + if (rankingExpressionFunction == null) return feature; + return transform(rankingExpressionFunction.function().getBody().getRoot(), context); // inline recursively and return } } diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroShadower.java b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroShadower.java index 13f31c0254e..62315257c2c 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroShadower.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/MacroShadower.java @@ -43,13 +43,13 @@ public class MacroShadower extends ExpressionTransformer<RankProfileTransformCon private ExpressionNode transformFunctionNode(FunctionNode function, RankProfileTransformContext context) { String name = function.getFunction().toString(); - RankProfile.Macro macro = context.rankProfile().getMacros().get(name); - if (macro == null) { + RankProfile.RankingExpressionFunction rankingExpressionFunction = context.rankProfile().getMacros().get(name); + if (rankingExpressionFunction == null) { return transformChildren(function, context); } int functionArity = function.getFunction().arity(); - if (functionArity != macro.function().arguments().size()) + if (functionArity != rankingExpressionFunction.function().arguments().size()) return transformChildren(function, context); ReferenceNode node = new ReferenceNode(name, function.children(), null); diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/RankProfileTransformContext.java b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/RankProfileTransformContext.java index 40c3b997daa..f45ef359338 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/RankProfileTransformContext.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/expressiontransforms/RankProfileTransformContext.java @@ -20,14 +20,14 @@ public class RankProfileTransformContext extends TransformContext { private final RankProfile rankProfile; private final QueryProfileRegistry queryProfiles; private final ImportedModels importedModels; - private final Map<String, RankProfile.Macro> inlineMacros; + private final Map<String, RankProfile.RankingExpressionFunction> inlineMacros; private final Map<String, String> rankProperties = new HashMap<>(); public RankProfileTransformContext(RankProfile rankProfile, QueryProfileRegistry queryProfiles, ImportedModels importedModels, Map<String, Value> constants, - Map<String, RankProfile.Macro> inlineMacros) { + Map<String, RankProfile.RankingExpressionFunction> inlineMacros) { super(constants); this.rankProfile = rankProfile; this.queryProfiles = queryProfiles; @@ -38,7 +38,7 @@ public class RankProfileTransformContext extends TransformContext { public RankProfile rankProfile() { return rankProfile; } public QueryProfileRegistry queryProfiles() { return queryProfiles; } public ImportedModels importedModels() { return importedModels; } - public Map<String, RankProfile.Macro> inlineMacros() { return inlineMacros; } + public Map<String, RankProfile.RankingExpressionFunction> inlineMacros() { return inlineMacros; } public Map<String, String> rankProperties() { return rankProperties; } } diff --git a/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java b/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java index fb1ca9a0822..ffc3c53fdb6 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/ml/ConvertedModel.java @@ -251,9 +251,9 @@ public class ConvertedModel { Set<String> constantsReplacedByMacros, String constantName, Tensor constantValue) { - RankProfile.Macro macroOverridingConstant = profile.getMacros().get(constantName); - if (macroOverridingConstant != null) { - TensorType macroType = macroOverridingConstant.function().getBody().type(profile.typeContext(queryProfiles)); + RankProfile.RankingExpressionFunction rankingExpressionFunctionOverridingConstant = profile.getMacros().get(constantName); + if (rankingExpressionFunctionOverridingConstant != null) { + TensorType macroType = rankingExpressionFunctionOverridingConstant.function().getBody().type(profile.typeContext(queryProfiles)); if ( ! macroType.equals(constantValue.type())) throw new IllegalArgumentException("Macro '" + constantName + "' replaces the constant with this name. " + typeMismatchExplanation(constantValue.type(), macroType)); @@ -301,8 +301,8 @@ public class ConvertedModel { TensorType requiredType = model.requiredMacros().get(macroName); if (requiredType == null) continue; // Not a required macro - RankProfile.Macro macro = profile.getMacros().get(macroName); - if (macro == null) + RankProfile.RankingExpressionFunction rankingExpressionFunction = profile.getMacros().get(macroName); + if (rankingExpressionFunction == null) throw new IllegalArgumentException("Model refers input '" + macroName + "' of type " + requiredType + " but this macro is not present in " + profile); @@ -310,7 +310,7 @@ public class ConvertedModel { // phase and summary features), as it may only resolve correctly given those bindings // Or, probably better, annotate the macros with type constraints here and verify during general // type verification - TensorType actualType = macro.function().getBody().getRoot().type(profile.typeContext(queryProfiles)); + TensorType actualType = rankingExpressionFunction.function().getBody().getRoot().type(profile.typeContext(queryProfiles)); if ( actualType == null) throw new IllegalArgumentException("Model refers input '" + macroName + "' of type " + requiredType + @@ -352,12 +352,12 @@ public class ConvertedModel { for (String macroName : macroNames) { if ( ! model.macros().containsKey(macroName)) continue; - RankProfile.Macro macro = profile.getMacros().get(macroName); - if (macro == null) { + RankProfile.RankingExpressionFunction rankingExpressionFunction = profile.getMacros().get(macroName); + if (rankingExpressionFunction == null) { throw new IllegalArgumentException("Model refers to generated macro '" + macroName + "but this macro is not present in " + profile); } - RankingExpression macroExpression = macro.function().getBody(); + RankingExpression macroExpression = rankingExpressionFunction.function().getBody(); macroExpression.setRoot(reduceBatchDimensionsAtInput(macroExpression.getRoot(), model, typeContext)); } diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionsTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionsTestCase.java index 132807bf8b8..76a7a01a3aa 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionsTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/processing/RankingExpressionsTestCase.java @@ -27,7 +27,7 @@ public class RankingExpressionsTestCase extends SearchDefinitionTestCase { new QueryProfileRegistry()).getSearch(); final RankProfile macrosRankProfile = rankProfileRegistry.get(search, "macros"); macrosRankProfile.parseExpressions(); - final Map<String, RankProfile.Macro> macros = macrosRankProfile.getMacros(); + final Map<String, RankProfile.RankingExpressionFunction> macros = macrosRankProfile.getMacros(); assertEquals(2, macros.get("titlematch$").function().arguments().size()); assertEquals("var1", macros.get("titlematch$").function().arguments().get(0)); assertEquals("var2", macros.get("titlematch$").function().arguments().get(1)); diff --git a/config-model/src/test/java/com/yahoo/searchdefinition/processing/ReservedMacroNamesTestCase.java b/config-model/src/test/java/com/yahoo/searchdefinition/processing/ReservedRankingExpressionFunctionNamesTestCase.java index 8a07e99101c..56639e6b9a8 100644 --- a/config-model/src/test/java/com/yahoo/searchdefinition/processing/ReservedMacroNamesTestCase.java +++ b/config-model/src/test/java/com/yahoo/searchdefinition/processing/ReservedRankingExpressionFunctionNamesTestCase.java @@ -16,7 +16,7 @@ import static org.junit.Assert.assertTrue; /** * @author lesters */ -public class ReservedMacroNamesTestCase { +public class ReservedRankingExpressionFunctionNamesTestCase { @Test public void requireThatMacrosWithReservedNamesIssueAWarning() throws ParseException { |