diff options
Diffstat (limited to 'config-model/src/main/java/com/yahoo/schema/expressiontransforms/FunctionInliner.java')
-rw-r--r-- | config-model/src/main/java/com/yahoo/schema/expressiontransforms/FunctionInliner.java | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/config-model/src/main/java/com/yahoo/schema/expressiontransforms/FunctionInliner.java b/config-model/src/main/java/com/yahoo/schema/expressiontransforms/FunctionInliner.java new file mode 100644 index 00000000000..382d51747bb --- /dev/null +++ b/config-model/src/main/java/com/yahoo/schema/expressiontransforms/FunctionInliner.java @@ -0,0 +1,33 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.schema.expressiontransforms; + +import com.yahoo.schema.RankProfile; +import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; +import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; +import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; +import com.yahoo.searchlib.rankingexpression.transform.ExpressionTransformer; + +/** + * Inlines functions in ranking expressions + * + * @author bratseth + */ +public class FunctionInliner extends ExpressionTransformer<RankProfileTransformContext> { + + @Override + public ExpressionNode transform(ExpressionNode node, RankProfileTransformContext context) { + if (node instanceof ReferenceNode) + return transformFeatureNode((ReferenceNode)node, context); + if (node instanceof CompositeNode) + return transformChildren((CompositeNode)node, context); + return node; + } + + private ExpressionNode transformFeatureNode(ReferenceNode feature, RankProfileTransformContext context) { + if (feature.getArguments().size() > 0) return feature; // From RankProfile: only inline no-arg functions + RankProfile.RankingExpressionFunction rankingExpressionFunction = context.inlineFunctions().get(feature.getName()); + if (rankingExpressionFunction == null) return feature; + return transform(rankingExpressionFunction.function().getBody().getRoot(), context); // inline recursively and return + } + +} |