aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search-core/src/main/java/com/yahoo/document/predicate/FeatureConjunction.java
blob: 4522a19d1ae407d2d54adb9ce2f533395617ec3e (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * A FeatureConjunction is a special type of Conjunction where
 * all children are either a FeatureSet or a Negation (with an underlying FeatureSet).
 * The underlying FeatureSets may only have one value.
 *
 * @author bjorncs
 */
public class FeatureConjunction extends PredicateOperator {

    private final List<Predicate> operands;

    public FeatureConjunction(List<Predicate> operands) {
        validateOperands(operands);
        this.operands = new ArrayList<>(operands);
    }

    private static void validateOperands(List<Predicate> operands) {
        if (operands.size() <= 1) {
            throw new IllegalArgumentException("Number of operands must 2 or more, was: " + operands.size());
        }
        if (!operands.stream()
                .allMatch(FeatureConjunction::isValidFeatureConjunctionOperand)) {
            throw new IllegalArgumentException(
                    "A FeatureConjunction may only contain instances of Negation and FeatureSet, " +
                            "and a FeatureSet may only have one value.");
        }

        long uniqueKeys = operands.stream().map(FeatureConjunction::getFeatureSetKey).distinct().count();
        if (operands.size() > uniqueKeys) {
            throw new IllegalArgumentException("Each FeatureSet key must have a unique key.");
        }
    }

    private static String getFeatureSetKey(Predicate predicate) {
        if (predicate instanceof FeatureSet) {
            return ((FeatureSet) predicate).getKey();
        } else {
            Negation negation = (Negation) predicate;
            return ((FeatureSet) negation.getOperand()).getKey();
        }
    }

    public static boolean isValidFeatureConjunctionOperand(Predicate operand) {
        return operand instanceof Negation
                    && ((Negation) operand).getOperand() instanceof FeatureSet
                    && isValidFeatureConjunctionOperand(((Negation) operand).getOperand())
                || operand instanceof FeatureSet && ((FeatureSet) operand).getValues().size() == 1;
    }

    @Override
    public List<Predicate> getOperands() {
        return operands;
    }

    @Override
    protected void appendTo(StringBuilder out) {
        for (Iterator<Predicate> it = operands.iterator(); it.hasNext(); ) {
            Predicate operand = it.next();
            if (operand instanceof Disjunction) {
                out.append('(');
                operand.appendTo(out);
                out.append(')');
            } else {
                operand.appendTo(out);
            }
            if (it.hasNext()) {
                out.append(" conj ");
            }
        }
    }

    @Override
    public FeatureConjunction clone() throws CloneNotSupportedException {
        return new FeatureConjunction(operands);
    }

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

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof FeatureConjunction)) {
            return false;
        }
        FeatureConjunction rhs = (FeatureConjunction)obj;
        if (!operands.equals(rhs.operands)) {
            return false;
        }
        return true;
    }

}