Search in sources :

Example 11 with Conjunction

use of com.yahoo.document.predicate.Conjunction in project vespa by vespa-engine.

the class BooleanSimplifier method simplifySubTree.

public Predicate simplifySubTree(Predicate predicate) {
    if (predicate == null) {
        return null;
    }
    if (predicate instanceof Conjunction) {
        List<Predicate> in = ((PredicateOperator) predicate).getOperands();
        List<Predicate> out = new ArrayList<>(in.size());
        for (Predicate operand : in) {
            operand = simplifySubTree(operand);
            if (isFalse(operand)) {
                return new BooleanPredicate(false);
            } else if (!isTrue(operand)) {
                out.add(operand);
            }
        }
        if (out.size() == 1) {
            return out.get(0);
        } else if (out.size() == 0) {
            return new BooleanPredicate(true);
        }
        ((Conjunction) predicate).setOperands(out);
    } else if (predicate instanceof Disjunction) {
        List<Predicate> in = ((PredicateOperator) predicate).getOperands();
        List<Predicate> out = new ArrayList<>(in.size());
        for (Predicate operand : in) {
            operand = simplifySubTree(operand);
            if (isTrue(operand)) {
                return new BooleanPredicate(true);
            } else if (!isFalse(operand)) {
                out.add(operand);
            }
        }
        if (out.size() == 1) {
            return out.get(0);
        } else if (out.size() == 0) {
            return new BooleanPredicate(false);
        }
        ((Disjunction) predicate).setOperands(out);
    } else if (predicate instanceof Negation) {
        Predicate operand = ((Negation) predicate).getOperand();
        operand = simplifySubTree(operand);
        if (isTrue(operand)) {
            return new BooleanPredicate(false);
        } else if (isFalse(operand)) {
            return new BooleanPredicate(true);
        }
        ((Negation) predicate).setOperand(operand);
    }
    return predicate;
}
Also used : Disjunction(com.yahoo.document.predicate.Disjunction) Negation(com.yahoo.document.predicate.Negation) Conjunction(com.yahoo.document.predicate.Conjunction) ArrayList(java.util.ArrayList) PredicateOperator(com.yahoo.document.predicate.PredicateOperator) List(java.util.List) ArrayList(java.util.ArrayList) BooleanPredicate(com.yahoo.document.predicate.BooleanPredicate) BooleanPredicate(com.yahoo.document.predicate.BooleanPredicate) Predicate(com.yahoo.document.predicate.Predicate)

Aggregations

Conjunction (com.yahoo.document.predicate.Conjunction)11 Disjunction (com.yahoo.document.predicate.Disjunction)9 Negation (com.yahoo.document.predicate.Negation)9 Predicate (com.yahoo.document.predicate.Predicate)7 FeatureConjunction (com.yahoo.document.predicate.FeatureConjunction)5 FeatureRange (com.yahoo.document.predicate.FeatureRange)5 FeatureSet (com.yahoo.document.predicate.FeatureSet)5 ArrayList (java.util.ArrayList)4 IndexableFeatureConjunction (com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction)3 List (java.util.List)3 BooleanPredicate (com.yahoo.document.predicate.BooleanPredicate)2 Map (java.util.Map)2 Test (org.junit.Test)2 Document (com.yahoo.document.Document)1 PredicateFieldValue (com.yahoo.document.datatypes.PredicateFieldValue)1 PredicateHash (com.yahoo.document.predicate.PredicateHash)1 PredicateOperator (com.yahoo.document.predicate.PredicateOperator)1 RangeEdgePartition (com.yahoo.document.predicate.RangeEdgePartition)1 RangePartition (com.yahoo.document.predicate.RangePartition)1 SerializationTestUtils.deserializeDocument (com.yahoo.document.serialization.SerializationTestUtils.deserializeDocument)1