| java.lang.Object org.ofbiz.rules.engine.Structure
All known Subclasses: org.ofbiz.rules.engine.ConsultingStructure, org.ofbiz.rules.engine.Gateway, org.ofbiz.rules.engine.ArithmeticOperator, org.ofbiz.rules.engine.Fact, org.ofbiz.rules.engine.Not,
Structure | public class Structure implements Term(Code) | | Title: Structure
Description: None
Copyright (c) 1999 Steven J. Metsker.
Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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.
author: Steven J. Metsker version: 1.0 |
Constructor Summary | |
public | Structure(Object functor) Contructs a structure from the specified object. | public | Structure(Object functor, Term[] terms) Constructs a structure with the specified functor and terms. |
Method Summary | |
public int | arity() Return the number of terms in this structure. | public boolean | canFindNextProof() Returns false .
Objects of this class, the superclass of all structures,
should not appear in dynamic rules. | public Term | copyForProof(AxiomSource as, Scope scope) Create a ConsultingStructure counterpart that
can unify with other structures. | public boolean | equals(Object o) Returns true if the supplied object is an equivalent
structure. | public Object | eval() Return this structure, if it is nonatomic, or just the
functor, if this is an atom. | public 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) This method helps the static list factories.
A list is a structure whose functor is "." and that has two
terms. | 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. | public static Structure | list(Object[] objects) Constructs a list that contains the supplied object, wrapped
as Facts. | 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. | 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);
Term[] the leading terms of the list. | public String | listTailString() Returns a representation of this list as the inner part of
some other list. | protected String | listTermsToString() Return a textual represenation of this list's terms, with
a normal representation of the first term, and with the
second term as the tail of a list. | public Term[] | terms() Return the terms of this structure. | public String | toString() Returns a string representation of this structure. | 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. | 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. | 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. | public Unification | variables() Returns the variables of the terms of this structure.
Note that a structure may contain variables or other structures as
terms. |
emptyList | final public static EmptyList emptyList(Code) | | the empty list singleton
|
Structure | public Structure(Object functor)(Code) | | Contructs a structure from the specified object.
Parameters: Object - the functor for this structure |
Structure | public Structure(Object functor, Term[] terms)(Code) | | Constructs a structure with the specified functor and terms.
Parameters: Object - the functor of the structure Term[] the terms of the structure, which may beeither variables or other structures |
arity | public int arity()(Code) | | Return the number of terms in this structure.
the number of terms in this structure |
canFindNextProof | public boolean canFindNextProof()(Code) | | 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.
false |
copyForProof | public Term copyForProof(AxiomSource as, Scope scope)(Code) | | Create a ConsultingStructure counterpart that
can unify with other structures.
Parameters: AxiomSource - where to find axioms to proveagainst Parameters: Scope - the scope to use for variables in theConsultingStructure a ConsultingStructure counterpart thatcan unify with other structures. |
equals | public boolean equals(Object o)(Code) | | Returns true if the supplied object is an equivalent
structure.
Parameters: object - the object to compare true, if the supplied object's functor equalsthis structure's functor, and both structures'terms are all equal |
eval | public Object eval()(Code) | | Return this structure, if it is nonatomic, or just the
functor, if this is an atom.
|
functorAndArityEquals | public boolean functorAndArityEquals(Structure s)(Code) | | Returns true if this structure's functor and
number of terms match the supplied structure.
Parameters: Structure - the structure to compare this one against true if this structure's functor andnumber of terms match the supplied structure |
headAndTail | protected static Term[] headAndTail(Term[] terms, Term tail)(Code) | | This method helps the static list factories.
A list is a structure whose functor is "." and that has two
terms. The first term of a list can be any term; the second
term of a list is another list, an empty list, or a
variable.
This method accepts an array of terms and a tail, which must
be a list structure or a variable. This method composes and
returns a two-element array.
The first element of the returned array will always be the
first element of the supplied terms array.
The second element of the returned array will be a list.
This list will be a concatenation of the remainder of the
given array with the supplied tail.
|
isList | public boolean isList()(Code) | | 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.
true if this structure is a list |
list | public static Structure list(Object[] objects)(Code) | | Constructs a list that contains the supplied object, wrapped
as Facts.
Object[] the contents of the list |
list | public static Structure list(Term[] terms)(Code) | | 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.
Term[] the terms of the list |
list | public static Structure list(Term[] terms, Term tail)(Code) | | 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);
Term[] the leading terms of the list. In practice,this array usually contains a single term. Parameters: Term - a list, or a variable that represents the tailof the list |
listTailString | public String listTailString()(Code) | | Returns a representation of this list as the inner part of
some other list. This method is used by toString()
.
|
listTermsToString | protected String listTermsToString()(Code) | | Return a textual represenation of this list's terms, with
a normal representation of the first term, and with the
second term as the tail of a list.
|
terms | public Term[] terms()(Code) | | Return the terms of this structure.
the terms of this structure |
toString | public String toString()(Code) | | Returns a string representation of this structure.
a string representation of this structure |
unify | public Unification unify(Structure s)(Code) | | 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
Parameters: Structure - a structure to unify with the sum of the variables that bind to values to makethe unification work. |
unify | public Unification unify(Term t)(Code) | | 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.
Parameters: Term - a term to unify with the sum of the variables that bind to values to makethe unification work. Returns null if theunification fails. |
unify | public Unification unify(Variable v)(Code) | | 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.
Parameters: Term - a term to unify with the sum of the variables that bind to values to makethe unification work. Returns null if theunification fails. |
variables | public Unification variables()(Code) | | 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.
unification all the variables of the terms of thisstructure |
|
|