aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/EmbracedNode.java
blob: fd9fab99db8d09c4a2f9418b3e9a9f1d8b631bad (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
// 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 com.yahoo.tensor.TensorType;
import com.yahoo.tensor.evaluation.TypeContext;

import java.util.Collections;
import java.util.Deque;
import java.util.List;

/**
 * This class represents another expression enclosed in braces.
 *
 * @author Simon Thoresen
 */
public final class EmbracedNode extends CompositeNode {

    // The node to embrace.
    private final ExpressionNode value;

    /**
     * Creates a new expression node that embraces another.
     *
     * @param value The node to embrace.
     */
    public EmbracedNode(ExpressionNode value) {
        this.value=value;
    }

    /** Returns the node enclosed by this */
    public ExpressionNode getValue() { return value; }

    @Override
    public List<ExpressionNode> children() {
        return Collections.singletonList(value);
    }

    @Override
    public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
        String expression = value.toString(context, path, this);
        if (value instanceof ReferenceNode)  return expression;
        return  "(" + expression + ")";
    }

    @Override
    public Value evaluate(Context context) {
        return value.evaluate(context);
    }

    @Override
    public TensorType type(TypeContext context) {
        return value.type(context);
    }

    @Override
    public CompositeNode setChildren(List<ExpressionNode> newChildren) {
        if (newChildren.size() != 1)
            throw new IllegalArgumentException("Expected 1 child but got " + newChildren.size());
        return new EmbracedNode(newChildren.get(0));
    }

}