package jp.ac.kobe_u.cs.prolog.lang; import java.io.Serializable; /** * The superclass of classes for term structures. * The subclasses of Term must override * the unify method. * * @author Mutsunori Banbara (banbara@kobe-u.ac.jp) * @author Naoyuki Tamura (tamura@kobe-u.ac.jp) * @version 1.0 */ public abstract class Term implements Serializable,Comparable { /** Holds an integer value 0. */ public static final int EQUAL = 0; /** Holds an integer value 1. */ public static final int AFTER = 1; /** Holds an integer value -1. */ public static final int BEFORE = -1; /** * Checks whether the argument term is unified with this one. * @param t the term to be unified with. * @param trail Trail Stack. * @return true if succeeds, otherwise false. */ abstract public boolean unify(Term t, Trail trail); /** * Check whether this term is a logical variable. * @return true if this instanceof VariableTerm, * otherwise false. * @see VariableTerm */ public boolean isVariable() { return this instanceof VariableTerm; } /** * Check whether this term is an integer. * @return true if this instanceof IntegerTerm, * otherwise false. * @see IntegerTerm */ public boolean isInteger() { return this instanceof IntegerTerm; } /** * Check whether this term is a float. * @return true if this instanceof DoubleTerm, * otherwise false. * @see DoubleTerm */ public boolean isDouble() { return this instanceof DoubleTerm; } /** * Check whether this term is a number. * @return true if this instanceof IntegerTerm || this instanceof DoubleTerm, * otherwise false. * @see IntegerTerm * @see DoubleTerm */ public boolean isNumber() { return ((this instanceof IntegerTerm) || (this instanceof DoubleTerm)); } /** * Check whether this term is an atom. * @return true if this instanceof SymbolTerm, * otherwise false. * @see SymbolTerm */ public boolean isSymbol() { return this instanceof SymbolTerm; } /** Check whether this term is an empty list. */ public boolean isNil() { return Prolog.Nil.equals(this); } /** * Check whether this term is a list structure. * @return true if this instanceof ListTerm, * otherwise false. * @see ListTerm */ public boolean isList() { return this instanceof ListTerm; } /** * Check whether this term is a compound term. * @return true if this instanceof StructureTerm, * otherwise false. * @see StructureTerm */ public boolean isStructure() { return this instanceof StructureTerm; } /** * Check whether this term is a java term. * @return true if this instanceof JavaObjectTerm, * otherwise false. * @see JavaObjectTerm */ public boolean isJavaObject() { return this instanceof JavaObjectTerm; } /** * Check whether this term is a closure term. * @return true if this instanceof ClosureTerm, * otherwise false. * @see ClosureTerm */ public boolean isClosure() { return this instanceof ClosureTerm; } /** * Check whether this object is convertible with the given Java class type. * @param type the Java class type to compare with. * @return true if this is convertible with * type. Otherwise false. * @see #convertible(Class, Class) */ public boolean convertible(Class type) { return convertible(getClass(), type); } /** Returns a copy of this object. */ protected Term copy(Prolog engine) { return this; } /** Returns the dereference value of this term. */ public Term dereference() { return this; } /** * Check whether this term is a ground term. * @return true if ground, otherwise false. */ public boolean isGround() { return true; } /** * Returns a Java object that corresponds to this term * if defined in Prolog Cafe interoperability with Java. * Otherwise, returns this. * @return a Java object if defined in Prolog Cafe interoperability with Java, * otherwise this. */ public Object toJava() { return this; } /** Returns a quoted string representation of this term. */ public String toQuotedString() { return this.toString(); } /** * Check whether there is a widening conversion from from to to. */ protected static boolean convertible(Class from, Class to) { if (from == null) return ! to.isPrimitive(); if (to.isAssignableFrom(from)) { return true; } else if (to.isPrimitive()) { if (from.equals(Boolean.class)) { return to.equals(Boolean.TYPE); } else if (from.equals(Byte.class)) { return to.equals(Byte.TYPE) || to.equals(Short.TYPE) || to.equals(Integer.TYPE) || to.equals(Long.TYPE) || to.equals(Float.TYPE) || to.equals(Double.TYPE); } else if (from.equals(Short.class)) { return to.equals(Short.TYPE) || to.equals(Integer.TYPE) || to.equals(Long.TYPE) || to.equals(Float.TYPE) || to.equals(Double.TYPE); } else if (from.equals(Character.class)) { return to.equals(Character.TYPE) || to.equals(Integer.TYPE) || to.equals(Long.TYPE) || to.equals(Float.TYPE) || to.equals(Double.TYPE); } else if (from.equals(Integer.class)) { return to.equals(Integer.TYPE) || to.equals(Long.TYPE) || to.equals(Float.TYPE) || to.equals(Double.TYPE); } else if (from.equals(Long.class)) { return to.equals(Long.TYPE) || to.equals(Float.TYPE) || to.equals(Double.TYPE); } else if (from.equals(Float.class)) { return to.equals(Float.TYPE) || to.equals(Double.TYPE); } else if (from.equals(Double.class)) { return to.equals(Double.TYPE); } } return false; } /** Checks whether a given object is an instance of Prolog term. */ public static boolean instanceOfTerm(Object obj) { return obj instanceof VariableTerm || obj instanceof IntegerTerm || obj instanceof DoubleTerm || obj instanceof SymbolTerm || obj instanceof ListTerm || obj instanceof StructureTerm || obj instanceof JavaObjectTerm || obj instanceof ClosureTerm; } }