summaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/Reference.java
blob: 05562f9c933caf3b29bcf5b5d16055ac9c501a9d (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
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression;

import com.yahoo.searchlib.rankingexpression.rule.Arguments;
import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode;
import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode;
import com.yahoo.tensor.evaluation.TypeContext;

import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * A reference to a feature, function, or value in ranking expressions
 *
 * @author bratseth
 */
public class Reference extends TypeContext.Name {

    private final String name;
    private final Arguments arguments;

    /** The output, or null if none */
    private final String output;

    public Reference(String name, Arguments arguments, String output) {
        super(name);
        Objects.requireNonNull(name, "name cannot be null");
        Objects.requireNonNull(arguments, "arguments cannot be null");
        this.name = name;
        this.arguments = arguments;
        this.output = output;
    }

    public String name() { return name; }
    public Arguments arguments() { return arguments; }
    public String output() { return output; }

    /** Creates a reference to a simple feature consisting of a name and a single argument */
    public static Reference simple(String name, String argumentValue) {
        return new Reference(name,
                             new Arguments(new ReferenceNode(argumentValue)),
                             null);
    }

    /**
     * Returns the given simple feature as a reference, or empty if it is not a valid simple
     * feature string on the form name(argument).
     */
    public static Optional<Reference> simple(String feature) {
        int startParenthesis = feature.indexOf('(');
        if (startParenthesis < 0)
            return Optional.empty();
        int endParenthesis = feature.lastIndexOf(')');
        String featureName = feature.substring(0, startParenthesis);
        if (startParenthesis < 1 || endParenthesis < startParenthesis) return Optional.empty();
        String argument = feature.substring(startParenthesis + 1, endParenthesis);
        if (argument.startsWith("'") || argument.startsWith("\""))
            argument = argument.substring(1);
        if (argument.endsWith("'") || argument.endsWith("\""))
            argument = argument.substring(0, argument.length() - 1);
        return Optional.of(simple(featureName, argument));
    }

    /** Returns whether this is a simple identifier - no arguments or output */
    public boolean isIdentifier() {
        return this.arguments.expressions().size() == 0 && output == null;
    }

    public Reference withArguments(Arguments arguments) {
        return new Reference(name, arguments, output);
    }

    public Reference withOutput(String output) {
        return new Reference(name, arguments, output);
    }

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

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

    @Override
    public String toString() {
        StringBuilder b = new StringBuilder(name);
        if (arguments != null && arguments.expressions().size() > 0)
            b.append("(").append(arguments.expressions().stream().map(ExpressionNode::toString).collect(Collectors.joining(","))).append(")");
        if (output !=null)
            b.append(".").append(output);
        return b.toString();
    }

}