org.deri.iris.utils
Class TermMatchingAndSubstitution

java.lang.Object
  extended by org.deri.iris.utils.TermMatchingAndSubstitution

public class TermMatchingAndSubstitution
extends Object

A collection of utility methods for term/tuple matching and variable substitution.


Constructor Summary
TermMatchingAndSubstitution()
           
 
Method Summary
static List<IVariable> getVariables(ITerm term, boolean unique)
          Extract variables from a term (could be constructed term).
static List<IVariable> getVariables(ITuple subGoalTuple, boolean unique)
          Extract the variables in the same order that they are discovered during term matching.
static boolean match(ITerm bodyTerm, ITerm relationTerm, Map<IVariable,ITerm> variableMap)
          Indicates if bodyTerm (containing >= 0 variables) matches groundTerm and is also consistent with previous matched terms, i.e.
static ITuple matchTuple(ITuple viewCriteria, ITuple relation)
          Match a tuple to view criteria.
static ITuple matchTuple(ITuple viewCriteria, ITuple relation, IEquivalentTerms equivalentTerms)
          Match a tuple to view criteria.
static ITerm substituteVariablesInToTerm(ITerm term, List<ITerm> variableValues, int[] indices)
          Substitute variable values in to a term to ground it using variable bindings as a list of terms with indices instead of a variable-term map.
static ITerm substituteVariablesInToTerm(ITerm term, Map<IVariable,ITerm> variableMap)
          Substitute the variable bindings in to a term to ground it.
static ITuple substituteVariablesInToTuple(ITuple tuple, List<ITerm> variableValues, int[] indices)
          Substitute variable values in to a tuple to ground it using a list of terms with indices instead of a variable-term map.
static ITuple substituteVariablesInToTuple(ITuple tuple, Map<IVariable,ITerm> variableMap)
          Substitute the variable bindings in to a tuple to ground it.
static boolean subsums(IAtom atom1, IAtom atom2)
           
static boolean subsums(ITuple tup1, ITuple tup2)
          Given two tuples, check if one tuple sub-sums the other one.
static boolean unify(ITerm t1, ITerm t2, Map<IVariable,ITerm> variableMap)
          Given two terms, unify to give variable bindings for all variables.
static boolean unify(ITerm t1, ITerm t2, Map<IVariable,ITerm> variableMap, boolean bothDirections)
          Given two terms, unify to give variable bindings for all variables.
static boolean unify(ITerm t1, ITerm t2, Map<IVariable,ITerm> variableMap, boolean bothDirections, IEquivalentTerms equivalentTerms)
          Given two terms, unify to give variable bindings for all variables.
static boolean unify(ITerm t1, ITerm t2, Map<IVariable,ITerm> variableMap, IEquivalentTerms equivalentTerms)
          Given two terms, unify to give variable bindings for all variables.
static boolean unify(ITuple tup1, ITuple tup2, Map<IVariable,ITerm> variableMap)
          Given two tuples, unify to give variable bindings for all variables.
static boolean unify(ITuple tup1, ITuple tup2, Map<IVariable,ITerm> variableMap, boolean bothDirections)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TermMatchingAndSubstitution

public TermMatchingAndSubstitution()
Method Detail

getVariables

public static List<IVariable> getVariables(ITuple subGoalTuple,
                                           boolean unique)
Extract the variables in the same order that they are discovered during term matching.

Parameters:
subGoalTuple - The tuple as it appears in the sub-goal
unique - true, if only unique variables are required
Returns:
The list of variables occurring in subGoalTuple

getVariables

public static List<IVariable> getVariables(ITerm term,
                                           boolean unique)
Extract variables from a term (could be constructed term).

Parameters:
term - The term to extract the variables from.
unique - If false, include each variable every time it appears.
Returns:
The list of variables.

matchTuple

public static ITuple matchTuple(ITuple viewCriteria,
                                ITuple relation)
Match a tuple to view criteria. If a match occurs, return a tuple with values for each distinct variable in the view criteria.

Parameters:
viewCriteria - The tuple from a sub-goal instance.
relation - The tuple from an EDB relation.
Returns:
The tuple of values for the view's variables or null if a match did not occur.

matchTuple

public static ITuple matchTuple(ITuple viewCriteria,
                                ITuple relation,
                                IEquivalentTerms equivalentTerms)
Match a tuple to view criteria. If a match occurs, return a tuple with values for each distinct variable in the view criteria.

Parameters:
viewCriteria - The tuple from a sub-goal instance.
relation - The tuple from an EDB relation.
equivalentTerms - The equivalent terms.
Returns:
The tuple of values for the view's variables or null if a match did not occur.

match

public static boolean match(ITerm bodyTerm,
                            ITerm relationTerm,
                            Map<IVariable,ITerm> variableMap)
Indicates if bodyTerm (containing >= 0 variables) matches groundTerm and is also consistent with previous matched terms, i.e. variables map to the same ground terms.

Parameters:
bodyTerm - The term from the sub-goal predicate
relationTerm - The term from the relation tuple
variableMap - The map of variable to ground term map
Returns:
true if the match succeeds, false otherwise

