Simple types derived from other simple types

You can derive simple types from other simple types and incorporate restrictions to the more general simple type using facets.

The decision to derive a simple type from another simple type implies incorporating restrictions. To incorporate such restrictions, you use facets. Facets are sets of predefined constraints that help define new simple types by restricted built-in types.

Example of a facet: a zip code

An example of a facet is a pattern where a zip code is defined as containing only five digits.

The following schema shows a simple type definition:

<simpleType name="myType">
    <restriction base="string">
    <pattern value="*A"/>
    </restriction>
</simpleType>
<attribute name = "typeValue" type = "myType"/>

In this example, the simple type myType is defined as a subtype of string. The typeValue attribute will be mapped to a dynamic class field of type java.lang.String.

When simple type definitions are mapped to dynamic class types, the type hierarchy tree is searched. Starting from the most restrictive type, the hierarchy tree is searched upwards until a built-in type is found that maintains the mapping type as the simple type definition mapping.

The following zip code example shows how user schema simple types are represented in the XOM and which methods are generated to manipulate them. A set of static methods are declared. They can be called from a rule. Two rules named checkFloatInInterval and findZipCodePattern are used to test the ZipCode objects.

Here is the schema:

<simpleType name="zip-code">
  <restriction base="string">
    <pattern value="A[0-9]{5}"/>
  </restriction>
</simpleType>
<simpleType name="Interval">
  <restriction base="float">
    <minInclusive value="1.1"/>
    <maxInclusive value="3.1"/>
  </restriction>
</simpleType>

Here is the XOM representation:

class ZipCode extends IlrXmlObject
{
   static String getPattern();
   static int getMinLength();
   static int getMaxLength();
   static getLength();
   static String[] getEnumerations();
}
class Interval
{
   static String getPattern();
   static int getMinLength();
   static int getMaxLength();
   static getLength();
   static float[] getEnumerations();
   static float getMinInclusive ();
   static float getMaxInclusive ();
   static float getMinExclusive ();
   static float getMaxExclusive ();
   static int getTotalDigits();
   static int getFractionDigits();
}

And here are the rules:

rule checkFloatInInterval
{
  when {
     f: Float ( i: floatValue(); Interval.getMinInclusive() <= i;
                i <= Internal.getMaxInclusive()   );
  }
  then {
     out.println ( f + " in Interval" );
  }
}
rule findZipCodePattern 
{
   when {
      s: String ( equals ZipCode.getPattern() );
   }
   then {
      out.println ( s + " equals zip code pattern" );
   }
}

The simple type dynamic method provides information on type facets values.

The fact that the method returns the last facet definition in the type hierarchy has the following consequences:

Facets can be of the following types:

The following table shows the returned values when the facet is not defined.

Table 1. Returned values with undefined facet 
Types getMinInclusive getMinExclusive GetMaxInclusive getMaxExclusive

getMinLength getMaxLength

getLength getTotalDigits getFractionDigits

getEnumerations
float -Float.MAX_VALUE Float.MAX_VALUE

IlrXmlObject.UNKNOWN_

POSITIVE_VALUE

float[0]
double -Double.MAX_VALUE Double.MAX_VALUE double[0]
byte Byte.MIN_VALUE Byte.MAX_VALUE byte[0]
int Integer.MIN_VALUE Integer.MAX_VALUE int[0]
long Long.MIN_VALUE Long.MAX_VALUE long[0]
Object null null Object[0]