diff options
author | Jon Bratseth <bratseth@oath.com> | 2018-09-17 09:53:03 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@oath.com> | 2018-09-17 09:53:03 +0200 |
commit | e2bc647f01162d7b5f0886337f308a9be6629a40 (patch) | |
tree | bbf66e2c251d780b1926f2af5aadad65c6ad9506 /config-model/src/main/java/com/yahoo/searchdefinition | |
parent | 347db07eb16da7ff3a4b2cac676caa932e8136dc (diff) |
Refactor: Macro -> RankingExpressionFunction
Diffstat (limited to 'config-model/src/main/java/com/yahoo/searchdefinition')
5 files changed, 40 insertions, 42 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; } } |