org.deri.iris.rules
Class RuleHeadEqualityRewriter

java.lang.Object
  extended by org.deri.iris.rules.RuleHeadEqualityRewriter
All Implemented Interfaces:
IRuleHeadEqualityPreProcessor

public class RuleHeadEqualityRewriter
extends Object
implements IRuleHeadEqualityPreProcessor

An utility class that creates new rules to realize support for rule head equality. This class replaces all occurrences of rule head equality with a special predicate. This class creates 2 unsafe rules (see rule 1 and rule 2), therefore any reasoner, that uses rules created by this class must support unsafe rules. Here, rule head equality is denoted by the predicate equivalent.

New rules are created to define rule head equality. Note that rule 1 and 2 are unsafe rules, since the property "each variable in the rule head appears in a non-negated, relational subgoal" is violated.

  1. equivalent(?X, ?X).
  2. equivalent(?X, ?Y) :- ?X = ?Y.
  3. equivalent(?Y, ?X) :- equivalent(?X, ?Y).
  4. equivalent(?X, ?Z) :- equivalent(?X, ?Y), equivalent(?Y, ?Z).

For each predicate occurring in a given set of facts (relations) or in any rule of a given collection of rules, n additional rules are created where n is the arity of the predicate. Assume a predicate hasName(?X, ?Y, ?Z) with arity 3. For this predicate the following three rules are created:

  1. hasName(?U, ?Y, ?Z) :- hasName(?X, ?Y, ?Z), equivalent(?X, ?U).
  2. hasName(?X, ?U, ?Z) :- hasName(?X, ?Y, ?Z), equivalent(?Y, ?U).
  3. hasName(?X, ?Y, ?U) :- hasName(?X, ?Y, ?Z), equivalent(?Z, ?U).

Author:
Adrian Marte

Field Summary
static IPredicate PREDICATE
          The predicate replacing the rule head equality predicate.
 
Constructor Summary
RuleHeadEqualityRewriter()
          Default constructor.
RuleHeadEqualityRewriter(boolean checkForOccurrence, boolean useUnsafeRules)
          Default constructor.
 
Method Summary
protected  Collection<IRule> createEqualityRules()
           Creates the necessary equality rules, that is:
protected static ILiteral createLiteral(boolean isPositive, ITerm x, ITerm y)
          Creates a literal representing rule head equality, e.g.
protected static ILiteral createLiteral(boolean isPositive, ITuple tuple)
          Creates a literal representing rule head equality, e.g.
protected static ILiteral createLiteral(ITerm x, ITerm y)
          Creates a positive literal representing rule head equality, e.g.
protected static ILiteral createLiteral(ITuple tuple)
          Creates a positive literal representing rule head equality, e.g.
protected  Set<IPredicate> extractPredicatesFromRules(Collection<IRule> rules)
          Extracts all predicates occurring in the specified collection of rules.
 List<IRule> process(List<IRule> rules, IFacts facts)
          Pre-process the given rules and facts.
static List<IRule> replaceHead(List<IRule> rules)
           
 Collection<IRule> rewrite(Collection<IRule> rules)
          Creates new rules for the specified collection of rules, depending on the value of checkForOccurence.
 Collection<IRule> rewrite(Collection<IRule> rules, IFacts facts)
          Creates new rules for the specified collection of rules and facts, depending on the value of checkForOccurence.
 Collection<IRule> rewrite(IPredicate predicate)
          Creates new rules for the specified predicate.
 Collection<IRule> rewrite(IRule rule)
          Creates new rules for the specified rule.
protected  Collection<IRule> rewritePredicates(Collection<IPredicate> predicates)
          Creates new rules for the specified predicates.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PREDICATE

public static final IPredicate PREDICATE
The predicate replacing the rule head equality predicate. The predicate symbol is $EQUIVALENT$.

Constructor Detail

RuleHeadEqualityRewriter

public RuleHeadEqualityRewriter()
Default constructor. Per default, the rewriter checks for occurrences of rules with rule head equality and creates unsafe rules.


RuleHeadEqualityRewriter

public RuleHeadEqualityRewriter(boolean checkForOccurrence,
                                boolean useUnsafeRules)
Default constructor. Depending on the value of checkForOccurence, the rewriter checks for occurrences of rules with rule head equality and rewrites given rules only if rules with rule head equality are present.

