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.compiler;
 
PreCompiler transforms clauses for compilation, substituting built-ins for any built-ins in the source expressions to compile.

CRC Card
Responsibilities Collaborations
Perform the built-ins transformation.

Author(s):
Rupert Smith
 
 public class PreCompiler extends BaseMachine implements LogicCompiler<ClauseClauseClause>
 {
    
Used for debugging.
 
     private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(PreCompiler.class.getName());

    
Holds the compiler output observer.
 
     private LogicCompilerObserver<ClauseClauseobserver;

    
Holds the default built in, for standard compilation and interners and symbol tables.
 
     private final DefaultBuiltIn defaultBuiltIn;

    
Holds the built in transformation.
 
     private BuiltInTransform builtInTransform;

    
Creates a new PreCompiler.

Parameters:
symbolTable The symbol table.
interner The machine to translate functor and variable names.
defaultBuiltIn The default built in, for standard compilation and interners and symbol tables.
 
     public PreCompiler(SymbolTable<IntegerStringObjectsymbolTableVariableAndFunctorInterner interner,
         DefaultBuiltIn defaultBuiltIn)
     {
         super(symbolTableinterner);
 
         this. = defaultBuiltIn;
          = new BuiltInTransform(defaultBuiltIn);
     }

    
 
     public void compile(Sentence<Clausesentencethrows SourceCodeException
     {
         Clause clause = sentence.getT();
 
         substituteBuiltIns(clause);
         initialiseSymbolTable(clause);
         topLevelCheck(clause);
 
         if ( != null)
         {
             if (clause.isQuery())
             {
                 .onQueryCompilation(sentence);
             }
             else
             {
                 .onCompilation(sentence);
             }
         }
     }

    
 
     public void setCompilerObserver(LogicCompilerObserver<ClauseClauseobserver)
     {
         this. = observer;
    }

    
    public void endScope() throws SourceCodeException
    {
    }

    
Substitutes built-ins within a clause, with their built-in definitions.

Parameters:
clause The clause to transform.
    private void substituteBuiltIns(Clause clause)
    {
        TermWalker walk =
            TermWalkers.positionalWalker(new BuiltInTransformVisitor(null));
        walk.walk(clause);
    }

    
Runs a symbol key traverser over the clause to be compiled, to ensure that all of its terms and sub-terms have their symbol keys initialised.

Parameters:
clause The clause to initialise the symbol keys of.
    private void initialiseSymbolTable(Clause clause)
    {
        // Run the symbol key traverser over the clause, to ensure that all terms have their symbol keys correctly
        // set up.
        SymbolKeyTraverser symbolKeyTraverser = new SymbolKeyTraverser(null);
        symbolKeyTraverser.setContextChangeVisitor(symbolKeyTraverser);
        TermWalker symWalker =
            new TermWalker(new DepthFirstBacktrackingSearch<TermTerm>(), symbolKeyTraversersymbolKeyTraverser);
        symWalker.walk(clause);
    }

    
Finds and marks all functors within the clause that are considered to be top-level.

Parameters:
clause The clause to top-level check.
    private void topLevelCheck(Clause clause)
    {
        TermWalker walk = TermWalkers.positionalWalker(new TopLevelCheckVisitor(null));
        walk.walk(clause);
    }
New to GrepCode? Check out our FAQ X