00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 package jpl;
00029
00030 import java.util.Iterator;
00031 import java.util.Map;
00032 import jpl.fli.Prolog;
00033 import jpl.fli.term_t;
00034
00035
00036
00062
00063
00064
00065 public class Variable extends Term {
00066
00067
00068
00069 private static long n = 0;
00070 public final String name;
00071 protected transient term_t term_ = null;
00072 protected transient int index;
00073
00074
00075
00082 public Variable(String name) {
00083 if (name == null) {
00084 throw new JPLException("constructor jpl.Variable(name): name cannot be null");
00085 }
00086 if (!isValidName(name)) {
00087 throw new JPLException("constructor jpl.Variable(name): name cannot be empty String");
00088 }
00089 this.name = name;
00090 }
00095 public Variable() {
00096 this.name = "_" + Long.toString(n++);
00097 }
00098
00099
00100
00107 public Term[] args() {
00108 throw new JPLException("jpl.Variable#args(): call is improper");
00109 }
00110 public boolean hasFunctor(String name, int arity) {
00111 throw new JPLException("jpl.Variable#hasFunctor(): term is not sufficiently instantiated");
00112 }
00113 public boolean hasFunctor(int value, int arity) {
00114 throw new JPLException("jpl.Variable#hasFunctor(): term is not sufficiently instantiated");
00115 }
00116 public boolean hasFunctor(double value, int arity) {
00117 throw new JPLException("jpl.Variable#hasFunctor(): term is not sufficiently instantiated");
00118 }
00119 public Object jrefToObject() {
00120 throw new JPLException("jpl.Variable#jrefToObject(): term is not a jref");
00121 }
00127 public final String name() {
00128 return this.name;
00129 }
00135 public final int type() {
00136 return Prolog.VARIABLE;
00137 }
00143 public String typeName() {
00144 return "Variable";
00145 }
00151 public String toString() {
00152 return this.name;
00153 }
00160 public final boolean equals(Object obj) {
00161 return obj instanceof Variable && !this.name.equals("_") && this.name.equals(((Variable) obj).name);
00162 }
00163
00169 public final Term arg(int i) {
00170 throw new JPLException("jpl.Variable#arg(int) is undefined");
00171 }
00172
00173
00174
00175
00182 private boolean isValidName(String s) {
00183 if (s == null) {
00184 throw new java.lang.NullPointerException();
00185 }
00186 int len = s.length();
00187 if (len == 0) {
00188 throw new JPLException("invalid variable name");
00189 }
00190 char c = s.charAt(0);
00191 if (!(c == '_' || c >= 'A' && c <= 'Z')) {
00192 return false;
00193 }
00194 for (int i = 1; i < len; i++) {
00195 c = s.charAt(i);
00196 if (!(c == '_' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || c >= '0' && c <= '9')) {
00197 return false;
00198 }
00199 }
00200 return true;
00201 }
00202
00203
00204
00211 public String debugString() {
00212 return "(Variable " + toString() + ")";
00213 }
00214
00215
00216
00231 protected final void put(Map varnames_to_vars, term_t term) {
00232 term_t var;
00233
00234 if (this.name.equals("_") || (var = (term_t) varnames_to_vars.get(this.name)) == null) {
00235 this.term_ = term;
00236 this.index = varnames_to_vars.size();
00237 Prolog.put_variable(term);
00238 if (!this.name.equals("_")) {
00239 varnames_to_vars.put(this.name, term);
00240 }
00241 } else {
00242 this.term_ = var;
00243 Prolog.put_term(term, var);
00244 }
00245 }
00246
00247
00248
00260 protected static Term getTerm1(Map vars_to_Vars, term_t var) {
00261 for (Iterator i = vars_to_Vars.keySet().iterator(); i.hasNext();) {
00262 term_t varX = (term_t) i.next();
00263 if (Prolog.compare(varX, var) == 0) {
00264 return (Term) vars_to_Vars.get(varX);
00265 }
00266 }
00267
00268 Variable Var = new Variable();
00269 Var.term_ = var;
00270 vars_to_Vars.put(var, Var);
00271 return Var;
00272 }
00273
00274
00275
00284 protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) {
00285
00286
00287 if (tellThem() && varnames_to_Terms.get(this.name) == null) {
00288 varnames_to_Terms.put(this.name, Term.getTerm(vars_to_Vars, this.term_));
00289 }
00290 }
00291
00292
00293
00294 private final boolean tellThem() {
00295 return !(this.name.equals("_") || jpl.JPL.modeDontTellMe && this.name.charAt(0) == '_');
00296
00297 }
00298 }
00299