aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/IfNode.java
blob: 1b429de0be58e9190c02c9584060ae9b176a20d5 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchlib.rankingexpression.rule;

import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;

import java.util.*;

/**
 * A conditional branch of a ranking expression.
 *
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 * @author bratseth
 */
public final class IfNode extends CompositeNode {

    /** The expression nodes that make up this condition. */
    private final ExpressionNode condition, trueExpression, falseExpression;

    private final Double trueProbability;

    public IfNode(ExpressionNode condition, ExpressionNode trueExpression, ExpressionNode falseExpression) {
        this(condition, trueExpression, falseExpression, null);
    }

    /**
     * Creates a new condition node.
     *
     * @param condition the condition of this
     * @param trueExpression  the expression to evaluate if the comparison is true
     * @param falseExpression the expression to evaluate if the comparison is false
     * @param trueProbability the probability that the condition will evaluate to true, or null if not known.
     * @throws IllegalArgumentException if trueProbability is non-null and not between 0.0 and 1.0
     */
    public IfNode(ExpressionNode condition, ExpressionNode trueExpression, ExpressionNode falseExpression,
                  Double trueProbability) {
        if (trueProbability != null && ( trueProbability < 0.0 || trueProbability > 1.0) )
            throw new IllegalArgumentException("trueProbability must be a between 0.0 and 1.0, not " + trueProbability);
        this.condition = condition;
        this.trueProbability = trueProbability;
        this.trueExpression = trueExpression;
        this.falseExpression = falseExpression;
    }

    @Override
    public List<ExpressionNode> children() {
        List<ExpressionNode> children = new ArrayList<ExpressionNode>(4);
        children.add(condition);
        children.add(trueExpression);
        children.add(falseExpression);
        return Collections.unmodifiableList(children);
    }

    public ExpressionNode getCondition() { return condition; }

    public ExpressionNode getTrueExpression() { return trueExpression; }

    public ExpressionNode getFalseExpression() { return falseExpression; }

    /** The average probability that the condition of this node will evaluate to true, or null if not known */
    public Double getTrueProbability() { return trueProbability; }

    @Override
    public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
        return "if (" +
               condition.toString(context, path, this) + ", " +
               trueExpression.toString(context, path, this) + ", " +
               falseExpression.toString(context, path, this) +
                (trueProbability != null ? ", " + trueProbability : "") + ")";
    }

    @Override
    public Value evaluate(Context context) {
        if (condition.evaluate(context).asBoolean())
            return trueExpression.evaluate(context);
        else
            return falseExpression.evaluate(context);
    }

    @Override
    public IfNode setChildren(List<ExpressionNode> children) {
        if (children.size() != 3) throw new IllegalArgumentException("Expected 3 children but got " + children.size());
        return new IfNode(children.get(0), children.get(1), children.get(2));
    }

}