subsums

public static boolean subsums(ITuple tup1,
                              ITuple tup2)
Given two tuples, check if one tuple sub-sums the other one. Used to check if it is necessary to add a atom to the memo table or if an equivalent atom is already in the memo table. Unification is not sufficient for this task, since e.g. p(?X) and p(1) are unifiable, but are likely to have different answers.

Parameters:
tup1 - tuple one
tup2 - tuple two
Returns:
true if the tuples are equivalent, false if not

subsums

public static boolean subsums(IAtom atom1,
                              IAtom atom2)

unify

public static boolean unify(ITuple tup1,
                            ITuple tup2,
                            Map<IVariable,ITerm> variableMap)
Given two tuples, unify to give variable bindings for all variables.

Parameters:
tup1 - tuple one
tup2 - tuple two
variableMap - Map where variable bindings are saved
Returns:
true if it is unifiable, false if not

unify

public static boolean unify(ITuple tup1,
                            ITuple tup2,
                            Map<IVariable,ITerm> variableMap,
                            boolean bothDirections)
Parameters:
bothDirections - Do the unification in both directions. Actually an additional flag used by the subsums() method.
See Also:
unify( ITuple tup1, ITuple tup2, Map variableMap)

unify

public static boolean unify(ITerm t1,
                            ITerm t2,
                            Map<IVariable,ITerm> variableMap)
Given two terms, unify to give variable bindings for all variables. Either, both or none of the input terms need be grounded.

Parameters:
t1 - The first term.
t2 - The second term.
variableMap - The variable bindings.
Returns:
true, if unifiable, false otherwise

unify

public static boolean unify(ITerm t1,
                            ITerm t2,
                            Map<IVariable,ITerm> variableMap,
                            IEquivalentTerms equivalentTerms)
Given two terms, unify to give variable bindings for all variables. Either, both or none of the input terms need be grounded.

Parameters:
t1 - The first term.
t2 - The second term.
variableMap - The variable bindings.
equivalentTerms - The equivalent terms.
Returns:
true, if unifiable, false otherwise

unify

public static boolean unify(ITerm t1,
                            ITerm t2,
                            Map<IVariable,ITerm> variableMap,
                            boolean bothDirections)
Given two terms, unify to give variable bindings for all variables. Either, both or none of the input terms need be grounded.

Parameters:
t1 - The first term.
t2 - The second term.
variableMap - The variable bindings.
bothDirections - Do the unification in both directions. Actually an additional flag used by the subsums() method.
Returns:
true, if unifiable, false otherwise
See Also:
unify(ITerm t1, ITerm t2, Map variableMap)

unify

public static boolean unify(ITerm t1,
                            ITerm t2,
                            Map<IVariable,ITerm> variableMap,
                            boolean bothDirections,
                            IEquivalentTerms equivalentTerms)
Given two terms, unify to give variable bindings for all variables. Either, both or none of the input terms need be grounded.

Parameters:
t1 - The first term.
t2 - The second term.
variableMap - The variable bindings.
bothDirections - Do the unification in both directions. Actually an additional flag used by the subsums() method.
equivalentTerms - The equivalent terms.
Returns:
true, if unifiable, false otherwise
See Also:
unify(ITerm t1, ITerm t2, Map variableMap)

substituteVariablesInToTuple

public static ITuple substituteVariablesInToTuple(ITuple tuple,
                                                  Map<IVariable,ITerm> variableMap)
Substitute the variable bindings in to a tuple to ground it.

Parameters:
tuple - The tuple containing variables to ground.
variableMap - The variable bindings to use.
Returns:
The grounded tuple.

substituteVariablesInToTerm

public static ITerm substituteVariablesInToTerm(ITerm term,
                                                Map<IVariable,ITerm> variableMap)
Substitute the variable bindings in to a term to ground it.

Parameters:
term - The term to ground.
variableMap - The variable bindings to use.
Returns:
The grounded term.

substituteVariablesInToTuple

public static ITuple substituteVariablesInToTuple(ITuple tuple,
                                                  List<ITerm> variableValues,
                                                  int[] indices)
Substitute variable values in to a tuple to ground it using a list of terms with indices instead of a variable-term map.

Parameters:
tuple - The tuple containing variables to ground.
variableValues - The variable values to be substituted.
indices - The indices in to variableValues for each occurrence of a variable in the tuple IN THE ORDER IN WHICH THEY ARE FOUND. An index value that is less than zero indicates that there is no binding for this variable.
Returns:
The grounded tuple.

substituteVariablesInToTerm

public static ITerm substituteVariablesInToTerm(ITerm term,
                                                List<ITerm> variableValues,
                                                int[] indices)
Substitute variable values in to a term to ground it using variable bindings as a list of terms with indices instead of a variable-term map.

Parameters:
term - The term to ground.
variableValues - A list of variable values
indices - The indices in to variableValues for each occurrence of a variable in the term IN THE ORDER IN WHICH THEY ARE FOUND. An index value that is less than zero indicates that there is no binding for this variable.
Returns:
The grounded term.