Search in sources :

Example 6 with Conjunction

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

the class PredicateFieldValueSerializationTestCase method requireThatPredicateDeserializationMatchesCpp.

@Test
public void requireThatPredicateDeserializationMatchesCpp() throws IOException {
    assertDeserialize("foo_in_bar_and_baz_in_cox", new Conjunction(new FeatureSet("foo", "bar"), new FeatureSet("baz", "cox")));
    assertDeserialize("foo_in_bar_or_baz_in_cox", new Disjunction(new FeatureSet("foo", "bar"), new FeatureSet("baz", "cox")));
    assertDeserialize("foo_in_6_9", new FeatureRange("foo", 6L, 9L));
    assertDeserialize("foo_in_6_x", new FeatureRange("foo", 6L, null));
    assertDeserialize("foo_in_x_9", new FeatureRange("foo", null, 9L));
    assertDeserialize("foo_in_x_x", new FeatureRange("foo", null, null));
    assertDeserialize("foo_in_x", new FeatureSet("foo"));
    assertDeserialize("foo_in_bar", new FeatureSet("foo", "bar"));
    assertDeserialize("foo_in_bar_baz", new FeatureSet("foo", "bar", "baz"));
    assertDeserialize("not_foo_in_bar", new Negation(new FeatureSet("foo", "bar")));
    assertDeserialize("true", new BooleanPredicate(true));
    assertDeserialize("false", new BooleanPredicate(false));
}
Also used : Disjunction(com.yahoo.document.predicate.Disjunction) Negation(com.yahoo.document.predicate.Negation) Conjunction(com.yahoo.document.predicate.Conjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet) BooleanPredicate(com.yahoo.document.predicate.BooleanPredicate) Test(org.junit.Test)

Example 7 with Conjunction

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

the class PredicateFieldValueSerializationTestCase method requireThatPredicateFieldValuesAreDeserialized.

@Test
public void requireThatPredicateFieldValuesAreDeserialized() {
    Document prevDocument = docFactory.createDocument();
    PredicateFieldValue prevPredicate = new PredicateFieldValue(new Conjunction(new FeatureSet("foo", "bar"), new FeatureRange("baz", 6L, 9L)));
    prevDocument.setFieldValue(PREDICATE_FIELD, prevPredicate);
    byte[] buf = serializeDocument(prevDocument);
    Document nextDocument = deserializeDocument(buf, docFactory);
    assertEquals(prevDocument, nextDocument);
    assertEquals(prevPredicate, nextDocument.getFieldValue(PREDICATE_FIELD));
}
Also used : Conjunction(com.yahoo.document.predicate.Conjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet) Document(com.yahoo.document.Document) SerializationTestUtils.serializeDocument(com.yahoo.document.serialization.SerializationTestUtils.serializeDocument) SerializationTestUtils.deserializeDocument(com.yahoo.document.serialization.SerializationTestUtils.deserializeDocument) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) Test(org.junit.Test)

Example 8 with Conjunction

use of com.yahoo.document.predicate.Conjunction 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 9 with Conjunction

use of com.yahoo.document.predicate.Conjunction 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 10 with Conjunction

use of com.yahoo.document.predicate.Conjunction 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)

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