Multiple filters on collections
- Last Updated: June 9, 2021
- 3 minute read
- Corticon.js
- Documentation
A slightly more complicated example will be constructed by adding a third conditional expression to the rule.
Once again, the Rulesheets differ only in the location of a conditional expression. In the first rulesheet, the gender test is modeled in the second conditional row, whereas in the other rulesheet (Rulesheet with two filters), it is implemented in the second filter row. Does this difference have an effect on rule execution? Build a Ruletest and use it to test the Rulesheet in Rulesheet with two conditions first.
As you see in this figure, the combination of a condition that uses a
collection operator (the size test) with another condition that does not (the gender test)
produces an interesting result. What appears to have happened is that, for a collection of
more than three skydivers, all females in that group were assigned a riskRating of high. Step-by-step, here is what
the rules engine did:
- The filter screened the collection of persons (represented by the alias
person) forskydivers. - If there are more than three surviving elements in
person(that is,skydivers), then all females in the filtered collection are assigned ariskRatingvalue ofhigh. It may be helpful to think of the rules engine checking to make sure there are more than three surviving elements, then reviewing those whose gender is female, and assigningriskRatingone element at a time.
Expressed as a plain-language rule statement, the Rulesheet implements the following rule statement:
![]()
It is important to note that conditions do not have the same filtering effect on collections that Filter expressions do, and the order of conditions in a rule has no effect on rule execution.
Now that you understand the results in the Ruletest for Rulesheet with 2 Conditions, look at what our second Rulesheet produces.
This time, no riskRating assignments were made
to any element of collection person. Why? Because multiple
filters are logically AND'ed together, forming a compound filter. In
order to survive the compound filter, elements of collection person must be both skydivers AND female. Elements that
survive this compound filter pass through to the size test in the Condition/Action rule,
where they are counted. If there are more than three remaining, then all surviving elements
are assigned a riskRating value of high. Rephrased, the Rulesheet implements the following rule statement:
![]()
To confirm that you understand how the rules engine executes this Rulesheet, modify the Ruletest and rerun:
That Ruletest includes four female skydivers, so, if you understand our rules
correctly, you expect all four to pass through the compound filter, and then satisfy the
size test in the conditions. This test should result in all four surviving elements
receiving a riskRating of high. That test confirms that the expectation is correct.