Chooses a value based on a condition being met.
The choose expression contains:
a data type specifier (see Supported Data Types) declaring the type of value which will be chosen;
an expression which specifies the value to test against the condition in each when expression in turn. If no test expression is specified, the condition in each when expression is tested in turn to check if it returns the value true;
each contains a condition to test, and a value to return if the condition passes; and
an expression containing a value to return (so that no matter what, a value is always chosen).
The conditions are evaluated in top-down order of the when expressions, stopping at the first condition to pass the test. Subsequent conditions are not evaluated.
The choose expression reflects that of if / else if /.../ else statements typical of most programming languages. It can be effectively used to implement a decision table.
You might consider ordering your conditions so that those most likely to succeed are near the top of the list (to prevent needless calculations).
However, for more complex conditions (and thus in general), you must carefully consider whether reordering your conditions will introduce any unwanted behavior changes.
<?xml version="1.0" encoding="UTF-8"?> <RuleSet name="Example_choose" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "http://www.curamsoftware.com/CreoleRulesSchema.xsd"> <Class name="Person"> <Attribute name="age"> <type> <javaclass name="Number"/> </type> <derivation> <specified/> </derivation> </Attribute> <Attribute name="ageCategory"> <type> <javaclass name="String"/> </type> <derivation> <choose> <!-- There's no explicit <test> clause, so this <choose> statement will test each condition to see if it is TRUE. --> <type> <javaclass name="String"/> </type> <!-- Note that the order of these conditions is important; if we were to swap the positions of the "Newborn" and "Infant" tests, then all children under 5 (including those under 1) would be identified as Infants; no children would be identified as Newborns. --> <when> <condition> <compare comparison="<"> <reference attribute="age"/> <Number value="1"/> </compare> </condition> <value> <String value="Newborn"/> </value> </when> <when> <condition> <compare comparison="<"> <reference attribute="age"/> <Number value="5"/> </compare> </condition> <value> <String value="Infant"/> </value> </when> <when> <condition> <compare comparison="<"> <reference attribute="age"/> <Number value="18"/> </compare> </condition> <value> <String value="Child"/> </value> </when> <otherwise> <value> <String value="Adult"/> </value> </otherwise> </choose> </derivation> </Attribute> <Attribute name="numberOfSpouses"> <type> <javaclass name="Number"/> </type> <derivation> <specified/> </derivation> </Attribute> <Attribute name="maritalStatus"> <type> <javaclass name="String"/> </type> <derivation> <choose> <type> <javaclass name="String"/> </type> <!-- Test the number of spouses --> <test> <reference attribute="numberOfSpouses"/> </test> <!-- Note that the order of the "0" and "1" tests do not matter - so you might want to order these according to whether most Person instances being tested have 0 or 1 spouses. --> <when> <condition> <Number value="0"/> </condition> <value> <String value="Unmarried"/> </value> </when> <when> <condition> <Number value="1"/> </condition> <value> <String value="Married - single spouse"/> </value> </when> <otherwise> <value> <String value="Married - multiple spouses"/> </value> </otherwise> </choose> </derivation> </Attribute> </Class> </RuleSet>