Recursive EOQualifier
Contents |
Why ?
Sometimes it can be useful, especially in tree structures, to apply a EOQualifier recursively to a number of objects. In this example we want to filter out all leafs that do not match the string and nodes, that have no children with this string.
This of course only works for in memory qualification.
How it's done
Filter a tree structure, where leafs and nodes have a title. Leafs return nil for children, nodes return an array of leafs and nodes. filteredArrayUsingQualifier: is a method on NSArray that evaluates each contained object with the qualifier.
The use of EOQualifierVariable and some special code in EOQualifier, we can make the qualifier appear recursively in itself:
q = [EOQualifier qualifierWithQualifierFormat:@"title caseinsensitivelike %@ or children filteredArrayUsingQualifier: $self", s]; dictionary = [NSDictionary dictionaryWithObject:q forKey:@"self"]; q = [q qualifierWithBindings:dictionary requiresAllVariables:YES];
What it does
Assume the following structure
+-node title="bar"
|
+-leaf title="foo"
|
+-node title="baz"
| |
| +-leaf title="fooble"
|
| +-leaf title="frob"
|
+-node title="other"
|
+-leaf title="sother"
so the node named bar is the root and has children ("foo" "baz" "other"), "baz" has children ( "fooble", "frob"), other has child ("sother").
Lets use for qualification the string "*oo*" to match "fooble" and "foo". Because "other" will return an empty array from the filteredArrayUsingQualifier: clause, which evaluates to NO and also doesn't match "*oo*", "other" will be excluded. Node "baz" will be included, because one leaf "fooble" matches and one of the OR clauses has been satisfied. The leaf "frob" will be filtered away though. Leaf "foo" also matches and consequently, because "baz" and "foo" have matched, "bar" also matches.
The filtered tree will therefore be:
+-node title="bar"
|
+-leaf title="foo"
|
+-node title="baz"
|
+-leaf title="fooble"
TODO
Needs more text. Probably only works in MulleEOF.