⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iclterm.java

📁 SRI international 发布的OAA框架软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.sri.oaa2.icl;
import java.util.*;
import java.io.*;
import java.io.Serializable;
import com.sri.oaa2.lib.LibOaa;
import antlr_oaa.RecognitionException;
import antlr_oaa.TokenStreamException;
import antlr_oaa.ANTLRException;

/**
 * The root of the IclTerm class heirarchy.  Implements the composite pattern.
 */
public abstract class IclTerm implements Cloneable, OaaPrologVocabTokenTypes, Serializable {

  // Initialize the library
  static 
  {
    LibOaa.libInit();
  }

  /// Children of this node
  protected ArrayList children = new ArrayList();

  /// Read-only term?
  protected boolean readOnly = false;

  protected int type = 0;

  /// CRC
  long crc = 0;

  long getCrc() 
  {
    return this.crc;
  }

  void setCrc(long crc) 
  {
    this.crc = crc;
  }
  
  /** 
   * Get the token type for this node. 
   */
  public int getType()
  {
    return this.type;
  }

  /** 
   * Set the token type for this node. 
   */
  public void setType(int ttype) 
  {
    this.type = ttype;
  }

  /**
   * Create an empty IclTerm.  You should only use this if you know what you're doing.
   */
  public IclTerm() 
  {
  }

  /**
   * Create a new term of a specific type.  Should only be used by subclasses.
   */
  protected IclTerm(int i) 
  {
    setType(i);
  }

  /**
   * Set read only flag.
   */
  public final void setReadOnly(boolean r) 
  {
    readOnly = r;
  }

  /**
   * Get read only flag.
   */
  public final boolean isReadOnly() 
  {
    return readOnly;
  }

  /**
   * Check read only flag.  This throws a RuntimeException for
   * backwards compatibility.
   *
   * @throws RuntimeException if not writable.
   */
  public final void checkWritable() 
  {
    if(isReadOnly()) {
      throw new RuntimeException("Cannot write to IclTerm");
    }
  }

  /**
   * Check if this term is atomic--can never have children.
   */
  public abstract boolean isAtomic();
  
  /**
   * Check if this term is composite--can have children.
   */
  public abstract boolean isComposite();

  /**
   * Check if this term is of a certain type.
   *
   * @return boolean: true if this term is of the given type and false otherwise
   */
  public final boolean isOfType(int t) 
  {
    return (getType() == t);
  }

  /**
   * Check if this term is a struct.
   *
   * @return boolean: true if this term is an IclStruct and false otherwise
   */
  public final boolean isStruct() 
  {
    return isOfType(STRUCT);
  }
  
  /**
   * Check if this term is a list.
   *
   * @return boolean: true if term is an IclList; false otherwise
   */
  public final boolean isList() 
  {
    return isOfType(LIST);
  }

  /**
   * Check if this term is a group.
   *
   * @return boolean: true if term is an IclGroup; false otherwise
   */
  public final boolean isGroup() 
  {
    return isOfType(GROUP);
  }

  /**
   * Check if this term is an IclDataQ.
   *
   * @return boolean: true if term is an IclDataQ; false otherwise
   */
  public final boolean isIclDataQ() 
  {
    return isOfType(ICLDATAQ);
  }

  /**
   * Check if this term is an int.
   *
   * @return boolean: true if term is an IclInt; false otherwise
   */
  public final boolean isInt() 
  {
    return isOfType(INT);
  }

  /**
   * Check if this term is a float.
   *
   * @return boolean: true if term is an IclFloat; false otherwise
   */
  public final boolean isFloat() 
  {
    return isOfType(FLOAT);
  }

  /**
   * Check if this term is an variable.
   *
   * @return boolean: true if term is an IclVar; false otherwise
   */
  public final boolean isVar() 
  {
    return isOfType(VAR);
  }

  /**
   * Check if this term is an atom.
   *
   * @return boolean: true if term is an IclStr; false otherwise
   */
  public final boolean isStr() 
  {
    return isOfType(STR);
  }

  /**
   * Check if this term is the empty list.
   *
   * @return boolean: true if term is an empty list; false otherwise
   */
  public final boolean isEmptyList() 
  {
    return (isList() && (getNumChildren() == 0));
  }

  /**
   * Get the children of this node as an ArrayList.  Returns a new, empty
   * ArrayList for nodes without children.  For IclTerms that cannot have
   * children, this returns null.  You should not alter this ArrayList.
   *
   * @deprecated Use iterator() or listIterator() methods.
   * @return ArrayList the list of children--altering this will alter the
   * internal data structure used by the IclTerm!  Don't do it!  Use the
   * listIterator() or iterator() methods.
   */
  public abstract ArrayList toArrayList();

