Das folgende Beispiel zeigt ein Regelwerk:
<?xml version="1.0" encoding="UTF-8"?>
<RuleSet name="Example_externalRuleObjects"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"http://www.curamsoftware.com/CreoleRulesSchema.xsd">
<Class name="Person">
<!-- These attributes must be specified at creation time -->
<Initialization>
<Attribute name="firstName">
<type>
<javaclass name="String"/>
</type>
</Attribute>
<Attribute name="lastName">
<type>
<javaclass name="String"/>
</type>
</Attribute>
</Initialization>
<Attribute name="incomes">
<type>
<javaclass name="List">
<ruleclass name="Income"/>
</javaclass>
</type>
<derivation>
<!-- Read all the rule objects of
type "Income" -->
<readall ruleclass="Income"/>
</derivation>
</Attribute>
</Class>
<Class name="Income">
<Attribute name="amount">
<type>
<javaclass name="Number"/>
</type>
<derivation>
<specified/>
</derivation>
</Attribute>
</Class>
</RuleSet>
Im obigen Regelwerk wird der Ausdruck "readall" (siehe readall) verwendet, um alle Instanzen der Regelklasse Income abzurufen.
Zur Erstellung eines externen Regelobjekts müssen der Client-Code oder die Tests beim Erstellen des Regelobjekts die Sitzung angeben:
package curam.creole.example;
import junit.framework.TestCase;
import curam.creole.calculator.CREOLETestHelper;
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.execution.session.StronglyTypedRuleObjectFactory;
import curam.creole.parser.RuleSetXmlReader;
import
curam.creole.ruleclass.Example_externalRuleObjects.impl.Income;
import
curam.creole.ruleclass.Example_externalRuleObjects.impl.Income_Factory;
import
curam.creole.ruleclass.Example_externalRuleObjects.impl.Person;
import
curam.creole.ruleclass.Example_externalRuleObjects.impl.Person_Factory;
import curam.creole.ruleitem.RuleSet;
import curam.creole.storage.inmemory.InMemoryDataStorage;
/**
* Tests external rule objects created directly by client code.
*/
public class TestCreateExternalRuleObjects extends TestCase {
/**
* Example showing the creation of external rule objects using
* generated code.
*/
public void testUsingGeneratedTestClasses() {
final Session session =
Session_Factory.getFactory().newInstance(
new RecalculationsProhibited(),
new InMemoryDataStorage(
new StronglyTypedRuleObjectFactory()));
/**
* Note that the compiler enforces that the right type of
* initialization arguments are provided.
*/
final Person person =
Person_Factory.getFactory().newInstance(session, "John",
"Smith");
CREOLETestHelper.assertEquals("John", person.firstName()
.getValue());
/**
* These objects will be retrieved by the
*
* <readall ruleclass="Income"/>
*
* expression in the rule set.
*/
final Income income1 =
Income_Factory.getFactory().newInstance(session);
income1.amount().specifyValue(123);
final Income income2 =
Income_Factory.getFactory().newInstance(session);
income2.amount().specifyValue(345);
}
/**
* Example showing the creation of external rule objects using
* the CER rule set interpreter.
*/
public void testUsingInterpreter() {
/* read in the rule set */
final RuleSet ruleSet = getRuleSet();
/* start an interpreted session */
final Session session =
Session_Factory.getFactory().newInstance(
new RecalculationsProhibited(),
new InMemoryDataStorage(
new InterpretedRuleObjectFactory()));
/**
* Note that the compiler cannot enforce that the right type of
* initialization arguments are provided - if these are wrong
* CER will report a runtime error.
*/
final RuleObject person =
session.createRuleObject(ruleSet.findClass("Person"),
"John", "Smith");
CREOLETestHelper.assertEquals("John", person
.getAttributeValue("firstName").getValue());
/**
* These objects will be retrieved by the
*
* <readall ruleclass="Income"/>
*
* expression in the rule set.
*/
final RuleObject income1 =
session.createRuleObject(ruleSet.findClass("Income"));
income1.getAttributeValue("amount").specifyValue(123);
final RuleObject income2 =
session.createRuleObject(ruleSet.findClass("Income"));
income2.getAttributeValue("amount").specifyValue(345);
}
/**
* Reads the Example_externalRuleObjects from its XML source
* file.
*/
private RuleSet getRuleSet() {
/* The relative path to the rule set source file */
final String ruleSetRelativePath =
"./rules/Example_externalRuleObjects.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();
}
}