/** * Parses a sequence of terms as a comma seperated argument list. The ',' operator in prolog can be used as an * operator, when it behaves as a functor of arity 2, or it can be used to separate a sequence of terms that are * arguments to a functor or list. The sequence of functors must first be parsed as a term, using the operator * precedence of "," to form the term. This method takes such a term and flattens it back into a list of terms, * breaking it only on a sequence of commas. Terms that have been parsed as a bracketed expression will not be * broken up. * * <p/>For example, 'a, b, c' is broken into the list { a, b, c}. The example, 'a, (b, c), d' is broken into the * list { a, (b, c), d} and so on. * * @return A sequence of terms parsed as a term, then flattened back into a list seperated on commas. * * @throws SourceCodeException If the token sequence is not a valid term. */ public Term[] arglist() throws SourceCodeException { Term term = term(); List<Term> result = TermUtils.flattenTerm(term, Term.class, ",", interner); return result.toArray(new Term[result.size()]); }
/** * Parses a single sentence in first order logic. A sentence consists of a term followed by a full stop. * * @return A sentence in first order logic. * * @throws SourceCodeException If the token sequence does not parse into a valid sentence. */ public Clause clause() throws SourceCodeException { // Each new sentence provides a new scope in which to make variables unique. variableContext.clear(); Term term = term(); Clause clause = TermUtils.convertToClause(term, interner); if (clause == null) { throw new SourceCodeException("Only queries and clauses are valid sentences in Prolog, not " + term + ".", null, null, null, term.getSourceCodePosition()); } return clause; }
/** * Unifies two terms and produces a list of bound variables that form the unification, when it it possible. * * @param query The left term to unify. * @param statement The right term to unify. * * @return A list of bound variables to form the unification, or <tt>null</tt> when no unification is possible. */ public List<Variable> unify(Term query, Term statement) { /*log.fine("unify(Term left = " + query + ", Term right = " + statement + "): called");*/ // Find all free variables in the query. Set<Variable> freeVars = TermUtils.findFreeNonAnonymousVariables(query); // Build up all the variable bindings in both sides of the unification in these bindings. List<Variable> queryBindings = new LinkedList<Variable>(); List<Variable> statementBindings = new LinkedList<Variable>(); // Fund the most general unifier, if possible. boolean unified = unifyInternal(query, statement, queryBindings, statementBindings); List<Variable> results = null; // If a unification was found, only retain the free variables in the query in the results returned. if (unified) { queryBindings.retainAll(freeVars); results = new ArrayList<Variable>(queryBindings); } return results; }
Set<Variable> groupVariables = TermUtils.findFreeVariables(clause.getBody()[i]); Set<Variable> headVariables = TermUtils.findFreeVariables(clause.getHead()); firstGroupVariables.addAll(headVariables); Set<Variable> firstArgVariables = TermUtils.findFreeVariables(clause.getBody()[0]); firstGroupVariables.addAll(firstArgVariables);
/** * Parses a sequence of terms as a comma seperated argument list. The ',' operator in prolog can be used as an * operator, when it behaves as a functor of arity 2, or it can be used to separate a sequence of terms that are * arguments to a functor or list. The sequence of functors must first be parsed as a term, using the operator * precedence of "," to form the term. This method takes such a term and flattens it back into a list of terms, * breaking it only on a sequence of commas. Terms that have been parsed as a bracketed expression will not be * broken up. * * <p/>For example, 'a, b, c' is broken into the list { a, b, c}. The example, 'a, (b, c), d' is broken into the * list { a, (b, c), d} and so on. * * @return A sequence of terms parsed as a term, then flattened back into a list seperated on commas. * * @throws SourceCodeException If the token sequence is not a valid term. */ public Term[] arglist() throws SourceCodeException { Term term = term(); List<Term> result = TermUtils.flattenTerm(term, Term.class, ",", interner); return result.toArray(new Term[result.size()]); }
/** * Unifies two terms and produces a list of bound variables that form the unification, when it it possible. * * @param query The left term to unify. * @param statement The right term to unify. * * @return A list of bound variables to form the unification, or <tt>null</tt> when no unification is possible. */ public List<Variable> unify(Term query, Term statement) { /*log.fine("unify(Term left = " + query + ", Term right = " + statement + "): called");*/ // Find all free variables in the query. Set<Variable> freeVars = TermUtils.findFreeNonAnonymousVariables(query); // Build up all the variable bindings in both sides of the unification in these bindings. List<Variable> queryBindings = new LinkedList<Variable>(); List<Variable> statementBindings = new LinkedList<Variable>(); // Fund the most general unifier, if possible. boolean unified = unifyInternal(query, statement, queryBindings, statementBindings); List<Variable> results = null; // If a unification was found, only retain the free variables in the query in the results returned. if (unified) { queryBindings.retainAll(freeVars); results = new ArrayList<Variable>(queryBindings); } return results; }
/** * Parses a single sentence in first order logic. A sentence consists of a term followed by a full stop. * * @return A sentence in first order logic. * * @throws SourceCodeException If the token sequence does not parse into a valid sentence. */ public Clause clause() throws SourceCodeException { // Each new sentence provides a new scope in which to make variables unique. variableContext.clear(); Term term = term(); Clause clause = TermUtils.convertToClause(term, interner); if (clause == null) { throw new SourceCodeException("Only queries and clauses are valid sentences in Prolog, not " + term + ".", null, null, null, term.getSourceCodePosition()); } return clause; }
List<Functor> flattenedArgs = flattenTerm(symbol.getArgument(1), Functor.class, ",", interner); List<Functor> flattenedArgs = flattenTerm(symbol.getArgument(0), Functor.class, ",", interner);
/** {@inheritDoc} */ public Set<Variable> resolve() { // Find all free variables in the query. Set<Variable> freeVars = TermUtils.findFreeNonAnonymousVariables(currentQuery); // Used to collect the results in. Set<Variable> results = null; // Search for the next available solution. SearchNode solution; try { solution = findGoalPath(); } catch (SearchNotExhaustiveException e) { // The search may fail if the maximum number of search steps is reached. This limit is not turned on by // default. If this happens, null is returned, indicating that no solution was found. // Exception ignored as empty search results are noted and dealt with by returning null. e = null; solution = null; results = null; } // Check that a solution was found and return the variable bindings from it if so. Only the variable bindings // that were free and non-anonymous in the original query are returned. if (solution != null) { results = new HashSet<Variable>(freeVars); } return results; }
List<Functor> flattenedArgs = flattenTerm(symbol.getArgument(1), Functor.class, ",", interner); List<Functor> flattenedArgs = flattenTerm(symbol.getArgument(0), Functor.class, ",", interner);
/** {@inheritDoc} */ public Iterator<Set<Variable>> iterator() { // Find all free variables in the query. final Set<Variable> freeVars = TermUtils.findFreeNonAnonymousVariables(currentQuery); Function<ResolutionState, Set<Variable>> listFunction = new Function<ResolutionState, Set<Variable>>() { public Set<Variable> apply(ResolutionState state) { return freeVars; } }; return new Filterator<ResolutionState, Set<Variable>>(Searches.allSolutions(this), listFunction); }
/** {@inheritDoc} */ public boolean proofStep(ResolutionState state) { // Flatten the head goal on the disjunction operator to produce a series of choice points. Functor goalTerm = state.getGoalStack().poll().getFunctor(); disjuncts = TermUtils.flattenTerm(goalTerm, Functor.class, state.getInterner().internFunctorName(";", 2)); return true; }
Set<Variable> freeVars = TermUtils.findFreeNonAnonymousVariables(clause); Set<Integer> freeVarNames = new TreeSet<Integer>();
Set<Variable> freeVars = TermUtils.findFreeNonAnonymousVariables(clause); Collection<Integer> freeVarNames = new TreeSet<Integer>();