Regelwerkinterpreter

CER enthält einen Interpreter, der dynamisch definierte Regelwerke ausführen kann.

Im folgenden Beispielcode wird der CER-Regelwerkinterpreter verwendet, um Regeln aus dem Regelwerk namens HelloWorldRuleSet auszuführen.

Abbildung 1. Ausführung von Regeln mit dem CER-Regelwerkinterpreter
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);
  }

}

Sie können diese Beispielklasse entweder als eigenständige Java-Anwendung (d. h. über ihre Methode main) oder als JUnit-Test ausführen. Diese beiden Möglichkeiten zur Ausführung dieser Klasse werden ausschließlich aus Gründen des Benutzungskomforts bereitgestellt. Verwenden Sie beim Schreiben von eigenem Code für die Ausführung von Regelwerken Ihr bevorzugtes Verfahren.

Dieser Code soll nachfolgend detaillierter betrachtet werden. Die Testmethode testUsingInterpreter führt die folgenden Schlüsselfunktionen aus: