The Rule Set Interpreter

CER contains an interpreter which can execute dynamically-defined rule sets.

Below is some sample code which uses the CER rule set interpreter to execute rules from a HelloWorldRuleSet.

Figure 1. Running rules with the CER rule set interpreter
package curam.creole.example;

import junit.framework.TestCase;
import curam.creole.execution.RuleObject;
import curam.creole.execution.session.InterpretedRuleObjectFactory;
import curam.creole.execution.session.RecalculationsProhibited;
import curam.creole.execution.session.Session;
import curam.creole.execution.session.Session_Factory;
import curam.creole.parser.RuleSetXmlReader;
import curam.creole.ruleitem.RuleSet;
import curam.creole.storage.inmemory.InMemoryDataStorage;

public class TestHelloWorldInterpreted extends TestCase {

  /**
   * Runs the class as a stand-alone Java application.
   */
  public static void main(final String[] args) {

    final TestHelloWorldInterpreted testHelloWorld =
        new TestHelloWorldInterpreted();
    testHelloWorld.testUsingInterpreter();
  }

  /**
   * Reads the HelloWorldRuleSet from its XML source file.
   */
  private RuleSet getRuleSet() {

    /* The relative path to the rule set source file */
    final String ruleSetRelativePath = "./rules/HelloWorld.xml";

    /* read in the rule set source */
    final RuleSetXmlReader ruleSetXmlReader =
        new RuleSetXmlReader(ruleSetRelativePath);

    /* dump out any problems */
    ruleSetXmlReader.validationProblemCollection().printProblems(
        System.err);

    /* fail if there are errors in the rule set */
    assertTrue(!ruleSetXmlReader.validationProblemCollection()
        .containsErrors());

    /* return the rule set from the reader */
    return ruleSetXmlReader.ruleSet();
  }

  /**
   * A simple test case, using the fully-dynamic CER rule set
   * interpreter.
   */
  public void testUsingInterpreter() {

    /* read in the rule set */
    final RuleSet ruleSet = getRuleSet();

    /* start a session which creates interpreted rule objects */
    final Session session =
        Session_Factory.getFactory().newInstance(
            new RecalculationsProhibited(),
            new InMemoryDataStorage(
                new InterpretedRuleObjectFactory()));

    /* create a rule object instance of the required rule class */
    final RuleObject helloWorld =
        session.createRuleObject(ruleSet.findClass("HelloWorld"));

    /*
     * Access the "greeting" rule attribute on the rule object -
     * the result must be cast to the expected type (String)
     */
    final String greeting =
        (String) helloWorld.getAttributeValue("greeting")
            .getValue();

    System.out.println(greeting);
    assertEquals("Hello, world!", greeting);
  }

}

You can run this sample class either as a stand-alone Java application (i.e. via its main method) or as a JUnit test. These two ways of running this class are provided merely as a convenience - so when you write your own code to run rule sets, pick whichever suits you better.

Now we will dive down into the detail of this code. The test method testUsingInterpreter performs these key functions: