org.deri.iris.evaluation.topdown
Class TopDownHelper

java.lang.Object
  extended by org.deri.iris.evaluation.topdown.TopDownHelper

public class TopDownHelper
extends Object

Collection of helper functions for top-down evaluation strategies

Author:
gigi

Constructor Summary
TopDownHelper()
           
 
Method Summary
static ITuple createTupleFromQueryAndVariableMap(IQuery query, Map<IVariable,ITerm> variableMap)
          Deprecated. 
static Map<IVariable,ITerm> createVariableMapFromTupleAndQuery(IQuery query, ITuple branchTuple)
          Given a query and a tuple, create a variable map with the corresponding variable mappings for each variable in the query.
static String getDebugPrefix(int recursionDepth, boolean inNegationAsFailureFlip)
          Creates a debug prefix for nice output
static IRelation getFullSubgoalRelation(IQuery query, IQuery subQuery, Map<IVariable,ITerm> substitution, IRelation relationFromSubtree)
           
static IRelation getFullSubgoalRelation(IQuery query, QueryWithSubstitution qws, IRelation relationFromSubtree)
          Creates a relation by combining the relation from subgoal evaluation and the variable bindings of the current query.
static Map<IVariable,ITerm> getVariableMapForVariableRenaming(IRule rule, IQuery query)
          Creates a variable map that will replace variable ?X with ?X1 if the variable occurs in both tuples.
static List<IVariable> getVariables(IQuery query)
          Get a list of variables, with no duplicates
static List<IVariable> getVariables(ITuple tuple)
          Get a list of variables in the right order, with no duplicates
static boolean match(ILiteral lit1, ILiteral lit2)
          Checks if two literals match
static boolean match(ILiteral queryLiteral, IPredicate factPredicate)
          Wrapper.
static boolean match(IPredicate pred1, IPredicate pred2)
          Checks if two predicates match
static Map<IVariable,ITerm> mergeSubstitutions(Map<IVariable,ITerm> deeperMap, Map<IVariable,ITerm> higherMap)
           
static void printDebug(String msg, int recursionDepth)
           
static void printDebug(String msg, int recursionDepth, boolean inNegationAsFailureFlip)
           
static List<QueryWithSubstitution> processBuiltin(IQuery query, ILiteral selectedQueryLiteral, IAtom queryLiteralAtom)
          Process a builtin atom.
static IRule reMapVariablesInRule(IRule rule, Map<IVariable,ITerm> variableMap)
          Does variable re-mapping.
static IRule replaceVariablesInRule(IRule rule, Map<IVariable,ITerm> variableMap)
          Replaces all variables in a rule with the mapped ones.
static ITuple resolveTuple(ILiteral literal, Map<IVariable,ITerm> variableMap)
           
static ITuple resolveTuple(IQuery query, Map<IVariable,ITerm> variableMap)
          If variableMap contains variable mappings of variables that are contained in query, those mappings will be resolved and a proper tuple will be created and returned.
static ITuple resolveTuple(List<IVariable> varList, Map<IVariable,ITerm> variableMap, boolean partialResult)
           
static IQuery substituteRuleHeadWithBody(IQuery query, ILiteral selectedLiteral, IRule rule)
          Replaces a rule head in a Query with the rule body.
static ILiteral substituteVariablesInToLiteral(ILiteral literal, Map<IVariable,ITerm> variableMap)
          Replaces Variables in a Literal, and gives back the substituted Literal
static IQuery substituteVariablesInToQuery(IQuery query, Map<IVariable,ITerm> variableMap)
          Replaces variables in a query.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TopDownHelper

public TopDownHelper()
Method Detail

substituteRuleHeadWithBody

public static IQuery substituteRuleHeadWithBody(IQuery query,
                                                ILiteral selectedLiteral,
                                                IRule rule)
                                         throws EvaluationException
Replaces a rule head in a Query with the rule body. Example: Query: ?- a(?X), b(?X), c(?X). Rule: b(?X) :- r(?X), s(?X). Return: ?- a(?X), r(?X), s(?X), c(?X). Note that the rule variables and the query variables MUST NOT intersect. If they do an EvaluationException is thrown. To avoid intersection, do an occur check first.

Parameters:
query - a query element that contains the rule head
selectedLiteral - the selected literal
rule - a rule element
Returns:
A new query element where the rule head is replaced with the rule body, if unifyable with the selected literal.
Throws:
EvaluationException

substituteVariablesInToQuery

public static IQuery substituteVariablesInToQuery(IQuery query,
                                                  Map<IVariable,ITerm> variableMap)
Replaces variables in a query. Example: Query: ?- a( ?X ), b( ?Y ), c( ?Z ). variableMap: { X => 3, Y => 9 } Return: ?- a( 3 ), b( 9 ), c( ?X ).

Parameters:
query - a query element
variableMap - a map of variables and terms
Returns:
A new query element where the matching variables are replaced with the terms from the variableMap

substituteVariablesInToLiteral

public static ILiteral substituteVariablesInToLiteral(ILiteral literal,
                                                      Map<IVariable,ITerm> variableMap)
Replaces Variables in a Literal, and gives back the substituted Literal

Parameters:
literal - a Literal element
variableMap - a map of variables and terms
Returns:
A new Literal element where the matching variables are replaced with the terms from the variableMap

match

public static boolean match(IPredicate pred1,
                            IPredicate pred2)
Checks if two predicates match

Parameters:
pred1 - a predicate element
pred2 - a second predicate element
Returns:
true if the predicates have the same name and arity, false otherwise.

match

public static boolean match(ILiteral lit1,
                            ILiteral lit2)
Checks if two literals match

