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;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
PositionAndOccurrenceVisitor visits a clause to gather information about the positions in which components of the clause appear.

For variables, the following information is gathered:

  1. A count of the number of times the variable occurs in the clause (singleton detection).
  2. A flag indicating that variable only ever appears in non-argument positions.
  3. The last functor body in the clause in which a variable appears, provided it only does so in argument position.

For constants, the following information is gathered:

  1. A flag indicating the constant only ever appears in non-argument positions.

CRC Card
Responsibilities Collaborations
Count variable occurrences in a clause.
Detect variables and constants only appearing in non-argument positions.
Identify the last functor in which a variable appears, if it only does as an argument.

Author(s):
Rupert Smith
 
 {
    
Holds the current top-level body functor. null when traversing the head.
 
     private Functor topLevelBodyFunctor;

    
Holds a set of all constants encountered.
 
     private Map<IntegerList<SymbolKey>> constants = new HashMap<IntegerList<SymbolKey>>();

    
Holds a set of all constants found to be in argument positions.
 
     private Set<IntegerargumentConstants = new HashSet<Integer>();

    
Creates a positional visitor.

Parameters:
interner The name interner.
symbolTable The compiler symbol table.
traverser The positional context traverser.
 
         SymbolTable<IntegerStringObjectsymbolTablePositionalTermTraverser traverser)
     {
         super(internersymbolTabletraverser);
     }

    

Counts variable occurrences and detects if the variable ever appears in an argument position.

 
     protected void enterVariable(Variable variable)
     {
         // Initialize the count to one or add one to an existing count.
         count = (count == null) ? 1 : (count + 1);
 
        /*log.fine("Variable " + variable + " has count " + count + ".");*/
        // Get the nonArgPosition flag, or initialize it to true.
        Boolean nonArgPositionOnly =
            (Boolean.get(variable.getSymbolKey(), .);
        nonArgPositionOnly = (nonArgPositionOnly == null) ? true : nonArgPositionOnly;
        // Clear the nonArgPosition flag if the variable occurs in an argument position.
        nonArgPositionOnly = inTopLevelFunctor() ? false : nonArgPositionOnly;
        .put(variable.getSymbolKey(), .nonArgPositionOnly);
        /*log.fine("Variable " + variable + " nonArgPosition is " + nonArgPositionOnly + ".");*/
        // If in an argument position, record the parent body functor against the variable, as potentially being
        // the last one it occurs in, in a purely argument position.
        // If not in an argument position, clear any parent functor recorded against the variable, as this current
        // last position of occurrence is not purely in argument position.
        if (inTopLevelFunctor())
        {
        }
        else
        {
            .put(variable.getSymbolKey(), .null);
        }
    }

    

Checks if a constant ever appears in an argument position.

Sets the 'inTopLevelFunctor' flag, whenever the traversal is directly within a top-level functors arguments. This is set at the end, so that subsequent calls to this will pick up the state of this flag at the point immediately below a top-level functor.

    protected void enterFunctor(Functor functor)
    {
        /*log.fine("Functor: " + functor.getName() + " <- " + symbolTable.getSymbolKey(functor.getName()));*/
        // Only check position of occurrence for constants.
        if (functor.getArity() == 0)
        {
            // Add the constant to the set of all constants encountered.
            List<SymbolKeyconstantSymKeys = .get(functor.getName());
            if (constantSymKeys == null)
            {
                constantSymKeys = new LinkedList<SymbolKey>();
                .put(functor.getName(), constantSymKeys);
            }
            constantSymKeys.add(functor.getSymbolKey());
            // If the constant ever appears in argument position, take note of this.
            if (inTopLevelFunctor())
            {
                .add(functor.getName());
            }
        }
        // Keep track of the current top-level body functor.
        if (isTopLevel() && !.isInHead())
        {
             = functor;
        }
    }

    
Upon leaving the clause, sets the nonArgPosition flag on any constants that need it.

Parameters:
clause The clause being left.
    protected void leaveClause(Clause clause)
    {
        // Remove the set of constants appearing in argument positions, from the set of all constants, to derive
        // the set of constants that appear in non-argument positions only.
        // Set the nonArgPosition flag on all symbol keys for all constants that only appear in non-arg positions.
        for (List<SymbolKeysymbolKeys : .values())
        {
            for (SymbolKey symbolKey : symbolKeys)
            {
                .put(symbolKey.true);
            }
        }
    }

    
Checks if the current position is immediately within a top-level functor.

Parameters:
context The position context to examine.
Returns:
true iff the current position is immediately within a top-level functor.
    private boolean inTopLevelFunctor(PositionalContext context)
    {
        PositionalContext parentContext = context.getParentContext();
        return parentContext.isTopLevel() || isTopLevel(parentContext);
    }

    
Functors are considered top-level when they appear at the top-level within a clause, or directly beneath a parent conjunction or disjunction that is considered to be top-level.

Parameters:
context The position context to examine.
Returns:
true iff the current position is a top-level functor.
    private boolean isTopLevel(PositionalContext context)
    {
        Term term = context.getTerm();
        if (term.getSymbolKey() == null)
        {
            return false;
        }
        return (isTopLevel == null) ? false : isTopLevel;
    }
New to GrepCode? Check out our FAQ X