*Reactivity on the Web* is an emerging research issue covering: *updating data on the Web*, exchanging information about *events* (such as executed updates) between Web nodes, and *reacting to combinations of such events*. Reactivity plays an important role for upcoming Web systems such as online marketplaces, adaptive Web and Semantic Web systems, as well as Web services and Grids.

Following a declarative approach to reactivity on the Web, a novel reactive, rule-based language called *XChange* (see publications) has been developed. The language *XChange* follows clear paradigms that aim at providing a better language understanding and ease programming. *XChange* paradigms:

*Event vs. Event Query.*An*event*is a happening to which each Web site may decide to react in a particular way or not to react to at all. One might argue that defining an event in such a way is too vague. The intention here is to emphasise that one can conceive every kind of changes on the Web as events. However, each Web-based reactive system can be interested in different types of events or in different combinations of (like a given temporal order between) such events. Thus, the large spectra of possible events are always filtered relatively to one’s interests. In order to notify Web nodes about events and to process event data, events need to have a data representation. In XChange, incoming events are represented as XML documents.*Event queries*are queries against event data. Event query specifications differ considerably from event representations (e.g. event queries may contain variables for selecting parts of the events’ representation). Most proposals dealing with reactivity do not significantly differentiate between event and event query. Overloading the notion of event precludes a clear language semantics and thus, makes the implementation of the language and its usage much more difficult. Event queries in XChange serve a double purpose: detecting events of interest and temporal combinations of them, and selecting data items from events’ representation. This double purpose is novel in the field of reactivity and reactive rules.

*Volatile vs. Persistent Data.*The development of the XChange language – its design and its implementation – reflects the novel view over the Web data that differentiates between*volatile data*(event data communicated on the Web between XChange programs) and*persistent data*(data of Web resources, such as XML or HTML documents). The clear distinction between volatile and persistent data aims at easing programming and avoiding the emergence of a parallel Web of events.

*Rule-Based Language.*Reactivity can be specified and realised by means of reactive rules. XChange is a rule-based language that uses reactive rules for specifying the desired reactive behavior and deductive rules for constructing views over Web resources’ data.An XChange program is located at one Web site and contains reactive rules, more precisely Event-Condition-Action rules (ECA rules) of the form*Event query – Web query – Action*. Every incoming event is queried using the*event query*(query against volatile data). If an answer is found and the*Web query*(query to persistent data) has also an answer, then the*Action*is executed. The fact that the event query and the Web query have answers determines the rule to be fired; the answers influence the action to be executed, as information contained in the answers are generally used in the action part.

*Pattern-Based Approach.*XChange is a*pattern-based language*: event queries, Web queries, event raising specifications, and updates describe*patterns*for events requiring a reaction, Web data, raising event messages, and updating Web data, respectively. Patterns are templates that closely resemble the structure of the data to be queried, constructed, or modified, thus being very intuitive and also straight-forward to visualize.

*Processing of Events**Local Processing and Incremental Evaluation*. Event queries are processed locally at each XChange-aware Web node. For efficiency reasons, (composite) event queries are evaluated in an*incremental manner*.*Bounded Event Lifespan.*An essential aspect of XChange is that each XChange-aware Web node controls its own event memory usage. In particular, the size of the event history kept in memory depends only on the event queries posed at this Web node. This is consistent with the clear distinction between events as volatile data and standard Web data as persistent data.

*Relationship Between Reactive and Query Languages.*A working hypothesis of the XChange project is that a reactive language for the Web should build upon a Web query language. XChange embeds the (Semantic) Web query language Xcerpt.

### 1.1 Production rule interchange

Production rules have an

*if*part, or*condition*, and a*then*part, or*action*. The condition is like the condition part of logic rules (as covered by RIF-Core and its basic logic dialect extension, RIF-BLD). The*then*part contains actions. An action can assert facts, modify facts, retract facts, and have other side-effects. In general, an action is different from the conclusion of a logic rule, which contains only a logical statement. However, the conclusion of rules interchanged using RIF-Core can be interpreted, according to RIF-PRD operational semantics, as actions that assert facts in the knowledge base.**Example 1.1.**The following are examples of production rules:*A customer becomes a “Gold” customer when his cumulative purchases during the current year reach $5000*.*Customers that become “Gold” customers must be notified immediately, and a golden customer card will be printed and sent to them within one week*.*For shopping carts worth more than $1000, “Gold” customers receive an additional discount of 10% of the total amount.*☐

Because RIF-PRD is a production rule interchange format, it specifies an abstract syntax that shares features with concrete production rule languages, and it associates the abstract constructs with normative semantics and a normative XML concrete syntax. Annotations (

*e.g.*rule author) are the only constructs in RIF-PRD without a formal semantics.The abstract syntax is specified in mathematical English, and the abstract syntactic constructs that are defined in the sections Abstract Syntax of Conditions, Abstract Syntax of Actions and Abstract Syntax of Rules and Rulesets, are mapped into the concrete XML constructs in the section XML syntax. A lightweight notation is used, instead of the XML syntax, to tie the abstract syntax to the specification of the semantics. A more complete presentation syntax is specified using an EBNF in Presentation Syntax. However, only the XML syntax and the associated semantics are normative. The normative XML schema is included in Appendix: XML Schema.

**Example 1.2.**In RIF-PRD presentation syntax, the first rule in example 1.1. can be represented as follows:Prefix(ex <http://example.com/2008/prd1#>) (* ex:rule_1 *) Forall ?customer ?purchasesYTD ( If And( ?customer#ex:Customer ?customer[ex:purchasesYTD->?purchasesYTD] External(pred:numeric-greater-than(?purchasesYTD 5000)) ) Then Do( Modify(?customer[ex:status->"Gold"]) ) )

☐

Production rules are statements of programming logic that specify the execution of one or more actions when their conditions are satisfied. Production rules have an operational semantics, that the OMG Production Rule Representation specification [OMG-PRR] summarizes as follows:

*Match:*the rules are instantiated based on the definition of the rule conditions and the current state of the data source;*Conflict resolution:*a decision algorithm, often called*the conflict resolution strategy*, is applied to select which rule instance will be executed;*Act:*the state of the data source is changed, by executing the selected rule instance’s actions. If a terminal state has not been reached, the control loops back to the first step (*Match*).

In the section Operational semantics of rules and rule sets, the semantics for rules and rule sets is specified, accordingly, as a labeled terminal transition system (PLO04), where state transitions result from executing the action part of instantiated rules. When several rules are found to be executable at the same time, during the rule execution process, a

*conflict resolution strategy*is used to select the rule to execute. The section Conflict resolution specifies how a conflict resolution strategy can be attached to a rule set. RIF-PRD defines a default conflict resolution strategy.In the section Semantics of condition formulas, the semantics of the condition part of rules in RIF-PRD is specified operationally, in terms of matching substitutions. To emphasize the overlap between the rule conditions of RIF-BLD and RIF-PRD, and to share the same RIF definitions for datatypes and built-ins [RIF-DTB], an alternative, and equivalent, specification of the semantics of rule conditions in RIF-PRD, using a model theory, is provided in the appendix Model-theoretic semantics of RIF-PRD condition formulas.

The semantics of condition formulas and the semantics of rules and rule sets make no assumption regarding how condition formulas are evaluated. In particular, they do not require that condition formula be evaluated using pattern matching. However, RIF-PRD conformance, as defined in the section Conformance and interoperability, requires only support for safe rules, that is, forward-chaining rules where the conditions can be evaluated based on pattern matching only.

In the section Operational semantics of actions, the semantics of the action part of rules in RIF-PRD is specified using a transition relation between successive states of the data source, represented by ground condition formulas, thus making the link between the model-theoretic semantics of conditions and the operational semantics of rules and rule sets.

The abstract syntax of RIF-PRD documents, and the semantics of the combination of multiple RIF-PRD documents, is specified in the section Document and imports.

In addition to externally specified functions and predicates, and in particular, in addition to the functions and predicates built-ins defined in [RIF-DTB], RIF-PRD supports externally specified actions, and defines one action built-in, as specified in the section Built-in functions, predicates and actions.

### 1.2 Running example

The same example rules will be used throughout the document to illustrate the syntax and the semantics of RIF-PRD.

The rules are about the status of customers at a shop, and the discount awarded to them. The rule set contains four rules, to be applied when a customer checks out:

- Gold rule:
*A “Silver” customer with a shopping cart worth at least $2,000 is awarded the “Gold” status*. - Discount rule:
*“Silver” and “Gold” customers are awarded a 5% discount on the total worth of their shopping cart*. - New customer and widget rule:
*A “New” customer who buys a widget is awarded a 10% discount on the total worth of her shopping cart, but she looses any voucher she may have been awarded*. - Unknown status rule:
*A message must be printed, identifying any customer whose status is unknown (that is, neither “New”, “Bronze”, “Silver” or “Gold”), and the customer must be assigned the status: “New”*.

The

*Gold rule*must be applied first; that is, e.g., a customer with “Silver” status and a shopping cart worth exactly $2,000 should be promoted to “Gold” status, before being given the 5% discount that would disallow the application of the*Gold rule*(since the total worth of his shopping cart would then be only $1,900).In the remainder of this document, the prefix

`ex1`stands for the fictitious namespace of this example:`http://example.com/2009/prd2#`.## 2 Conditions

This section specifies the syntax and semantics of the condition language of RIF-PRD.

The RIF-PRD condition language specification depends on Section Constants, Symbol Spaces, and Datatypes, in the RIF data types and builtins specification [RIF-DTB].

### 2.1 Abstract syntax

The alphabet of the RIF-PRD condition language consists of:

- a countably infinite set of
**constant symbols**`Const`, - a countably infinite set of
**variable symbols**`Var`(disjoint from`Const`), - and syntactic constructs to denote:
- lists,
- function calls,
- relations, including equality, class membership and subclass relations
- conjunction, disjunction and negation,
- and existential conditions.

For the sake of readability and simplicity, this specification introduces a notation for these constructs. The notation is not intended to be a concrete syntax, so it leaves out many details. The only concrete syntax for RIF-PRD is the XML syntax.

RIF-PRD supports externally defined functions only (including the built-in functions specified in [RIF-DTB]). RIF-PRD, unlike RIF-BLD, does not support uninterpreted function symbols (sometimes called logically defined functions).

RIF-PRD supports a form of negation. Neither RIF-Core nor RIF-BLD support negation, because logic rule languages use many different and incompatible kinds of negation. See also the RIF framework for logic dialects [RIF-FLD].

#### 2.1.1 Terms

The most basic construct in the RIF-PRD condition language is the

*term*. RIF-PRD defines several kinds of term:*constants*,*variables*,*lists*and*positional*terms.**Definition (Term)**.*Constants and variables*. If`t`∈`Const`or`t`∈`Var`then`t`is a;**simple term***List terms*. Ahas the form**list**`List(t`_{1}…`t`_{m}`)`, where`m≥0`and`t`_{1}, …,`t`_{m}are ground terms, i.e. without variables. A list of the form`List()`(i.e., a list in which`m=0`) is called the;**empty list***Positional terms*. If`t`∈`Const`and`t`, …,_{1}`t`,_{n}`n≥0`, are terms then`t(t`is a_{1}... t_{n}).**positional term**

Here, the constant`t`represents a function and`t`, …,_{1}`t`represent argument values. ☐_{n}

To emphasize interoperability with RIF-BLD, positional terms may also be written:

`External(t(t`._{1}...t_{n}))**Example 2.1.**`List("New" "Bronze" "Silver" "Gold")`is a term that denotes the list of the values for a customer’s status that are known to the system. The elements of the list,`"New"`,`"Bronze"`,`"Silver"`and`"Gold"`are terms denoting string constants;`func:numeric-multiply(?value, 0.90)`is a positional term that denotes the product of the value assigned to the variable`?value`and the constant`0.90`. That positional term can be used, for instance, to represent the new value, taking the discount into account, to be assigned a customer’s shopping cart, in the rule*New customer and widget rule*. An alternative notation is to mark explicitly the positional term as externally defined, by wrapping it with the`External`indication:`External(func:numeric-multiply(?value, 0.90))`☐

#### 2.1.2 Atomic formulas

