use of com.yahoo.document.predicate.RangePartition in project vespa by vespa-engine.
the class PredicateTreeAnnotator method assignIntervalLabels.
/**
* Visits the predicate tree in depth-first order and assigns intervals for features in
* {@link com.yahoo.document.predicate.FeatureSet} and {@link com.yahoo.document.predicate.FeatureRange}.
*/
private static void assignIntervalLabels(Predicate predicate, int begin, int end, boolean isNegated, AnnotatorContext context) {
// Otherwise, conjunctions and disjunctions must be switched if negated (De Morgan's law).
if (predicate instanceof Conjunction) {
List<Predicate> children = ((Conjunction) predicate).getOperands();
int current = begin;
for (int i = 0; i < children.size(); i++) {
Predicate child = children.get(i);
int subTreeSize = context.subTreeSizes.get(child);
if (i == children.size() - 1) {
// Last child (and sometimes the only one)
assignIntervalLabels(child, current, end, isNegated, context);
// No need to update/touch current since this is the last child.
} else if (i == 0) {
// First child
int next = context.leftNodeLeaves + subTreeSize + 1;
assignIntervalLabels(child, current, next - 1, isNegated, context);
current = next;
} else {
// Middle children
int next = current + subTreeSize;
assignIntervalLabels(child, current, next - 1, isNegated, context);
current = next;
}
}
} else if (predicate instanceof FeatureConjunction) {
// Register FeatureConjunction as it was a FeatureSet with a single child.
// Note: FeatureConjunction should never be negated as AndOrSimplifier will push negations down to
// the leafs (FeatureSets).
int zStarEnd = isNegated ? calculateZStarIntervalEnd(end, context) : end;
IndexableFeatureConjunction indexable = new IndexableFeatureConjunction((FeatureConjunction) predicate);
int interval = Interval.fromBoundaries(begin, zStarEnd);
context.featureConjunctions.computeIfAbsent(indexable, (k) -> new ArrayList<>()).add(interval);
if (isNegated) {
registerZStarInterval(begin, end, zStarEnd, context);
}
context.leftNodeLeaves += 1;
} else if (predicate instanceof Disjunction) {
// the values will be same as that of the parent OR node
for (Predicate child : ((Disjunction) predicate).getOperands()) {
assignIntervalLabels(child, begin, end, isNegated, context);
}
} else if (predicate instanceof FeatureSet) {
FeatureSet featureSet = (FeatureSet) predicate;
int zStarEnd = isNegated ? calculateZStarIntervalEnd(end, context) : end;
for (String value : featureSet.getValues()) {
long featureHash = Feature.createHash(featureSet.getKey(), value);
int interval = Interval.fromBoundaries(begin, zStarEnd);
registerFeatureInterval(featureHash, interval, context.intervals);
}
if (isNegated) {
registerZStarInterval(begin, end, zStarEnd, context);
}
context.leftNodeLeaves += 1;
} else if (predicate instanceof Negation) {
assignIntervalLabels(((Negation) predicate).getOperand(), begin, end, !isNegated, context);
} else if (predicate instanceof FeatureRange) {
FeatureRange featureRange = (FeatureRange) predicate;
int zStarEnd = isNegated ? calculateZStarIntervalEnd(end, context) : end;
int interval = Interval.fromBoundaries(begin, zStarEnd);
for (RangePartition partition : featureRange.getPartitions()) {
long featureHash = PredicateHash.hash64(partition.getLabel());
registerFeatureInterval(featureHash, interval, context.intervals);
}
for (RangeEdgePartition edgePartition : featureRange.getEdgePartitions()) {
long featureHash = PredicateHash.hash64(edgePartition.getLabel());
IntervalWithBounds intervalWithBounds = new IntervalWithBounds(interval, (int) edgePartition.encodeBounds());
registerFeatureInterval(featureHash, intervalWithBounds, context.intervalsWithBounds);
}
if (isNegated) {
registerZStarInterval(begin, end, zStarEnd, context);
}
context.leftNodeLeaves += 1;
} else {
throw new UnsupportedOperationException("Cannot handle predicate of type " + predicate.getClass().getSimpleName());
}
}
use of com.yahoo.document.predicate.RangePartition in project vespa by vespa-engine.
the class ComplexNodeTransformerTest method requireThatExistingPartitionsAreCleared.
@Test
public void requireThatExistingPartitionsAreCleared() {
testConvert("foo in [10..19]", "foo in [10..19 (foo=10-19)]");
Predicate p = Predicate.fromString("foo in [10..19]");
((FeatureRange) p).addPartition(new RangePartition("foo", 10000L, 20000L, false));
ComplexNodeTransformer tranformer = new ComplexNodeTransformer();
Predicate converted = tranformer.process(p, new PredicateOptions(10, lowerBound, upperBound));
assertEquals("foo in [10..19 (foo=10-19)]", converted.toString());
}
Aggregations