Reaction Rules in R2ML

A reaction rule extracted from Booking Service Use Case:


If there is a valid CheckAvailability event (i.e. checkInDate < checkOutDate) and a room is available 
then send a CheckAvailabilityResponse message.

The r2ml:RuleBase element (see Line 1) is the parent of one or many rule sets. Namespaces which must be available in all rules can be declared here (see Line 2, for example).

A reaction rule set is declared by the element r2ml:ReactionRuleSet (see Line 3).

1 <r2ml:RuleBase xmlns:r2ml="http://www.rewerse.net/I1/2006/R2ML" xmlns:dc="http://purl.org/dc/elements/1.1/" 
2              xmlns:ex="http://www.bookingservice.com/schema" 
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:r2mlv="http://www.rewerse.net/I1/2006/R2ML/R2MLV"   
               xsi:schemaLocation="http://www.rewerse.net/I1/2006/R2ML   
 http://oxygen.informatik.tu-cottbus.de/R2ML/0.5/R2ML.xsd"

3 <r2ml:ReactionRuleSet r2ml:ruleSetID="UServReactionRuleSet" 
                    
4 <r2ml:ReactionRule r2ml:ruleID="RR_01">  
5  <r2ml:Documentation>
6   <r2ml:RuleText r2ml:textFormat="plain">
7    <[!CDATA[
      If there is a valid CheckAvailability event (i.e. checkInDate < checkOutDate) and a room is available 
      then send a CheckAvailabilityResponse message.
    ]]>
8   </r2ml:RuleText>
9  </r2ml:Documentation>

Each reaction rule is enclosed into a r2ml:ReactionRule element (see Line 4). An optional element r2ml:Documentation (see Line 5) can contains elements that enclose the rule text (Lines 5-9) and the representation of the rule in a specific rules language.

10  <r2ml:triggeringEventExpr>
11   <r2ml:AtomicEventExpression r2ml:eventType="ex:CheckAvailability">
12    <r2ml:contextArgument>
13     <r2ml:ObjectVariable r2ml:name="chkAv" r2ml:class="ex:CheckAvailability"/>
14    </r2ml:contextArgument>
15   </r2ml:AtomicEventExpression>
16  </r2ml:triggeringEventExpr>

A R2ML reaction rule is a statement of programming logic that specifies the execution of one or more actions in the case of a trigerring event occurence and if its conditions are satisfied.

The trigerring event is in our case the CheckAvailability AtomicEventExpression. An AtomicEventExpression is an atomic event that refers an to an event type, which is its classifier (e.g. r2ml:eventType attribute)and is composed from a set of r2ml:Slot or one r2ml:contextArgument.

The body of the rule is represented by the content of r2ml:conditions role element (Lines 17-75). It contains all the atoms that form the rule conditions. By default, all atoms from the rule body are connected by conjunction.

17  <r2ml:conditions>
18   <r2ml:DatatypePredicateAtom r2ml:datatypePredicate="swrlb:lessThan">
19    <r2ml:dataArguments>
20     <r2ml:AttributeFunctionTerm r2ml:attribute="ex:CheckAvailability.checkInDate">
21      <r2ml:contextArgument>
22       <r2ml:ObjectVariable r2ml:name="chkAv" r2ml:class="ex:CheckAvailability"/>
23      </r2ml:contextArgument>
24     </r2ml:AttributeFunctionTerm>
25     <r2ml:AttributeFunctionTerm r2ml:attribute="ex:CheckAvailability.checkOutDate">
26      <r2ml:contextArgument>
27       <r2ml:ObjectVariable r2ml:name="chkAv" r2ml:class="ex:CheckAvailability"/>
28      </r2ml:contextArgument>
29     </r2ml:AttributeFunctionTerm>
30    </r2ml:dataArguments>
31   </r2ml:DatatypePredicateAtom>