Parameters:
lit1 - a literal element
lit2 - a second literal element
Returns:
true if the predicates of the literals have the same name and arity, false otherwise.

match

public static boolean match(ILiteral queryLiteral,
                            IPredicate factPredicate)
Wrapper. See match(IPredicate pred1, IPredicate pred2).

Parameters:
queryLiteral -
factPredicate -
Returns:
true if the predicates of the literals have the same name and arity, false otherwise.

getVariables

public static List<IVariable> getVariables(IQuery query)
Get a list of variables, with no duplicates

Parameters:
query - the query
Returns:
A unique list of variables

getVariables

public static List<IVariable> getVariables(ITuple tuple)
Get a list of variables in the right order, with no duplicates

Parameters:
tuple - the ITuple tuple
Returns:
A unique list of variables. The list is empty if the tuple contains no variables.

getVariableMapForVariableRenaming

public static Map<IVariable,ITerm> getVariableMapForVariableRenaming(IRule rule,
                                                                     IQuery query)
                                                              throws RuleUnsafeException
Creates a variable map that will replace variable ?X with ?X1 if the variable occurs in both tuples.

Parameters:
rule - a rule
query - a query
Returns:
a variable map that maps the old variable names to the new ones
Throws:
RuleUnsafeException - thrown if the rule contains unbound variables

createVariableMapFromTupleAndQuery

public static Map<IVariable,ITerm> createVariableMapFromTupleAndQuery(IQuery query,
                                                                      ITuple branchTuple)
Given a query and a tuple, create a variable map with the corresponding variable mappings for each variable in the query.

Parameters:
query - the query
branchTuple - the tuple
Returns:
a new variable map containing mappings for all unique variables in query

createTupleFromQueryAndVariableMap

@Deprecated
public static ITuple createTupleFromQueryAndVariableMap(IQuery query,
                                                                   Map<IVariable,ITerm> variableMap)
Deprecated. 

Given a query and a variable map, create a tuple with the corresponding terms for each variable in the query.

Parameters:
query - the query
variableMap - a map containing the variable mappings
Returns:
a new tuple

resolveTuple

public static ITuple resolveTuple(IQuery query,
                                  Map<IVariable,ITerm> variableMap)
If variableMap contains variable mappings of variables that are contained in query, those mappings will be resolved and a proper tuple will be created and returned. e.g. query = ?- q(?X) variableMap = ?X = 1 return = (1)

Parameters:
query - a IQuery which can contain variables
variableMap - map that stores variable mappings
Returns:
a tuple containing the resolved variables of the query

resolveTuple

public static ITuple resolveTuple(ILiteral literal,
                                  Map<IVariable,ITerm> variableMap)

resolveTuple

public static ITuple resolveTuple(List<IVariable> varList,
                                  Map<IVariable,ITerm> variableMap,
                                  boolean partialResult)

mergeSubstitutions

public static Map<IVariable,ITerm> mergeSubstitutions(Map<IVariable,ITerm> deeperMap,
                                                      Map<IVariable,ITerm> higherMap)

replaceVariablesInRule

public static IRule replaceVariablesInRule(IRule rule,
                                           Map<IVariable,ITerm> variableMap)
Replaces all variables in a rule with the mapped ones. Always modifies the head too.

Parameters:
rule - a rule
variableMap - a variable map
Returns:
rule with replaced variables

reMapVariablesInRule

public static IRule reMapVariablesInRule(IRule rule,
                                         Map<IVariable,ITerm> variableMap)
Does variable re-mapping. Inverse function to replaceVariablesInRule(IRule, Map)

Parameters:
rule - a rule
variableMap - a variable map
Returns:
rule with replaced variables.

getDebugPrefix

public static String getDebugPrefix(int recursionDepth,
                                    boolean inNegationAsFailureFlip)
Creates a debug prefix for nice output

Parameters:
recursionDepth - depth of recursion (0 = root)
inNegationAsFailureFlip - true is this a NAF tree, false otherwise
Returns:
debug prefix string

printDebug

public static void printDebug(String msg,
                              int recursionDepth)

printDebug

public static void printDebug(String msg,
                              int recursionDepth,
                              boolean inNegationAsFailureFlip)

getFullSubgoalRelation

public static IRelation getFullSubgoalRelation(IQuery query,
                                               QueryWithSubstitution qws,
                                               IRelation relationFromSubtree)
Creates a relation by combining the relation from subgoal evaluation and the variable bindings of the current query. e.g. ?- p( ?X, ?Y, ?Z ) | ?- q( ?Y, ?Z ) // subgoal When a mapping ?X = 1 is already known, and the subgoal evaluation returned ( 2, 3 ) - which means that ?Y = 2 and ?Z = 3 - a relation ( 1, 2, 3 ) for the variables ( ?X, ?Y, ?Z ) will be created.

Parameters:
query - a query
qws - a query with proper substitution, which is a subgoal of query
relationFromSubtree - relation returned by evaluation the subgoal qws
Returns:
a relation which covers all variables in query

getFullSubgoalRelation

public static IRelation getFullSubgoalRelation(IQuery query,
                                               IQuery subQuery,
                                               Map<IVariable,ITerm> substitution,
                                               IRelation relationFromSubtree)

processBuiltin

public static List<QueryWithSubstitution> processBuiltin(IQuery query,
                                                         ILiteral selectedQueryLiteral,
                                                         IAtom queryLiteralAtom)
                                                  throws EvaluationException
Process a builtin atom.

Parameters:
query - the whole query
selectedQueryLiteral - the selected literal
queryLiteralAtom -
Returns:
List of new queries and the associated substitutions
Throws:
EvaluationException - on failure