Interchange between JBoss Rules and F-Logic

REWERSE Use-Cases Examples

Editors: Adrian Giurca, Gerd Wagner (REWERSE Working Group I1)

JBoss Rules (Related to the UCR 2.1)

The use case case illustrates the usage of RIF to supply a vendor-neutral representation of rules, so that rule-system developers can do their work without concern about a vendor-specific format and in particular without concern about the compatibility with the technology of its business partners.

Below are two rules from the use case:

Rule R1

If an item is perishable and it is delivered more than 10 days after the scheduled delivery date then the item will be rejected.

Rule R2

If an item is perishable and it is delivered more than 7 days after the scheduled delivery date but less than 14 days after the scheduled delivery date then a discount of 18.7% will be applied to this delivery.

Rules are working on top of vocabularies

Looking to these rules it is very clear you need to understand the vocabulary which these rules use i.e. what is an item, scheduled delivery etc. Therefore first I will develop a vocabulary for the rules.

Different developpers may choose different vocabulary languages do do that. Some of them may choose RDFS, some others may choose OWL or UML. In this document I will use UML to represent an excerpt from the entire use case vocabulary. As a consequence some recommendations for RIF may be taken into consideration:

  • RIF needs to deal with different vocabularies.
  • RIF needs an uniform mechanism to address vocabulary elements. The usage of URI's may be such a mechanism.
  • Vocabulary and rules are separate layers

The next image shows an excerpt from the UML vocabulary we need for encoding our rules. Suppose that Jane's ecommerce system use JBoss Rules since Java platform is widely used in e-commerce applications.

Vocabulary

Below we present an excerpt from the John's OWL vocabulary.

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
  	     xmlns:owl="http://www.w3.org/2002/07/owl#"
         xmlns=http://www.example.org/JohnSystem/vocabulary">
 
  <owl:Class rdf:ID="Item"/>
  
  <owl:DatatypeProperty rdf:ID="perishable">
    <rdfs:domain rdf:resource="#Item"/>
    <rdfs:range rdf:resource="xs:boolean"/>
  </owl:DatatypeProperty>

  <owl:DatatypeProperty rdf:ID="actualDeliveryDate">
    <rdfs:domain rdf:resource="#Item"/>
    <rdfs:range rdf:resource="xs:date"/>
  </owl:DatatypeProperty>

  <owl:DatatypeProperty rdf:ID="scheduledDeliveryDate">

    <rdfs:domain rdf:resource="#Item"/>
    <rdfs:range rdf:resource="xs:date"/>
  </owl:DatatypeProperty>

  <owl:DatatypeProperty rdf:ID="status">
    <rdfs:domain rdf:resource="#Item"/>
    <rdfs:range rdf:resource="Status"/>

  </owl:DatatypeProperty>

  <owl:DatatypeProperty rdf:ID="discount">
    <rdfs:domain rdf:resource="#Item"/>
    <rdfs:range rdf:resource="xs:double"/>
  </owl:DatatypeProperty>
  
  <owl:Class rdf:ID="Delivery"/>

  
  <owl:Class rdf:ID="Status">
     <owl:oneOf rdf:parseType="Collection">
     <rdf:List>
       <rdf:first rdf:datatype="xs:string">rejected</rdf:first>
       <rdf:rest>

         <rdf:first rdf:datatype="xs:string">accepted</rdf:first>
       </rdf:rest>
     </rdf:List>
    </owl:oneOf>
  </owl:Class>

 </rdf:RDF>

Encoding of Rules

John Rules

We will not discuss the F-Logic language, just provide the rules code.

Rule R1(John)


If an item is perishable and it is delivered more than 10 days after the scheduled delivery date then the item will be rejected.

I:item[status->"rejected"] <- I:item[isPerishable -> true] AND I[actualDeliveryDate -> A] AND I[scheduledDeliveryDate-> S] AND R is (A - S) AND R > 10.

Rule R2(John)

If an item is perishable and it is delivered more than 7 days after the scheduled delivery date but less than 14 days after the scheduled delivery date then a discount of 18.7% will be applied to this delivery.

I:item[discount -> 18.7] <- D:delivery[hasItems->>Items] AND I:item[isPerishable -> true] AND I[actualDeliveryDate -> A] AND I[scheduledDeliveryDate-> S] AND member(I, Items) AND R is (A - S) AND R > 7.

Jane's Rules

We will not discuss the JBoss Rule Language, just provide the rules code.

Rule R1(Jane)

package com.sample
 
import com.sample.Item;

rule "Jane R1"
 when
  i : Item(actualDeliveryDate : actualDeliveryDate, scheduledDeliveryDate : scheduledDeliveryDate )
  eval( actualDeliveryDate.getDay() - scheduledDeliveryDate.getDay() > 10)
 then
  i.setPerishable(true);
