use of com.yahoo.document.predicate.FeatureRange 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.FeatureRange in project vespa by vespa-engine.
the class PredicateTreeAnnotatorTest method range.
private static FeatureRange range(String key, Long lower, Long upper, RangePartition... partitions) {
FeatureRange range = new FeatureRange(key, lower, upper);
Arrays.asList(partitions).forEach(range::addPartition);
return range;
}
Aggregations