aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/ExpressionNode.java
blob: dba0da7301d3f7a453b0d6a8d5a11e1dcf9c31c3 (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
// 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.Reference;
import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.TypeContext;

import java.io.Serializable;
import java.util.Deque;

/**
 * Superclass of all expression nodes. Expression nodes have their identity determined by their content.
 * All expression nodes are immutable.
 *
 * @author Simon Thoresen Hult
 */
public abstract class ExpressionNode implements Serializable {

    @Override
    public final int hashCode() {
        return toString().hashCode();
    }

    @Override
    public final boolean equals(Object obj) {
        return obj instanceof ExpressionNode && toString().equals(obj.toString());
    }

    @Override
    public final String toString() {
        return toString(new StringBuilder(), new SerializationContext(), null, null).toString();
    }

    /**
     * Returns a script instance of this based on the supplied script functions.
     *
     * @param builder the StringBuilder that will be appended to
     * @param context the serialization context
     * @param path the call path to this, used for cycle detection, or null if this is a root
     * @param parent the parent node of this, or null if it a root
     * @return the main script, referring to script instances.
     */
    public abstract StringBuilder toString(StringBuilder builder, SerializationContext context, Deque<String> path, CompositeNode parent);

    /**
     * Returns the type this will return if evaluated with the given context.
     *
     * @param context the variable type bindings to use for this evaluation
     * @throws IllegalArgumentException if there are variables which are not bound in the given map
     */
    public abstract TensorType type(TypeContext<Reference> context);

    /**
     * Returns the value of evaluating this expression over the given context.
     *
     * @param context the variable bindings to use for this evaluation
     * @throws IllegalArgumentException if there are variables which are not bound in the given map
     */
    public abstract Value evaluate(Context context);

}