Constructing Timelines in Java Code

In Java, each piece of Timeline data is an instance of the curam.creole.value.Timeline parameterized class. For full details of this class, see its JavaDoc available at EJBServer/components/CREOLEInfrastructure/doc in a development installation of the application.

Each Timeline holds a collection of Intervals, where an Interval is value applicable from a particular start date. A collection of appropriate intervals must be passed to the Timeline's constructor.

For example, suppose you need to create a Timeline<Number> with these intervals (recall that a timeline stretches infinitely far into the past and future):

Here is some sample Java code to create such a timeline:

package curam.creole.example;

import curam.creole.value.Interval;
import curam.creole.value.Timeline;
import curam.util.type.Date;

public class CreateTimeline {

  /**
   * Creates a Number Timeline with these interval values:
   * <ul>
   * <li>0 up to and including 31st December 2000;</li>
   * <li>10,000 from 1st January 2001 up to and including 30th
   * November 2003; and</li>
   * <li>12,000 from 1st December 2004 until further notice.</li>
   * </ul>
   */
  public static Timeline<Number> createNumberTimeline() {

    return new Timeline<Number>(

    // first interval, application from the "start of time"
        new Interval<Number>(null, 0),

        // second interval
        new Interval<Number>(Date.fromISO8601("20010101"), 10000),

        // last interval (until further notice)
        new Interval<Number>(Date.fromISO8601("20041201"), 12000)

    );

  }
}

As another example, here is some sample Java code, which is callable a CER call expression, to calculate a timeline for a person's age, up to the person's 200th birthday (recall that age timelines must be artificially limited so that the timeline contains a finite number of value changes):

package curam.creole.example;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;

import curam.creole.execution.session.Session;
import curam.creole.value.Interval;
import curam.creole.value.Timeline;
import curam.util.type.Date;

public class AgeTimeline {

  /**
   * Creates a timeline for the age of a person, artificially
   * limited to 200 birthdays.
   * <p>
   * Can be invoked from CER rules via a &lt;call&gt; expression.
   */
  public static Timeline<? extends Number> createAgeTimeline(
      final Session session, final Date dateOfBirth) {

    /**
     * The artificial limit, so that the age timeline has a finite
     * number of value changes.
     */
    final int NUMBER_OF_BIRTHDAYS = 200;

    final Collection<Interval<Integer>> intervals =
        new ArrayList<Interval<Integer>>(NUMBER_OF_BIRTHDAYS + 2);

    /*
     * age before date of birth will still be recorded as 0 -
     * create an initial interval application from the
     * "start of time"
     */
    final Interval<Integer> initialInterval =
        new Interval<Integer>(null, 0);
    intervals.add(initialInterval);

    /*
     * Identify each birthday up to the limit. Note that the person
     * is deemed to be age 0 even before the date-of-birth (see
     * above); so the interval here from the date of birth up to
     * the first birthday will be merged into a single interval by
     * the timeline; no matter (it's clearer to keep the logic as
     * is).
     */

    for (int age = 0; age <= NUMBER_OF_BIRTHDAYS; age++) {

      // compute the birthday date
      final Calendar birthdayCalendar = dateOfBirth.getCalendar();

      /*
       * NB use .roll rather than .add to get the correct
       * processing for leap years
       */
      birthdayCalendar.roll(Calendar.YEAR, age);
      final Date birthdayDate = new Date(birthdayCalendar);

      /*
       * the age applies from this birthday until the next birthday
       */
      intervals.add(new Interval<Integer>(birthdayDate, age));
    }

    final Timeline<Integer> ageTimeline =
        new Timeline<Integer>(intervals);

    return ageTimeline;

  }
}
Note: In general, timeline data tends to be created outside of rules, by clients of CER.

In particular, the Cúram V6 Eligibility/Entitlement processing contains logic to help convert Cúram Evidence into timeline data.

See the Inside Cúram Eligibility and Entitlement Using Cúram Express Rules guide for more details.