📄 structure.java
字号:
System.arraycopy(terms, 1, rest, 0, rest.length);
headAndTail[1] = list(rest, tail);
}
return headAndTail;
}
/**
* 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.
*
* @return true if this structure is a list
*/
public boolean isList() {
return
terms.length == 2 &&
functor.equals(".") &&
terms[1].isList();
}
/**
* Constructs a list that contains the supplied object, wrapped
* as Facts.
*
* @param Object[] the contents of the list
*/
public static Structure list(Object[] objects) {
return list(Fact.facts(objects));
}
/**
* Constructs a list from the given terms.
* <p>
* 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.
*
* @param Term[] the terms of the list
*/
public static Structure list(Term[] terms) {
return new Structure(".", headAndTail(terms, emptyList));
}
/**
* Constructs a list that terminates with a known list, or a
* variable.
* <p>
* This allows construction of a list such as:
*
* <blockquote><pre>
* Variable head = new Variable("Head");
* Variable tail = new Variable("Tail");
* Structure ht = Structure.list(new Term[] {head}, tail);
* </pre></blockquote>
*
* @param Term[] the leading terms of the list. In practice,
* this array usually contains a single term.
*
* @param Term a list, or a variable that represents the tail
* of the list
*/
public static Structure list(Term[] terms, Term tail) {
return new Structure(".", headAndTail(terms, tail));
}
/**
* Returns a representation of this list as the inner part of
* some other list. This method is used by <code>toString()
* </code>.
*/
public String listTailString() {
return ", " + 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.
*/
protected String listTermsToString() {
String s = terms[0].toString();
if (terms.length > 1) {
s += terms[1].listTailString();
}
return s;
}
/**
* Return the terms of this structure.
*
* @return the terms of this structure
*/
public Term[] terms() {
return terms;
}
/**
* Returns a string representation of this structure.
*
* @return a string representation of this structure
*/
public String toString() {
if (isList()) {
/*
* Show the brackets, the head, and the tail. The
* tail prints itself recursively, until the empty
* list stops the recursion.
*/
return "[" + listTermsToString() + "]";
}
StringBuffer buf = new StringBuffer(functor.toString());
if (terms.length > 0) {
buf.append("(");
for (int i = 0; i < terms.length; i++) {
if (i > 0) {
buf.append(", ");
}
buf.append(terms[i].toString());
}
buf.append(")");
}
return buf.toString();
}
/**
* Unifies the terms in this structure with the terms in the
* given structure, and returns the variable bindings that
* result.
* <p>
* 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:
* <blockquote><pre>
* address(Detail, city(City), state(State))
* address(mall(fayette), city(lexington), state(ky))
* </pre></blockquote>
* The unification of these structures is:
* <blockquote><pre>
* Detail = mall(fayette),
* City = lexington,
* State = ky
* </pre></blockquote>
*
* @param Structure a structure to unify with
*
* @return the sum of the variables that bind to values to make
* the unification work.
*/
public Unification unify(Structure s) {
if (!functorAndArityEquals(s)) {
return null;
}
Unification u = new Unification();
Term others[] = s.terms();
for (int i = 0; i < terms().length; i++) {
Unification subUnification = terms()[i].unify(others[i]);
if (subUnification == null) {
u.unbind();
return null;
}
u.append(subUnification);
}
return u;
}
/**
* Unifies this structure with the supplied term.
* <p>
* 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.
*
* @param Term a term to unify with
*
* @return the sum of the variables that bind to values to make
* the unification work. Returns null if the
* unification fails.
*/
public Unification unify(Term t) {
return t.unify(this);
}
/**
* Unifies this structure with the supplied variable.
* <p>
* 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.
*
* @param Term a term to unify with
*
* @return the sum of the variables that bind to values to make
* the unification work. Returns null if the
* unification fails.
*/
public Unification unify(Variable v) {
return v.unify(this);
}
/**
* Returns the variables of the terms of this structure.
* <p>
* 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.
* <p>
* For example, the variables of:
* <blockquote><pre>
* address(street(StreetName), city(CityName), State)
* </pre></blockquote>
* are StreetName, CityName, and State.
*
* @return unification all the variables of the terms of this
* structure
*/
public Unification variables() {
Unification u = new Unification();
if (terms.length > 0) {
for (int i = 0; i < terms().length; i++) {
u.append(terms[i].variables());
}
}
return u;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -