aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/expression/MultiArgFunctionNode.java
blob: f7a34c030bd7918565faf836b93cffa1254304f4 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.expression;

import com.yahoo.vespa.objects.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>This is an abstract super-class for all functions that accepts multiple arguments. This node implements the
 * necessary API for manipulating arguments.</p>
 *
 * @author baldersheim
 * @author Simon Thoresen Hult
 */
public abstract class MultiArgFunctionNode extends FunctionNode {

    public static final int classId = registerClass(0x4000 + 45, MultiArgFunctionNode.class);
    private List<ExpressionNode> args = new ArrayList<ExpressionNode>();

    /**
     * <p>Adds the given argument to this function.</p>
     *
     * @param arg The argument to add.
     * @return This, to allow chaining.
     */
    public MultiArgFunctionNode addArg(ExpressionNode arg) {
        arg.getClass(); // throws NullPointerException
        args.add(arg);
        return this;
    }

    /**
     * <p>Returns the argument at the given index.</p>
     *
     * @param i The index of the argument to return.
     * @return The argument.
     */
    public ExpressionNode getArg(int i) {
        return args.get(i);
    }

    /**
     * <p>Returns the number of arguments this function has.</p>
     *
     * @return The size of the argument list.
     */
    public int getNumArgs() {
        return args.size();
    }

    @Override
    protected boolean onExecute() {
        for (ExpressionNode arg : args) {
            arg.execute();
        }
        return calculate(args, getResult());
    }

    @Override
    protected void onPrepare() {
        for (ExpressionNode arg : args) {
            arg.prepare();
        }
        prepareResult();
    }

    /**
     * <p>Perform the appropriate calculation of the arguments into a result node.</p>
     *
     * @param args   A list of operands.
     * @param result Place to put the result.
     * @return True if successful, false if not.
     */
    private boolean calculate(final List<ExpressionNode> args, ResultNode result) {
        return onCalculate(args, result);
    }

    private void prepareResult() {
        onPrepareResult();
    }

    protected boolean onCalculate(final List<ExpressionNode> args, ResultNode result) {
        result.set(args.get(0).getResult());
        for (int i = 1; i < args.size(); i++) {
            executeIterative(args.get(i).getResult(), result);
        }
        return true;
    }

    protected void onPrepareResult() {
        if (args.size() == 1) {
            setResult(ArithmeticTypeConversion.getType(args.get(0).getResult()));
        } else if (args.size() > 1) {
            setResult((ResultNode)args.get(0).getResult().clone());
            for (int i = 1; i < args.size(); i++) {
                if (args.get(i).getResult() != null) {
                    setResult(ArithmeticTypeConversion.getType(getResult(), args.get(i).getResult()));
                }
            }
        }
    }

    @Override
    protected int onGetClassId() {
        return classId;
    }

    @Override
    protected void onSerialize(Serializer buf) {
        super.onSerialize(buf);
        int numArgs = args.size();
        buf.putInt(null, numArgs);
        for (ExpressionNode node : args) {
            serializeOptional(buf, node); // TODO: Not optional.
        }
    }

    @Override
    protected void onDeserialize(Deserializer buf) {
        super.onDeserialize(buf);
        args.clear();
        int numArgs = buf.getInt(null);
        for (int i = 0; i < numArgs; i++) {
            ExpressionNode node = (ExpressionNode)deserializeOptional(buf); // TODO: Not optional.
            args.add(node);
        }
    }

    @Override
    public int hashCode() {
        int ret = super.hashCode();
        for (ExpressionNode node : args) {
            ret += node.hashCode();
        }
        return ret;
    }

    @Override
    protected final boolean equalsFunction(FunctionNode obj) {
        MultiArgFunctionNode rhs = (MultiArgFunctionNode)obj;
        if (!args.equals(rhs.args)) {
            return false;
        }
        if (!equalsMultiArgFunction(rhs)) {
            return false;
        }
        return true;
    }

    protected abstract boolean equalsMultiArgFunction(MultiArgFunctionNode obj);

    @Override
    public MultiArgFunctionNode clone() {
        MultiArgFunctionNode obj = (MultiArgFunctionNode)super.clone();
        obj.args = new ArrayList<ExpressionNode>();
        for (ExpressionNode node : args) {
            obj.args.add(node.clone());
        }
        return obj;
    }

    @Override
    public void visitMembers(ObjectVisitor visitor) {
        super.visitMembers(visitor);
        visitor.visit("args", args);
    }

    @Override
    public void selectMembers(ObjectPredicate predicate, ObjectOperation operation) {
        super.selectMembers(predicate, operation);
        for (ExpressionNode arg : args) {
            arg.select(predicate, operation);
        }
    }
}