Adding tests to a condition
About this task
You can add tests to conditions when you write technical rules in IRL.
Procedure
- To add standard tests to a condition, use the following
pattern:
[variable:] ClassName(<test> [; <test>]*); not ClassName(<test> [; <test>]*); collect ClassName(<test> [; <test>]*); exists ClassName(<test> [; <test>]*); -
To write a test is based on the attribute value of an object that is bound in the current
condition, use the following pattern:
attribute <operator> <value> attribute booleanmethod <value> attribute.method (<arguments>) <operator> <value>In this example, the rule applies to all accounts whose balance is over 2,000, with a currency in Euros (EUR) and a date greater than the current date.
when { account: Account( balance > 2000; currency equals Currency.EUR; date.compareTo(currentDate) > 0); } -
To write a test that is based on the return value of the method that is called on an object
that is bound in the current condition, use the following pattern:
non-void-method(<arguments>) <operator> <value> non-void-method(<arguments>) booleanmethod <value> // only in case of a method with a single argument, returning a Boolean: attribute.method(<arguments>)<operator> <value>In this example, the rule applies to all accounts whose balance is over 2,000, with a currency in Euros and a date greater than the current date. This test is the same as the test on attribute values, but it uses methods instead.
when { account: Account( getBalance()>2000; getCurrency() equals Currency.EUR; getDate().compareTo(currentDate) > 0); } - To write a test based on a value that belongs (
in) or not (!in) to a set of values, use the following pattern:<value> in|!in <group> <value> in|!in {value1 [,value]*}Where
<group>is a collection or an array.In this example, the rule applies for all accounts whose currency is in Euros or U.S. dollars (USD), and for all contracts whose status does not belong to the list of returned methods.
when { account : Account(getCurrency() in { Currency.EUR, Currency.USD}); contract : Contract( status !in getBlockedStatusList() ); } - To write a condition with a join test, use the following
pattern:
variable1: ClassName1(<tests>); variable2: ClassName2(<tests using variable1>); [variableP: ClassNameP(<tests using variable1, 2, …>);]Note: A join test uses one or more variables that were defined in previous conditions.In this example, the rule applies for all purchases that are related to a contract, and for which an account exists in the same currency as the purchase.
when { purchase : Purchase(pcurrency : getCurrency()); contract : Contract(getID() equals purchase.getContractID()); Account(getContractID() equals contract.getID(); getCurrency() == pcurrency); }