Building Parsers with Java

sjm.engine
Class Structure

java.lang.Object
  |
  +--sjm.engine.Structure
Direct Known Subclasses:
ArithmeticOperator, ConsultingStructure, Fact, Gateway, Not

public class Structure
extends java.lang.Object
implements Term

A Structure is a functor associated with a number of terms; a functor can be any object. A term is an object that implements the Term interface, including structures and variables.

An example of a structure is:

     starred(jamesCagney, "Yankee Doodle Dandy", Year)
 
This structure has the String "starred" as its functor. This structure's first term is another structure that has "jamesCagney" as its functor and no terms of its own. Similarly, the second term is a structure with the functor "Yankee Doodle Dandy" and no terms of its own. The third term is a variable, Year.

This particular example has two elements that favor a parser: the quotes around the film title and the capitalization of the variable. When using Structure and Variable directly, you do not need the kinds of clues a parser needs. So, Yankee Doodle Dandy is just another string, whose internal blanks are not at all confusing. Further, a variable can have any string as its name, not necessarily capitalized.

You can create the starred example with

 
     Structure s = new Structure(
         "starred", 
         new Term[]{
             new Structure("jamesCagney"),
             new Structure("Yankee Doodle Dandy"),
             new Variable("Year")}); 
 
To be able to prove itself against a program, a structure must appear in a Rule. Rules associate like-named variables in a "scope", which is essentially a dictionary. A rule makes an executable copy of itself by creating a new variable dictionary, and by making "consulting" copies of its structures.


Field Summary
static EmptyList emptyList
           
protected  java.lang.Object functor
           
protected  Term[] terms
           
 
Constructor Summary
Structure(java.lang.Object functor)
          Contructs a structure from the specified object.
Structure(java.lang.Object functor, Term[] terms)
          Constructs a structure with the specified functor and terms.
 
Method Summary
 int arity()
          Return the number of terms in this structure.
 boolean canFindNextProof()
          Returns false.
 Term copyForProof(AxiomSource as, Scope scope)
          Create a ConsultingStructure counterpart that can unify with other structures.
 boolean equals(java.lang.Object o)
          Returns true if the supplied object is an equivalent structure.
 java.lang.Object eval()
          Return this structure, if it is nonatomic, or just the functor, if this is an atom.
 boolean functorAndArityEquals(Structure s)
          Returns true if this structure's functor and number of terms match the supplied structure.
protected static Term[] headAndTail(Term[] terms, Term tail)
           
 boolean isList()
          Return true, if this structure is a list, which means it has an functor of ".", and has two terms, the second of which must be a list.
static Structure list(java.lang.Object[] objects)
          Constructs a list that contains the supplied object, wrapped as Facts.
static Structure list(Term[] terms)
          Constructs a list from the given terms.
static Structure list(Term[] terms, Term tail)
          Constructs a list that terminates with a known list, or a variable.
 java.lang.String listTailString()
          Returns a representation of this list as the inner part of some other list.
protected  java.lang.String listTermsToString()
           
 Term[] terms()
          Return the terms of this structure.
 java.lang.String toString()
          Returns a string representation of this structure.
 Unification unify(Structure s)
          Unifies the terms in this structure with the terms in the given structure, and returns the variable bindings that result.
 Unification unify(Term t)
          Unifies this structure with the supplied term.
 Unification unify(Variable v)
          Unifies this structure with the supplied variable.
 Unification variables()
          Returns the variables of the terms of this structure.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

functor

protected java.lang.Object functor

terms

protected Term[] terms

emptyList

public static final EmptyList emptyList
Constructor Detail

Structure

public Structure(java.lang.Object functor)
Contructs a structure from the specified object.
Parameters:
Object - the functor for this structure

Structure

public Structure(java.lang.Object functor,
                 Term[] terms)
Constructs a structure with the specified functor and terms.
Parameters:
Object - the functor of the structure
Term[] - the terms of the structure, which may be either variables or other structures
Method Detail

arity

public int arity()
Return the number of terms in this structure.
Returns:
the number of terms in this structure

canFindNextProof

public boolean canFindNextProof()
Returns false.

Objects of this class, the superclass of all structures, should not appear in dynamic rules. When a nondynamic rule creates its dynamic counterpart, it populates it with provable versions of its structures. A general Structure object will construct a ConsultingStructure when it participates in building a dynamic rule.

This particular method is almost never called. Subclasses implement more interesting behavior.

Returns:
false

copyForProof

public Term copyForProof(AxiomSource as,
                         Scope scope)
