RuleSet.java

/*
** Module   : RuleSet.java
** Abstract : A rule container with a choice of input mode and walk depth.
**
** Copyright (c) 2005-2017, Golden Code Development Corporation.
**
** -#- -I- --Date-- -T- --JPRM-- ----------------Description-----------------
** 001 ECF 20050228 ADD   @20142 Created initial version. Adds to the base
**                               class a named expression implementation and a
**                               choice of input mode.
** 002 ECF 20050310 DEL   @20266 Moved named expression library support to
**                               superclass. This allows other subclasses to
**                               take advantage of this support as well.
** 003 ECF 20050525 CHG   @21292 Changes to support the new expression engine
**                               implementation. Added parent parameter to
**                               constructor to support scoping.
** 004 ECF 20050716 CHG   @21732 Added maximum walk depth attribute. Specifies
**                               the maximum level to which the pattern engine
**                               iterator should walk an AST when this ruleset
**                               is applied.
** 005 GES 20050728 CHG   @21899 Added support for the honor-hidden attribute.
**                               Defaults to false, but when enabled, non-root
**                               AST nodes that are accessed will drop from
**                               the walk if their hidden flag is set.
*/
/*
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Affero General Public License as
** published by the Free Software Foundation, either version 3 of the
** License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Affero General Public License for more details.
**
** You may find a copy of the GNU Affero GPL version 3 at the following
** location: https://www.gnu.org/licenses/agpl-3.0.en.html
** 
** Additional terms under GNU Affero GPL version 3 section 7:
** 
**   Under Section 7 of the GNU Affero GPL version 3, the following additional
**   terms apply to the works covered under the License.  These additional terms
**   are non-permissive additional terms allowed under Section 7 of the GNU
**   Affero GPL version 3 and may not be removed by you.
** 
**   0. Attribution Requirement.
** 
**     You must preserve all legal notices or author attributions in the covered
**     work or Appropriate Legal Notices displayed by works containing the covered
**     work.  You may not remove from the covered work any author or developer
**     credit already included within the covered work.
** 
**   1. No License To Use Trademarks.
** 
**     This license does not grant any license or rights to use the trademarks
**     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
**     of Golden Code Development Corporation. You are not authorized to use the
**     name Golden Code, FWD, or the names of any author or contributor, for
**     publicity purposes without written authorization.
** 
**   2. No Misrepresentation of Affiliation.
** 
**     You may not represent yourself as Golden Code Development Corporation or FWD.
** 
**     You may not represent yourself for publicity purposes as associated with
**     Golden Code Development Corporation, FWD, or any author or contributor to
**     the covered work, without written authorization.
** 
**   3. No Misrepresentation of Source or Origin.
** 
**     You may not represent the covered work as solely your work.  All modified
**     versions of the covered work must be marked in a reasonable way to make it
**     clear that the modified work is not originating from Golden Code Development
**     Corporation or FWD.  All modified versions must contain the notices of
**     attribution required in this license.
*/

package com.goldencode.p2j.pattern;

import java.util.*;

/**
 * A rule container implementation which adds a choice of input mode. A
 * ruleset is essentially a list of rules which is applied against one or
 * more ASTs. For each AST, the entire list of rules is applied in turn.
 * Rules can test conditions based upon an AST's properties and perform
 * actions conditionally for diverse purposes.
 * <p>
 * Input mode determines whether this ruleset is applied against an entire
 * AST via a depth first iteration of its nodes, or against a filtered view
 * of an AST which contains only those nodes which have been added to the
 * view, based upon some previous test.
 *
 * @see  #setInputMode
 * @see  Rule
 */
public final class RuleSet
extends RuleContainer
{
   /** Indicates this ruleset will iterate over an AST structure */
   public static final int INPUT_TREE = 0;
   
   /** Indicates this ruleset will iterate over a filtered list of ASTs */
   public static final int INPUT_VIEW = 1;
   
   /** Input mode (tree or view) associated with this ruleset */
   private int inputMode = INPUT_TREE;
   
   /** Maximum walk depth for ASTs to which this ruleset is applied */
   private int depth = 0;
   
   /** Honor hidden processing mode flag. */
   private boolean honorHidden = false;
   
   /**
    * Constructor which assigns enclosing scope.
    *
    * @param   parent
    *          Rule container which provides enclosing scope to this ruleset.
    */
   public RuleSet(RuleContainer parent)
   {
      super(parent);
   }
   
   /**
    * Gets the input mode currently associated with this ruleset.
    *
    * @return  <code>INPUT_TREE</code> or <code>INPUT_VIEW</code>.
    *
    * @see     #setInputMode
    */
   int getInputMode()
   {
      return inputMode;
   }
   
   /**
    * Set the input mode for use with this ruleset. Input mode determines
    * whether this ruleset is applied against an AST directly or through a
    * filtered view created by a ruleset which has run previously in a
    * pipeline of rulesets. A filtered view is created each time a ruleset
    * is applied. An individual rule may add AST nodes to the view using
    * user functions implemented by {@link CommonAstSupport}. This view
    * is passed along to the next ruleset in the pipeline and is used as
    * the basis for that ruleset's iteration if its input mode is set to
    * <code>INPUT_VIEW</code>.
    *
    * @param   inputMode
    *          <code>INPUT_TREE</code> to apply this ruleset against an AST
    *          directly; <code>INPUT_VIEW</code> to apply this ruleset
    *          against a filtered view of ASTs.
    */
   public void setInputMode(int inputMode)
   {
      switch (inputMode)
      {
         case INPUT_TREE:
         case INPUT_VIEW:
            this.inputMode = inputMode;
            break;
         default:
            throw new IllegalArgumentException(
               "Invalid input mode: " + inputMode);
      }
   }
   
   /**
    * Get the maximum walk depth, where 0 indicates all levels should be
    * visited.
    *
    * @return  Maximum walk depth. 
    */
   public int getDepth()
   {
      return depth;
   }
   
   /**
    * Set the maximum walk depth, where 0 indicates all levels should be
    * visited.
    *
    * @param   depth
    *          Maximum walk depth.
    */
   public void setDepth(int depth)
   {
      this.depth = depth;
   }
   
   /**
    * Gets the flag that determines if the hidden status of an AST node
    * should be honored. Note: this is only effective in top-level rule-sets
    * (the value is meaningless in nested rule-sets).
    *
    * @return   <code>true</code> if a node's hidden flag should be honored. 
    */
   public boolean isHonorHidden()
   {
      return honorHidden;
   }
   
   /**
    * Sets the flag that determines if the hidden status of an AST node
    * should be honored. Note: this is only effective in top-level rule-sets
    * (the value is meaningless in nested rule-sets).
    *
    * @param    hide
    *           <code>true</code> if a node's hidden flag should be honored. 
    */
   public void setHonorHidden(boolean hide)
   {
      honorHidden = hide;
   }   
}