aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionNode.java
blob: 142e282e5c67da0f0dbb4cbf1008ef427b6ad347 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.rule;

import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

/**
 * Invocation of a native function.
 *
 * @author simon
 * @author bratseth
 */
public final class FunctionNode extends CompositeNode {

    /** The type of function. */
    private final Function function;

    /** The arguments to this function. */
    private final Arguments arguments;

    /* Creates an unary function node */
    public FunctionNode(Function function, ExpressionNode argument) {
        if (function.arity() != 1) throw new IllegalArgumentException(function + " is not unary");
        this.function = function;
        this.arguments = new Arguments(Collections.singletonList(argument));
    }

    /** Creates a binary function node */
    public FunctionNode(Function function, ExpressionNode argument1, ExpressionNode argument2) {
        if (function.arity() != 2) throw new IllegalArgumentException(function + " is not binary");
        this.function = function;
        List<ExpressionNode> argumentList = new ArrayList<>();
        argumentList.add(argument1);
        argumentList.add(argument2);
        arguments=new Arguments(argumentList);
    }

    public Function getFunction() { return function; }

    /** Returns the arguments of this */
    @Override
    public List<ExpressionNode> children() {
        return arguments.expressions();
    }

    @Override
    public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
        StringBuilder b=new StringBuilder(function.toString());
        b.append("(");
        for (int i = 0; i < this.arguments.expressions().size(); ++i) {
            b.append(this.arguments.expressions().get(i).toString(context, path, this));
            if (i < this.arguments.expressions().size() - 1) {
                b.append(",");
            }
        }
        b.append(")");
        return b.toString();
    }

    @Override
    public Value evaluate(Context context) {
        if (arguments.expressions().size() == 0)
            return DoubleValue.zero.function(function,DoubleValue.zero);

        Value argument1 = arguments.expressions().get(0).evaluate(context);
        if (arguments.expressions().size() == 1)
            return argument1.function(function, DoubleValue.zero);

        Value argument2 = arguments.expressions().get(1).evaluate(context);
        return argument1.function(function,argument2);
    }

    /** Returns a new function node with the children replaced by the given children */
    @Override
    public FunctionNode setChildren(List<ExpressionNode> children) {
        if (arguments.expressions().size() != children.size())
            throw new IllegalArgumentException("Expected " + arguments.expressions().size() + " children but got " + children.size());
        if (children.size() == 1)
            return new FunctionNode(function, children.get(0));
        else // binary
            return new FunctionNode(function, children.get(0), children.get(1));
    }

}