How to use the rule engine to get/set properties of RuleEngineObjects
JRuleEngine can be used to set/get properties of org.jruleengine.RuleEngineObject instances.
These instances are added to the rule engine.
Objects derived from RuleEngineObject interface have a getRuleEngineObjectName method that return a unique instance identifier:
JRuleEngine internally uses getRuleEngineObjectName method to reference a specific RuleEngineObject instance.
"If conditions" and "then actions" can reference RuleEngineObject instances and use these instances to evaluate or set some properties.
So you can identify and reference any "custom" object inside the rule engine, by implementing the RuleEngineObject interface in the "custom" object.
Example
The following is an example of XML file which contains two simple rules used to reference IsFatherOf instances and create (deduct) a IsFrandFatherOf instance:
<?xml version="1.0" encoding="UTF-8"?>
<rule-execution-set>
<name>RuleExecutionSet1</name>
<description>Rule Execution Set</description>
<!--
relation1 and relation2 must be of IsFahterOf type
relation3 must be of IsGrandFatherOf type
-->
<rule name="Rule1" description="grandfather relationship: son-father-grandfather" >
<if leftTerm="relation1.getFatherName" op="=" rightTerm="relation2.getName" />
<then method="relation3.setName" arg1="relation1.getName" />
<then method="relation3.setGrandFatherName" arg1="relation2.getFatherName" />
</rule>
<rule name="Rule2" description="grandfather relationship: grandfather-father-son" >
<if leftTerm="relation1.getName" op="=" rightTerm="relation2.getFatherName" />
<then method="relation3.setName" arg1="relation2.getName" />
<then method="relation3.setGrandFatherName" arg1="relation1.getFatherName" />
</rule>
</rule-execution-set>
You can define two objects: IsFatherOf and IsGrandFatherOf which implement RuleEngineObject interface.
While creating IsFatherObject you pass to it the object identifier (the third argument of the constructor).
IsGrandFatherOf class has a setGrandFatherName method, called by the rule engine, if a rule is fired.
The following is a sample java code that calls the rule engine by passing to it
two IsFatherOf objects and one IsGrandFatherOf object, used to store the result.
IsFatherOf relation1 = new IsFatherOf("John","George","relation1");
IsFatherOf relation2 = new IsFatherOf("George","Joseph","relation2");
IsGrandFatherOf relation3 = new IsGrandFatherOf("relation3");
// Create a input list.
List input = new ArrayList();
input.add(relation1);
input.add(relation2);
input.add(relation3);
// Print the input.
System.out.println("Calling rule session...");
// Execute the rules without a filter.
List results = statelessRuleSession.executeRules(input);
|