  /**
   * Get the children of this node.
   */
  protected final ArrayList getChildren() 
  {
    return children;
  }

  /**
   * Set the children for this node.
   *
   * @param c the new children
   */
  abstract void setChildren(ArrayList c);

  /**
   * Clone this node.
   *
   * @return Object: the clone
   */
  public Object clone() 
  {
    IclTerm ret;
    try {
      ret = (IclTerm)(super.clone());
      ret.setReadOnly(false);
    }
    catch(CloneNotSupportedException cnse) {
      return null;
    }
    if(children != null) {
      ret.children = new ArrayList(getNumChildren());
      for(int i = 0; i < getNumChildren(); ++i) {
	ret.add(((IclTerm)(getChild(i)).clone()));
      }
    }
    return ret;
  }

  /**
   * Get the number of children of this node.
   *
   * @return int: the number of children
   */
  public abstract int getNumChildren();

  /**
   * Add a child to the end of the list of children.
   *
   * @param n the term to add.
   * @throws UnsupportOperationException if term cannot have children
   */
  public abstract void add(IclTerm n) throws UnsupportedOperationException;

  /**
   * Add a child to this term at the given index
   * @throws UnsupportedOperationException if term cannot have children
   */
  public abstract void add(int index, IclTerm n) throws UnsupportedOperationException;

  /**
   * Add all the children of the given term to this term.  The order in
   * which they are added is the same order in which they are encountered
   * using l.toArrayList().listIterator().  Uses the add(IclTerm) method to
   * actually do the adding.
   *
   * @param l the IclTerm containing the children to add.
   * @throws UnsupportedOperationException if term cannot have children
   */
  public abstract void addAll(IclTerm l) throws UnsupportedOperationException;

  /**
   * Remove all children of this node.
   * @throws UnsupportedOperationException if term cannot have children
   */
  public abstract void clearTerms() throws UnsupportedOperationException;

  /**
   * Get the child at the given Index. Assumes children exists.
   *
   * @param int i: the index
   * @return IclTerm: the child at the index
   * @throws UnsupportedOperationException if term cannot have children
   */
  protected abstract IclTerm getChild(int i) throws UnsupportedOperationException;

  /**
   * Get the term at the given index.  For a readonly term, returns a clone.
   *
   * @param i index of term (first term has index 0, and last has
   * index (size() -1)
   * @return IclTerm: the term, or null if no such child
   * @throws IndexOutOfBoundsException if i is out of bounds
   */
  public abstract IclTerm getTerm(int i) throws UnsupportedOperationException;

  /**
   * Get the number of children of this node.
   * 
   * @return int: the number of children
   */
  public abstract int size();

  /** 
   * Accept a visitor.  This is handy for traversing an IclTerm tree.
   * It's also handy for doing conversions, as in the ToInt class.
   *
   * @param visitor the visitor
   * @param data generic data for the visitor
   * @return Object: generic return value
   * @see com.sri.oaa2.icl.IclTermTraverser for an alternative
   */
  protected Object accept(OaaPrologVisitor visitor, Object data) {
    return visitor.visit(this, data);
  }

  /** 
   * Have children accept the visitor.  This is the second part of
   * the visitor interface.
   *
   * @param visitor the visitor
   * @param data generic data for the visitor
   * @return Object: generic return value
   */
  protected final Object childrenAccept(OaaPrologVisitor visitor, Object data) {
    for (int i = 0; i < this.getNumChildren(); ++i) {
      ((IclTerm)children.get(i)).accept(visitor, data);
    }
    return data;
  }

  /**
   * Convert this term into a string.
   * <p>
   * Unfortunately, this is very slow!  If you happen to know a term is atomic,
   * you might be better off casting to the specific type, and then grabbing the
   * data from one of the public methods of that class.
   */
  public final String toString() 
  { 
    return ToString.getInstance().from(this);
  }

  /**
   * Do the conversion with a prefix.
   */
  public String toString(String prefix) 
  { 
    return prefix + toString(); 
  }

  /**
   * Get the identifying string.  Usually, this is whatever is returned by toString(), but
   * is the functor in the case of an IclStruct, and the unquoted atom in the case of
   * an IclStr.
   */
  public String toIdentifyingString() 
  {
    return toString();
  }

  /**
   * Override this method if you want to customize how the node dumps
   * out its children. 
   */
  protected final void dump(String prefix) {
    System.out.println(toString(prefix));
    if (children != null) {
      for (int i = 0; i < this.getNumChildren(); ++i) {
        IclTerm n = (IclTerm)children.get(i);
        if (n != null) {
          n.dump(prefix + " ");
        }
      }
    }
  }

  /**
   * Remove all terms from this term's children for which the given term
   * unifies.
   *
   * @param n the term against which children must unify to be removed

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -