blob: 6aef39db4da26712bb7d848d2e8cf21f242d366f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.expressiontransforms;
import com.yahoo.searchdefinition.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 macros in ranking expressions
*
* @author bratseth
*/
public class MacroInliner 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) {
RankProfile.Macro macro = context.inlineMacros().get(feature.getName());
if (macro == null) return feature;
return transform(macro.getRankingExpression().getRoot(), context); // inline recursively and return
}
}
|