choose

일치하는 조건에 따라 값을 선택합니다.

choose 표현식에는 다음이 포함되어 있습니다.

하향식 순서로 when 표현식의 조건을 평가하며 테스트를 통과하는 첫 번째 조건에서 평가가 중지됩니다. 그 다음 조건은 평가되지 않습니다.

choose 표현식은 대부분의 프로그래밍 언어에서 일반적인 if / else if /.../ else문의 표현식을 반영합니다. 결정 테이블을 구현하는 데 효과적으로 사용할 수 있습니다.

성공할 가능성이 가장 큰 조건을 목록의 맨 윗 부분에 배치하도록(불필요한 계산을 제거하기 위해) 조건의 순서를 지정할 수도 있습니다.

경고: 단순 조건의 경우(예: 단일 값을 산출해 내는 등식을 테스트하는 조건) 일반적으로 규칙 세트의 동작에 영향을 주지 않고 조건을 재정렬할 수 있습니다.

그러나 보다 복잡한 조건(따라서 일반적인)의 경우 조건의 순서를 다시 지정하면 원하지 않게 동작이 변경되는지를 신중하게 고려해야 합니다.

<?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>
          <!-- 명시적인 <test>절이 없으므로
               이 <choose>문은 각 조건이 TRUE인지를 확인하기 위해
               각 조건을 테스트합니다. -->
          <type>
            <javaclass name="String"/>
          </type>

          <!-- 이러한 조건의 순서는 중요합니다.
               "신생아" 및 "유아" 테스트 위치를
               바꾸면 5세 미만(1세 미만 포함)의
               모든 아동이 유아로 식별되며
               신생아로 식별되는 아동이
               없게 됩니다. -->
          <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>
            <reference attribute="numberOfSpouses"/>
          </test>
          <!--  "0"과 "1" 테스트의 순서는 중요하지
 않습니다.
               따라서 테스트되고 있는 대부분의 개인에게
 0 또는 1명의
               배우자가 있는지에 따라 이 순서를 지정할 수 있습니다.
      -->
          <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>