choose

Chooses a value based on a condition being met.

The choose expression contains:

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).

warning: For simple conditions (e.g. those which test equality to a single value), typically you can reorder your conditions without affecting the behavior of the rule set.

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="&lt;">
                <reference attribute="age"/>
                <Number value="1"/>
              </compare>
            </condition>
            <value>
              <String value="Newborn"/>
            </value>
          </when>
          <when>
            <condition>
              <compare comparison="&lt;">
                <reference attribute="age"/>
                <Number value="5"/>
              </compare>
            </condition>
            <value>
              <String value="Infant"/>
            </value>
          </when>
          <when>
            <condition>
              <compare comparison="&lt;">
                <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>