Parameters:
checkForOccurrence - If set to true this rewriter creates new rules for given rules if and only if given rules contain rules with rule head equality. If set to false this rewriter will create new rules no matter what.
useUnsafeRules - If set to true this rewriter creates unsafe rules. If set to false this rewriter will only create safe rules, and therefore may be incomplete.
Method Detail

createLiteral

protected static ILiteral createLiteral(ITerm x,
                                        ITerm y)
Creates a positive literal representing rule head equality, e.g. ?X = ?Y where ?X is x and ?Y is y.

Parameters:
x - The first term.
y - The second term.
Returns:
A positive literal representing rule head equality.

createLiteral

protected static ILiteral createLiteral(boolean isPositive,
                                        ITerm x,
                                        ITerm y)
Creates a literal representing rule head equality, e.g. ?X = ?Y where ?X is x and ?Y is y.

Parameters:
isPositive - true if the literal is positive, false otherwise.
x - The first term.
y - The second term.
Returns:
A positive literal representing rule head equality.

createLiteral

protected static ILiteral createLiteral(ITuple tuple)
                                 throws IllegalArgumentException
Creates a positive literal representing rule head equality, e.g. ?X = ?Y where ?X is the first and ?Y is second element of the tuple.

Parameters:
tuple - The tuple.
Returns:
A positive literal representing rule head equality.
Throws:
IllegalArgumentException - If the size of the tuple is not 2.

createLiteral

protected static ILiteral createLiteral(boolean isPositive,
                                        ITuple tuple)
                                 throws IllegalArgumentException
Creates a literal representing rule head equality, e.g. ?X = ?Y where ?X is the first and ?Y is second element of the tuple.

Parameters:
isPositive - true if the literal is positive, false otherwise.
tuple - The tuple.
Returns:
A positive literal representing rule head equality.
Throws:
IllegalArgumentException - If the size of the tuple is not 2.

rewrite

public Collection<IRule> rewrite(Collection<IRule> rules)
Creates new rules for the specified collection of rules, depending on the value of checkForOccurence. Note that the specified collection of rules is not added to the resulting collection.

Parameters:
rules - The rules for which new rules should be created.
Returns:
The new rules without the specified collection of rules, or an empty set if no new rules have been created.

rewrite

public Collection<IRule> rewrite(Collection<IRule> rules,
                                 IFacts facts)
Creates new rules for the specified collection of rules and facts, depending on the value of checkForOccurence. Note that the specified collection of rules is not added to the resulting collection.

Parameters:
rules - The rules for which new rules should be created.
facts - The facts for which new rules should be created.
Returns:
The new rules without the specified collection of rules, or an empty collection if no new rules have been created.

rewrite

public Collection<IRule> rewrite(IPredicate predicate)
Creates new rules for the specified predicate.

Parameters:
predicate - The predicate.
Returns:
A collection of new rules for the specified predicate.

rewritePredicates

protected Collection<IRule> rewritePredicates(Collection<IPredicate> predicates)
Creates new rules for the specified predicates.

Parameters:
predicates - The collection of predicates.
Returns:
A collection of new rules for the specified collection of predicates.

rewrite

public Collection<IRule> rewrite(IRule rule)
Creates new rules for the specified rule. Note that the specified rule is not added to the resulting collection of new rules.

Parameters:
rule - The rule.
Returns:
A collection of new rules for the specified rule.

extractPredicatesFromRules

protected Set<IPredicate> extractPredicatesFromRules(Collection<IRule> rules)
Extracts all predicates occurring in the specified collection of rules.

Parameters:
rules - The collection of rules.
Returns:
A set of all predicates occurring in the specified collection of rules.

createEqualityRules

protected Collection<IRule> createEqualityRules()

Creates the necessary equality rules, that is:

  1. equivalent(?X, ?X).
  2. equivalent(?X, ?Y) :- ?X = ?Y.
  3. equivalent(?Y, ?X) :- equivalent(?X, ?Y).
  4. equivalent(?X, ?Z) :- equivalent(?X, ?Y), equivalent(?Y, ?Z).

Returns:
A collection of new rules.

process

public List<IRule> process(List<IRule> rules,
                           IFacts facts)
Description copied from interface: IRuleHeadEqualityPreProcessor
Pre-process the given rules and facts.

Specified by:
process in interface IRuleHeadEqualityPreProcessor
Parameters:
rules - The rule to pre-process.
facts - The facts to pre-process.
Returns:
The resulting rules after pre-processing.

replaceHead

public static List<IRule> replaceHead(List<IRule> rules)