Interval

Interval #

An Interval class is used to create data objects that specify a range of values. Therefore, a base data class must be specified to define the type of the elements. Intervals can be only defined the numeric and chronological data classes as well for string classes with a total order predicate. How to create an interval predicate class for numeric or chronological classes is described here.

After the definition of a custom lower and upper bounded atomic class, it is possible to define a custom Interval class using that custom atomic class. This can be done in the model.xml file like shown below:

model.xml

    <IntervalClass name="customIntervalClass">
        <ElementClass name="customIntegerIntervalClass"/>
    </IntervalClass>

To create this class during runtime, the following java code has to be used:

Wiki_IntervalTest.java

    IntervalClass intervalClass = (IntervalClass) ModelFactory.getDefaultModel().getIntervalSystemClass();
    IntervalClass customIntervalClass = (IntervalClass) intervalClass.createSubclass("customIntervalClass");
    customIntervalClass.setElementClass(ModelFactory.getDefaultModel().getClass("customIntegerIntervalClass"));
    customIntervalClass.setAbstract(false);
    customIntervalClass.finishEditing();
Please consider that the definition of the Atomic class, here customIntegerIntervalClass, has to be made before the Interval class is created. Otherwise, an exception would be thrown. In addition, it should be noted that for an interval class using the setAbstract(false) method call, the class must be set to non-abstract. Otherwise, it cannot be instantiated.

To create an object of this example class, the following java code has to be used:

Wiki_IntervalTest.java

    IntervalObject intervalObject = (IntervalObject) ModelFactory.getDefaultModel().createObject("customIntervalClass");

    IntegerObject lowerBound = (IntegerObject) ModelFactory.getDefaultModel().getClass("customIntegerIntervalClass").newObject();
    lowerBound.setNativeInteger(50);
    IntegerObject upperBound = (IntegerObject) ModelFactory.getDefaultModel().getClass("customIntegerIntervalClass").newObject();
    upperBound.setNativeInteger(100);

    intervalObject.setBounds(lowerBound, upperBound);

The XML representation of the interval object depicted above is as follows:

casebase.xml

    <cdol:Int c="customIntervalClass" id="testInterval">
        <cdol:LowerBound v="50" c="customIntegerIntervalClass"/>
        <cdol:UpperBound v="100" c="customIntegerIntervalClass"/>
    </cdol:Int>

The use of intervals on string classes with a total order predicate is analogous. For example, the XML definition is:

model.xml

    <IntervalClass name="WeekdaysInterval">
        <ElementClass name="Weekdays"/>
    </IntervalClass>

However, it is not necessary to create explicit instance interval predicates here. The example refers to the total order Weekdays used in the wiki.