Browse files

Intelligently merge filter rules to cope with Not / Any / All mixes

Filter predicates are now merged in a smarter way. Previously, selecting two NOT filters would result in both being ignored as they were merged in the predicate pipeline with an OR compound. NOT filters are now being merged using an AND compound.
  • Loading branch information...
1 parent b03e4a0 commit b9c472cda2bf66a72405270fca418ad7cbe398b9 @fpillet fpillet committed May 4, 2011
Showing with 29 additions and 4 deletions.
  1. +29 −4 Desktop Viewer/Classes/LoggerWindowController.m
View
33 Desktop Viewer/Classes/LoggerWindowController.m
@@ -299,9 +299,31 @@ - (NSPredicate *)filterPredicateFromCurrentSelection
// the current filter is the aggregate (OR clause) of all the selected filters
NSArray *predicates = [[filterListController selectedObjects] valueForKey:@"predicate"];
if (![predicates count])
- return [NSPredicate predicateWithValue:YES];
+ return nil;
if ([predicates count] == 1)
return [predicates lastObject];
+
+ // Isolate the NOT type predicates, merge predicates this way:
+ // result = (AND all NOT predicates)) AND (OR all ANY/ALL predicates)
+ NSMutableArray *anyAllPredicates = [NSMutableArray arrayWithCapacity:[predicates count]];
+ NSMutableArray *notPredicates = [NSMutableArray arrayWithCapacity:[predicates count]];
+ for (NSCompoundPredicate *pred in predicates)
+ {
+ if ([pred isKindOfClass:[NSCompoundPredicate class]] && [pred compoundPredicateType] == NSNotPredicateType)
+ [notPredicates addObject:pred];
+ else
+ [anyAllPredicates addObject:pred];
+ }
+ if ([notPredicates count] && [anyAllPredicates count])
+ {
+ return [NSCompoundPredicate andPredicateWithSubpredicates:
+ [NSArray arrayWithObjects:
+ [NSCompoundPredicate andPredicateWithSubpredicates:notPredicates],
+ [NSCompoundPredicate orPredicateWithSubpredicates:anyAllPredicates],
+ nil]];
+ }
+ if ([notPredicates count])
+ return [NSCompoundPredicate andPredicateWithSubpredicates:notPredicates];
return [NSCompoundPredicate orPredicateWithSubpredicates:predicates];
}
@@ -367,11 +389,14 @@ - (void)updateFilterPredicate
}
if ([andPredicates count])
{
- [andPredicates addObject:p];
+ if (p != nil)
+ [andPredicates addObject:p];
p = [NSCompoundPredicate andPredicateWithSubpredicates:andPredicates];
}
- // @@@ TODO: eliminate TRUEPREDICATE (p is constant value YES) by testing it and not assembling the OR predicate as needed
- p = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:[self alwaysVisibleEntriesPredicate], p, nil]];
+ if (p == nil)
+ p = [NSPredicate predicateWithValue:YES];
+ else
+ p = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:[self alwaysVisibleEntriesPredicate], p, nil]];
[filterPredicate autorelease];
filterPredicate = [p retain];
[andPredicates release];

0 comments on commit b9c472c

Please sign in to comment.