Previous topic  Top  Next topic  Print this Topic
 

Rule Unfolding

 

Rule unfolding means substitution of rule heads by rule bodies in other rules. Lets look for a simple example. Given the following rule and query:

p(?X) :- q(?X,?Y) AND r(?Y).

?- p(?Z) AND t(?Z).

Then unfolding substitutes the literal p(Z) in the query by the body of the rule and deletes the rule:

?- q(?Z,?Y) AND r(?Y) AND t(?Z).

This unfolding has some advantages:

If q and r and t are extensional predicates (there are no rules with q,r,t in the head) then the cost-based dynamic reordering of the rule literals provides better results. As q,r,t are extensional predicates, their extensions are directly known (they are not derived) and thus statistics on these extensions help to reorder the rule body literals in an optimal way.
Indices must not be created dynamically during query evaluation for intensional predicates but are created once and are retained for all extensional predicates (they are not derived by rules) for future query evaluations.
If the storage model is persistent, i.e. the H2 relational database of the SQL rewriter creates SQL expressions out of the body literals. This works for extensional predicates only.
For integration scenarios with external databases rules are also directly rewritten into sql-statements which access the external databases. Because the performance is strongly improved if a lot of external database accesses are combined to a few sql-statements, the use of the StrongUnfolder is very often a very effective option.

In contrast to a weaker version of unfolding, the StrongUnfolder also unfolds body literals for which several rules exist. This is shown in the following example

?- q(?X) AND r(?X).

q(?X) :- t(?X).

q(?X) :- s(?X).

This set of rules is unfolded to the following set of queries:

?- s(?X) AND r(?X).

?- t(?X) AND r(?X).

Unfortunately the interplay of different rules together with the StrongUnfolder sometimes leads to an explosion of rules. In these cases generation of the resulting rules and the evaluation of these rules may take a lot of time. It is hardly predictable when this effect occurs. Therefore the StrongUnfolder is switched off by default, but it is strongly recommended to try it especially in integration scenarios.

Simple Unfolder

Given the following rule and query:

p(?X) :- q(?X,?Y) AND r(?Y).

?- p(?Z) AND t(?Z).

Then unfolding substitutes the literal p(Z) in the query by the body of the rule and deletes the rule:

?- q(?Z,?Y) AND r(?Y) AND t(?Z).

The switch

Rewriter.SimpleUnfolder = on

is per default set to "on". In most cases this option should never be set to "off".