aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/treenet/rule/Condition.java
blob: 5be2de43d73a9699527992ac46e5e8d0075817a9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.treenet.rule;

import java.util.Iterator;

/**
 * Represents a condition
 *
 * @author  bratseth
 */
public abstract class Condition extends TreeNode {

    private final String leftValue;
    private final String trueLabel;
    private final String falseLabel;

    public Condition(String leftValue, String trueLabel, String falseLabel) {
        this.leftValue = leftValue;
        this.trueLabel = trueLabel;
        this.falseLabel = falseLabel;
    }

    /** Returns the name of the feature to compare to a constant. */
    public String getLeftValue() { return leftValue; }

    /** Return the label to jump to if this condition is true. */
    public String getTrueLabel() { return trueLabel; }

    /** Return the label to jump to if this condition is false. */
    public String getFalseLabel() { return falseLabel; }

    @Override
    public final String toRankingExpression() {
        StringBuilder b = new StringBuilder("if (");
        b.append(getLeftValue());
        b.append(" ");
        b.append(conditionToRankingExpression());
        b.append(", ");
        b.append(getParent().getNodes().get(getTrueLabel()).toRankingExpression());
        b.append(", ");
        b.append(getParent().getNodes().get(getFalseLabel()).toRankingExpression());
        b.append(")");
        return b.toString();
    }

    /**
     * Returns the ranking expression string for the condition part of this condition, i.e the ... part of
     * <pre>
     *     if(leftValue ..., trueExpression, falseExpression)
     * </pre>
     */
    protected abstract String conditionToRankingExpression();

}