*Atomic formulas*are the basic tests of the RIF-PRD condition language.**Definition (Atomic formula)**. Ancan have several different forms and is defined as follows:**atomic formula***Positional atomic formulas*. If`t`∈`Const`and`t`, …,_{1}`t`,_{n}`n≥0`, are terms then`t(t`is a_{1}... t_{n})(or simply an**positional atomic formula**)**atom***Equality atomic formulas*.`t = s`is an(or simply an**equality atomic formula**), if**equality**`t`and`s`are terms*Class membership atomic formulas*.`t#s`is a(or simply**membership atomic formula**) if**membership**`t`and`s`are terms. The term`t`is the*object*and the term`s`is the*class**Subclass atomic formulas*.`t##s`is a(or simply a**subclass atomic formula**) if**subclass**`t`and`s`are terms*Frame atomic formulas*.`t[p`is a_{1}->v_{1}... p_{n}->v_{n}](or simply a**frame atomic formula**) if**frame**`t`,`p`, …,_{1}`p`,_{n}`v`, …,_{1}`v`,_{n}`n ≥ 0`, are terms. The term`t`is the*object*of the frame; the`p`are the_{i}*property*or*attribute*names; and the`v`are the property or attribute_{i}*values*. In this document, an attribute/value pair is sometimes called a*slot**Externally defined atomic formulas.*If`t`is a positional atomic formula then`External(t)`is an. ☐**externally defined atomic formula**

Class membership, subclass, and frame atomic formulas are used to represent classifications, class hierarchies and object-attribute-value relations.

Externally defined atomic formulas are used, in particular, for representing built-in predicates.

In the RIF-BLD specification, as is common practice in logic languages, atomic formulas are also called

*terms*.**Example 2.2.**- The membership formula
`?customer # ex1:Customer`tests whether the individual bound to the variable`?customer`is a member of the class denoted by`ex1:Customer`. - The atom
`ex1:Gold(?customer)`tests whether the customer represented by the variable`?customer`has the*“Gold”*status. - Alternatively, gold status can be tested in a way that is closer to an object-oriented representation using the frame formula
`?customer[ex1:status->"Gold"]`. - The following atom uses the built-in predicate
`pred:list-contains`to validate the status of a customer against a list of allowed customer statuses:`External(pred:list-contains(List("New", "Bronze", "Silver", "Gold"), ?status))`. ☐

#### 2.1.3 Formulas

Composite truth-valued constructs are called

*formulas*, in RIF-PRD.Note that terms (constants, variables, lists and functions) are

*not*formulas.More general formulas are constructed out of atomic formulas with the help of logical connectives.

**Definition (Condition formula)**. Acan have several different forms and is defined as follows:**condition formula***Atomic formula*: If`φ`is an atomic formula then it is also a condition formula.*Conjunction*: If`φ`, …,_{1}`φ`,_{n}`n ≥ 0`, are condition formulas then so is`And(φ`, called a_{1}... φ_{n})*conjunctive*formula. As a special case,`And()`is allowed and is treated as a tautology, i.e., a formula that is always true.*Disjunction*: If`φ`, …,_{1}`φ`,_{n}`n ≥ 0`, are condition formulas then so is`Or(φ`, called a_{1}... φ_{n})*disjunctive*formula. As a special case,`Or()`is permitted and is treated as a contradiction, i.e., a formula that is always false.*Negation*: If`φ`is a condition formula, then so is`Not(φ)`, called a*negative*formula.*Existentials*: If`φ`is a condition formula and`?V`, …,_{1}`?V`,_{n}`n>0`, are variables then`Exists ?V`is an_{1}... ?V_{n}(φ)*existential*formula. ☐

In the definition of a formula, the component formulas

`φ`and`φ`are said to be_{i}of the respective condition formulas that are built using these components.**subformulas****Example 2.3.**- The condition of the
*New customer and widget rule*:*A “New” customer who buys a widget*, can be represented by the following RIF-PRD condition formula:

And( ?customer # ex1:Customer ?customer[ex1:status->"New"] Exists ?shoppingCart ?item ( And ( ?customer[ex1:shoppingCart->?shoppingCart] ?shoppingCart[ex1:containsItem->?item] ?item # ex1:Widget) ) ) )

☐

The function

, that maps a term, an atomic formula or a condition formula to the set of its free variables is defined as follows:**Var**- if
*e*∈*Const*, then*Var(e) = ∅*; - if
*e*∈*Var*, then*Var(e)*= {*e*}; - if
*e*is a*list term*, then*Var(e) = ∅*; - if
*f(arg*,_{1}…arg_{n})*n ≥ 0*, is a positional term, then,*Var(f(arg*= ∪_{1}…arg_{n})_{i=1…n}*Var(arg*;_{i}) - if
*p(arg*, n ≥ 0, is an atom, then,_{1}…arg_{n})*Var(p(arg*=_{1}…arg_{n})*Var(External(p(arg*= ∪_{1}…arg_{n}))_{i=1…n}*Var(arg*;_{i}) - if
*t*and_{1}*t*are terms, then_{2}*Var(t*=_{1}[=|#|##] t_{2})*Var(t*∪_{1})*Var(t*;_{2}) - if
*o’*,*k*_{i},*i*= 1…n, and*v*_{i},*i*= 1…n, n ≥ 1, are terms, then*Var(o[k*=_{1}->v_{1}… k_{n}->v_{n}])*Var(o)*∪_{i=1…n}*Var(k*∪_{i})_{i=1…n}*Var(v*._{i}) - if
*f*,_{i}*i*= 0…n, n ≥ 0, are condition formulas, then*Var([And|Or](f*= ∪_{1}…f_{n}))_{i=0…n}*Var(f*;_{i}) - if
*f*is a condition formula, then*Var(Not(f))*=*Var(f)*; - if
*f*is a condition formula and*x*∈_{i}*Var*for*i*= 1…n,*n ≥ 1*, then,*Var(Exists x*=_{1}… x_{n}(f))*Var(f)*–*{x*._{1}…x_{n}}

**Definition (Ground formula)**. A condition formula`φ`is aif and only if**ground formula***Var*`φ`=*∅*and`φ`does not contain any existential subformula. ☐In other words, a ground formula does not contain any variable term.

#### 2.1.4 Well-formed formulas

Not all formulas are well-formed in RIF-PRD: it is required that no constant appear in more than one context. What this means precisely is explained below.

The set of all constant symbols,

`Const`, is partitioned into the following subsets:- A subset of individuals. The symbols in
`Const`that belong to the primitive datatypes are required to be individuals; - A subset for external function symbols;
- A subset of plain predicate symbols;
- A subset for external predicate symbols.

As seen from the following definitions, these subsets are not specified explicitly but, rather, are inferred from the occurrences of the symbols.

**Definition (Context of a symbol)**. Theof a symbol,**context of an occurrence**`s∈Const`, in a formula,`φ`, is determined as follows:- If
`s`occurs as a predicate in an atomic subformula of the form`s(...)`then`s`occurs in the*context of a (plain) predicate symbol*; - If
`s`occurs as a predicate in an atomic subformula`External(s(...))`then`s`occurs in the*context of an external predicate symbol*; - If
`s`occurs as a function in a term (which is not a subformula)`s(...)`(or`External(s(...))`) then`s`occurs in the*context of an (external) function symbol*; - If
`s`occurs in any other context (e.g. in a frame:`s[...]`,`...[s->...]`, or`...[...->s]`; or in a positional atom:`p(...s...)`), it is said to occur as an*individual*. ☐

**Definition (Well-formed formula)**. A formula`φ`isiff:**well-formed**- every constant symbol mentioned in
`φ`occurs in exactly one context; - whenever a formula contains a positional term,
`t`(or`External(t)`), or an external atomic formula,`External(t)`,`t`must be an instance of a schema in the coherent set of external schemas (Section Schemas for Externally Defined Terms in [RIF-DTB]) associated with the language of RIF-PRD; - if
`t`is an instance of a schema in the coherent set of external schemas associated with the language then`t`can occur only as an external term or atomic formula. ☐

**Definition (RIF-PRD condition language)**. Theconsists of the set of all well-formed formulas. ☐**RIF-PRD condition language**### 2.2 Operational semantics of condition formulas

This section specifies the semantics of the condition formulas in a RIF-PRD document.

Informally, a condition formula is evaluated with respect to a state of facts and it is

*satisfied*, or*true*, if and only if:- it is an atomic condition formula and its variables are bound to individuals such that, when these constants are substituted for the variables, either
- it matches a fact, or
- it is implied by some background knowledge, or
- it is an externally defined predicate, and its evaluation yelds
*true*, or

- it is a compound condition formula: conjunction, disjunction, negation or existential; and it is evaluated as expected, based on the truth value of its atomic components.

The semantics is specified in terms of matching substitutions in the sections below. The specification makes no assumption regarding how matching substitutions are determined. In particular, it does not require from well-formed condition formulas that they can be evaluated using pattern matching only. However, RIF-PRD requires safeness from well-formed rules, which implies that all the variables in the left-hand side can be bound by pattern matching.

For compatibility with other RIF specifications (in particular, RIF data types and built-ins [RIF-DTB] and RIF RDF and OWL compatibility [RIF-RDF-OWL]), and to make explicit the interoperability with RIF logic dialects (in particular RIF Core [RIF-Core] and RIF-BLD [RIF-BLD]), the semantics of RIF-PRD condition formulas is also specified using model theory, in appendix Model theoretic semantics of RIF-PRD condition formulas.

The two specifications are equivalent and normative.

#### 2.2.1 Matching substitution

Let

`Term`be the set of the terms in the RIF-PRD condition language (as defined in section Terms).**Definition (Substitution).**Ais a finitely non-identical assignment of terms to variables; i.e., a function**substitution***σ*from`Var`to`Term`such that the set {*x*∈`Var`|*x*≠ σ(*x*)} is finite. This set is called the domain of σ and denoted by*Dom*(σ). Such a substitution is also written as a set such as σ = {*t*/_{i}*x*}_{i}_{i=1..n}where*Dom*(σ) = {*x*}_{i}_{i=1..n}and σ(*x*) =_{i}*t*,_{i}*i*= 1..n. ☐**Definition (Ground Substitution).**A*ground substitution*is a substitution σ that assigns only ground terms to the variables in*Dom*(σ): ∀*x*∈*Dom*(σ),*Var*(σ(*x*)) = ∅ ☐Because RIF-PRD covers only externally defined interpreted functions, a ground positional term can always be replaced by the (non-positional) ground term to which it evaluates. As a consequence, a ground RIF-PRD formula can always be restricted, without loss of generality, to contain no positional term; that is, to be such that any ground positional terms have been replaced with the non-positional ground terms to which they evaluate. In the remainder of this document, it will always be assumed that a ground condition formula never contains any positional term. As a consequence, a ground substitution never assigns a ground positional term to the variables in its domain.

If

*t*is a term or a condition formula, and if*σ*is a ground substitution such that*Var(t) ∈ Dom(σ)*,*σ(t)*denotes the ground term or the ground condition formula obtained by substituting, in*t*:*σ(x)*for all*x ∈ Var(t)*, and- the externally defined results of interpreting a function with ground arguments, for all externally defined terms.

**Definition (Matching substitution).**Let*ψ*be a RIF-PRD condition formula; let*σ*be a ground substitution such that*Var(ψ) ⊆ Dom(σ)*; and let*Φ*be a set of ground RIF-PRD atomic formulas.We say that the ground substitution

*σ***matches***ψ*to*Φ*if and only if one of the following is true:*ψ*is an atomic formula and either*σ(ψ)*∈*Φ*, or*ψ*is a frame with multiple slots,`o[s`,_{1}->v_{1}...s_{n}->v_{n}]*n > 1*, and there is one*i, 1≤i≤n*, such that*σ*matches the conjunction`And(o[s`to_{i}->v_{i}] o[s_{1}->v_{1}...s_{i-1}->v_{i-1}s_{i+1}->v_{i+1}...s_{n}->v_{n}]*Φ*; or*ψ*is an equality formula,`t`, and either_{1}= t_{2}*σ(t*and_{1})*σ(t*are the same ground term;_{2})- or the ground terms
*σ(t*and_{1})*σ(t*are list terms with the same length_{2})*n≥0*and, for all*i, 0≤i≤n-1*, such that*l*and_{1i}*l*are the ground terms of rank_{2i}*i*in*σ(t*and_{1})*σ(t*, respectively, either_{2})*l*and_{1i}*l*are both constants in symbol spaces that are data types and they have the same value, or_{2i}`l`∈_{1i}= l_{2i}*Φ*, - or the ground terms
*σ(t*and_{1})*σ(t*are constants in symbol spaces that are data types and they have the same value; or_{2})

*ψ*is a membership formula`o # c`, and there is a ground term*c’*such that*σ*matches the conjunction`And(o#c' c'##c)`to*Φ*, or*ψ*is an external atomic formula and the external definition maps*σ(ψ)*to**t**(or*true*),

*ψ*is`Not(f)`and*σ*does not match the condition formula`f`to*Φ*,*ψ*is`And(f`and either_{1}... f_{n})`n = 0`or*∀ i, 1 ≤ i ≤ n*,*σ*matches`f`to_{i}*Φ*,*ψ*is`Or(f`and_{1}... f_{n})`n`*> 0*and ∃*i, 1 ≤ i ≤ n*, such that*σ*matches`f`to_{i}*Φ*, or*ψ*is`Exists ?v`, and there is a substitution_{1}... ?v_{n}(f)*σ’*that extends*σ*in such a way that*σ’*agrees with*σ*where*σ*is defined, and*Var(*; and`f`) ⊆ Dom(σ’)*σ’*matches`f`to*Φ*. ☐

#### 2.2.2 Condition satisfaction

We define, now, what it means for a

*state of the fact base*to satisfy a condition formula. The satisfaction of condition formulas in a state of the fact base provides formal underpinning to the operational semantics of rule sets interchanged using RIF-PRD.**Definition (State of the fact base).**A,**state of the fact base***w*, is associated to every set of ground atomic formulas,_{Φ}*Φ*, that contains no frame with multiple slots and that satisfies all the following conditions:- for every equality formula
`t`∈_{1}= t_{2}*Φ*, if*t*and_{1}*t*are, both, constants in symbol spaces that are data types, then they have the same value;_{2} - for every equality formula
`t`∈_{1}= t_{2}*Φ*, either*t*is not a constant in a symbol space that is a data type, or_{1}*t*is not a list term;_{2} - for every pair of constants
*c*and_{1}*c*, if_{2}`c`∈_{1}= c_{2}*Φ*, then`c`∈_{2}= c_{1}*Φ*; - for every triple of constants
*c*,_{1}*c*and_{2}*c*, if_{3}`c`∈_{1}= c_{2}*Φ*and`c`∈_{2}= c_{3}*Φ*, then`c`∈_{1}= c_{3}*Φ*; - for all triple of constants
*c*,_{1}, c_{2}*c*, if_{3}`c`∈_{1}##c_{2}*Φ*and`c`∈_{2}##c_{3}*Φ*, then`c`∈_{1}##c_{3}*Φ*.

We say that

*w*is_{Φ}*represented*by*Φ*; or, equivalently, by the conjunction of all the ground atomic formulas in*Φ*. ☐Each ground atomic formula in

*Φ*represents a single**fact**, and, often, the ground atomic formulas, themselves, are called*facts*, as well. Notice that the restriction that*Φ*can contain only single slot frames, in the definition of a state of the fact base is not a limitation: given the definition of a matching substitution, a frame with multiple slots is only syntactic shorthand for the semantically equivalent conjunction of single slot frames.**Definition (Condition satisfaction).**A RIF-PRD condition formula`ψ`isin a state of the fact base,**satisfied***w*, if and only if*w*is represented by a set of ground atomic formulas*Φ*, and there is a ground substitution*σ*that matches*ψ*to*Φ*. ☐Alternative, but equivalent, definitions of a state of the fact base and of the satisfaction of a condition are given in the appendix Model theoretic semantics of RIF-PRD condition formulas: they provide the formal link between the model theory of RIF-PRD condition formulas and the operational semantics of RIF-PRD documents.

## 3 Actions

This section specifies the syntax and semantics of the RIF-PRD

*action*language. The conclusion of a production rule is often called the*action*part, the*then*part, or the*right-hand side*, or*RHS*.The RIF-PRD action language is used to add, delete and modify facts in the fact base. As a rule interchange format, RIF-PRD does not make any assumption regarding the nature of the data sources that the producer or the consumer of a RIF-PRD document uses (e.g. a rule engine’s working memory, an external data base, etc). As a consequence, the syntax of the actions that RIF-PRD supports are defined with respect to the RIF-PRD condition formulas that represent the facts that the actions affect. In the same way, the semantics of the actions is specified in terms of how their execution affects the evaluation of rule conditions.

### 3.1 Abstract syntax

The alphabet of the RIF-PRD action language includes symbols to denote:

- the assertion of a fact represented by a positional atom, a frame, or a membership atomic formula,
- the retraction of a fact represented by a positional atom or a frame,
- the retraction of all the facts about the values of a given slot of a given frame object,
- the addition of a new frame object,
- the removal of a frame object and the retraction of all the facts about it, represented by the corresponding frame and class membership atomic formulas,
- the replacement of all the values of an object’s attribute by a single, new value,
- the execution of an externally defined action, and
- a sequence of these actions, including the declaration of local variables and a mechanism to bind a local variable to a frame slot value or a new frame object.

#### 3.1.1 Actions

The RIF-PRD action language includes constructs for actions that are atomic, from a transactional point of view, and constructs that represent compounds of atomic actions. Action constructs take constructs from the RIF-PRD condition language as their arguments.

**Definition (Atomic action).**Anis a construct that represents an atomic transaction. An atomic action can have several different forms and is defined as follows:**atomic action***Assert simple fact*: If`φ`is a positional atom, a single slot frame or a membership atomic formula in the RIF-PRD condition language, then`Assert(φ)`is an atomic action.`φ`is called the*target*of the action.*Retract simple fact*: If`φ`is a positional atom or a single slot frame in the RIF-PRD condition language, then`Retract(φ)`is an atomic action.`φ`is called the*target*of the action.*Retract all slot values*: If`o`and`s`are terms in the RIF-PRD condition language, then`Retract(o s)`is an atomic action. The pair`(o, s)`is called the*target*of the action.*Retract object*: If`t`is a term in the RIF-PRD condition language, then`Retract(t)`is an atomic action.`t`is called the*target*of the action.*Execute*: if`φ`is a positional atom in the RIF-PRD condition language, then`Execute(φ)`is an atomic action.`φ`is called the*target*of the action. ☐

**Definition (Compound action).**Ais a construct that can be replaced equivalently by a pre-defined, and fixed, sequence of atomic actions. In RIF-PRD, a compound action can have three different forms, defined as follows:**compound action***Assert compound fact*: If`φ`is a frame with multiple slots:`φ`=`o[s`,_{1}->v_{1}...s_{n}->v_{n}]*n > 1*; then`Assert(φ)`is a compound action, defined by the sequence`Assert(o[s`._{1}->v_{1}]) ... Assert(o[s_{n}->v_{n}])`φ`is called the*target*of the action.*Retract compound fact*: If`φ`is a frame with multiple slots:`φ`=`o[s`,_{1}->v_{1}...s_{n}->v_{n}]*n > 1*; then`Retract(φ)`is a compound action, defined by the sequence`Retract(o[s`._{1}->v_{1}]) ... Retract(o[s_{n}->v_{n}])`φ`is called the*target*of the action.*Modify fact*: if`φ`is a frame in the RIF-PRD condition language:`φ`=`o[s`,_{1}->v_{1}...s_{n}->v_{n}]*n > 0*; then`Modify(φ)`is a compound action, defined by the sequence:`Retract(o s`…_{1})`Retract(o s`, followed by_{n})`Assert(φ)`.`φ`is called the*target*of the action. ☐

