Java Classes

Use this information to learn about how to handle java classes including package names, immutable objects,inheritance, and parametrized classes.

You can use any Java class on your application's classpath as a data type in your CER rule set.

Important: When storing rule objects on the database, you can use only data types for which there is a type handler registered with CER. CER includes type handlers for most commonly-used data types.

Package Names

The name of a Java class must be fully qualified with its package name, except for classes in the following packages:

  • java.lang.*
  • java.util.*
<?xml version="1.0" encoding="UTF-8"?>
<RuleSet name="Example_javaclassDataType"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation=
"http://www.curamsoftware.com/CreoleRulesSchema.xsd">
  <Class name="Person">
    <Attribute name="isMarried">
      <type>
        <!-- java.lang.Boolean does not need its
             package specified -->
        <javaclass name="Boolean"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="dateOfBirth">
      <type>
        <!-- Fully qualified name to a Cúram class -->
        <javaclass name="curam.util.type.Date"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>


  </Class>

</RuleSet>
Tip: You cannot use primitive Java types such as boolean in CER. Use their class equivalents instead, for example, Boolean.

Immutable Objects

A core principle of CER is that each value, once calculated, cannot be changed.

To comply with this principle, any Java classes that you use must be immutable.

Important: If you use a mutable Java class as a data type in your CER rule set, you must ensure that no Java code attempts to modify the value of any objects of that Java class. CER cannot guarantee the reliability of calculations if values are being changed "underneath it"!

Fortunately, there are a wide range of immutable classes which will typically cater for most of your data type requirements. In general, to determine if a Java class is immutable refer to the Javadoc information.

Listed here are some useful immutable classes which in all likelihood will prove sufficient for your needs:

  • java.lang.String;
  • java.lang.Boolean;
  • Implementations of java.lang.Number; in any case, CER converts instances of Number to its own numerical format (backed by java.math.BigDecimal) before performing any arithmetic or comparison;
  • Implementations of java.util.List which do not support its optional operations (see the JavaDoc for List);
  • curam.util.type.Date;
  • Implementations of curam.creole.value.Message; and
  • curam.creole.value.CodeTableItem.

Inheritance of Java Classes and Interfaces

CER recognizes the inheritance hierarchy of Java classes and interfaces.

CER will allow a value of a Java class to be returned wherever one of its ancestor Java classes or interfaces is expected:

<?xml version="1.0" encoding="UTF-8"?>
<RuleSet name="Example_javaclassInheritance"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation=
"http://www.curamsoftware.com/CreoleRulesSchema.xsd">
  <Class name="Person">
    <Attribute name="isMarried">
      <type>
        <javaclass name="Boolean"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="isMarriedAsObject">
      <type>
        <!-- For the sake of example, returning this
             value as an java.lang.Object (which is
             unlikely to be useful in a "real" rule
             set. -->
        <javaclass name="Object"/>
      </type>
      <derivation>
        <!-- This is ok, as a Boolean *IS* an Object. -->
        <reference attribute="isMarried"/>
      </derivation>
    </Attribute>

    <Attribute name="isMarriedAsString">
      <type>
        <javaclass name="String"/>
      </type>
      <derivation>
        <!-- The CER rule set validator would report the error
             below (as a Boolean *IS NOT* an String):

             ERROR    Person.isMarriedAsString
             Example_javaclassInheritance.xml(28, 41)
             Child 'reference' returns 'java.lang.Boolean',
             but this item requires a 'java.lang.String'. -->
        <!-- <reference attribute="isMarried"/> -->

        <!-- (Declaring as specified so that this example
              builds cleanly) -->
        <specified/>

      </derivation>
    </Attribute>

  </Class>

</RuleSet>

Parameterized Classes

Java 5 introduced support for parameterized classes, and CER enables you to use parameterized Java classes in your rule set.

The parameters to a parameterized class are simply listed within the <javaclass> declaration:

<?xml version="1.0" encoding="UTF-8"?>
<RuleSet name="Example_javaclassParameterized"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation=
"http://www.curamsoftware.com/CreoleRulesSchema.xsd">
  <Class name="Person">
    <Attribute name="favoriteWords">
      <type>
        <!-- A list of Strings -->
        <javaclass name="List">
          <javaclass name="String"/>
        </javaclass>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="luckyNumbers">
      <type>
        <!-- A list of Numbers -->
        <javaclass name="List">
          <javaclass name="Number"/>
        </javaclass>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <Attribute name="children">
      <!-- A list of Person rule objects.

           Because java.util.List can be parameterized with
           any Object, we can use a rule class as a parameter.
      -->
      <type>
        <javaclass name="List">
          <ruleclass name="Person"/>
        </javaclass>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>


    <!-- The dogs owned by this person. -->
    <Attribute name="dogs">
      <type>
        <javaclass name="List">
          <ruleclass name="Dog"/>
        </javaclass>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

    <!-- The cats owned by this person. -->
    <Attribute name="cats">
      <type>
        <javaclass name="List">
          <ruleclass name="Cat"/>
        </javaclass>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>


    <!-- All the pets owned by this person. -->
    <Attribute name="pets">
      <type>
        <javaclass name="List">
          <ruleclass name="Pet"/>
        </javaclass>
      </type>
      <derivation>
        <joinlists>
          <fixedlist>
            <listof>
              <javaclass name="List">
                <ruleclass name="Pet"/>
              </javaclass>
            </listof>
            <members>
              <!-- all the dogs - dogs are a type of pet -->
              <reference attribute="dogs"/>
              <!-- all the cats - cats are a type of pet -->
              <reference attribute="cats"/>

              <!-- CER will not allow "children" in this
                   expression; a child is not a pet regardless
                   of whether he or she is adorable or claws
                   the furniture.  -->
              <!-- CANNOT BE USED -->
              <!-- <reference attribute="children"/> -->
              <!-- CANNOT BE USED -->

            </members>
          </fixedlist>

        </joinlists>
      </derivation>
    </Attribute>


  </Class>

  <Class abstract="true" name="Pet">

    <Attribute name="name">
      <type>
        <javaclass name="String"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

  </Class>

  <Class name="Dog" extends="Pet">

    <Attribute name="favoriteTrick">
      <type>
        <javaclass name="String"/>
      </type>
      <derivation>
        <specified/>
      </derivation>
    </Attribute>

  </Class>

  <Class name="Cat" extends="Pet">

    <Attribute name="numberOfLives">
      <type>
        <javaclass name="Number"/>
      </type>
      <derivation>
        <!-- It's well known that every cat
             has 9 lives. -->
        <Number value="9"/>
      </derivation>
    </Attribute>

  </Class>

</RuleSet>

For parameters that allow java.lang.Object, CER allows you to use any type (including rule objects). CER enforces "strong typing" even though the parameter, for example, a rule class, is dynamically defined. CER also recognizes the inheritance hierarchy of rule classes when deciding whether one parameterized class is assignable to another.