aboutsummaryrefslogtreecommitdiffstats
path: root/model-evaluation/src/main/java/ai/vespa/models/evaluation/FunctionReference.java
blob: 00fcad94ce8597161e7d3c1d47c9ea4e52beca9c (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
// 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 java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * A reference to a function.
 * The function may be
 * - free: Callable from users of models, or
 * - bound: Representing a specific invocation from another ranking expression.
 * In bound functions, any arguments are replaced by the values supplied in the function invocation.
 * Function references has a serial form (textual representation) used in ranking expressions received in ranking
 * expression configurations.
 *
 * This is immutable.
 *
 * @author bratseth
 */
class FunctionReference {

    private static final Pattern referencePattern =
            Pattern.compile("rankingExpression\\(([a-zA-Z0-9_.]+)(@[a-f0-9]+\\.[a-f0-9]+)?\\)(\\.rankingScript)?");

    /** The name of the function referenced */
    private final String name;

    /** The id of the specific invocation of the function, or null if it is free */
    private final String instance;

    private FunctionReference(String name, String instance) {
        this.name = name;
        this.instance = instance;
    }

    /** Returns the name of the function referenced */
    String functionName() { return name; }

    boolean isFree() {
        return instance == null;
    }

    String serialForm() {
        return "rankingExpression(" + name + (instance != null ? instance : "") + ")";
    }

    @Override
    public String toString() { return "reference to function '" + name + "'" +
                                      ( instance != null ? " instance '" + instance + "'" : ""); }

    @Override
    public int hashCode() { return Objects.hash(name, instance); }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof FunctionReference)) return false;
        FunctionReference other = (FunctionReference)o;
        if ( ! Objects.equals(this.name, other.name)) return false;
        if ( ! Objects.equals(this.instance, other.instance)) return false;
        return true;
    }

    /** Returns a function reference from the given serial form, or empty if the string is not a valid reference */
    static Optional<FunctionReference> fromSerial(String serialForm) {
        Matcher expressionMatcher = referencePattern.matcher(serialForm);
        if ( ! expressionMatcher.matches()) return Optional.empty();

        String name = expressionMatcher.group(1);
        String instance = expressionMatcher.group(2);
        return Optional.of(new FunctionReference(name, instance));
    }

}