**Definition (Action).**Ais either an atomic action or a compound action. ☐**action****Definition (Ground action).**An action with target`t`is aif and only if**ground action**`t`is an atomic formula and*Var*(`t`) = ∅;- or
`t = (o, s)`is a pair of terms and*Var*(`o`) =*Var*(`s`) = ∅.

☐

**Example 3.1.**`Assert( ?customer[ex1:voucher->?voucher] )`and`Retract( ?customer[ex1:voucher->?voucher] )`denote two atomic actions with the frame`?customer[ex1:voucher->?voucher]`as their target,`Retract( ?customer ex1:voucher )`denotes an atomic action with the pair of terms`(?customer, ex1:voucher)`as its target,`Modify(?customer[ex1:voucher->?voucher])`denotes a compound action with the frame`?customer[ex1:voucher->?voucher]`as its target.`Modify(?customer[ex1:voucher->?voucher])`can always be equivalently replaced by the sequence:`Retract( ?customer ex1:voucher )`then`Assert( ?customer[ex1:voucher->?voucher] )`;`Retract( ?voucher )`denotes an atomic action whose target is the individual bound to the variable`?voucher`,`Execute( act:print("Hello, world!") )`denotes an atomic action whose target is the externally defined action`act:print`. ☐

#### 3.1.2 Action blocks

The

*action block*is the top level construct to represent the conclusions of RIF-PRD rules. An action block contains a non-empty sequence of actions. It may also include*action variable declarations*.The

*action variable declaration*construct is used to declare variables that are local to the action block, called*action variables*, and to assign them a value within the action block.**Definition (Action variable declaration).**Anis a pair,**action variable declaration***(v p)*made of an*action variable*,*v*, and an*action variable binding*(or, simply,*binding*),*p*, where*p*has one of two forms:*frame object declaration*: if the action variable,*v*, is to be assigned the identifier of a new frame, then the action variable binding is a*frame object declaration*:`New()`. In that case, the notation for the action variable declaration is:`(?o New())`;*frame slot value*: if the action variable,*v*, is to be assigned the value of a slot of a ground frame, then the action variable binding is a frame:*p*=`o[s->`*v*`]`, where`o`is a term that represents the identifier of the ground frame and`s`is a term that represents the name of the slot. The associated notation is:`(?value o[s->?value])`. ☐

**Definition (Action block).**If`(v`,_{1}p_{1}), ..., (v_{n}p_{n})*n ≥ 0*, are action variable declarations, and if`a`,_{1}, ..., a_{m}*m ≥ 1*, are actions, then`Do((v`denotes an_{1}p_{1}) ... (v_{n}p_{n}) a_{1}... a_{m})**action block**. ☐**Example 3.2.**In the following action block, a local variable`?oldValue`is bound to a value of the attribute`value`of the object bound to the variable`?shoppingCart`. The`?oldValue`is then used to compute a new value, and the`Modify`action is used to overwrite the old value with the new value in the fact base:Do( (?oldValue ?shoppingCart[ex1:value->?oldValue]) Modify( ?shoppingCart[ex1:value->func:numeric-multiply(?oldValue 0.90)] ) )

☐

#### 3.1.3 Well-formed action blocks

Not all action blocks are well-formed in RIF-PRD:

- one and only one action variable binding can assign a value to each action variable, and
- the assertion of a membership atomic formula is meaningful only if it is about a frame object that is created in the same action block.

The notion of well-formedness, already defined for condition formulas, is extended to actions, action variable declarations and action blocks.

**Definition (Well-formed action).**An action`α`isif and only if one of the following is true:**well-formed**`α`is an`Assert`and its target is a well-formed atom, a well-formed frame or a well-formed membership atomic formula,`α`is a`Retract`with one single argument and its target is a well-formed term or a well-formed atom or a well-formed frame atomic formula,`α`is a`Retract`with two arguments:`o`and`s`, and both are well-formed terms,`α`is a`Modify`and its target is a well-formed frame, or`α`is an`Execute`and its content is an instance of the coherent set of external schemas (Section Schemas for Externally Defined Terms in RIF data types and builtins [RIF-DTB]) associated with the RIF-PRD language (section Built-in functions, predicates and actions). ☐

**Definition (Well-formed action variable declaration).**An action variable declaration`(?v`*p*`)`isif and only if one of the following is true:**well-formed**- the action variable binding,
*p*, is the declaration of a new frame object:*p*=`New()`, or - the action variable binding,
*p*, is a well formed frame atomic formula,*p*=`o[a`,_{1}->t_{1}...a_{n}->t_{n}]*n ≥ 1*, and the action variable,`v`occurs in the position of a slot value, and nowhere else, that is:`v`∈ {`t`} and_{1}... t_{n}`v`∉*Var(o) ∪ Var(*and ∀`a`) ∪ … ∪ Var(_{1}`a`)_{n}`t`, either_{i}*v*=`t`or_{i}*v*∉*Var(*`t`_{i}*)*. ☐

For the definition of a well-formed action block, the function

*Var(f)*, that has been defined for condition formulas, is extended to actions and frame object declarations as follows:- if
*f*is an action with target*t*and*t*is an atomic formula, then*Var(f) = Var(t)*; - if
*f*is an action with target*t*and*t*is a pair,*(o, s)*of terms, then*Var(f) = Var(o) ∪ Var(s)*; - if
*f*is a frame object declaration,`New()`, then*Var(f) = ∅*.

**Definition (Well-formed action block).**An action block isif and only if all of the following are true:**well-formed**- all the action variable declarations, if any, are well-formed,
- each action variable, if any, is assigned a value by one and only one action variable binding, that is: if
*b*=_{1}`(v`and_{1}p_{1})*b*=_{2}`(v`are two action variable declarations in the action block with different bindings:_{2}p_{2}), then`p`≠_{1}`p`_{2},`v`≠_{1}`v`_{2} - in addition, the action variable declarations, if any, are partially ordered by the ordering defined as follows: if
*b*=_{1}`(v`and_{1}p_{1})*b*=_{2}`(v`are two action variable declarations in the action block, then_{2}p_{2})*b*if and only if_{1}< b_{2}`v`∈_{1}*Var(*`p`_{2}*)*, - all the actions in the action block are well-formed actions, and
- if an action in the action block asserts a membership atomic formula,
`Assert(t`, then the object term in the membership atomic formula,_{1}# t_{2})`t`, is an action variable that is declared in the action block and the action variable binding is a frame object declaration. ☐_{1}

**Definition (RIF-PRD action language).**Theconsists of the set of all the well-formed action blocks. ☐**RIF-PRD action language**### 3.2 Operational semantics of atomic actions

This section specifies the semantics of the atomic actions in a RIF-PRD document.

The effect of the ground atomic actions in the RIF-PRD action language is to modify the state of the fact base, in such a way that it changes the set of conditions that are satisfied before and after each atomic action is performed.

As a consequence, the semantics of the ground atomic actions in the RIF-PRD action language determines a relation, called the

*RIF-PRD transition relation*: →_{RIF-PRD}⊆*W*×*L*×*W*, where*W*denotes the set of all the states of the fact base, and where*L*denotes the set of all the ground atomic actions in the RIF-PRD action language.The semantics of a compound action follows directly from the semantics of the atomic actions that compose it.

Individual states of the fact base are represented by sets of ground atomic formulas (Section Satisfaction of a condition). In the following, the operational semantics of RIF-PRD actions, rules, and rule sets is specified by describing the changes they induce in the fact base.

**Definition (RIF-PRD transition relation).**The semantics of RIF-PRD atomic actions is specified by the**transition relation →**⊆_{RIF-PRD}*W*×*L*×*W*.*(w,*if and only if`α`, w’) ∈ →_{RIF-PRD}*w*∈*W*,*w’*∈*W*,`α`is a ground atomic action, and one of the following is true, where*Φ*is a set of ground atomic formulas that represents*w*and*Φ’*is a set of ground atomic formulas that represent*w’*:`α`is`Assert(φ)`, where φ is a ground atomic formula, and*Φ’ = Φ ∪ {φ}*;`α`is`Retract(φ)`, where φ is a ground atomic formula, and*Φ’ = Φ \ {φ}*;`α`is`Retract(o s)`, where`o`and`s`are constants, and*Φ’ = (Φ \ {*`o[s->v]`| for all the values of`v`});`α`is`Retract(o)`, where`o`is a constant, and*Φ’ = Φ \ {*;`o[s->v]`| for all the values of terms`s`and`v`} – {`o#c`| for all the values of term`c`}`α`is`Execute(φ)`, where φ is a ground atomic builtin action, and*Φ’ = Φ*. ☐

Rule 1 says that all the atomic condition formulas that were satisfied before an assertion will be satisfied after, and that, in addition, the atomic condition formulas that are satisfied by the asserted ground formula will be satisfied after the assertion. No other atomic condition formula will be satisfied after the execution of the action.

Rule 2 says that all the atomic condition formulas that were satisfied before a retraction will be satisfied after, except if they are satisfied only by the retracted fact. No other atomic condition formula will be satisfied after the execution of the action.

Rule 3 says that all the condition formulas that were satisfied before the retraction of all the values of a given slot of a given object will be satisfied after, except if they are satisfied only by one of the frame formulas about the object and the slot that are the target of the action, or a conjunction of such formulas. No other condition formula will be satisfied after the execution of the action.

Rule 4 says that all the condition formulas that were satisfied before the removal of a frame object will be satisfied after, except if they are satisfied only by one of the frame or membership formulas about the removed object or a conjunction of such formulas. No other condition formula will be satisfied after the execution of the action.

Rule 5 says that all the condition formulas that were satisfied before the execution of an action builtin will be satisfied after. No other condition formula will be satisfied after the execution of the action.

**Example 3.3.**Assume an initial state of the fact base that is represented by the following set,*w*, of ground atomic formulas, where_{0}`_c1`,`_v1`and`_s1`denote individuals and where`ex1:Customer`,`ex1:Voucher`and`ex1:ShoppingCart`represent classes:Initial state:

*w*_{0}= {`_c1#ex1:Customer _v1#ex1:Voucher _s1#ex1:ShoppingCart _c1[ex1:voucher->_v1] _c1[ex1:shoppingCart->_s1] _v1[ex1:value->5] _s1[ex1:value->500]`}

`Assert( _c1[ex1:status->"New"] )`denotes an atomic action that adds to the fact base, a fact that is represented by the ground atomic formula:`_c1[ex1:status->"New"]`. After the action is executed, the new state of the fact base is represented by*w*_{1}= {`_c1#ex1:Customer _v1#ex1:Voucher _s1#ex1:ShoppingCart _c1[ex1:voucher->_v1] _c1[ex1:shoppingCart->_s1] _v1[ex1:value->5] _s1[ex1:value->500] _c1[ex1:status->"New"]`}

`Retract( _c1[ex1:voucher->_v1] )`denotes an atomic action that removes from the fact base, the fact that is represented by the ground atomic formula`_c1[ex1:voucher->_v1]`. After the action, the new state of the fact base is represenetd by:*w*_{2}= {`_c1#ex1:Customer _v1#ex1:Voucher _s1#ex1:ShoppingCart _c1[ex1:shoppingCart->_s1] _v1[ex1:value->5] _s1[ex1:value->500] _c1[ex1:status->"New"]`}

`Retract( _v1 )`denotes an atomic action that removes the individual denoted by the constant`_v1`from the fact base. All the class membership and the object-attribute-value facts where`_v1`is the object are removed. After the action, the new state of the fact base is represenetd by:*w*_{3}= {`_c1#ex1:Customer _s1#ex1:ShoppingCart _c1[ex1:shoppingCart->_s1] _s1[ex1:value->500] _c1[ex1:status->"New"]`}

`Retract( _s1 ex1:value )`denotes an atomic action that removes all the object-attribute-value facts that assign a`ex1:value`to the`ex1:ShoppingCart``_s1`. After the action, the new state of the fact base is represented by*w*_{4}= {`_c1#ex1:Customer _s1#ex1:ShoppingCart _c1[ex1:shoppingCart->_s1] _c1[ex1:status->"New"]`}

`Assert( _s1[ex1:value->450] ) adds in the fact base_the single fact that is represented by the ground frame: <tt>_s1[ex1:value->450]`. After the action, the new state of the fact base is represented by:*w*_{5}= {`_c1#ex1:Customer _s1#ex1:ShoppingCart _c1[ex1:shoppingCart->_s1] _s1[ex1:value->450] _c1[ex1:status->"New"]`}

`Execute( act:print(func:concat("New customer: " _c1)) )`denotes an action that does not impact the state of the fact base, but that prints a string to an output stream. After the action, the new state of the fact base is represented by:*w*_{6}= w_{5}= {`_c1#ex1:Customer _s1#ex1:ShoppingCart _c1[ex1:shoppingCart->_s1] _s1[ex1:value->450] _c1[ex1:status->"New"]`}

Notice that steps 4 and 5 can be equivalently replaced by the single compound action:

`Modify( _s1[ex1:value->450] )`, which denotes an action that replaces all the object-attribute-value facts that assign a`ex1:value`to the`ex1:ShoppingCart``_s1`by the single fact that is represented by the ground frame:`_s1[ex1:value->450]`.

☐

## 4 Production rules and rule sets

This section specifies the syntax and semantics of RIF-PRD rules and rule sets.

### 4.1 Abstract syntax

The alphabet of the RIF-PRD rule language includes the alphabets of the RIF-PRD condition language and the RIF-PRD action language and adds symbols for:

- combining a condition and an action block into a rule,
- declaring (some) variables that are free in a rule
*R*, specifying their bindings, and combining them with*R*into a new rule*R’*(with fewer free variables), - grouping rules and associating specific operational semantics to groups of rules.

#### 4.1.1 Rules

**Definition (Rule).**Acan be one of:**rule**- an
*unconditional action block*, - a
*conditional action block*: if`condition`is a formula in the RIF-PRD condition language, and if`action`is a well-formed action block, then`If condition, Then action`is a rule, - a
*rule with variable declaration*: if`?v`,_{1}... ?v_{n}*n ≥ 1*, are variables;`p`,_{1}... p_{m}*m ≥ 1*, are condition formulas (called*patterns*), and`rule`is a rule, then`Forall ?v`is a rule. ☐_{1}...?v_{n}such that (p_{1}...p_{m}) (rule)

**Example 4.1.**The*Gold rule*, from the running example:*A “Silver” customer with a shopping cart worth at least $2,000 is awarded the “Gold” status*, can be represented using the following rule with variable declaration:Forall ?customer such that And( ?customer # ex1:Customer ?customer[ex1:status->"Silver"] ) (Forall ?shoppingCart such that And( ?shoppingCart # ex1:ShoppingCart ?customer[ex1:shoppingCart->?shoppingCart] ) (If Exists ?value (And( ?shoppingCart[ex1:value->?value] pred:numeric-greater-than-or-equal(?value 2000)) Then Do( Modify( ?customer[ex1:status->"Gold"] ) ) )

☐

The function

*Var(f)*, that has been defined for condition formulas and extended to actions, is further extended to rules, as follows:- if
*f*is an action block that declares action variables`?v`,_{1}... ?v_{n}*n ≥ 0*, and that contains actions`a`,_{1}... a_{m}*m ≥ 1*, then*Var(f) = ∪*;_{1 ≤ i ≤ m}Var(`a`) \ {_{i}`?v`}_{1}... ?v_{n} - if
*f*is a conditional action block where`c`is the condition formula and`a`is the action block, then*Var(f) = Var(c) ∪ Var(a)*; - if
*f*is a quantified rule where`?v`,_{1}... ?v_{n}*n > 0*, are the declared variables;`p`,_{1}... p_{m}*m ≥ 0*, are the patterns, and`r`is the rule, then*Var(f) = (Var(*.`r`) ∪ Var(`p`) ∪ … ∪ Var(_{1}`p`)) \ {_{m}`?v`}_{1}... ?v_{n}

#### 4.1.2 Groups

As was already mentioned in the Overview, production rules have an operational semantics that can be described in terms of matching rules against states of the fact base, selecting rule instances to be executed, and executing rule instances’ actions to transition to new states of the fact base.

When production rules are interchanged, the intended rule instance selection strategy, often called the

*conflict resolution strategy*, needs to be interchanged along with the rules. In RIF-PRD, the*group*construct is used to group sets of rules and to associate them with a conflict resolution strategy. Many production rule systems use priorities associated with rules as part of their conflict resolution strategy. In RIF-PRD, the group is also used to carry the priority information that may be associated with the interchanged rules.**Definition (Group).**Aconsists of a, possibly empty, set of rules and groups, associated with a conflict resolution strategy and, a priority. If**group**`strategy`is an IRI that identifies a conflict resolution strategy, if`priority`is an integer, and if each`rg`,_{j}*0 ≤ j ≤ n*, is either a rule or a group, then any of the following represents a group:`Group (rg`,_{0}... rg_{n})*n ≥ 0*;`Group strategy (rg`,_{0}... rg_{n})*n ≥ 0*;`Group priority (rg`,_{0}... rg_{n})*n ≥ 0*;`Group strategy priority (rg`,_{0}... rg_{n})*n ≥ 0*.

If a conflict resolution strategy is not explicitly attached to a group, the strategy defaults to

`rif:forwardChaining`(specified below, in section Conflict resolution). ☐#### 4.1.3 Safeness

The definitions in this section are unchanged from the definitions in the section Safeness in [RIF-Core], except for the definition of

*RIF-PRD rule safeness*, that is extended from the definition of*RIF-Core rule safeness*. The definitions are reproduced for the reader’s convenience.Intuitively, safeness of rules guarantees that all the variables in a rule can be bound, using pattern matching only, before they are used, in a test or in an action.

