summaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/RankingConstant.java
blob: 377e19eedf211ee334cdb5b2e1cfc248b46d8169 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.tensor.TensorType;

/**
 * A global ranking constant distributed using file distribution.
 * Ranking constants must be sent to some services to be useful - this is done
 * by calling the sentTo method during the prepare phase of building models.
 *
 * @author arnej
 * @author bratseth
 */
public class RankingConstant extends DistributableResource {
    private TensorType tensorType = null;

    public RankingConstant(String name) {
        super(name);
    }

    public RankingConstant(String name, TensorType type, String fileName) {
        this(name, type, fileName, PathType.FILE);
    }
    public RankingConstant(String name, TensorType type, String fileName, PathType pathType) {
        super(name, fileName, pathType);
        this.tensorType = type;
        validate();
    }

    public void setType(TensorType type) {
        this.tensorType = type;
    }

    public TensorType getTensorType() { return tensorType; }
    public String getType() { return tensorType.toString(); }

    public void validate() {
        super.validate();
        if (tensorType == null)
            throw new IllegalArgumentException("Ranking constant '" + getName() + "' must have a type.");
        if (tensorType.dimensions().stream().anyMatch(d -> d.isIndexed() && d.size().isEmpty()))
            throw new IllegalArgumentException("Illegal type in field " + getName() + " type " + tensorType +
                                               ": Dense tensor dimensions must have a size");
    }

    public String toString() {
        StringBuilder b = new StringBuilder(super.toString())
                .append("' of type '").append(tensorType).append("'");
        return b.toString();
    }

}