Create a ConsultingStructure counterpart that can unify with other structures.
Specified by:
copyForProof in interface Term
Parameters:
AxiomSource - where to find axioms to prove against
Scope - the scope to use for variables in the ConsultingStructure
Returns:
a ConsultingStructure counterpart that can unify with other structures.

equals

public boolean equals(java.lang.Object o)
Returns true if the supplied object is an equivalent structure.
Overrides:
equals in class java.lang.Object
Parameters:
object - the object to compare
Returns:
true, if the supplied object's functor equals this structure's functor, and both structures' terms are all equal

eval

public java.lang.Object eval()
Return this structure, if it is nonatomic, or just the functor, if this is an atom.
Specified by:
eval in interface Term
Tags copied from interface: Term
Returns:
the value that this term should present to an evaluating function, such as an ArithmeticOperator.

functorAndArityEquals

public boolean functorAndArityEquals(Structure s)
Returns true if this structure's functor and number of terms match the supplied structure.
Parameters:
Structure - the structure to compare this one against
Returns:
true if this structure's functor and number of terms match the supplied structure

headAndTail

protected static Term[] headAndTail(Term[] terms,
                                    Term tail)

isList

public boolean isList()
Return true, if this structure is a list, which means it has an functor of ".", and has two terms, the second of which must be a list.
Specified by:
isList in interface Term
Returns:
true if this structure is a list

list

public static Structure list(java.lang.Object[] objects)
Constructs a list that contains the supplied object, wrapped as Facts.
Parameters:
Object[] - the contents of the list

list

public static Structure list(Term[] terms)
Constructs a list from the given terms.

This constructor creates a list of two terms, regardless of the number of terms supplied here. The new list's first term is the first term of the supplied array. Its second term is a list of the remaining terms.

Parameters:
Term[] - the terms of the list

list

public static Structure list(Term[] terms,
                             Term tail)
Constructs a list that terminates with a known list, or a variable.

This allows construction of a list such as:

     Variable head = new Variable("Head");
     Variable tail = new Variable("Tail");       
     Structure ht = Structure.list(new Term[] {head}, tail);
 
Parameters:
Term[] - the leading terms of the list. In practice, this array usually contains a single term.
Term - a list, or a variable that represents the tail of the list

listTailString

public java.lang.String listTailString()
Returns a representation of this list as the inner part of some other list. This method is used by toString() .
Specified by:
listTailString in interface Term
Tags copied from interface: Term
Returns:
a string representation of this listTailTerm

listTermsToString

protected java.lang.String listTermsToString()

terms

public Term[] terms()
Return the terms of this structure.
Returns:
the terms of this structure

toString

public java.lang.String toString()
Returns a string representation of this structure.
Overrides:
toString in class java.lang.Object
Returns:
a string representation of this structure

unify

public Unification unify(Structure s)
Unifies the terms in this structure with the terms in the given structure, and returns the variable bindings that result.

If two structures have equal functors and the same number of terms, they can unify if all of their terms unify. For example, the following structures can unify:

     address(Detail, city(City), state(State))
     address(mall(fayette), city(lexington), state(ky))
 
The unification of these structures is:
     Detail = mall(fayette),
     City = lexington,
     State = ky
 
Specified by:
unify in interface Term
Parameters:
Structure - a structure to unify with
Returns:
the sum of the variables that bind to values to make the unification work.

unify

public Unification unify(Term t)
Unifies this structure with the supplied term.

This method dispatches the unify request to either a structure or a variable. The receiver will get a signature match from this object as a Structure, not just a Term.

Specified by:
unify in interface Term
Parameters:
Term - a term to unify with
Returns:
the sum of the variables that bind to values to make the unification work. Returns null if the unification fails.

unify

public Unification unify(Variable v)
Unifies this structure with the supplied variable.

This method dispatches the unify request to the variable. Note that the variable may be instantiated to a structure that contains variables. An uninstantiated variable will bind to this structure, but an instantiated variable will forward the unification request to its instantiation.

Specified by:
unify in interface Term
Parameters:
Term - a term to unify with
Returns:
the sum of the variables that bind to values to make the unification work. Returns null if the unification fails.

variables

public Unification variables()
Returns the variables of the terms of this structure.

Note that a structure may contain variables or other structures as terms. This method adds this structure's variables directly to the returned unification. In addition, this method adds in all the variables from the structures among this structure's terms.

For example, the variables of:

     address(street(StreetName), city(CityName), State)
 
are StreetName, CityName, and State.
Specified by:
variables in interface Term
Returns:
unification all the variables of the terms of this structure

by Steve Metsker