To define safeness, we need to define, first, the notion of

*binding patterns*for externally defined functions and predicates, as well as under what conditions variables are considered*bound*.**Definition (Binding pattern).**(from [RIF-Core])for externally defined functions and predicates are lists of the form (**Binding patterns**`p`,_{1}`...`,`p`), such that_{n}`p`=_{i}`b`or`p`=_{i}`u`, for`1 ≤ i ≤ n`:`b`stands for a “bound” and`u`stands for an “unbound” argument. ☐Each external function or predicate has an associated list of

. We define here the binding patterns valid for the functions and predicates defined in [RIF-DTB].**valid binding patterns**Every function or predicate

`f`defined in [RIF-DTB] has a valid binding pattern for each of its schemas with only the symbol`b`such that its length is the number of arguments in the schema. In addition,- the external predicate
`pred:iri-string`has the valid binding patterns (`b`,`u`) and (`u`,`b`) and - the external predicate
`pred:list-contains`has the valid binding pattern (`b`,`u`).

The functions and predicates defined in [RIF-DTB] have no other valid binding patterns.

To keep the definitions concise and intuitive, boundedness and safeness are defined, in [RIF-Core], for condition formulas in disjunctive normal form, that can be existentially quantified themselves, but that contain, otherwise, no existential sub-formula. The definitions apply to any valid RIF-Core condition formula, because they can always, in principle, be put in that form, by applying the following syntactic transforms, in sequence:

- if
*f*contains existential sub-formulas, all the quantified variables are renamed, if necessary, and given a name that is unique in*f*, and the scope of the quantifiers is extended to*f*. Assume, for instance, that*f*has an existential sub-formula,*sf =*, such that the names`Exists v`, n ≥ 1_{1}...v_{n}(sf')do not occur in`v`_{1}...v_{n}*f*outside of*sf*. After the transform,*f*becomes, where`Exists v`_{1}...v_{n}(f')*f’*is*f*with*sf*replaced by*sf’*. The transform is applied iteratively to all the existential sub-formulas in*f*; - the (possibly existentially quantified) resulting formula is rewritten in disjunctive normal form ([Mendelson97], p. 30).

In RIF-PRD, the definitions apply to conditions formulas in the same form as in [RIF-Core], with the exception that, in the disjunctive normal form, negated sub-formulas can be atomic formulas or existential formulas: in the latter case, the existentially quantified formula must be, itself, in disjunctive normal form, and contain no further existential sub-formulas. The definitions apply to any valid RIF-PRD condition formula, because they can always, in principle, be put in that form, by applying the above syntactic transform, modified as follows to take negation into account:

- if the condition formula under consideration,
*f*, contains negative sub-formulas, existential formulas that occur inside a negated formula are handled as if they were atomic formulas, with respect to the two processing steps. Extending the scope of an existential quantifier beyond a negation would require its transformation into an universal quantifier, and universal formulas are not part of RIF-PRD condition language; - in addition, the two pre-processing steps are applied, separately, to these existentially quantified formulas, to be able to determine the status of the existentially quantified variables with respect to boundedness.

**Definition (Boundedness).**(from [RIF-Core]) An external term`External(f(t`is_{1},...,t_{n}))in a condition formula, if and only if**bound**`f`has a valid binding pattern (`p`,_{1}`...`,`p`) and, for all_{n}*j, 1 ≤ j ≤ n*, such that`p`=_{j}`b`,`t`is bound in the formula._{j}A variable,

*v*, isin an atomic formula,**bound***a*, if and only if*a*is neither an equality nor an external predicate, and*v*occurs as an argument in*a*;- or
*v*is bound in the conjunctive formula*f =*.`And(a)`

A variable,

*v*, isin a conjunction formula,**bound***f =*, if and only if, either`And(c`, n ≥ 1_{1}...c_{n})*v*is bound in at least one of the conjuncts;- or
*v*occurs as the*j-th*argument in a conjunct,, that is an externally defined predicate, and the`c`_{i}*j-th*position in a binding pattern that is associated withis`c`_{i}`u`, and all the arguments that occur, in, in positions with value`c`_{i}in the same binding pattern are bound in`b`*f’ =*;`And(c`_{1}...c_{i-1}c_{i+1}...c_{n}) - or
*v*occurs in a conjunct,, that is an equality formula, and`c`_{i}*v*occurs as the term on one side of the equality, and the term on the other side of the equality is bound in*f’ =*.`And(c`_{1}...c_{i-1}c_{i+1}...c_{n})

A variable,

*v*, isin a disjunction formula, if and only if**bound***v*is bound in every disjunct where it occurs;A variable,

