Class Term

  • All Implemented Interfaces:
    Iterable<Term>
    Direct Known Subclasses:
    AtomicTerm, NodeTerm

    public abstract class Term
    extends Object
    implements Iterable<Term>
    Term is the abstract class used to represent trees of Terms (term-trees). A term (a node of a term tree) consists of a special label called a functor and a sequence of child terms (called arguments). Strings, Numbers, and Symbols are atomic-terms and are the leaves of a term-tree, i.e. they have no children. Trees of symbols lie midway between sequences of symbols and graphs of symbols in expressiveness and therefor represent somewhat of a sweet-spot in representation space. Historically, term-trees can be found in Prolog and Mathematica and are used for many of the same purposes that others use S-Expressions or XML. All are generic means for representing trees of symbols, and are useful for representing a great variety of kinds of data There exist encodings that freely go between S-Expressions or XML into term-trees. Powerful matching techniques exist that operate over term-trees and at this point in time are 50 years old. Techniques that are 60 years old show how powerful term-trees can be as the implementation vehicle for interpreters and compilers. Finally, "Term Language" (TL), the literal expression of term-trees is much more readable and writable than either S-expressions or XML and is an excelent. way to test the output of a parser by comparing a simple literal. The Term protocol distunguishes between atoms (such as literals) and nodes with children, and leaf nodes that are atoms or nodes without children. All Terms have a functor which acts somewhat like a type or classifier. Term-trees are especially useful as Abstract Syntax Trees (AST) and in implementing expression languages.
    Author:
    djl 2009.08.31
    • Constructor Detail

      • Term

        public Term()
    • Method Detail

      • getFunctor

        public abstract String getFunctor()
        Obtain the functor representation of the Term.
        Returns:
        the functor
      • termAt

        public abstract Term termAt​(int index)
        Obtain the child term at the given index. The index is 1 based for children and with at(0) returning the functor as an AtomicTerm. Beware, your 0 based habits can cause problems but 1 based indexing is useful since the functor is an interesting part of the information space. We are bowing here to the wisdom of Mathematica Expressions.
        Parameters:
        index - index of the child or functor to return
        Returns:
        the child Term or functor as AtomicTerm if index is 0
      • children

        public abstract Term[] children()
        Obtain the childern Terms
        Returns:
        the children of the receiver
      • withChild

        public abstract Term withChild​(Term t)
        Join the receiver with the given child Term. AtomicTerms will construct a general list term (functor .list.) and NodeTerms may be mutated.
        Parameters:
        t - the term to join with
        Returns:
        the Term resulting from joining.
      • termEqual

        public abstract boolean termEqual​(Term t)
        Answer whether the receiver is equal to the given Term. Terms are equal if their functors are equal and their children are termEqual to the children of the given term.
        Parameters:
        t - the Term to check for termEqual
        Returns:
        the boolean result of the comparison
      • fullFormString

        public abstract String fullFormString()
        Answer a String representation of the Term that is allowed to show more internal details than toString() which does not compress information. Similar to Object.toString().
        Returns:
        a String representation of the receiver
      • isLeaf

        public boolean isLeaf()
        Answer whether the receiver has children.
        Returns:
        the boolean result of the isLeaf() test
      • isAtom

        public boolean isAtom()
        Answer whether the receiver is an Atomic Term.
        Returns:
        the boolean result of the isAtom() test
      • isNumber

        public boolean isNumber()
        Answer whether the receiver is an Atomic Term representing a Number.
        Returns:
        the boolean result of the isNumber() test
      • length

        public int length()
        Answer whether the length of the receivers children
        Returns:
        the length
      • findChild

        public Term findChild​(String sFunctor)
        Find the Term amoungst the children whose functor equals the given functor.
        Parameters:
        sFunctor - the functor to search for
        Returns:
        the found Term or null if not found
      • findAttribute

        public Term findAttribute​(String sFunctor)
        Find the Term amoungst the children whose functor equals the given functor that has a singleton child.
        Parameters:
        sFunctor - the functor to search for
        Returns:
        the found Term's first child or null if not found
      • childrenTermEqual

        public boolean childrenTermEqual​(Term t)
        Answer whether the receiver's children is equal to the given Terms children. Terms are equal if their functors are equal and their children are termEqual to the children of the given term.
        Parameters:
        t - term whose children are to be checked for equality
        Returns:
        the found Term or null if not found
      • headChildrenTermEqual

        public boolean headChildrenTermEqual​(Term t)
        Find the Term amongst the children whose functor equals the given functor.
        Parameters:
        t - the functor to search for
        Returns:
        the found Term or null if not found
      • accept

        public void accept​(TermWalker walker)
        Do a dispatch back to the given walker.
        Parameters:
        walker - the TermWalker that implements the visitor for Terms