CER Test Code Generator

The CER rule set interpreter above allows reference to rule class and attribute names via strings.

Whilst this allows fully dynamic configuration of rule sets, for testing purposes it can be cumbersome to have to use strings and cast attribute values. If you mis-type a rule class or attribute name, or use the wrong type of cast, then your code may compile with no errors but you will get errors at runtime.

CER includes with a code generator which can generate Java wrapper classes for your rule classes. These generated classes can simplify the writing of your test code and allow the compiler to detect problems which otherwise would only occur at runtime.

Now we will rewrite our code for running the HelloWorldRuleSet:

Figure 1. Running rules with the CER-generated test rule classes
package curam.creole.example;

import junit.framework.TestCase;
import curam.creole.execution.session.RecalculationsProhibited;
import curam.creole.execution.session.Session;
import curam.creole.execution.session.Session_Factory;
import
 curam.creole.execution.session.StronglyTypedRuleObjectFactory;
import curam.creole.ruleclass.HelloWorldRuleSet.impl.HelloWorld;
import
 curam.creole.ruleclass.HelloWorldRuleSet.impl.HelloWorld_Factory;
import curam.creole.storage.inmemory.InMemoryDataStorage;

public class TestHelloWorldCodeGen extends TestCase {

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

    final TestHelloWorldCodeGen testHelloWorld =
        new TestHelloWorldCodeGen();
    testHelloWorld.testUsingGeneratedTestClasses();

  }

  /**
   * A simple test case, using the CER-generated test classes for
   * strong typing and ease of coding tests.
   */
  public void testUsingGeneratedTestClasses() {

    /* start a strongly-typed session */
    final Session session =
        Session_Factory.getFactory().newInstance(
            new RecalculationsProhibited(),
            new InMemoryDataStorage(
                new StronglyTypedRuleObjectFactory()));

    /*
     * create a rule object instance of the required rule class, by
     * using its generated factory
     */
    final HelloWorld helloWorld =
        HelloWorld_Factory.getFactory().newInstance(session);

    /*
     * use the generated accessor to get at the "greeting" rule
     * attribute - no cast necessary, and any error in the
     * attribute name would lead to a compile error
     */
    final String greeting = helloWorld.greeting().getValue();

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

}

Note the following comparisons with our TestHelloWorldInterpreted code:

warning: The generated code is only intended for use in test environments where it is a straightforward matter to recompile changes to code.

The generated code is not portable across machines, as it contains absolute paths to the rule sets on the local machine.

In particular, you must not use the generated code in any production environment where rule sets may change dynamically.