*v*, isin an existential formula,**bound**`Exists v`,_{1},...,v_{n}(f')*n ≥ 1*, if and only if*v*is bound in. ☐`f'`Notice that the variables,

`v`, that are existentially quantified in an existential formula_{1},...,v_{n}*f =*, are bound in any formula,`Exists v`_{1},...,v_{n}(f')*F*, that contains*f*as a sub-formula, if and only if they are bound in*f*, since they do not exist outside of*f*.**Definition (Variable safeness).**(from [RIF-Core]) A variable,*v*, isin a condition formula,**safe***f*, if and only if*f*is an atomic formula and*f*is not an equality formula in which both terms are variables and*v*occurs in*f*;- or
*f*is a conjunction, ,*f =*, and`And(c`, n ≥ 1_{1}...c_{n})*v*is safe in at least one conjunct in*f*, or*v*occurs in a conjunct,, that is an equality formula in which both terms are variables, and`c`_{i}*v*occurs as the term on one side of the equality, and the variable on the other side of the equality is safe in*f’ =*;`And(c`_{1}...c_{i-1}c_{i+1}...c_{n}) - or
*f*is a disjunction, and*v*is safe in every disjunct; - or
*f*is an existential formula,*f =*, and`Exists v`, n ≥ 1_{1},...,v_{n}(f')*v*is safe in*f’*. ☐

Notice that the two definitions, above, are not extended for negation and, followingly, that an universally quantified (rule) variable is never bound or safe in a condition formula as a consequence of occurring in a negative formula.

The definition of

*rule safeness*is replaced by the following one, that extends the one for RIF-Core rules.**Definition (RIF-PRD rule safeness).**A RIF-PRD rule,*r*, isif and only if**safe***r*is an unconditional action block, and*Var(r) = ∅*;- or
*r*is a conditional action block,`If C Then A`, and all the variables in*Var(A)*are safe in*C*, and all the variables in*Var(r)*are bound in*C*; - or
*r*is a rule with variable declaration,`∀ v`,_{1}...v_{n}such that p_{1}...p_{m}(r')*n ≥ 1, m ≥ 0*, and either*r’*is an unconditional action block,`A`, and the conditional action block`If And(p`is safe;_{1}...p_{m}) Then A- or
*r’*is a conditional action block,`If C Then A`, and the conditional action block`If And(C p`is safe;_{1}...p_{m}) Then A - or
*r’*is a rule with variable declaration,`∀ v'`,_{1}...v'_{n'}such that p'_{1}...p'_{m'}(r")*n’ ≥ 1, m’ ≥ 0*, and the rule with variable declaration`∀ v`, is safe. ☐_{1}...v_{n}v'_{1}...v'_{n'}such that p_{1}...p_{m}p'_{1}...p'_{m'}(r")

**Definition (Group safeness).**(from [RIF-Core]) A group,`Group (s`,_{1}...s_{n})*n ≥ 0*, isif and only if**safe**- it is empty, that is,
*n = 0*; - or
*s*and … and_{1}*s*are safe. ☐_{n}

#### 4.1.4 Well-formed rules and groups

If

*f*is a rule,*Var(f)*is the set of the free variables in*f*.**Definition (Well-formed rule).**A rule,`r`, is aif and only if either**well-formed rule**`r`is an unconditional well-formed action block,`a`,- or
`r`is a conditional action block where the condition formula,`c`, is a well-formed condition formula, and the action block,`a`, is a well-formed action block, - or
`r`is a quantified rule (with or without patterns),`Forall V [such that P](r')`, and- each of the patterns,
`p`∈_{i}`P = {p`,_{1},...,p_{n}}*n ≥ 0*, is a well-formed condition formula, - and the quantified rule,
`r'`, is a well-formed rule. ☐

- each of the patterns,

**Definition (Well-formed group).**A group isif and only if it is safe and it contains only well-formed groups,**well-formed group***g*, and well-formed rules,_{1}…g_{n}, n ≥ 0*r*, such that_{1}…r_{m}, m ≥ 0*Var(r*for all_{i}) = ∅*i, 0 ≤ i ≤ m*. ☐The variables that are universally quantified in a rule are sometimes called

*rule variables*in the remainder of this document, to distinguish them from the action variables and from the existentially quantified variables. The function*CVar*, that maps a rule to the set of its rule variables is defined as follows:- if
*r*is a conditional or unconditional action block,*CVar(r) = ∅* - if
*r*is a rule with variable declaration,`Forall ?v`,_{1}...?v_{n}(r')*CVar(r) = CVar(r’) ∪ {*.`?v`…_{1}`?v`}_{n}

The set of the well-formed groups contains all the production rule sets that can be meaningfully interchanged using RIF-PRD.

### 4.2 Operational semantics of rules and rule sets

#### 4.2.1 Motivation and example

As mentioned in the Overview, the description of a production rule system as a transition system is used to specify the semantics of production rules and rule sets interchanged using RIF-PRD.

The intuition of describing a production rule system as a transition system is that, given a set of production rules

*RS*and a fact base*w*, the rules in_{0}*RS*that are satisfied, in some sense, in*w*determine an action_{0}*a*, whose execution results in a new fact base_{1}*w*; the rules in_{1}*RS*that are satisfied in*w*determine an action_{1}*a*to execute in_{2}*w*, and so on, until the system reaches a final state and stops. The result is the fact base_{1}*w*when the system stops._{n}**Example 4.2.**The Rif Shop, Inc. is a rif-raf retail chain, with brick and mortar shops all over the world and virtual storefronts in many on-line shops. The Rif Shop, Inc. maintains its customer fidelity management policies in the form of production rule sets. The customer management department uses RIF-PRD to publish rule sets to all the shops and licensees so that everyone uses the latest version of the rules, even though several different rule engines are in use (in fact, some of the smallest shops actually run the rules by hand).Here is a small rule set that governs discounts and customer status updates at checkout time (to keep the example short, this is a subset of the rules described in the running example):

(* ex1:CheckoutRuleset *) Group rif:forwardChaining ( (* ex1:GoldRule *) Group 10 ( Forall ?customer such that (And( ?customer # ex1:Customer ?customer[ex1:status->"Silver"] ) ) (Forall ?shoppingCart such that (?customer[ex1:shoppingCart->?shoppingCart]) (If Exists ?value (And( ?shoppingCart[ex1:value->?value] pred:numeric-greater-than-or-equal(?value 2000)) Then Do( Modify( ?customer[ex1:status->"Gold"] ) ) ) ) (* ex1:DiscountRule *) Group ( Forall ?customer such that (And( ?customer # ex1:Customer ) ) (If Or (?customer[ex1:status->"Silver"] ?customer[ex1:status->"Gold"] ) Then Do( (?s ?customer[ex1:shoppingCart->?s]) (?v ?s[ex1:value->?v]) Modify( ?s[ex1:value->func:numeric-multiply(?v 0.95)] ) ) ) )

To see how the rule set works, consider the case of a shop where the checkout processing of customer John is about to start. The initial state of the fact base can be represented as follows:

*w*_{0}= {`_john#ex1:Customer _john[ex1:status->"Silver"] _s1#ex1:ShoppingCart _john[ex1:shoppingCart->_s1] _s1[ex1:value->2000]`}When instantiated against

*w*, the first pattern in the “Gold rule”,_{0}`And( ?customer#ex1:Customer ?customer[ex1:status->"Silver"] )`, yields the single matching substitution:*{(_john/?customer)}*. The second pattern in the same rule also yields a single matching substitution:*{(_john/?customer)(_s1/?shoppingCart)}*, for which the existential condition is satisfied.Likewise, the instantiation of the “Discount rule” yields a single matching substitution that satisfies the condition:

*{(_john/?customer)}*. The conflict set is:

{ex1:GoldRule/{(_john/?customer)(_s1/?shoppingCart)}, ex1:DiscountRule/{(_john/?customer)}}The instance

*ex1:GoldRule/{(_john/?customer)(_s1/?shoppingCart)}*is selected because of its higher priority. The ground compound action:`Modify(_john[ex1:status->"Gold"])`, is executed, resulting in a new state of the fact base, represented as follows:*w*_{1}= {`_john#ex1:Customer _john[ex1:status->"Gold"] _s1#ex1:ShoppingCart _john[ex1:shoppingCart->_s1] _s1[ex1:value->2000]`}In the next cycle, there is no substitution for the rule variable

`?customer`that matches the pattern to the state of the fact base, and the only matching rule instance is: ex1:DiscountRule/{(_john/?customer)}, which is selected for execution. The action variables`?s`and`?v`are bound, based on the state of the fact base, to`_s1`and`200`, respectively, and the ground compound action,`Modify(_s1[ex1:value->1900])`, is executed, resulting in a new state of the fact base:*w*_{2}= {`_john#ex1:Customer _john[ex1:status->"Gold"] _s1#ex1:ShoppingCart _john[ex1:shoppingCart->_s1] _s1[ex1:value->1900]`}In

*w*, the only matching rule instance is, again: ex1:DiscountRule/{(_john/?customer)}. However, that same instance has already been selected and the corresponding action has been executed. Nothing has changed in the state of the fact base that would justify that the rule instance be selected gain. The principle of refraction applies, and the rule instance is removed from consideration._{2}This leaves the conflict set empty, and the system, having detected a final state, stops.

The result of the execution of the system is

*w*. ☐_{2}#### 4.2.2 Rules normalization

A rule,

*R*, whose condition, rewritten in disjunctive normal form as described in section Safeness, consists of more than one disjunct, is equivalent, logically as well as operationally, to a set (or conjunction) of rules that have, all, the same conclusion as*R*, and each rule has one of the disjuncts as its condition: the rule*R: If C*is equivalent to the set of rules_{1}Or … Or C_{n}Then A*{r*._{i=0..n}| r_{i}: If C_{i}Then A}Without loss of generality, and to keep the specification as simple and intuitive as possible, the operational semantics of production rules and rule sets is specified, in the following sections, for rules and rule sets that have been normalized as follows:

- All the rules are rewritten in disjunctive normal form as described in section Safeness;
- Each rule is replaced by a group of rules
- with the same priority as the rule it replaces,
- that contains as many rules as the condition of the original rule in disjunctive normal form contains disjuncts,
- where the condition, in each rule in the group is one of the disjunct in the condition of the original rule,
- and where all the rules in the group have a different condition and the same action part as the original rule.

In the same way, without loss of generality, and to keep the specification as simple and intuitive as possible, the operational semantics of production rules and rule sets is specified, in the following sections, for rules and rule sets where all the compound actions have been replaced by the equivalent sequences of atomic actions.

#### 4.2.3 Definitions and notational conventions

Formally, a production rule system is defined as a labeled terminal transition system (e.g. PLO04), for the purpose of specifying the semantics of a RIF-PRD rule or group of rules.

**Definition (labeled terminal transition system):**A labeled terminal transition system is a structure {**C**,**L**, →,**T**}, where**C**is a set of elements,*c*, called configurations, or states;**L**is a set of elements,*a*, called labels, or actions;- → ⊆ C × L × C is the transition relation, that is: (
*c, a, c’*) ∈ → iff there is a transition labeled*a*from the state*c*to the state*c’*. In the case of a production rule system: in the state*c*of the fact base, the execution of action*a*causes a transition to state the*c’*of the fact base; **T**⊆ C is the set of final states, that is, the set of all the states*c*from which there are no transitions: T = {*c*∈ C | ∀*a*∈ L, ∀*c’*∈ C, (*c, a, c’*) ∉ →}. ☐

For many purposes, a representation of the states of the fact base is an appropriate representation of the states of a production rule system seen as a transition system. However, the most widely used conflict resolution strategies require information about the history of the system, in particular with respect to the rule instances that have been selected for execution in previous states. Therefore, each state of the transition system used to represent a production rule system must keep a memory of the previous states and of the rule instances that where selected and that triggered the transition in those states.

Here, a

*rule instance*is defined as the result of the substitution of constants for all the rule variables in a rule.Let

*R*denote the set of all the rules in the rule language under consideration.**Definition (Rule instance).**Given a rule,*r ∈ R*, and a ground substitution, σ, such that*CVar(r) ⊆ Dom(σ)*, where*CVar(r)*denotes the set of the rule variables in*r*, the result,*ri = σ(r)*, of the substitution of the constant*σ(*for each variable`?x`)`?x`*∈ CVar(r)*is a(or, simply, an**rule instance**) of**instance***r*. ☐Given a rule instance

*ri*, let*rule(ri)*identify the rule from which*ri*is derived by substitution of constants for the rule variables, and let*substitution(ri)*denote the substitution by which*ri*is derived from*rule(ri)*.In the following, two rule instances

*ri*and_{1}*ri*of a same rule_{2}*r*will be considered*different*if and only if*substitution(ri*and_{1})*substitution(ri*substitute a different constant for at least one of the rule variables in_{2})*CVar(r)*.A rule instance,

*ri*, is said to match a state of a fact base,*w*, if its defining substitution,*substitution(ri)*, matches the RIF-PRD condition formula that represents the condition of the instantiated rule,*rule(ri)*, to the set of ground atomic formulas that represents the state of facts*w*.Let

*W*denote the set of all the possible states of a fact base.**Definition (Matching rule instance).**Given a rule instance,*ri*, and a state of the fact base,*w ∈ W*,*ri*is said to**match***w*if and only if one of the following is true:*rule(ri)*is an unconditional action block;*rule(ri)*is a conditional action block:`If condition, Then action`, and*substitution(ri)*matches the condition formula`condition`to the set of ground atomic condition formulas that represents*w*;*rule(ri)*is a rule with variable declaration:`Forall ?v`,_{1}...?v_{n}(p_{1}...p_{m}) (r')*n ≥ 0*,*m ≥ 0*, and*substitution(ri)*matches each of the condition formulas`p`,_{i}*0 ≤ i ≤ m*, to the set of ground atomic condition formulas that represents*w*, and the rule instance*ri’*matches*w*, where*rule(ri’)*=`r'`and*substitution(ri’) = substitution(ri)*. ☐

**Definition (Action instance).**Given a state of the fact base,*w ∈ W*, given a rule instance,*ri*, of a rule in a rule set,*RS*, and given the action block in the action part of the rule*rule(ri)*:`Do((v`,_{1}p_{1})...(v_{n}p_{n}) a_{1}...a_{m})*n ≥ 0*,*m ≥ 1*, where the`(v`,_{i}p_{i})*0 ≤ i ≤ n*, represent the action variable declarations and the`a`,_{j}*1 ≤ j ≤ m*, represent the sequence of atomic actions in the action block; if*ri*matches*w*, the substitution*σ = substitution(ri)*is extended to the action variables*v*, in the following way:_{1}…v_{n}, n ≥ 0- if the binding,
`p`, associated to_{i}*v*, in the action variable declaration, is the declaration of a new frame object:_{i}`(`*v*_{i}`New())`, then*σ(v*, where_{i}) = c_{new}*c*is a constant of type_{new}`rif:IRI`that does not occur in any of the ground atomic formulas in*w*; - if
*v*is assigned the value of a frame’s slot by the action variable declaration:_{i}`(`*v*_{i}`o[s->`*v*_{i}`])`, then*σ(v*is a ground term such that the substitution σ matches the frame formula_{i})`o[s->`*v*_{i}`]`to*w*.

The sequence of ground atomic actions that is the result of substituting a constant for each variable in the atomic actions of the action block of the rule instance,

*ri*, according to the extended substitution, is theassociated to**action instance***ri*. ☐Let

*actions(ri)*denote the action instance that is associated to a rule instance*ri*. By extension, given an ordered set of rule instances,*ori*,*actions(ori)*denotes the sequence of ground atomic actions that is the concatenation, preserving the order in*ori*, of the action instances associated to the rule instances in*ori*.Notice that RIF-PRD does not specify semantics for the case where there is no matching substitution for the binding frame formula

`o[s->v`in an action variable declaration_{i}]`(v`. Indeed, although the rule might be valid from an interchange viewpoint, applying it in a context where object_{i}o[s->v_{i}])`o`has no value for attribute`s`is applying it outside the domain where it is meaningful, and the specification of the context where an otherwise valid RIF-PRD rule is validly applicable is out of the scope of RIF-PRD.The components of the states of a production rule system seen as a transition system can now be defined more precisely. To avoid confusion between the states of the fact base and the states of the transition system, the latter will be called

*production rule system states*.**Definition (Production rule system state).**A(or, simply, a**production rule system state**) is either a**system state**or a**system cycle state**. Every production rule system state,**system transitional state***s*, cycle or transitional, is characterized by- a state of the fact base,
*facts(s)*; - if
*s*is not the current state, an ordered set of rule instances,*picked(s)*, defined as follows:- if
*s*is a system cycle state,*picked(s)*is the ordered set of rule instances picked by the conflict resolution strategy, among the set of all the rule instances that matched*facts(s)*; - if
*s*is a system transitional state,*picked(s)*is the empty set;

- if
- if
*s*is not the initial state, a previous system state,*previous(s)*, defined as follows: given a system cycle state,*s*, and given the sequence of system transitional states,_{c}*s*,_{1},…,s_{n}*n ≥ 0*, such that the execution of the first ground atomic action in*action(picked(s*transitioned the system from_{c}))*s*to_{c}*s*and … and the_{1}*n-th*ground atomic action in*action(picked(s*transitioned the system from_{c}))*s*to_{n-1}*s*, then_{n}*previous(s) = s*if and only if the_{n}*(n+1)-th*ground atomic action in*action(picked(s*transitioned the system from_{c}))*s*to_{n}*s*. ☐

In the following, we will write

*previous(s) = NIL*to denote that a system state*s*is the initial state.**Definition (Conflict set).**Given a rule set,*RS ⊆ R*, and a system state,*s*, thedetermined by**conflict set***RS*in*s*is the set,*conflictSet(RS, s)*of all the different instances of the rules in*RS*that match the state of the fact base,*facts(s) ∈ W*. ☐The rule instances that are in the conflict set are, sometimes, said to be

*fireable*.In each non-final cycle state,

*s*, of a production rule system, a subset,*picked(s)*, of the rule instances in the conflict set is selected and ordered; their action parts are instantiated, and the resulting sequence of ground atomic actions is executed. This is sometimes called:*firing*the selected instances.#### 4.2.4 Operational semantics of a production rule system

All the elements that are required to define a production rule system as a labeled terminal transition system have now been defined.

**Definition (RIF-PRD Production Rule System).**Ais defined as a labeled terminal transition system**RIF-PRD production rule system***PRS = {S, A, →*, where :_{PRS}, T}*S*is a set of system states, called the*system cycle states*;*A*is a set of transition labels, where each transition label is a sequence of ground RIF-PRD atomic actions;- The transition relation →
_{PRS}⊆*S*×*A*×*S*, is defined as follows:

∀ (*s*,*a*,*s’*) ∈*S*×*A*×*S*, (*s*,*a*,*s’*) ∈ →_{PRS}if and only if all of the following hold:*(facts(s), a, facts(s’))*∈ →^{*}_{RIF-PRD}, where →^{*}_{RIF-PRD}denotes the transitive closure of the transition relation →_{RIF-PRD}that is determined by the specification of the semantics of the atomic actions supported by RIF-PRD;*a = actions(picked(s))*;

*T ⊆ S*, a set of final system states. ☐

Intuitively, the first condition in the definition of the transition relation →

_{PRS}states that a production rule system can transition from one system cycle state to another only if the state of facts in the latter system cycle state can be reached from the state of facts in the former by performing a sequence of ground atomic actions supported by RIF-PRD, according to the semantics of the atomic actions.The second condition states that the allowed paths out of any given system cycle state are determined only by how rule instances are

*picked*for execution, from the conflict set, by the conflict resolution strategy.Given a rule set

*RS ⊆ R*, the associated conflict resolution strategy,*LS*, and halting test,*H*, and an initial state of the fact base,*w ∈ W*, the input function to a RIF-PRD production rule system is defined as:

*Eval(RS, LS, H, w) →*, such that_{PRS}s ∈ S*facts(s) = w*and*previous(s) = NIL*.Using*→*to denote the transitive closure of the transition relation^{*}_{PRS}*→*, there are zero, one or more final states of the system,_{PRS}*s’ ∈ T*, such that:

*Eval(RS, LS, H, w) →*The execution of a rule set,^{*}_{PRS}s’.*RS*, in a state,*w*, of a fact base, may result in zero, one or more final state of the fact base,*w’ = facts(s’)*, depending on the conflict resolution strategy and the set of final system states.Therefore, the behavior of a RIF-PRD production rule system also depends on:

- the conflict resolution strategy, that is, how rule instances are precisely selected for execution from the rule instances that match a given state of the fact base, and
- how the set
*T*of final system states is precisely defined.

#### 4.2.5 Conflict resolution

The process of selecting one or more rule instances from the conflict set for firing is often called:

*conflict resolution*.In RIF-PRD the conflict resolution algorithm (or conflict resolution

*strategy*) that is intended for a set of rules is denoted by a keyword or a set of keywords that is attached to the rule set. In this version of the RIF-PRD specification, a single conflict resolution strategy is specified normatively: it is denoted by the keyword`rif:forwardChaining`(a constant of type*rif:IRI*), because it accounts for a common conflict resolution strategy used in most forward-chaining production rule systems. That conflict resolution strategy selects a single rule instance for execution.Future versions of the RIF-PRD specification may specify normatively the intended conflict resolution strategies to be attached to additional keywords. In addition, RIF-PRD documents may include non-standard keywords: it is the responsibility of the producers and consumers of such document to agree on the intended conflict resolution strategies that are denoted by such non-standard keywords. Future or non-standard conflict resolution strategies may select an ordered set of rule instances for execution, instead of a single one: the functions

*picked*and*actions*, in the previous section, have been defined to take this case into account.**Conflict resolution strategy:**`rif:forwardChaining`Most existing production rule systems implement conflict resolution algorithms that are a combination of the following elements (under these or other, idiosyncratic names; and possibly combined with additional, idiosyncratic rules):

*Refraction.*The essential idea of*refraction*is that a given instance of a rule must not be fired more than once as long as the reasons that made it eligible for firing hold. In other terms, if an instance has been fired in a given state of the system, it is no longer eligible for firing as long as it satisfies the states of facts associated to all the subsequent system states (cycle and transitional);*Priority.*The rule instances are ordered by priority of the instantiated rules, and only the rule instances with the highest priority are eligible for firing;*Recency.*the rule instances are ordered by the number of consecutive system states, cycle and transitional, in which they have been in the conflict set, and only the most recently fireable ones are eligible for firing. Note that the recency rule, used alone, results in depth-first processing.

Many existing production rule systems implement also some kind of

*fire the most specific rule first*strategy, in combination with the above. However, whereas they agree on the definition of refraction and the priority or recency ordering, existing production rule systems vary widely on the precise definition of the specificity ordering. As a consequence, rule instance specificity was not included in the basic conflict resolution strategy that RIF-PRD specifies normatively.The RIF-PRD keyword

`rif:forwardChaining`denotes the common conflict resolution strategy that can be summarized as follows: given a conflict set- Refraction is applied to the conflict set, that is, all the refracted rule instances are removed from further consideration;
- The remaining rule instances are ordered by decreasing priority, and only the rule instances with the highest priority are kept for further consideration;
- The remaining rule instances are ordered by decreasing recency, and only the most recent rule instances are kept for further consideration;
- Any remaining tie is broken is some way, and a single rule instance is kept for firing.

As specified earlier,

*picked(s)*denotes the ordered list of the rule instances that were picked in a system state,*s*. Under the conflict resolution strategy denoted by`rif:forwardChaining`, for any given system cycle state,*s*, the list denoted by*picked(s)*contains a single rule instance. By definition, if*s*is a system transitional state,*picked(s)*is the empty set.Given a system state,

*s*, a rule set,*RS*, and a rule instance,*ri ∈ conflictSet(RS, s)*, let*recency(ri, s)*denote the number of system states before*s*, in which*ri*has been continuously a matching instance: if*s*is the current system state,*recency(ri, s)*provides a measure of the recency of the rule instance*ri*.*recency(ri, s)*is specified recursively as follows:- if
*previous(s) = NIL*, then*recency(ri, s) = 1*; - else if
*ri ∈ conflictSet(RS, previous(s))*, then*recency(ri, s) = 1 + recency(ri, previous(s))*; - else,
*recency(ri, s) = 1*.

In the same way, given a rule instance,

*ri*, and a system state,*s*, let*lastPicked(ri, s)*denote the number of system states before*s*, since*ri*has been last fired.*lastPicked(ri, s)*is specified recursively as follows:- if
*previous(s) = NIL*, then*lastPicked(ri, s) = 1*; - else if
*ri ∈ picked(previous(s))*, then*lastPicked(ri, s) = 1*; - else,
*lastPicked(ri, s) = 1 + lastPicked(ri, previous(s))*.

Given a rule instance,

*ri*, let*priority(ri)*denote the priority that is associated to*rule(ri)*, or zero, if no priority is associated to*rule(ri)*. If*rule(ri)*is inside nested`Group`s,*priority(ri)*denotes the priority that is associated with the innermost`Group`to which a priority is explicitly associated, or zero.**Example 4.3.**Consider the following RIF-PRD document:Document ( Prefix( ex2 <http://example.com/2009/prd3#> ) (* ex2:ExampleRuleSet *) Group ( (* ex2:Rule_1 *) Forall ... (* ex2:HighPriorityRules *) Group 10 ( (* ex2:Rule_2 *) Forall ... (* ex2:Rule_3 *) Group 9 (Forall ... ) ) (* ex2:NoPriorityRules *) Group ( (* ex2:Rule_4 *) Forall ... (* ex2:Rule_5 *) Forall ... ) )

No conflict resolution strategy is identified explicitly, so the default strategy

`rif:forwardChaining`is used.Because the

*ex2:ExampleRuleSet*group does not specify a priority, the default priority*0*is used. Rule 1, not being in any other group, inherits its priority,*0*, from the top-level group.Rule 2 inherits its priority,

*10*, from the enclosing group, identified as*ex2:HighPriorityRules*. Rule 3 specifies its own, lower, priority:*9*.Since neither Rule 4 nor Rule 5 specify a priority, they inherit their priority from the enclosing group

*ex2:NoPriorityRules*, which does not specify one either, and, thus, they inherit*0*from the top-level group,*ex2:ExampleRuleSet*. ☐Given a set of rule instances,

*cs*, the conflict resolution strategy`rif:forwardChaining`can now be described with the help of four rules, where*ri*and*ri’*are rule instances:**Refraction rule**: if*ri ∈ cs*and*lastPicked(ri, s) < recency(ri, s)*, then*cs = cs – ri*;**Priority rule**: if*ri ∈ cs*and*ri’ ∈ cs*and*priority(ri) < priority(ri’)*, then*cs = cs – ri*;**Recency rule**: if*ri ∈ cs*and*ri’ ∈ cs*and*recency(ri, s) > recency(ri’, s)*, then*cs = cs – ri*;**Tie-break rule**: if*ri ∈ cs*, then*cs = {ri}*. RIF-PRD does not specify the tie-break rule more precisely: how a single instance is selected from the remaining set is implementation specific.

The

*refraction rule*removes the instances that have been in the conflict set in all the system states at least since they were last fired; the*priority rule*removes the instances such that there is at least one instance with a higher priority; the*recency rule*removes the instances such that there is at least one instance that is more recent; and the*tie-break rule*keeps one rule from the set.To select the singleton rule instance,

*picked(s)*, to be fired in a system state,*s*, given a rule set,*RS*, the conflict resolution strategy denoted by the keyword`rif:forwardChaining`consists of the following sequence of steps:- initialize
*picked(s)*with the conflict set, that a rule set*RS*determines in a system state*s*:*picked(s) = conflictSet(RS, s)*; - apply the
*refraction rule*to all the rule instances in*picked(s)*; - then apply the
*priority rule*to all the remaining instances in*picked(s)*; - then apply the
*recency rule*to all the remaining instances in*picked(s)*; - then apply the
*tie-break rule*to the remaing instance in*picked(s)*; - return
*picked(s)*.

**Example 4.4.**Consider, from example 4.2, the conflict set that the rule set`ex1:CheckoutRuleset`determines in the system state,*s*, that corresponds to the state_{2}*w*of the fact base, and use it to initialize the set of rule instance considered for firing,_{2}= facts(s_{2})*picked(s*:_{2})*conflictSet(*`ex1:CheckoutRuleset`, s_{2}) = { ex1:DiscountRule/{(_john/?customer)} } = picked(s_{2})The single rule instance in the conflict set,

*ri = ex1:DiscountRule/{(_john/?customer)}*, did already belong to the conflict sets in the two previous states,*conflictSet(*and`ex1:CheckoutRuleset`, s_{1})*conflictSet(*; so that its recency in`ex1:CheckoutRuleset`, s_{0})*s*is:_{2}*recency(ri, s*._{2}) = 3On the other hand, that rule instance was fired in system state

*s*:_{1}*picked(s*; so that, in_{1}) = (ex1:DiscountRule/{(_john/?customer)})*s*, it has been last fired one cycle before:_{2}*lastPicked(ri, s*._{2}) = 1Therefore,

*lastPicked(ri, s*, and_{2}) < recency(ri, s_{2})*ri*is removed from*picked(s*by refraction, leaving_{2})*picked(s*empty. ☐_{2})#### 4.2.6 Halting test

By default, a system state is final, given a rule set,

*RS*, and a conflict resolution strategy,*LS*, if there is no rule instance available for firing after application of the conflict resolution strategy.For the conflict resolution strategy identified by the RIF-PRD keyword

`rif:forwardChaining`, a system state,*s*, isgiven a rule set,**final***RS*if and only if the remaining conflict set is empty after application of the*refraction rule*to all the rule instances in*conflictSet(RS, s)*. In particular, all the system states,*s*, such that*conflictSet(RS, s) = ∅*are final.## 5 Document and imports

This section specifies the structure of a RIF-PRD document and its semantics when it includes import directives.

### 5.1 Abstract syntax

In addition to the language of conditions, actions, and rules, RIF-PRD provides a construct to denote the import of a RIF or non-RIF document. Import enables the modular interchange of RIF documents, and the interchange of combinations of multiple RIF and non-RIF documents.

#### 5.1.1 Import directive

**Definition (Import directive).**Anconsists of:**import directive**- an IRI, the
*locator*, that identifies and locates the document to be imported, and - an optional second IRI that identifies the
*profile*of the import. ☐

RIF-PRD gives meaning to one-argument import directives only. Such directives can be used to import other RIF-PRD and RIF-Core documents. Two-argument import directives are provided to enable import of other types of documents, and their semantics is covered by other specifications. For example, the syntax and semantics of the import of RDF and OWL documents, and their combination with a RIF document, is specified in [RIF-RDF-OWL].

#### 5.1.2 RIF-PRD document

**Definition (RIF-PRD document).**A RIF-PRDconsists of zero or more import directives, and zero or one group. ☐**document****Definition (Imported document).**A document is said to beby a RIF document,**directly imported***D*, if and only if it is identified by the locator IRI in an import directive in*D*. A document is said to beby a RIF document,**imported***D*, if it is directly imported by*D*, or if it is imported, directly or not, by a RIF document that is directly imported by*D*. ☐**Definition (Document safeness).**(from [RIF-Core]) A document isif and only if it**safe**- it contains a safe group, or no group at all,
- and all the documents that it imports are safe. ☐

#### 5.1.3 Well-formed documents

**Definition (Conflict resolution strategy associated with a document).**A,**conflict resolution strategy is associated with a RIF-PRD document***D*, if and only if- it is explicitly or implicitly attached to the top-level group in
*D*, or - it is explicitly or implicitly attached to the top-level group in a RIF-PRD document that is imported by
*D*. ☐

**Definition (Well-formed RIF-PRD document).**A RIF-PRD document,*D*, isif and only if it satisfies all the following conditions:**well-formed**- the locator IRI provided by all the import directives in
*D*, if any, identify well-formed RIF-PRD documents, *D*contains a well-formed group or no group at all,*D*has only one associated conflict resolution strategy (that is, all the conflict resolution strategies that can be associated with it are the same), and- every non-
`rif:local`constant that occurs in*D*or in one of the documents imported by*D*, occurs in the same context in*D*and in all the documents imported by*D*. ☐

The last condition in the above definition makes the intent behind the

`rif:local`constants clear: occurrences of such constants in different documents can be interpreted differently even if they have the same name. Therefore, each document can choose the names for the`rif:local`constants freely and without regard to the names of such constants used in the imported documents.### 5.2 Operational semantics of RIF-PRD documents

The semantics of a well-formed RIF-PRD document that contains no import directive is the semantics of the rule set that is represented by the top-level group in the document, evaluated with the conflict resolution strategy that is associated to the document, and the default halting test, as specified above, in section Halting test.

The semantics of a well-formed RIF-PRD document,

*D*, that imports the well-formed RIF-PRD documents*D*, …,_{1}*D*,_{n}*n ≥ 1*, is the semantics of the rule set that is the union of the rule sets represented by the top-level groups in*D*and the imported documents, with the`rif:local`constants renamed to ensure that the same symbol does not occur in two different component rule sets, and evaluated with the conflict resolution strategy that is associated to the document, and the default halting test.## 6 Built-in functions, predicates and actions

In addition to externally specified functions and predicates, and in particular, in addition to the functions and predicates built-ins defined in [RIF-DTB], RIF-PRD supports externally specified actions, and defines action built-ins.

The syntax and semantics of action built-ins are specified like for the other buit-ins, as described in the section Syntax and Semantics of Built-ins in [RIF-DTB]. However, their formal semantics is trivial: action built-ins behave like predicates that are always true, since action built-ins, in RIF-PRD, MUST NOT affect the semantics of the rules.

Although they must not affect the semantics of the rules, action built-ins may have other side effects.

RIF action built-ins are defined in the namespace: http://www.w3.org/2007/rif-builtin-action#. In this document, we will use the prefix:

`act:`to denote the RIF action built-ins namespace.### 6.1 Built-in actions

#### 6.1.1 act:print

*Schema*:`(?arg; act:print(?arg))`*Domains*:The value space of the single argument is`xs:string`.*Mapping:*When`s`belongs to its domain,**I**_{truth}`ο`**I**_{External}( ?arg; act:print(?arg) )(s) =**t**.If an argument value is outside of its domain, the truth value of the function is left unspecified.*Side effects:*The value of the argument MUST be printed to an output stream, to be determined by the user implementation.

## 7 Conformance and interoperability

### 7.1 Semantics-preserving transformations

RIF-PRD conformance is described partially in terms of semantics-preserving transformations.

The intuitive idea is that, for any initial state of facts, the conformant consumer of a conformant RIF-PRD document must reach at least one of the final state of facts intended by the conformant producer of the document, and that it must never reach any final state of facts that was not intended by the producer. That is:

- a conformant RIF-PRD producer,
*P*, must translate any rule set from its own rule language,*L*, into RIF-PRD, in such a way that, for any possible initial state of the fact base, the RIF-PRD translation of the rule set must never produce, according to the semantics specified in this document, a final state of the fact base that would not be a possible result of the execution of the rule set according to the semantics of_{P}*L*(where the state of the facts base are meant to be represented in_{P}*L*or in RIF-PRD as appropriate), and_{P} - a conformant RIF-PRD consumer,
*C*, must translate any rule set from a RIF-PRD document into a rule set in its own language,*L*, in such a way that, for any possible initial state of the fact base, the translation in_{C}*L*of the rule set, must never produce, according to the semantics of_{C}*L*, a final state of the fact base that would not be a possible result of the execution of the rule set according to the semantics specified in this document (where the state of the facts base are meant to be represented in_{C}*L*or in RIF-PRD as appropriate)._{C}

Let Τ be a set of datatypes and symbol spaces that includes the datatypes specified in [RIF-DTB] and the symbol spaces

`rif:iri`and`rif:local`. Suppose also that Ε is a set of external predicates and functions that includes the built-ins listed in [RIF-DTB] and in the section Built-in actions. We say that a rule*r*is a*RIF-PRD*_{Τ,Ε}rule if and only if*r*is a well-formed RIF-PRD rule,- all the datatypes and symbol spaces used in
*r*are in Τ, and - all the externally defined functions and predicates used in
*r*are in Ε.

Suppose, further, that C is a set of conflict resolution strategies that includes the one specified in section Conflict resolution, and that H is a set of halting tests that includes the one specified in section Halting test: we say that a rule set ,

*R*, is a*RIF-PRD*_{Τ,Ε,C,H}rule set if and only if*R*contains only*RIF-PRD*_{Τ,Ε}rules,- the conflict resolution strategy that is associated to
*R*is in C, and - the halting test that is associated to
*R*is in H.

Given a

*RIF-PRD*_{Τ,Ε,C,H}rule set,*R*, an initial state of the fact base,*w*, a conflict resolution strategy*c ∈*C and a halting test*h ∈*H, let*F*denote the set of all the sets,_{R,w,c,h}*f*, of RIF-PRD ground atomic formulas that represent final states of the fact base,*w’*, according to the operational semantics of a RIF-PRD production rule system, that is:*f ∈ F*if and only if there is a state,_{R,w,c,h}*s’*, of the system, such that*Eval(R, c, h, w) →*and^{*}_{PRS}s’*w’ = facts(s’)*and*f*is a representation of*w’*.In addition, given a rule language,

*L*, a rule set expressed in*L*,*R*, a conflict resolution strategy,_{L}*c*, a halting test,*h*, and an initial state of the fact base,*w*, let*F*denote the set of all the formulas in_{L,RL, c, h, w}*L*that represent a final state of the fact base that an*L*processor can possibly reach.**Definition (Semantics preserving mapping).**- A mapping from a
*RIF-PRD*_{Τ,Ε,C,H},*R*, to a rule set,*R*, expressed in a language_{L}*L*, is**semantics-preserving**if and only if, for any initial state of the fact base,*w*, conflict resolution strategy,*c*, and halting test,*h*, it also maps each*L*formula in*F*onto a set of RIF-PRD ground formulas in_{L,RL, c, h, w}*F*;_{R,w,c,h} - A mapping from a rule set,
*R*, expressed in a language_{L}*L*, to a*RIF-PRD*_{Τ,Ε,C,H},*R*, is**semantics-preserving**if an only if, for any initial state of the fact base,*w*, conflict resolution strategy,*c*, and halting test,*h*, it also maps each set of ground RIF-PRD atomic formulas in*F*onto an_{R,w,c,h}*L*formula in*F*. ☐_{L,RL, c, h, w}

### 7.2 Conformance Clauses

**Definition (RIF-PRD conformance).**- A RIF processor is a
**conformant***RIF-PRD*_{Τ,Ε,C,H}iff it implements a**consumer***semantics-preserving mapping*from the set of all*safe**RIF-PRD*_{Τ,Ε,C,H}rule sets to the language*L*of the processor; - A RIF processor is a
**conformant***RIF-PRD*_{Τ,Ε,C,H}iff it implements a**producer***semantics-preserving mapping*from a subset of the language*L*of the processor to a set of*safe**RIF-PRD*_{Τ,Ε,C,H}rule sets; - An
is an XML document that conforms to all the syntactic constraints of RIF-PRD, including ones that cannot be checked by an XML Schema validator;**admissible document** - A
is a conformant RIF-PRD**conformant RIF-PRD consumer**_{Τ,Ε,C,H}consumer in which Τ consists only of the symbol spaces and datatypes, Ε consists only of the externally defined functions and predicates, C consists only of the conflict resolution strategies, and H consists only of halting tests that are required by RIF-PRD. The required symbol spaces are`rif:iri`and`rif:local`, and the datatypes and externally defined terms (built-ins) are the ones specified in [RIF-DTB] and in the section Built-in actions. The required conflict resolution strategy is the one that is identified as`rif:forwardChaining`, as specified in section Conflict resolution; and the required halting test is the one specified in section Halting test. A conformant RIF-PRD consumer must reject any document containing features it does not support. - A
is a conformant RIF-PRD**conformant RIF-PRD producer**_{Τ,Ε,C,H}producer which produces documents that include only the symbol spaces, datatypes, externals, conflict resolution strategies and halting tests that are required by RIF-PRD. ☐

In addition, conformant RIF-PRD producers and consumers SHOULD preserve annotations.

### 7.3 Interoperability

[RIF-Core] is specified as a specialization of RIF-PRD: all valid [RIF-Core] documents are valid RIF-PRD documents and must be accepted by any conformant RIF-PRD consumer.

Conversely, it is desirable that any valid RIF-PRD document that uses only abstract syntax that is defined in [RIF-Core] be a valid [RIF-Core] document as well. For some abstract constructs that are defined in both RIF-Core and RIF-PRD, RIF-PRD defines alternative XML syntax that is not valid RIF-Core XML syntax. For example, an action block that contains no action variable declaration and only assert atomic actions can be expressed in RIF-PRD using the XML elements

`Do`or`And`. Only the latter option is valid RIF-Core XML syntax.To maximize interoperability with RIF-Core and its non-RIF-PRD extensions, a conformant RIF-PRD consumer SHOULD produce valid [RIF-Core] documents whenever possible. Specifically, a conformant RIF-PRD producer SHOULD use only valid [RIF-Core] XML syntax to serialize a rule set that satisfies all of the following:

- the conflict resolution strategy is effectively equivalent to the stratagy that RIF-PRD identifies by the IRI
`rif:forwardChaining`, - no condition formula contains a negation, in any rule in the rule set,
- no rule in the rule set has an action block that contains an action variable declaration, and
- in all the rules in the rule set, the action block contains only assert atomic actions.

When processing a rule set that satisfies all the above conditions, a RIF-PRD producer is guaranteed to produce a valid [RIF-Core] XML document by applying the following rules recursively:

*Remove redundant information*. The`behavior`role element and all its sub-elements should be omitted in the RIF-PRD XML document;*Remove nested rule variable declarations*. If the`rule`inside a rule with variable delcaration,*r*, is also a rule with variable declaration,_{1}*r*, all the rule variable delarations and all the patterns that occur in_{2}*r*should be added to the rule variable declarations and the patterns that occur in_{1}*r*, and, after the transform,_{2}*r*should be replaced by_{1}*r*, in the rule set. If the names of some variables declared in_{2}*r*are the same as the names of some variables declared in_{1}*r*, the former names must be changed prior to the transform.;_{2}*Remove patterns*. If a pattern occurs in a rule with variable declaration,*r*:_{1}- if the
`rule`inside*r*is a unconditional action block,_{1}*r*,_{2}*r*should be transformed into a conditional action block, where the condition is the pattern, and the pattern should be removed from_{2}*r*,_{1} - if the
`rule`inside*r*is a conditional action block,_{1}*r*, the formula that represents the condition in_{2}*r*should be replaced by the conjunction of that formula and the formula that represents the pattern, and the pattern should be removed from_{2}*r*;_{1}

- if the
*Convert action blocks*. The action block, in each rule, should be replaced by a conjunction, and, inside the conjunction, each assert action should be replaced by its target atomic formula.

**Example 7.1.**Consider the following rule,*R*, derived from the*Gold rule*, in the running example, to have only assertions in the action part:R: Forall ?customer such that (And( ?customer # ex1:Customer ?customer[status->"Silver"] ) ) (Forall ?shoppingCart such that (?customer[shoppingCart->?shoppingCart]) (If Exists ?value (And( ?shoppingCart[value->?value] pred:numeric-greater-than-or-equal(?value 2000)) Then Do( Assert(ex1:Foo(?customer)) Assert(ex1:Bar(?shoppingCart)) ) ) )

The serialization of

*R*in the following RIF-Core conformant XML form does not impacts its semantics (see example 8.12 for another valid RIF-PRD XML serialization, that is not RIF-Core conformant):<Forall> <declare><Var>?customer</Var></declare> <declare><Var>?shoppingCart</Var></declare> <formula> <Implies> <if> <And> <formula> <!-- first pattern --> <And> <formula><Member> ... </Member></formula> <formula><Frame> ... </Frame></formula> </And> </formula> <formula> <!-- second pattern --> <Member> ... </Member> </formula> <formula> <!-- original existential condition --> ... </formula> </And> </if> <then> <And> <formula> <!-- serialization of ex1:Foo(?customer) --> ... </formula> <formula> <!-- serialization of ex1:Bar(?shoppingCart) --> ... </formula> </then> </Implies> </formula> </Forall>

## XChange: an ongoing research project

The design, the core language constructs, and the semantics of XChange are completed. The proof-of-concept implementation follows a modular approach that mirrors the operational semantics. Issues of efficiency of the implementation, esp. for event detection and update execution, are subject to future work.

Source: http://reactiveweb.org/xchange/