The relational operation (checkInDate < checkOutDate) is expressed in R2ML by DatatypePredicateAtom construct and using SWRL built-ins as predicate name (e.g."swrlb:lessThan"). The r2ml:dataArguments comprises the operands translation into R2ML terms, depending on the involved property. Our case involves two r2ml:AttributeFunctionTerm(s), as the properties represent class attributes (e.g. ex:CheckAvailability.checkInDate and ex:CheckAvailability.checkOutDate)(see Lines 18-31).

32   <r2ml:ObjectClassificationAtom r2ml:class="ex:Booking">
33    <r2ml:ObjectVariable r2ml:name="booking"/>
34   </r2ml:ObjectClassificationAtom>

The meaning of ObjectClassificationAtom is to classify an object to the class that it belongs to and consists of an object term. The class is specified by the value of the attribute r2ml:class (e.g. ex:Booking). The object referred in this example is expressed using R2ML ObjectVariable construct and is designated using the attribute r2ml:name="booking".

35   <r2ml:ReferencePropertyAtom r2ml:referenceProperty="ex:Booking.room">
36    <r2ml:subject>
37     <r2ml:ObjectVariable r2ml:name="booking"/>
38    </r2ml:subject>
39    <r2ml:object>
40     <r2ml:ObjectVariable r2ml:name="room" r2ml:class="ex:Room"/>
41    </r2ml:object>
42   </r2ml:ReferencePropertyAtom>

A ReferencePropertyAtom associates two object terms, having different meanings: subject and object. For example, to express the concept of: "booking a room" we use the code above, where the subject is booking and the object is room.

43   <r2ml:AttributionAtom r2ml:attribute="ex:Booking.from">
44    <r2ml:subject>
45     <r2ml:ObjectVariable r2ml:name="booking"/>
46    </r2ml:subject>
47    <r2ml:dataValue>
48     <r2ml:AttributeFunctionTerm r2ml:attribute="ex:CheckAvailability.checkInDate">
49      <r2ml:contextArgument>
50       <r2ml:ObjectVariable r2ml:name="chkAv" r2ml:class="ex:CheckAvailability"/>
51      </r2ml:contextArgument>
52     </r2ml:AttributeFunctionTerm>
53    </r2ml:dataValue>
54   </r2ml:AttributionAtom>

55   <r2ml:AttributionAtom r2ml:attribute="ex:Booking.from">
56    <r2ml:subject>
57     <r2ml:ObjectVariable r2ml:name="booking"/>
58    </r2ml:subject>
59    <r2ml:dataValue>
60     <r2ml:AttributeFunctionTerm r2ml:attribute="ex:CheckAvailability.checkOutDate">
61      <r2ml:contextArgument>
62       <r2ml:ObjectVariable r2ml:name="chkAv" r2ml:class="ex:CheckAvailability"/>
63      </r2ml:contextArgument>
64     </r2ml:AttributeFunctionTerm>
65    </r2ml:dataValue>
66   </r2ml:AttributionAtom>

67   <r2ml:AttributionAtom r2ml:attribute="isAvailable">
68    <r2ml:subject>
69     <r2ml:ObjectVariable r2ml:name="room"/>
70    </r2ml:subject>
71    <r2ml:dataValue>
72     <r2ml:TypedLiteral r2ml:lexicalValue="true" r2ml:datatype="xs:boolean"/>
73    </r2ml:dataValue>
74   </r2ml:AttributionAtom>
75  </r2ml:conditions>

As its name sugests, an AttributionAtom consists of a reference to a property (e.g. isAvailable), an object term that has this property (e.g. room), and a data term as the value of the property (e.g. boolean value true).

76  <r2ml:triggeredEventExpr>
77   <r2ml:AtomicEventExpression r2ml:eventType="CheckAvailabilityResponse">
78    <r2ml:DataSlot r2ml:attribute="ex:CheckAvailabilityResponse.yesNoAnswer">
79     <r2ml:value>
80      <r2ml:TypedLiteral r2ml:lexicalValue="true" r2ml:datatype="xs:boolean"/>
81     </r2ml:value>
82    </r2ml:DataSlot>
83   </r2ml:AtomicEventExpression>
84  </r2ml:triggeredEventExpr>
The head of the rule is expressed using the R2ML role element r2ml:triggeredEventExpr. It also contains a AtomicEventExpression that encodes an atomic event expression that has no duration: the message event response. It refers to an event type using r2ml:eventType attribute (e.g. CheckAvailabilityResponse) and is composed from a set of r2ml:Slot(s) or one r2ml:contextArgument. Our rule example provide a r2ml:DataSlot attribute to capture the message's data.
85 </r2ml:ReactionRule>
86</r2ml:ReactionRuleSet>

