// 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.google.common.collect.ImmutableList;
import com.yahoo.searchlib.rankingexpression.evaluation.BooleanValue;
import com.yahoo.searchlib.rankingexpression.evaluation.Context;
import com.yahoo.searchlib.rankingexpression.evaluation.Value;

import java.util.*;

/**
 * A node which returns true or false depending on a set membership test
 *
 * @author bratseth
 */
public class SetMembershipNode extends BooleanNode {

    private final ExpressionNode testValue;

    private final List<ExpressionNode> setValues;

    public SetMembershipNode(ExpressionNode testValue, List<ExpressionNode> setValues) {
        this.testValue = testValue;
        this.setValues = ImmutableList.copyOf(setValues);
    }

    /** The value to check for membership in the set */
    public ExpressionNode getTestValue() { return testValue; }

    /** Returns an immutable list of the values of the set */
    public List<ExpressionNode> getSetValues() { return setValues; }

    @Override
    public List<ExpressionNode> children() {
        ArrayList<ExpressionNode> children = new ArrayList<>();
        children.add(testValue);
        children.addAll(setValues);
        return children;
    }

    @Override
    public String toString(SerializationContext context, Deque<String> path, CompositeNode parent) {
        StringBuilder b = new StringBuilder(testValue.toString(context, path, this));
        b.append(" in [");
        for (int i = 0, len = setValues.size(); i < len; ++i) {
            b.append(setValues.get(i).toString(context, path, this));
            if (i < len - 1) {
                b.append(", ");
            }
        }
        b.append("]");
        return b.toString();
    }

    @Override
    public Value evaluate(Context context) {
        Value value = testValue.evaluate(context);
        for (ExpressionNode setValue : setValues) {
            if (setValue.evaluate(context).equals(value))
                return new BooleanValue(true);
        }
        return new BooleanValue(false);
    }

    @Override
    public SetMembershipNode setChildren(List<ExpressionNode> children) {
        if (children.size()<1) throw new IllegalArgumentException("A set membership test must have at least 1 child");
        return new SetMembershipNode(children.get(0), children.subList(1, children.size()));
    }

}

@@ -408,34 +408,227 @@
 ion.
+Tensor
 Value;%0A
-%0Aimport java.util.*
+import com.yahoo.searchlib.rankingexpression.evaluation.Value;%0Aimport com.yahoo.tensor.Tensor;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Deque;%0Aimport java.util.List;%0Aimport java.util.function.Predicate
 ;%0A%0A/
@@ -2176,51 +2176,121 @@
     
-for (ExpressionNode setValue : setValues) %7B
+if (value instanceof TensorValue) %7B%0A            return evaluateTensor(((TensorValue) value).asTensor(), context);
 %0A   
@@ -2298,33 +2298,89 @@
     
+%7D%0A
     
-if (set
+    return evaluateValue(value, context);%0A    %7D%0A%0A    private 
 Value
-.
+ 
 evaluate
 (con
@@ -2379,88 +2379,667 @@
 uate
-(context).equals(value
+Value(Value value, Context context) %7B%0A        return new BooleanValue(testMembership(value::equals, context
 ))
+;
 %0A    
+%7D%0A%0A
     
-        return new BooleanValue(true
+private Value evaluateTensor(Tensor tensor, Context context) %7B%0A        return new TensorValue(tensor.map((value) -%3E contains(value, context) ? 1.0 : 0.0)
 );%0A    
+%7D%0A%0A
     
+private boolean contains(double value, Context context) %7B%0A        return testMembership((setValue) -%3E setValue.asDouble() == value, context);%0A    %7D%0A%0A    private boolean testMembership(Predicate%3CValue%3E test, Context context) %7B%0A        for (ExpressionNode setValue : setValues) %7B%0A            if (test.test(setValue.evaluate(context)))%0A                return true;%0A        
 %7D%0A  
@@ -3055,31 +3055,13 @@
 urn 
-new BooleanValue(
 false
-)
 ;%0A  

Add Java ranking set membership for tensors