Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright The Sett Ltd, 2005 to 2014.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.thesett.aima.logic.fol.wam.printer;
 
 import java.util.List;
 
WAMCompiledTermsPrintingVisitor assists with pretty printing queries and predicates compiled by the com.thesett.aima.logic.fol.wam.compiler.InstructionCompiler.

This class is abstract because it implements a default behaviour for all terms, which is to apply a stack of column printers to them. Depending on the entry point of this pretty printer, the top-level term being printed should have its visit method overridden to set up the stack of printers on entry, and to finalize the output on exit. The initializePrinters() and printTable() methods are provided to do this.

CRC Card
Responsibilities Collaborations
Pretty print compiled terms internal information about the compilation.

Author(s):
Rupert Smith
 
 {
    
The positional traverser used to traverse the clause being printed.
 
     protected PositionalTermTraverser traverser;

    
The name interner.
 
     private VariableAndFunctorInterner interner;

    
The symbol table.
 
     private SymbolTable<IntegerStringObjectsymbolTable;

    
Holds the string buffer to pretty print the results into.
 
     private StringBuffer result;

    
Holds a list of all column printers to apply.
 
     List<AllTermsVisitorprinters = new ArrayList<AllTermsVisitor>();

    
Holds the table model to render the output to.
 
     TextTableModel printTable = new TextTableImpl();

    
Creates a pretty printing visitor for clauses being compiled in WAM.

Parameters:
interner The symbol name table.
symbolTable The symbol table for the compilation.
result A string buffer to place the results in.
 
         SymbolTable<IntegerStringObjectsymbolTableStringBuffer result)
     {
         super(null);
         this. = interner;
         this. = result;
         this. = symbolTable;
 
     }

    
 
     public void setPositionalTraverser(PositionalTermTraverser traverser)
     {
         this. = traverser;
     }

    
 
     public void visit(Predicate predicate)
     {
         for (AllTermsVisitor printer : )
         {
             printer.visit(predicate);
        }
        super.visit(predicate);
    }

    
    public void visit(Clause clause)
    {
        for (AllTermsVisitor printer : )
        {
            printer.visit(clause);
        }
        super.visit(clause);
    }

    
    public void visit(Functor functor)
    {
        for (AllTermsVisitor printer : )
        {
            printer.visit(functor);
        }
        super.visit(functor);
    }

    
    public void visit(Variable variable)
    {
        for (AllTermsVisitor printer : )
        {
            printer.visit(variable);
        }
        super.visit(variable);
    }

    
Sets up the stack of column printers.
    protected void initializePrinters()
    {
        int maxColumns = 0;
        .add(new SourceClausePrinter(maxColumns++, ));
        .add(new PositionPrinter(maxColumns++, ));
        .add(new UnoptimizedLabelPrinter(maxColumns++, ));
        .add(new LabelPrinter(maxColumns++, ));
        .add(new ByteCodePrinter(maxColumns++, ));
    }

    
Assembles the accumulated output in all rows and columns into a table. The table is appended onto result
    protected void printTable()
    {
        for (int i = 0; i < .getRowCount(); i++)
        {
            for (int j = 0; j < .getColumnCount(); j++)
            {
                String valueToPrint = .get(ji);
                valueToPrint = (valueToPrint == null) ? "" : valueToPrint;
                .append(valueToPrint);
                Integer maxColumnSize = .getMaxColumnSize(j);
                int padding = ((maxColumnSize == null) ? 0 : maxColumnSize) - valueToPrint.length();
                padding = (padding < 0) ? 0 : padding;
                for (int s = 0; s < paddings++)
                {
                    .append(" ");
                }
                .append(" % ");
            }
            .append("\n");
        }
    }
New to GrepCode? Check out our FAQ X