The R2MLV vocabulary of the rule is presented below. It describes the OOP concept of classes (e.g. Customer), its properties and input/output/fault messages.

<r2mlv:Vocabulary>
  <r2mlv:Class r2mlv:ID="Customer">
   <r2mlv:Attribute r2mlv:ID="custNo">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:integer" r2mlv:arity="1"/>
    </r2mlv:range>
   </r2mlv:Attribute>
  </r2mlv:Class>
  <r2mlv:MessageType r2mlv:messageTypeType="InputMessageType" r2mlv:ID="ex:CheckAvailability">
   <r2mlv:Attribute r2mlv:ID="ex:CheckAvailability.checkInDate">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:dateTime"/>
    </r2mlv:range>
   </r2mlv:Attribute>
   <r2mlv:Attribute r2mlv:ID="ex:CheckAvailability.checkOutDate">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:dateTime"/>
    </r2mlv:range>
   </r2mlv:Attribute>
   <r2mlv:Attribute r2mlv:ID="ex:CheckAvailability.roomType">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:integer"/>
    </r2mlv:range>
   </r2mlv:Attribute>
   <r2mlv:ReferenceProperty r2mlv:ID="ex:CheckAvailability.customer">
    <r2mlv:range>
     <r2mlv:Class r2mlv:ID="ex:Customer"/>
    </r2mlv:range>
   </r2mlv:ReferenceProperty>
  </r2mlv:MessageType>
  <r2mlv:MessageType r2mlv:messageTypeType="InFault" r2mlv:ID="ex:InfaultDataError">
   <r2mlv:Attribute r2mlv:ID="ex:InfaultDataError.message">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:string"/>
    </r2mlv:range>
   </r2mlv:Attribute>
  </r2mlv:MessageType>
  <r2mlv:MessageType r2mlv:messageTypeType="OutputMessageType" r2mlv:ID="ex:CheckAvailabilityResponse">
   <r2mlv:Attribute r2mlv:ID="ex:CheckAvailabilityResponse.yesNoAnswer">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:boolean"/>
    </r2mlv:range>
   </r2mlv:Attribute>
  </r2mlv:MessageType>
  <r2mlv:Class r2mlv:ID="ex:Room">
   <r2mlv:Attribute r2mlv:ID="ex:Room.isAvailable">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:boolean"/>
    </r2mlv:range>
   </r2mlv:Attribute>
   <r2mlv:Attribute r2mlv:ID="ex:Room.roomNo">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:integer"/>
    </r2mlv:range>
   </r2mlv:Attribute>
   <r2mlv:Attribute r2mlv:ID="ex:Room.roomType">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:integer"/>
    </r2mlv:range>
   </r2mlv:Attribute>
  </r2mlv:Class>
  <r2mlv:Class r2mlv:ID="ex:Booking">
   <r2mlv:Attribute r2mlv:ID="ex:Booking.from">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:float"/>
    </r2mlv:range>
   </r2mlv:Attribute>
   <r2mlv:Attribute r2mlv:ID="ex:Booking.to">
    <r2mlv:range>
     <r2mlv:Datatype r2mlv:ID="xs:float"/>
    </r2mlv:range>
   </r2mlv:Attribute>
   <r2mlv:ReferenceProperty r2mlv:ID="ex:Booking.room">
    <r2mlv:range>
     <r2mlv:Class r2mlv:ID="ex:Room"/>
    </r2mlv:range>
   </r2mlv:ReferenceProperty>
  </r2mlv:Class>
 </r2mlv:Vocabulary>