Search in sources :

Example 6 with Negation

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

the class PredicateTreeAnalyzer method findMinFeature.

// Second analysis pass. Traverses tree in depth-first order. Determines the min-feature value.
private static double findMinFeature(Predicate predicate, boolean isNegated, AnalyzerContext context) {
    if (predicate instanceof Conjunction) {
        // Sum of children values.
        return ((Conjunction) predicate).getOperands().stream().mapToDouble(child -> findMinFeature(child, isNegated, context)).sum();
    } else if (predicate instanceof FeatureConjunction) {
        if (isNegated) {
            return 0.0;
        }
        // The FeatureConjunction is handled as a leaf node in the interval algorithm.
        IndexableFeatureConjunction ifc = new IndexableFeatureConjunction((FeatureConjunction) predicate);
        return 1.0 / context.conjunctionOccurrences.get(ifc.id);
    } else if (predicate instanceof Disjunction) {
        // Minimum value of children.
        return ((Disjunction) predicate).getOperands().stream().mapToDouble(child -> findMinFeature(child, isNegated, context)).min().getAsDouble();
    } else if (predicate instanceof Negation) {
        return findMinFeature(((Negation) predicate).getOperand(), !isNegated, context);
    } else if (predicate instanceof FeatureSet) {
        if (isNegated) {
            return 0.0;
        }
        double minFeature = 1.0;
        FeatureSet featureSet = (FeatureSet) predicate;
        for (String value : featureSet.getValues()) {
            long featureHash = Feature.createHash(featureSet.getKey(), value);
            // Clever mathematics to handle scenarios where same feature is used several places in predicate tree.
            minFeature = Math.min(minFeature, 1.0 / context.featureOccurrences.get(featureHash));
        }
        return minFeature;
    } else if (predicate instanceof FeatureRange) {
        if (isNegated) {
            return 0.0;
        }
        return 1.0 / context.featureOccurrences.get(PredicateHash.hash64(((FeatureRange) predicate).getKey()));
    } else {
        throw new UnsupportedOperationException("Cannot handle predicate of type " + predicate.getClass().getSimpleName());
    }
}
Also used : FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet) Map(java.util.Map) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Negation(com.yahoo.document.predicate.Negation) PredicateHash(com.yahoo.document.predicate.PredicateHash) HashMap(java.util.HashMap) Feature(com.yahoo.search.predicate.index.Feature) Conjunction(com.yahoo.document.predicate.Conjunction) Disjunction(com.yahoo.document.predicate.Disjunction) Predicate(com.yahoo.document.predicate.Predicate) Disjunction(com.yahoo.document.predicate.Disjunction) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Negation(com.yahoo.document.predicate.Negation) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Conjunction(com.yahoo.document.predicate.Conjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet)

Example 7 with Negation

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

the class OrSimplifier method simplifyTree.

public Predicate simplifyTree(Predicate predicate) {
    if (predicate instanceof Disjunction) {
        Disjunction disjunction = (Disjunction) predicate;
        List<Predicate> newChildren = disjunction.getOperands().stream().map(this::simplifyTree).collect(toList());
        return compressFeatureSets(newChildren);
    } else if (predicate instanceof Negation) {
        Negation negation = (Negation) predicate;
        negation.setOperand(simplifyTree(negation.getOperand()));
        return negation;
    } else if (predicate instanceof Conjunction) {
        Conjunction conjunction = (Conjunction) predicate;
        List<Predicate> newChildren = conjunction.getOperands().stream().map(this::simplifyTree).collect(toList());
        conjunction.setOperands(newChildren);
        return conjunction;
    } else {
        return predicate;
    }
}
Also used : Disjunction(com.yahoo.document.predicate.Disjunction) Negation(com.yahoo.document.predicate.Negation) Conjunction(com.yahoo.document.predicate.Conjunction) Predicate(com.yahoo.document.predicate.Predicate)

Example 8 with Negation

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

the class FeatureConjunctionTransformer method transform.

private static Predicate transform(Predicate predicate) {
    if (predicate instanceof Conjunction) {
        Conjunction conjunction = (Conjunction) predicate;
        conjunction.getOperands().replaceAll(FeatureConjunctionTransformer::transform);
        long nValidOperands = numberOfValidFeatureSetOperands(conjunction);
        if (nValidOperands > 1 && nValidOperands <= CONVERSION_THRESHOLD) {
            return convertConjunction(conjunction, nValidOperands);
        }
    } else if (predicate instanceof Disjunction) {
        ((Disjunction) predicate).getOperands().replaceAll(FeatureConjunctionTransformer::transform);
    } else if (predicate instanceof Negation) {
        Negation negation = (Negation) predicate;
        negation.setOperand(transform(negation.getOperand()));
    }
    return predicate;
}
Also used : Disjunction(com.yahoo.document.predicate.Disjunction) Negation(com.yahoo.document.predicate.Negation) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) Conjunction(com.yahoo.document.predicate.Conjunction)

Example 9 with Negation

use of com.yahoo.document.predicate.Negation 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)9 Disjunction (com.yahoo.document.predicate.Disjunction)9 Negation (com.yahoo.document.predicate.Negation)9 Predicate (com.yahoo.document.predicate.Predicate)6 FeatureConjunction (com.yahoo.document.predicate.FeatureConjunction)4 FeatureRange (com.yahoo.document.predicate.FeatureRange)4 FeatureSet (com.yahoo.document.predicate.FeatureSet)4 IndexableFeatureConjunction (com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BooleanPredicate (com.yahoo.document.predicate.BooleanPredicate)2 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 Feature (com.yahoo.search.predicate.index.Feature)1 IntervalWithBounds (com.yahoo.search.predicate.index.IntervalWithBounds)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1