|
Building Parsers with Java | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Object | +--sjm.engine.Structure
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 |
protected java.lang.Object functor
protected Term[] terms
public static final EmptyList emptyList
| Constructor Detail |
public Structure(java.lang.Object functor)
Object - the functor for this structure
public Structure(java.lang.Object functor,
Term[] terms)
Object - the functor of the structureTerm[] - the terms of the structure, which may be
either variables or other structures| Method Detail |
public int arity()
public boolean canFindNextProof()
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.
false
public Term copyForProof(AxiomSource as,
Scope scope)
ConsultingStructure counterpart that
can unify with other structures.AxiomSource - where to find axioms to prove
againstScope - the scope to use for variables in the
ConsultingStructureConsultingStructure counterpart that
can unify with other structures.public boolean equals(java.lang.Object o)
object - the object to comparepublic java.lang.Object eval()
public boolean functorAndArityEquals(Structure s)
true if this structure's functor and
number of terms match the supplied structure.Structure - the structure to compare this one againsttrue if this structure's functor and
number of terms match the supplied structure
protected static Term[] headAndTail(Term[] terms,
Term tail)
public boolean isList()
public static Structure list(java.lang.Object[] objects)
Object[] - the contents of the listpublic static Structure list(Term[] 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.
Term[] - the terms of the list
public static Structure list(Term[] terms,
Term tail)
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);
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 listpublic java.lang.String listTailString()
toString()
.protected java.lang.String listTermsToString()
public Term[] terms()
public java.lang.String toString()
public Unification unify(Structure s)
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
Structure - a structure to unify withpublic Unification unify(Term t)
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.
Term - a term to unify withpublic Unification unify(Variable v)
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.
Term - a term to unify withpublic Unification variables()
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.
|
by Steve Metsker | ||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||