summaryrefslogtreecommitdiffstats
path: root/model-evaluation/src/main/java/ai/vespa/models/evaluation/FunctionEvaluator.java
blob: 8c728867f450bd8830b1625699af7e6a94230174 (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.models.evaluation;

import com.google.common.annotations.Beta;
import com.yahoo.searchlib.rankingexpression.ExpressionFunction;
import com.yahoo.searchlib.rankingexpression.evaluation.DoubleValue;
import com.yahoo.searchlib.rankingexpression.evaluation.TensorValue;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * An evaluator which can be used to evaluate a single function once.
 *
 * @author bratseth
 */
@Beta
// This wraps all access to the context and the ranking expression to avoid incorrect usage
public class FunctionEvaluator {

    private final ExpressionFunction function;
    private final LazyArrayContext context;
    private boolean evaluated = false;

    FunctionEvaluator(ExpressionFunction function, LazyArrayContext context) {
        this.function = function;
        this.context = context;
    }

    /**
     * Binds the given variable referred in this expression to the given value.
     *
     * @param name the variable to bind
     * @param value the value this becomes bound to
     * @return this for chaining
     */
    public FunctionEvaluator bind(String name, Tensor value) {
        if (evaluated)
            throw new IllegalStateException("Cannot bind a new value in a used evaluator");
        TensorType requiredType = function.argumentTypes().get(name);
        if (requiredType == null)
            throw new IllegalArgumentException("'" + name + "' is not a valid argument in " + function +
                                               ". Expected arguments: " + function.argumentTypes().entrySet().stream()
                                                                                  .map(e -> e.getKey() + ": " + e.getValue())
                                                                                  .collect(Collectors.joining(", ")));
        if ( ! value.type().isAssignableTo(requiredType))
            throw new IllegalArgumentException("'" + name + "' must be of type " + requiredType + ", not " + value.type());
        context.put(name, new TensorValue(value));
        return this;
    }

    /**
     * Binds the given variable referred in this expression to the given value.
     * This is equivalent to <code>bind(name, Tensor.Builder.of(TensorType.empty).cell(value).build())</code>
     *
     * @param name the variable to bind
     * @param value the value this becomes bound to
     * @return this for chaining
     */
    public FunctionEvaluator bind(String name, double value) {
        return bind(name, Tensor.Builder.of(TensorType.empty).cell(value).build());
    }

    public Tensor evaluate() {
        for (Map.Entry<String, TensorType> argument : function.argumentTypes().entrySet()) {
            if (argument.getValue().rank() == 0) continue; // Scalar argumentds can be skipped (defaults to 0)
            if (context.get(argument.getKey()) == LazyArrayContext.defaultContextValue)
                throw new IllegalStateException("Missing argument '" + argument.getKey() +
                                                "': Must be bound to a value of type " + argument.getValue());
        }
        evaluated = true;
        return function.getBody().evaluate(context).asTensor();
    }

    /** Returns the function evaluated by this */
    public ExpressionFunction function() { return function; }

    public LazyArrayContext context() { return context; }

}