aboutsummaryrefslogtreecommitdiffstats
path: root/model-integration/src/main/java/ai/vespa/rankingexpression/importer/operations/Switch.java
blob: 502f076935037ddba9b114df7def20e0da3cdc75 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.rankingexpression.importer.operations;

import ai.vespa.rankingexpression.importer.OrderedTensorType;
import com.yahoo.searchlib.rankingexpression.Reference;
import com.yahoo.tensor.functions.TensorFunction;

import java.util.List;
import java.util.Optional;

public class Switch extends IntermediateOperation {

    private final int port;

    public Switch(String modelName, String nodeName, List<IntermediateOperation> inputs, int port) {
        super(modelName, nodeName, inputs);
        this.port = port;
    }

    @Override
    protected OrderedTensorType lazyGetType() {
        if (!allInputTypesPresent(2)) {
            return null;
        }
        Optional<OrderedTensorType> predicate = inputs.get(1).type();
        if (predicate.get().type().rank() != 0) {
            throw new IllegalArgumentException("Switch in " + name + ": predicate must be a scalar");
        }
        return inputs.get(0).type().orElse(null);
    }

    @Override
    protected TensorFunction<Reference> lazyGetFunction() {
        IntermediateOperation predicateOperation = inputs().get(1);
        if (!predicateOperation.getConstantValue().isPresent()) {
            throw new IllegalArgumentException("Switch in " + name + ": predicate must be a constant");
        }
        if (port < 0 || port > 1) {
            throw new IllegalArgumentException("Switch in " + name + ": choice should be boolean");
        }

        double predicate = predicateOperation.getConstantValue().get().asDouble();
        return predicate == port ? inputs().get(0).function().get() : null;
    }

    @Override
    public Switch withInputs(List<IntermediateOperation> inputs) {
        return new Switch(modelName(), name(), inputs, port);
    }

    @Override
    public String operationName() { return "Switch"; }

}