aboutsummaryrefslogtreecommitdiffstats
path: root/model-evaluation/src/main/java/ai/vespa/models/evaluation/LazyValue.java
blob: 8601103cbdeeffad8bbb2c54d1ad73df4fbf3f0a (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
177
178
179
180
181
182
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.models.evaluation;

import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;
import com.yahoo.searchlib.rankingexpression.rule.Function;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;

/**
 * A Value which is computed from an expression when first requested.
 * This is not multithread safe.
 *
 * @author bratseth
 */
class LazyValue extends Value {

    /** The reference to the function computing the value of this */
    private final FunctionReference function;

    /** The context used to compute the function of this */
    private final Context context;

    /** The model this is part of */
    private final Model model;

    private Value computedValue = null;

    public LazyValue(FunctionReference function, Context context, Model model) {
        this.function = function;
        this.context = context;
        this.model = model;
    }

    private Value computedValue() {
        if (computedValue == null)
            computedValue = model.requireReferencedFunction(function).getBody().evaluate(context);
        return computedValue;
    }

    @Override
    public TensorType type() {
        return model.requireReferencedFunction(function).returnType().get();
    }

    @Override
    public double asDouble() {
        return computedValue().asDouble();
    }

    @Override
    public Tensor asTensor() {
        return computedValue().asTensor();
    }

    @Override
    public boolean hasDouble() {
        return type().rank() == 0;
    }

    @Override
    public boolean asBoolean() {
        return computedValue().asBoolean();
    }

    @Override
    public Value negate() {
        return computedValue().negate();
    }

    @Override
    public Value not() {
        return computedValue().not();
    }

    @Override
    public Value or(Value value) {
        return computedValue().or(value);
    }

    @Override
    public Value and(Value value) {
        return computedValue().and(value);
    }

    @Override
    public Value largerOrEqual(Value value) {
        return computedValue().largerOrEqual(value);
    }

    @Override
    public Value larger(Value value) {
        return computedValue().larger(value);
    }

    @Override
    public Value smallerOrEqual(Value value) {
        return computedValue().smallerOrEqual(value);
    }

    @Override
    public Value smaller(Value value) {
        return computedValue().smaller(value);
    }

    @Override
    public Value approxEqual(Value value) {
        return computedValue().approxEqual(value);
    }

    @Override
    public Value notEqual(Value value) {
        return computedValue().notEqual(value);
    }

    @Override
    public Value equal(Value value) {
        return computedValue().equal(value);
    }

    @Override
    public Value add(Value value) {
        return computedValue().add(value);
    }

    @Override
    public Value subtract(Value value) {
        return computedValue().subtract(value);
    }

    @Override
    public Value multiply(Value value) {
        return computedValue().multiply(value);
    }

    @Override
    public Value divide(Value value) {
        return computedValue().divide(value);
    }

    @Override
    public Value modulo(Value value) {
        return computedValue().modulo(value);
    }

    @Override
    public Value power(Value value) {
        return computedValue().power(value);
    }

    @Override
    public Value function(Function function, Value value) {
        return computedValue().function(function, value);
    }

    @Override
    public Value asMutable() {
        return computedValue().asMutable();
    }

    @Override
    public String toString() {
        return "value of " + function;
    }

    @Override
    public boolean equals(Object other) {
        if (other == this) return true;
        if (!(other instanceof Value)) return false;
        return computedValue().equals(other);
    }

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

    LazyValue copyFor(Context context) {
        return new LazyValue(this.function, context, model);
    }

}