Example

Here is an example CER rule set which uses the create expression to conditionally create rule objects from rules:

<?xml version="1.0" encoding="UTF-8"?>
<RuleSet name="Example_internalRuleObjects"
  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>

    <!-- Uses <create> to get a new rule object.

         Other calculations (on this or other rule objects) can
         access this newly-created rule object by referring to
         this attribute, i.e.

         <reference attribute="minorAgeRangeTest"/> .

         The rule object created CANNOT be retrieved using a
         <readall> expression.
     -->
    <Attribute name="minorAgeRangeTest">
      <type>
        <ruleclass name="AgeRangeTest"/>
      </type>
      <derivation>
        <!-- Create an age-range test which checks whether this
             person is aged between 0-17 inclusive (i.e. is
             under 18 years).
          -->
        <create ruleclass="AgeRangeTest">
          <this/>
          <Number value="0"/>
          <Number value="17"/>
        </create>
      </derivation>

    </Attribute>

    <!-- Uses the age-range check to determine whether this person
         is a minor. -->
    <Attribute name="isMinor">
      <type>
        <javaclass name="Boolean"/>
      </type>
      <derivation>
        <reference attribute="isPersonInAgeRange">
          <reference attribute="minorAgeRangeTest"/>
        </reference>
      </derivation>
    </Attribute>

    <!-- Uses <create> to get a new rule object, within
         another expression.

         Because the new rule object is created "anonymously",
         it is not available to any other rule objects (but
         it will still show up as "created" in any SessionDoc.
    -->
    <Attribute name="isOfWorkingAge">
      <type>
        <javaclass name="Boolean"/>
      </type>
      <derivation>
        <!-- Create an age-range test which checks whether this
             person is legally permitted to work (i.e. is aged
             at least 16 and less than 65), and then check
             whether the test passes. -->
        <reference attribute="isPersonInAgeRange">
          <create ruleclass="AgeRangeTest">
            <this/>
            <Number value="16"/>
            <Number value="64"/>
          </create>
        </reference>
      </derivation>

    </Attribute>

  </Class>

  <!-- A generic test which checks whether the
       person's age lies within a specified (inclusive)
       range. -->
  <Class name="AgeRangeTest">
    <Initialization>
      <Attribute name="person">
        <type>
          <ruleclass name="Person"/>
        </type>
      </Attribute>
      <Attribute name="minimumAge">
        <type>
          <javaclass name="Number"/>
        </type>
      </Attribute>
      <Attribute name="maximumAge">
        <type>
          <javaclass name="Number"/>
        </type>
      </Attribute>
    </Initialization>

    <Attribute name="isPersonInAgeRange">
      <type>
        <javaclass name="Boolean"/>
      </type>
      <derivation>
        <all>
          <fixedlist>
            <listof>
              <javaclass name="Boolean"/>
            </listof>
            <members>
              <compare comparison=">=">
                <reference attribute="age">
                  <reference attribute="person"/>
                </reference>
                <reference attribute="minimumAge"/>
              </compare>
              <compare comparison="&lt;=">
                <reference attribute="age">
                  <reference attribute="person"/>
                </reference>
                <reference attribute="maximumAge"/>
              </compare>
            </members>
          </fixedlist>
        </all>
      </derivation>
    </Attribute>

  </Class>

</RuleSet>
Note: As for all CER expressions, the create expressions will only be calculated if requested, e.g. the minorAgeRangeTest rule object will only be created if its value or the value of isMinor is requested by client code or another calculation.

In the above example, the isOfWorkingAge uses the technique of creating a rule object "anonymously" by wrapping the creation of a rule object within an expression which references some attribute on the newly-created rule object. Such rule objects are not available to other calculations but will still show up in any generated SessionDoc.

An anonymous rule object can be useful when you need to access rule attributes in a created rule object, but you do not need to make that created rule object itself available to any other calculations.

Pooling of Internal Rule Object

Since Cúram V6, CER keeps a "pool" of internal rule objects that have been created during a Session.

The Session's pool is queried whenever a create expression is evaluated. If a rule object with the same initialization and/or specified parameters has already been created, then it will be reused from the pool rather than a new rule object created.

This pooling approach improves efficiency in the situation where many create statements attempt to create "identical" rule objects. The use of a single rule object means that any calculated attributes on the single rule object are calculated at most once, instead of identical calculations occurring on many "identical" rule objects.

Re-use of pooled rule objects is guaranteed to be safe, because CER's core principles ensure that any calculation depends only on its inputs; and thus identical inputs guarantee identical outputs.