/** * Examines all top-level functors within a clause, including any head and body, and determines which functor has * the highest number of arguments. * * @param clause The clause to determine the highest number of arguments within. * * @return The highest number of arguments within any top-level functor in the clause. */ private int findMaxArgumentsInClause(Clause clause) { int result = 0; Functor head = clause.getHead(); if (head != null) { result = head.getArity(); } Functor[] body = clause.getBody(); if (body != null) { for (int i = 0; i < body.length; i++) { int arity = body[i].getArity(); result = (arity > result) ? arity : result; } } return result; }
/** {@inheritDoc} */ public Iterator<Operator<Term>> traverse(Predicate predicate, boolean reverse) { /*log.fine("Traversing predicate " + predicate.toString());*/ Clause[] body = predicate.getBody(); Queue<Operator<Term>> queue = (!reverse) ? new StackQueue<Operator<Term>>() : new LinkedList<Operator<Term>>(); // For the predicate bodies. if (body != null) { for (int i = leftToRightPredicateBodies ? 0 : (body.length - 1); leftToRightPredicateBodies ? (i < body.length) : (i >= 0); i = i + (leftToRightPredicateBodies ? 1 : -1)) { Clause bodyClause = body[i]; bodyClause.setReversable(createClauseOperator(bodyClause, i, body, predicate)); bodyClause.setTermTraverser(this); queue.offer(bodyClause); } } return queue.iterator(); }
/** {@inheritDoc} */ public String toString(VariableAndFunctorInterner interner, boolean printVarName, boolean printBindings) { String result = ""; if (body != null) { for (int i = 0; i < body.length; i++) { result += body[i].toString(interner, printVarName, printBindings) + ((i < (body.length - 1)) ? "\n" : ""); } } return result; }
/** {@inheritDoc} */ public String toString(VariableAndFunctorInterner interner, boolean printVarName, boolean printBindings) { String result = ""; if (head != null) { result += head.toString(interner, printVarName, printBindings); } if (body != null) { result += isQuery() ? "?- " : " :- "; for (int i = 0; i < body.length; i++) { result += body[i].toString(interner, printVarName, printBindings) + ((i < (body.length - 1)) ? ", " : ""); } } return result; } }
result = new WAMCompiledPredicate(clause.getHead().getName());
/** {@inheritDoc} */ public boolean proofStep(ResolutionState state) { Clause query = state.getCurrentClause(); // The query goals are placed onto the goal stack backwards so that their insertion order is reversed for an // intuitive left-to-right evaluation order. for (int i = query.getBody().length - 1; i >= 0; i--) { BuiltInFunctor newGoal = state.getBuiltInTransform().apply(query.getBody()[i]); state.getGoalStack().offer(newGoal); } return true; } }
/** {@inheritDoc} */ public void visit(Clause clause) { if (traverser.isEnteringContext()) { enterClause(clause); } else if (traverser.isLeavingContext()) { leaveClause(clause); clause.setTermTraverser(null); } }
/** * Evaluates a query against the resolver or adds a clause to the resolvers domain. * * @param sentence The clausal sentence to run as a query or as a clause to add to the domain. * * @throws SourceCodeException If the query or domain clause fails to compile or link into the resolver. */ private void evaluate(Sentence<Clause> sentence) throws SourceCodeException { Clause clause = sentence.getT(); if (clause.isQuery()) { engine.endScope(); engine.compile(sentence); evaluateQuery(); } else { // Check if the program clause is new, or a continuation of the current predicate. int name = clause.getHead().getName(); if ((currentPredicateName == null) || (currentPredicateName != name)) { engine.endScope(); currentPredicateName = name; } addProgramClause(sentence); } }
/** {@inheritDoc} */ public String toString(VariableAndFunctorInterner interner, boolean printVarName, boolean printBindings) { String result = ""; if (head != null) { result += head.toString(interner, printVarName, printBindings); } if (body != null) { result += isQuery() ? "?- " : " :- "; for (int i = 0; i < body.length; i++) { result += body[i].toString(interner, printVarName, printBindings) + ((i < (body.length - 1)) ? ", " : ""); } } return result; } }
Term firstClauseArg = clause.getHead().getArgument(index);
parentClause.getBody()[pos] = builtInFunctor;
/** {@inheritDoc} */ public void visit(Clause clause) { if (traverser.isEnteringContext()) { enterClause(clause); } else if (traverser.isLeavingContext()) { leaveClause(clause); clause.setTermTraverser(null); } }
if ((clause.getBody() != null)) for (int i = clause.getBody().length - 1; i >= 1; i--) Set<Variable> groupVariables = TermUtils.findFreeVariables(clause.getBody()[i]); if (clause.getHead() != null) Set<Variable> headVariables = TermUtils.findFreeVariables(clause.getHead()); firstGroupVariables.addAll(headVariables); if ((clause.getBody() != null) && (clause.getBody().length > 0)) Set<Variable> firstArgVariables = TermUtils.findFreeVariables(clause.getBody()[0]); firstGroupVariables.addAll(firstArgVariables); int[] permVarsRemainingCount = new int[(clause.getBody() != null) ? clause.getBody().length : 0]; symbolTable.put(clause.getBody()[i].getSymbolKey(), SymbolTableKeys.SYMKEY_PERM_VARS_REMAINING, permVarsRemaining); permVarsRemaining += permVarsRemainingCount[i];
/** {@inheritDoc} */ public void compile(Sentence<Clause> sentence) throws SourceCodeException { Clause clause = sentence.getT(); substituteBuiltIns(clause); initialiseSymbolTable(clause); topLevelCheck(clause); if (observer != null) { if (clause.isQuery()) { observer.onQueryCompilation(sentence); } else { observer.onCompilation(sentence); } } }