aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/expressiontransforms/FunctionInliner.java
blob: 2d64073a46ca8d3be0cc9419dc954bc1a2fb5ea0 (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
33
// Copyright Vespa.ai. 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
    }

}