lojix/logic/src/main/com/thesett/aima/logic/fol/compiler/SymbolKeyTraverser.java
public Iterator<Operator<Term>> traverse(Clause clause, boolean reverse)
    {
        /*log.fine("Traversing clause " + clause.toString(interner, true, false));*/

        Functor head = clause.getHead();
        Functor[] body = clause.getBody();

        Queue<Operator<Term>> queue = (!reverse) ? new StackQueue<Operator<Term>>() : new LinkedList<Operator<Term>>();

        // Create a nested scope in the symbol table for the clause, under its functor name/arity.
        int predicateName;
        int clauseIndex;

        if (clause.isQuery())
        {
            predicateName = -1;
        }
        else
        {
            predicateName = head.getName();
        }

        Integer numberOfClauses = (Integer) rootSymbolTable.get(predicateName, CLAUSE_NO_SYMBOL_FIELD);

        if (numberOfClauses == null)
        {
            numberOfClauses = 0;
        }

        rootSymbolTable.put(predicateName, CLAUSE_NO_SYMBOL_FIELD, numberOfClauses + 1);

        clauseIndex = numberOfClauses;

        SymbolTable<Integer, String, Object> predicateScopedSymbolTable = rootSymbolTable.enterScope(predicateName);
        /*log.fine(indenter.generateTraceIndent(2) + "Enter predicate scope " + predicateName);*/

        clauseScopedSymbolTable = predicateScopedSymbolTable.enterScope(clauseIndex);
        /*log.fine(indenter.generateTraceIndent(2) + "Enter clause scope " + clauseIndex);*/

        // For the head functor, clear the top-level flag, set in head context.
        if (head != null)
        {
            head.setReversable(new ContextOperator(clauseScopedSymbolTable, 0, createHeadOperator(head, clause)));
            head.setTermTraverser(this);
            queue.offer(head);

            /*log.fine("Set SKT as traverser on " + head.toString(interner, true, false));*/
            /*log.fine("Created: " + ("head operator " + 0 + " on " + head.toString(interner, true, false)));*/
        }

        // For the body functors, set the top-level flag, clear in head context.
        if (body != null)
        {
            for (int i = 0; i < body.length; i++)
            {
                Functor bodyFunctor = body[i];

                bodyFunctor.setReversable(new ContextOperator(clauseScopedSymbolTable, i + 1,
                        createBodyOperator(bodyFunctor, i, body, clause)));
                bodyFunctor.setTermTraverser(this);
                queue.offer(bodyFunctor);

                /*log.fine("Set SKT as traverser on " + bodyFunctor.toString(interner, true, false));*/
                /*log.fine("Created: " +
                    ("body operator " + (i + 1) + " on " + bodyFunctor.toString(interner, true, false)));*/
            }
        }

        return queue.iterator();
    }
public Iterator<Operator<Term>> traverse(Clause clause, boolean reverse)
    {
        /*log.fine("Traversing clause " + clause.toString(interner, true, false));*/

        Functor head = clause.getHead();
        Functor[] body = clause.getBody();

        Queue<Operator<Term>> queue = (!reverse) ? new StackQueue<Operator<Term>>() : new LinkedList<Operator<Term>>();

        // Create a nested scope in the symbol table for the clause, under its functor name/arity.
        int predicateName;
        int clauseIndex;

        if (clause.isQuery())
        {
            predicateName = -1;
        }
        else
        {
            predicateName = head.getName();
        }

        Integer numberOfClauses = (Integer) rootSymbolTable.get(predicateName, CLAUSE_NO_SYMBOL_FIELD);

        if (numberOfClauses == null)
        {
            numberOfClauses = 0;
        }

        rootSymbolTable.put(predicateName, CLAUSE_NO_SYMBOL_FIELD, numberOfClauses + 1);

        clauseIndex = numberOfClauses;

        SymbolTable<Integer, String, Object> predicateScopedSymbolTable = rootSymbolTable.enterScope(predicateName);
        /*log.fine(indenter.generateTraceIndent(2) + "Enter predicate scope " + predicateName);*/

        clauseScopedSymbolTable = predicateScopedSymbolTable.enterScope(clauseIndex);
        /*log.fine(indenter.generateTraceIndent(2) + "Enter clause scope " + clauseIndex);*/

        // For the head functor, clear the top-level flag, set in head context.
        if (head != null)
        {
            head.setReversable(new ContextOperator(clauseScopedSymbolTable, 0, createHeadOperator(head, clause)));
            head.setTermTraverser(this);
            queue.offer(head);

            /*log.fine("Set SKT as traverser on " + head.toString(interner, true, false));*/
            /*log.fine("Created: " + ("head operator " + 0 + " on " + head.toString(interner, true, false)));*/
        }

        // For the body functors, set the top-level flag, clear in head context.
        if (body != null)
        {
            for (int i = 0; i < body.length; i++)
            {
                Functor bodyFunctor = body[i];

                bodyFunctor.setReversable(new ContextOperator(clauseScopedSymbolTable, i + 1,
                        createBodyOperator(bodyFunctor, i, body, clause)));
                bodyFunctor.setTermTraverser(this);
                queue.offer(bodyFunctor);

                /*log.fine("Set SKT as traverser on " + bodyFunctor.toString(interner, true, false));*/
                /*log.fine("Created: " +
                    ("body operator " + (i + 1) + " on " + bodyFunctor.toString(interner, true, false)));*/
            }
        }

        return queue.iterator();
    }
{@inheritDoc}