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.Map;
00031
00032 import jpl.fli.IntHolder;
00033 import jpl.fli.Prolog;
00034 import jpl.fli.StringHolder;
00035 import jpl.fli.term_t;
00036
00037
00038
00080 public class Compound extends Term {
00081
00082
00083
00087 protected final String name;
00091 protected final Term[] args;
00092
00093
00094
00103 protected Compound(String name) {
00104 if (name == null) {
00105 throw new JPLException("jpl.Atom: cannot construct with null name");
00106 }
00107 this.name = name;
00108 this.args = new Term[] {};
00109 }
00116 public Compound(String name, Term[] args) {
00117 if (name == null) {
00118 throw new JPLException("jpl.Compound: cannot construct with null name");
00119 }
00120 if (args == null) {
00121 throw new JPLException("jpl.Compound: cannot construct with null args");
00122 }
00123 if (args.length == 0) {
00124 throw new JPLException("jpl.Compound: cannot construct with zero args");
00125 }
00126 this.name = name;
00127 this.args = args;
00128 }
00137 public Compound(String name, int arity) {
00138 if (name == null) {
00139 throw new JPLException("jpl.Compound: cannot construct with null name");
00140 }
00141 if (arity < 0) {
00142 throw new JPLException("jpl.Compound: cannot construct with negative arity");
00143 }
00144 this.name = name;
00145 this.args = new Term[arity];
00146 }
00147
00148
00149
00156 public final Term arg(int i) {
00157 return args[i - 1];
00158 }
00164 public final boolean hasFunctor(String name, int arity) {
00165 return name.equals(this.name) && arity == args.length;
00166 }
00172 public boolean isJFalse() {
00173 return hasFunctor("@", 1) && arg(1).hasFunctor("false", 0);
00174 }
00180 public boolean isJTrue() {
00181 return hasFunctor("@", 1) && arg(1).hasFunctor("true", 0);
00182 }
00188 public boolean isJNull() {
00189 return hasFunctor("@", 1) && arg(1).hasFunctor("null", 0);
00190 }
00196 public boolean isJVoid() {
00197 return hasFunctor("@", 1) && arg(1).hasFunctor("void", 0);
00198 }
00204 public boolean isJObject() {
00205 return hasFunctor("@", 1) && arg(1).isAtom() && JPL.isTag(arg(1).name());
00206 }
00212 public boolean isJRef() {
00213 return isJObject() || isJNull();
00214 }
00215 public Object jrefToObject() {
00216 if (this.isJObject()) {
00217 return Prolog.tag_to_object(arg(1).name());
00218 } else if (this.isJNull()) {
00219 return null;
00220 } else {
00221 throw new JPLException("Term.jrefToObject: term is not a JRef");
00222 }
00223 }
00229 public final String name() {
00230 return name;
00231 }
00237 public final int arity() {
00238 return args.length;
00239 }
00247 public String toString() {
00248 return quotedName() + (args.length > 0 ? "(" + Term.toString(args) + ")" : "");
00249 }
00257 public final boolean equals(Object obj) {
00258 return (this == obj || (obj instanceof Compound && name.equals(((Compound) obj).name) && Term.terms_equals(args, ((Compound) obj).args)));
00259 }
00265 public int type() {
00266 return Prolog.COMPOUND;
00267 }
00273 public String typeName(){
00274 return "Compound";
00275 }
00284 public void setArg(int i, Term arg) {
00285 if (i <= 0) {
00286 throw new JPLException("jpl.Compound#setArg: bad (non-positive) argument index");
00287 }
00288 if (i > args.length) {
00289 throw new JPLException("jpl.Compound#setArg: bad (out-of-range) argument index");
00290 }
00291 if (arg == null) {
00292 throw new JPLException("jpl.Compound#setArg: bad (null) argument");
00293 }
00294 args[i - 1] = arg;
00295 }
00296
00297
00298
00305 protected String quotedName() {
00306 return ((Atom) (new Query(new Compound("sformat", new Term[] { new Variable("S"), new Atom("~q"), new Compound(".", new Term[] { new Atom(this.name), new Atom("[]") }) }))).oneSolution().get(
00307 "S")).name;
00308 }
00309
00310
00311
00318 public final Term[] args() {
00319 return args;
00320 }
00327 public final Term arg0(int i) {
00328 return args[i];
00329 }
00336 public String debugString() {
00337 return "(Compound " + name + " " + Term.debugString(args) + ")";
00338 }
00339
00340
00341
00353 protected final void put(Map varnames_to_vars, term_t term) {
00354 Prolog.cons_functor_v(term, Prolog.new_functor(Prolog.new_atom(name), args.length), Term.putTerms(varnames_to_vars, args));
00355 }
00356
00357
00358
00369 protected static Term getTerm1(Map varnames_to_vars, term_t term) {
00370
00371 StringHolder name_holder = new StringHolder();
00372 IntHolder arity_holder = new IntHolder();
00373 Prolog.get_name_arity(term, name_holder, arity_holder);
00374 Term args[] = new Term[arity_holder.value];
00375 for (int i = 1; i <= arity_holder.value; i++) {
00376 term_t termi = Prolog.new_term_ref();
00377 Prolog.get_arg(i, term, termi);
00378 args[i - 1] = Term.getTerm(varnames_to_vars, termi);
00379 }
00380 return new Compound(name_holder.value, args);
00381 }
00382
00383
00384
00391 protected final void getSubst(Map varnames_to_Terms, Map vars_to_Vars) {
00392 Term.getSubsts(varnames_to_Terms, vars_to_Vars, args);
00393 }
00394 public boolean hasFunctor(int value, int arity) {
00395 return false;
00396 }
00397 public boolean hasFunctor(double value, int arity) {
00398 return false;
00399 }
00400 }
00401