aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/intent/model/Node.java
blob: 4a49db240a0ace78f23e04f7154f6c60a6f3f20f (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.intent.model;

import java.util.Map;

/**
 * A node in the intent model tree
 *
 * @author bratseth
 */
public abstract class Node implements Comparable<Node> {

    /**
     * The score, unless getScore/setScore is overridden which is the case with interpretations,
     * so DO NOT ACCESS SCORE DIRECTLY, ALWAYS USE GET/SET
     */
    private double score;

    public Node(double score) {
        this.score = score;
    }

    /** Returns the normalized (0-1) score of this node */
    public double getScore() { return score; }

    /** Sets the normalized (0-1) score of this node */
    public void setScore(double score) { this.score = score; }

    /** Increases this score by an increment and returns the new score */
    public double increaseScore(double increment) {
        setScore(getScore() + increment);
        return getScore();
    }

    @Override
    public int compareTo(Node other) {
        if (this.getScore() < other.getScore()) return 1;
        if (this.getScore() > other.getScore()) return -1;
        return 0;
    }

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

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

    /**
     * Adds the sources at (and beneath) this node to the given
     * sparsely represented source vector, weighted by the score of this node
     * times the given weight from the parent path
     */
    abstract void addSources(double weight, Map<Source, SourceNode> sources);

}