ESSENTIAL RuleML

The discount rulebase use case

This version:
http://home.comcast.net/~stabet/ruleml/essential/essentialruleml.html
Editors:
Said Tabet mailto:stabet@attbi.com
Harold Boley, NRC mailto:harold.boley@nrc.gc.ca
Benjamin Grosof, MIT Sloan mailto:bgrosof@mit.edu

Abstract

This document is an introduction to RuleML essentials using examples of the popular discount use case. Each rule will be shown in four different format to illustrate the various syntaxes. The business ruls are implemented in RuleML 0.8 version of both Datalog and Hornlog.

The discount rulebase defines non-prioritized business rules on customer-and-product discounting and, using chaining, on premium customers. It also gives business facts on products and on a specific customer.

These examples could be easily extended with further categories of customers, product discounting conditions, etc.

The examples given in this use case are implemented in RuleML 0.8 and validate w.r.t the Datalog and Hornlog DTDs:

http://www.ruleml.org/dtd/0.8/ruleml-datalog.dtd

http://www.ruleml.org/dtd/0.8/ruleml-hornlog.dtd

A RuleML Datalog schema is also available from the RuleML web site:

http://www.ruleml.org/ruleml/xsd/0.8/


Table of Contents

1. Introduction to the Customer Discount Use Case

2. Customer Discount in RuleML Datalog

3. Customer Discount in RuleML Hornlog

4. More Formalizations and Extensions


1. Introduction to the Customer Discount use case

Consider the following business rules:

RULE 1:

"The discount for a customer buying a product is 5.0 percent if the customer is premium and the product is regular"

RULE 2:

"A customer is premium if their spending has been high the previous year"

Also suppose there are some (obvious) facts.

These clauses can be transcribed into Datalog with "..." constants still containing parts of the English meanings.

Below, we will present two implementations of this scenario using RuleML Datalog and RuleML Hornlog. Discussion on possible extensions will focus only, in this use case, on URI support.

2. Customer Discount in RuleML Datalog

The customer discount scenario is a simple but non-trivial Datalog example - with rule chaining - and its model-theoretic semantics.

We will give 4 different formats for each rule, starting with an IF-THEN format borrowed from IBM CommonRules, followed by the RuleML encoding, and two encodings in Prolog (for backward chaining) and Jess (for forward chaining).

A running implementation in Jess will be shown (trace viewed in a command line DOS window). The source code is available by email request.

A RuleML implication rule (“imp” element in the XML syntax) expresses an if-then rule (a.k.a. a “clause” in the LP KR literature). It is a kind of statement. An implication rule is a pure-belief rule that does not directly specify any procedural attachments for sensing or effecting.

The detailed implementation in RuleML 0.8 is available here.

Premium Customer Discount Rule

In Natural Language:

A premium customer buying a regular product gets a 5% discount.

In an IF-THEN format (a la SCLP syntax of CommonRules 3.0):

IF   premium(?customer)
AND  regular(?product)
THEN discount(?customer, ?product, "5.0 percent");

In RuleML:

<imp>
   <_rlab>discountrule1</_rlab>
   <_head>
      <atom>
         <_opr>discount</_opr>
         <var>customer</var>
         <var>product</var>
         <ind>5.0 percent</ind>
      </atom>
   </_head>
   <_body>
         <atom>
            <_opr>premium</_opr>
            <var>customer</var>
         </atom>
         <atom>
            <_opr>regular</_opr>
            <var>product</var>
         </atom>
   </_body>
</imp>

In Prolog syntax:

discount(Customer,Product,"5.0 percent")  :-       
   premium(Customer) , regular(Product). 

In Jess (forward chaining rules engine):

(defrule discountrule1

   (premium ?Customer)
   (regular ?Product)
=>
   (assert (discount ?Customer ?Product "5.0 percent"))      
)

Customer Premium Status Rule

In Natural Language:

A customer is premium if their spending has been high the previous year.

In an IF-THEN format (a la SCLP syntax of CommonRules 3.0):

IF   spending(?customer, "High", "previous year")
THEN premium(?customer);

In RuleML:

<imp>
   <_rlab>statusrule1</_rlab>
   <_head>
      <atom>
         <_opr>premium</_opr>
         <var>customer</var>
      </atom>
   </_head>
   <_body>
      <atom>
         <_opr>spending</_opr>
         <var>customer</var>
         <ind>"high"</ind>
         <ind>"previous year"</ind>
      </atom>
   </_body>
</imp>

In Prolog (backward chaining):

premium(Customer) :-       
   spending(Customer, "high", "previous year"). 

In Jess (forward chaining rules engine):

(defrule statusrule1
   
   (spending ?customer "high" "previous year")       
=>
   (assert (premium ?customer)
) 

Facts

spending("Peter Miller","high","previous year").

regular("Honda").

The Associated Herbrand Model

{
 discount("Peter Miller","Honda","5.0 percent"),
 premium("Peter Miller"),
 spending("Peter Miller","high","previous year"),  
 regular("Honda")
}

Running the datalog discount in Jess ( Java environment):

The screen below shows the rules execution after the fact assertion to the working memory:

RuleML Datalog Discount use case Run using Jess

3. Customer Discount in RuleML HornLog

Some of the "..." constants can be further formalized in Hornlog with complex terms.

Here's a simple but non-trivial Hornlog example - with rule chaining - and its model-theoretic semantics. Complex terms are presented in bold to distinguish them from relation applications in the IF-THEN SCLP encoding and in the Prolog syntax. In RuleML, these are implemented using the <cterm> element.

The detailed implementation in RuleML 0.8 is available here.

Premium Customer Discount rule

In an IF-THEN syntax (a la SCLP):

IF   premium(?customer)
AND  regular(?product)
THEN discount(?customer, ?product, percent(5.0)); 

In RuleML:

<imp>
   <_rlab>discountrule1b</_rlab>
   <_head>
      <atom>
         <_opr>discount</_opr>
         <var>customer</var>
         <var>product</var>
         <cterm>
            <_opc>percent</_opc>
            <ind>5.0</ind>
         </cterm>
      </atom>
   </_head>
   <_body>
      <atom>
         <_opr>premium</_opr>
         <var>customer</var>
      </atom>
      <atom>
         <_opr>regular</_opr>
         <var>product</var>
      </atom>
   </_body>
</imp>

In Prolog syntax:

discount(Customer,Product,percent(5.0))  :-
   premium(Customer) , regular(Product).

Customer Premium Status rule:

In Natural language:

A customer is premium if their spending during the previous year 
was at least 5000 dollars. 

In an IF-THEN syntax (a la SCLP):

IF   spending(?customer, atleast(dollar(5000)), "previous year")
THEN premium(?customer); 

In RuleML:

<imp>
   <_rlab>statusrule1b</_rlab>
   <_head>
      <atom>
         <_opr>premium</_opr>
         <var>customer</var>
      </atom>
   </_head>
   <_body>
      <atom>
         <_opr>spending</_opr>
         <var>customer</var>
         <cterm>
            <_opc>atleast</_opc>
            <cterm>
               <_opc>dollar</_opc>
               <ind>5000</ind>
            </cterm>
         </cterm>
         <ind>"previous year"</ind>
      </atom>
   </body>
</imp>

In Prolog syntax:

premium(Customer)  :-
   spending(Customer,atleast(dollar(5000)),"previous year").

Facts:

spending("Peter Miller",atleast(dollar(5000)),"previous year").

regular("Honda").

Associated Herbrand Model:

{
 discount("Peter Miller","Honda",percent(5.0)),
 premium("Peter Miller"),
 spending("Peter Miller",atleast(dollar(5000)),"previous year"),  
 regular("Honda")
}

4. URI constant names feature

Objects (resources) are inds (individual constants represented by the <ind> element in RuleML) using a URL/URI as their OID, as in SHOE or RDF.

In our use case, an href attribute can be defined for inds representing "Peter Miller" a customer or "Honda" a product. In RuleML hornlog, <ctor> elements can also use an href attribute.

The fact:

"Peter Miller" is a premium customer where "Peter Miller" is represented by its URI: http://customers.ebiz.com/petermiller

can be encoded as follows:

<fact>
      <atom>
         <_opr>premium</_opr>
         <ind href="http://customers.ebiz.com/petermiller"/>
      </atom>
</fact>