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));
}
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));
}
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());
}
}
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;
}
}
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;
}
Aggregations