Tutorial - Define rules on an XML file SourceForge.net Logo
 Home
 Documentation
 To do
 Download
 Project page
 JSR-94

Rule Definition

Rules can be loaded in two modes:

  • from an XML file
  • from LocalRuleExecutionSetProvider.createRuleExecutionSet method, passing a list of RuleImpl objects


A Rule is defined as:
  • a name
  • a description
  • a list of Assumption objects, having the format: "leftTerm" ["operator" "rightTerm"]
    All assumptions are connected by an AND operator.
    If assumption is in the form "leftTerm" "operator" "rightTerm", then the operator between left and right terms may be: "=", "<>", "contains", "notcontains", "containsatleastone", "notcontainsanyone", "<", ">", "<=", ">=" where the last 4 are applied only on numeric terms.
    If assumption has only leftTerm, then operator will be "exists" and the rule engine will control if leftTerm is contained in the working memory.
    A term may be:
    • a letteral (text, numeric value...) or
    • a value obtained by a getter method of an object
    • an enumeration of values, expressed as [value1, value2, ...] in case of "containsatleastone" or "notcontainsanyone" operators
    (ex. Customer.getCreditLimit) or a variable, identified by the prefix ":" (for example: ":X").
    Variables are allowed only as leftTerm and the operator must be "=" or "contains".
  • a list of Action objects, having the format: "class.method" ["arg-1" "arg-2" ... "arg-N"]
    i.e. the "method" of "class" object is invoked, where "class" is in working memory (an object passed to the rule engine) or is created in that moment.
    "method" may have zero to N arguments.
    "arg-i" may be a term (see above).


DTD for a Rule Execution Set XML file

Rules can be defined in an XML file. This file must respect the following DTD:

<!ELEMENT rule-execution-set (name, description, synonymn*, rule*)>

<!ELEMENT name (#PCDATA)>

<!ELEMENT description (#PCDATA)>

<!ELEMENT synonymn>
<!ATTLIST synonymn name CDATA #REQUIRED>
<!ATTLIST synonymn class CDATA #REQUIRED>

<!ELEMENT rule (if*, then*)>
<!ATTLIST rule name CDATA #REQUIRED>
<!ATTLIST rule description CDATA #REQUIRED>

<!ELEMENT if >
<!ATTLIST if leftTerm CDATA #REQUIRED>
<!ATTLIST if op CDATA #IMPLIED>
<!ATTLIST if rightTerm CDATA #IMPLIED>

<!ELEMENT then >
<!ATTLIST then method CDATA #REQUIRED>
<!ATTLIST then arg1 CDATA #IMPLIED>
<!ATTLIST then arg2 CDATA #IMPLIED>
...
<!ATTLIST then argN CDATA #IMPLIED>



XML file example

This is an example of XML file which contains one rule:


  <?xml version="1.0" encoding="UTF-8"?>
  <rule-execution-set>
   <name>RuleExecutionSet1</name>
   <description>Rule Execution Set</description>

   <synonymn name="customer" class="example1.Customer" />

   <!--
   If the credit limit of the customer is greater than the amount of the
   invoice and the status of the invoice is "unpaid" then
   decrement the credit limit with the amount of the invoice and
   set the status of the invoice to "paid".
   -->
   <rule name="Rule1" description="credit limit control rule" >
    <if leftTerm="customer.getCreditLimit" op=">" rightTerm="example1.Invoice.getAmount" />
    <if leftTerm="example1.Invoice.getStatus" op="=" rightTerm="unpaid" />
    <if leftTerm="example1.getSelectedItems" op="containsatleastone" rightTerm="[Item1,Item2,Item3]" />
    <then method="customer.decrementCreditLimit" arg1="example1.Invoice.getAmount" />
    <then method="example1.Invoice.setStatus" arg1="paid" />
   </rule>
  </rule-execution-set>

Notice that leftTerm and rightTerm attributes of the if rule condition can execute a getter method of an object.
Customer and Invoice object must be passed to the rule engine.
Notice that method attribute of the then rule action can execute a setter method of an object.


(c) 2006 by Mauro Carniel