Any Java class on your application's classpath may be used as a data type in your CER rule set.
CER includes type handlers for most commonly-used data types.
The name of a Java class must be fully qualified with its package name, except for classes in the following packages:
<?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>
A core principle of CER is that each value, once calculated, cannot be changed.
To comply with this principle, any Java classes you use should be immutable.
Fortunately, there are a wide range of immutable classes which will typically cater for most of your data type requirements. In general you may need to look at a Java class's JavaDoc to determine whether it is immutable.
Listed here are some useful immutable classes which in all likelihood will prove sufficient for your needs:
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>
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>
CER allows you to use any type (including rule objects) for parameters which allow java.lang.Object. CER will enforce "strong typing" even though the parameter (e.g. a rule class) are dynamically defined. CER will also recognize the inheritance hierarchy of rule classes when deciding whether one parameterized class is assignable to another.