aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShawn O. Pearce <sop@google.com>2011-06-06 08:40:04 -0700
committerShawn O. Pearce <sop@google.com>2011-06-06 09:18:36 -0700
commit46addafcab94965f1401d97443ee0d95e73ed0b4 (patch)
tree1730e2463a07358849a0654f9aa6b60fa09b2e76
parentb5c37304f66636cfb5feaf741d351e2dac69def1 (diff)
downloadprolog-cafe-46addafcab94965f1401d97443ee0d95e73ed0b4.tar.gz
Add name() to Term API
This can help when matching a StructureTerm with isStructure().
-rw-r--r--src/lang/ClosureTerm.java2
-rw-r--r--src/lang/DoubleTerm.java4
-rw-r--r--src/lang/IntegerTerm.java2
-rw-r--r--src/lang/JavaObjectTerm.java22
-rw-r--r--src/lang/ListTerm.java2
-rw-r--r--src/lang/Term.java3
-rw-r--r--src/lang/VariableTerm.java14
7 files changed, 30 insertions, 19 deletions
diff --git a/src/lang/ClosureTerm.java b/src/lang/ClosureTerm.java
index 7287fb5..8a21788 100644
--- a/src/lang/ClosureTerm.java
+++ b/src/lang/ClosureTerm.java
@@ -30,6 +30,8 @@ public class ClosureTerm extends Term {
public String toQuotedString() { return toString(); }
+ public String name() { return ""; }
+
/* Object */
/**
* Checks <em>term equality</em> of two terms.
diff --git a/src/lang/DoubleTerm.java b/src/lang/DoubleTerm.java
index 337f5dd..f387213 100644
--- a/src/lang/DoubleTerm.java
+++ b/src/lang/DoubleTerm.java
@@ -38,6 +38,8 @@ public class DoubleTerm extends NumberTerm {
return this.val == ((DoubleTerm)t).value();
}
+ public String name() { return ""; }
+
/**
* @return the <code>boolean</code> whose value is
* <code>convertible(Double.class, type)</code>.
@@ -45,8 +47,6 @@ public class DoubleTerm extends NumberTerm {
*/
public boolean convertible(Class type) { return convertible(Double.class, type); }
- // protected Term copy(Prolog engine) { return new DoubleTerm(val); }
-
/**
* Returns a <code>java.lang.Double</code> corresponds to this <code>DoubleTerm</code>
* according to <em>Prolog Cafe interoperability with Java</em>.
diff --git a/src/lang/IntegerTerm.java b/src/lang/IntegerTerm.java
index f79899f..8aebbd8 100644
--- a/src/lang/IntegerTerm.java
+++ b/src/lang/IntegerTerm.java
@@ -42,7 +42,7 @@ public class IntegerTerm extends NumberTerm {
*/
public boolean convertible(Class type) { return convertible(Integer.class, type); }
- // protected Term copy(Prolog engine) { return new IntegerTerm(val); }
+ public String name() { return ""; }
/**
* Returns a <code>java.lang.Integer</code> corresponds to this <code>IntegerTerm</code>
diff --git a/src/lang/JavaObjectTerm.java b/src/lang/JavaObjectTerm.java
index cedb708..eb5d782 100644
--- a/src/lang/JavaObjectTerm.java
+++ b/src/lang/JavaObjectTerm.java
@@ -15,28 +15,25 @@ package com.googlecode.prolog_cafe.lang;
*/
public class JavaObjectTerm extends Term {
/** Holds a java object that this <code>JavaObjectTerm</code> wraps. */
- protected Object obj = null;
-
- /** Holds a <code>java.lang.Class</code> of object wrapped by this <code>JavaObjectTerm</code>. */
- protected Class clazz = null;
+ protected Object obj;
/** Constructs a new Prolog java-term that wraps the argument object. */
public JavaObjectTerm(Object _obj) {
- if (_obj != null)
- setObject(_obj);
+ obj = _obj;
}
/** Sets the argument object to this <code>JavaObjectTerm</code>. */
public void setObject(Object _obj) {
obj = _obj;
- clazz = _obj.getClass();
}
/** Returns the object wrapped by this <code>JavaObjectTerm</code>. */
public Object object() { return obj; }
/** Returns a <code>java.lang.Class</code> of object wrapped by this <code>JavaObjectTerm</code>. */
- public Class getClazz() { return clazz; }
+ public Class getClazz() { return obj.getClass(); }
+
+ public String name() { return ""; }
public String toQuotedString() { return toString(); }
@@ -52,11 +49,11 @@ public class JavaObjectTerm extends Term {
/**
* Check whether the wrapped object is convertible with the given Java class type.
* @return the <code>boolean</code> whose value is
- * <code>convertible(clazz, type)</code>.
- * @see #clazz
+ * <code>convertible(getClazz(), type)</code>.
+ * @see #getClazz()
* @see Term#convertible(Class, Class)
*/
- public boolean convertible(Class type) { return convertible(clazz, type); }
+ public boolean convertible(Class type) { return convertible(obj.getClass(), type); }
/**
* Returns the object wrapped by this <code>JavaObjectTerm</code>.
@@ -89,7 +86,8 @@ public class JavaObjectTerm extends Term {
/** Returns a string representation of this <code>JavaObjectTerm</code>. */
public String toString() {
- return clazz.getName() + "(" + hashCode() + ")";
+ return obj.getClass().getName()
+ + "(0x" + Integer.toHexString(hashCode()) + ")";
}
/* Comparable */
diff --git a/src/lang/ListTerm.java b/src/lang/ListTerm.java
index 4cb4ae5..b8d51a2 100644
--- a/src/lang/ListTerm.java
+++ b/src/lang/ListTerm.java
@@ -109,6 +109,8 @@ public class ListTerm extends Term {
return true;
}
+ public String name() { return SYM_DOT.name(); }
+
/** Returns the length of this <code>ListTerm</code>. */
public int length() {
int count = 0;
diff --git a/src/lang/Term.java b/src/lang/Term.java
index 9d823e1..cfcd732 100644
--- a/src/lang/Term.java
+++ b/src/lang/Term.java
@@ -102,6 +102,9 @@ public abstract class Term implements Serializable,Comparable<Term> {
*/
public final boolean isClosure() { return this instanceof ClosureTerm; }
+ /** @return the name of this Term, if {@link #isStructure()}. */
+ public abstract String name();
+
/**
* Check whether this object is convertible with the given Java class type.
* @param type the Java class type to compare with.
diff --git a/src/lang/VariableTerm.java b/src/lang/VariableTerm.java
index 65da789..db9f56e 100644
--- a/src/lang/VariableTerm.java
+++ b/src/lang/VariableTerm.java
@@ -22,7 +22,7 @@ public class VariableTerm extends Term implements Undoable {
*/
public VariableTerm() {
val = this;
- timeStamp = Long.MIN_VALUE;
+ timeStamp = Long.MIN_VALUE;
}
/** Constructs a new logical variable so that
@@ -37,7 +37,7 @@ public class VariableTerm extends Term implements Undoable {
}
/** Returns a string representation of this object.*/
- protected String name() { return "_" + Integer.toHexString(hashCode()).toUpperCase(); }
+ protected String variableName() { return "_" + Integer.toHexString(hashCode()).toUpperCase(); }
/* Term */
/**
@@ -130,6 +130,12 @@ public class VariableTerm extends Term implements Undoable {
return false;
}
+ public String name() {
+ if (val == this)
+ return "";
+ return val.dereference().name();
+ }
+
/**
* Returns <code>this</code> if this variable is unbound.
* Otherwise, returns a Java object that corresponds to the dereferenced term:
@@ -152,7 +158,7 @@ public class VariableTerm extends Term implements Undoable {
public String toQuotedString() {
if (val != this)
return val.toQuotedString();
- return name();
+ return variableName();
}
/* Object */
@@ -184,7 +190,7 @@ public class VariableTerm extends Term implements Undoable {
public String toString() {
if (val != this)
return val.toString();
- return name();
+ return variableName();
}
/* Undoable */