end

Rule R2(Jane)

rule "Jane R2"
 when
   d:Delivery(hasItems: hasItems)
   i : Item(actualDeliveryDate : actualDeliveryDate, scheduledDeliveryDate : scheduledDeliveryDate )
   eval( (actualDeliveryDate.getDay() - scheduledDeliveryDate.getDay() > 7) && hasItems.contains(i))
 then
   i.setDiscount(18.7);
   modify(i);

end

Interchanging of Rules

This section presents a scenario of interchange using REWERSE proposal.

Importing Jane's Rule R1 in the John's rule system

Phase 1: Translating from JBoss Rules into RIF
Rule("Jane_R1" 

 conditions(
   
   AttributionAtom(jane:Item.actualDeliveryDate OVar(i jane:Item) DVar(actualDeliveryDate xs:date))
   
   AttributionAtom(jane:Item.scheduledDeliveryDate OVar(i jane:Item) DVar(scheduledDeliveryDate xs:date))
	 
   DatatypePredicateAtom(swrlb:greaterThan 
    DatatypeFTerm(op:numeric-subtract
     DataOperationFTerm(java.util.Date.getDate contextArg(DVar(actualDeliveryDate xs:date)))
     DataOperationFTerm(java.util.Date.getDate contextArg(DVar(scheduledDeliveryDate xs:date)))
    )
    TLit(10 xs:int)
   )
 )
 conclusion(
  AttributionAtom(jane:Item.discount  OVar(i jane:Item) TLit("18.7" xs:double))
 )
)	 

here the jane is the namespace http://sample.com.

Phase 2: Translating from RIF into F-Logic

This intermediary rule translates into F-Logic:

I:jane#Item[jane#Item.discount->"18.7"] <- I:jane#Item[jane#Item.actualDeliveryDate->ActualDeliveryDate] AND I:jane#Item[jane#Item.scheduledDeliveryDate->ScheduledDeliveryDate] AND R is (ActualDeliveryDate - ScheduledDeliveryDate) AND R > 10.

Observations:

This last rule can be adopted by John's rule system under at least the following circumstances:

  • Both Jane and John rule systems are namespace aware
  • RIF must be namespace aware
  • Jane rule system and John rule system share the same vocabulary OR there exists an interchange of vocabularies.
  • RIF is based on vocabularies
  • RIF is able to deal with types.

Importing John's Rule R2 into Jane's rule system

Phase 1: Translating from F-logic Rules into RIF

John's Rule R2 is translated to RIF according with the following mapping:

  • All variables are created according with the types from the OWL vocabulary.
  • Each F-molecule is translated into ReferencePropertyAtom/AttributionAtom according with the value type of the property. This type is obtained by inspecting the OWL vocabulary.
Rule(
 conditions(
   
   AttributionAtom(hasItems OVar(D delivery) DVar(Items))
	 
   AttributionAtom(isPerishable OVar(I item) TLit("true" xs:boolean))
   
   AttributionAtom(actualDeliveryDate OVar(I item) DVar(A xs:date))
   
   AttributionAtom(scheduledDeliveryDate OVar(I item) DVar(S xs:date))

   DatatypePredicateAtom(swrlb:member OVar(I) DVar(Items))	

   DatatypePredicateAtom(swrlb:equal DVar(R) DatatypeFTerm(op:numeric-subtract DVar(A xs:date) DVar(S xs:date)))	
	  
   DatatypePredicateAtom(swrlb:greaterThan DVar(R) TLit(7 xs:int)))
 )
 conclusion(
   AttributionAtom(discount OVar(I item) TLit("18.7" xs:double))
 )
)
Phase 2: Translating from RIF to JBoss Rules

John's (RIF) Rule R2 is translated to JBoss Rules according with the following mapping rules:

  • All vocabulary classes(namespace aware) are translated into the corresponding imports.
  • All variables are generated according with JBoss rules syntax.
  • A rule ID is generated.
  • Rule conditions:
    • All properties(reference properties and attributes) from rule conditions which refers to the same subject are translated into the same condition.
    • All datatype predicate atoms and datatype function terms are translated into the corresponding java expressions and evaluated with eval().
  • Rule conclusion:
  • The attribution atom is translated into the corresponding setter call of the bean.
package com.sample
 
import com.sample.Item;

rule "John_R1_generatedID"
 when
  d : Delivery(items: hasItems)
  i : Item( isPerishable == true, A : actualDeliveryDate, S : scheduledDeliveryDate)
  eval((A.getDay() - S.getDay() > 7) && items.contains(i))
 then
  i.setDiscount(18.7);
  modify(i);
end