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.Hashtable;
00031 import java.util.Map;
00032
00033
00034
00056 public final class Util {
00057
00058
00066 public static Term termArrayToList(Term[] terms) {
00067 Term list = new Atom("[]");
00068
00069 for (int i = terms.length - 1; i >= 0; --i) {
00070 list = new Compound(".", new Term[] { terms[i], list });
00071 }
00072 return list;
00073 }
00074
00081 public static Term[] bindingsToTermArray(Map varnames_to_Terms) {
00082 Term[] ts = new Term[varnames_to_Terms.size()];
00083
00084 for (java.util.Iterator i = varnames_to_Terms.keySet().iterator(); i.hasNext();) {
00085 Variable k = (Variable) i.next();
00086 ts[k.index] = (Term) (varnames_to_Terms.get(k));
00087 }
00088 return ts;
00089 }
00090
00091
00092
00099 public static String toString(Map varnames_to_Terms) {
00100 if (varnames_to_Terms == null) {
00101 return "[no solution]";
00102 }
00103 java.util.Iterator varnames = varnames_to_Terms.keySet().iterator();
00104
00105 String s = "Bindings: ";
00106 while (varnames.hasNext()) {
00107 String varname = (String) varnames.next();
00108 s += varname + "=" + varnames_to_Terms.get(varname).toString() + "; ";
00109 }
00110 return s;
00111 }
00112
00113
00114
00122 public static Map namevarsToMap(Term nvs) {
00123
00124 try {
00125 Map vars_to_Vars = new Hashtable();
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 while (nvs.hasFunctor(".", 2) && nvs.arg(1).hasFunctor("=", 2)) {
00137
00138 vars_to_Vars.put(((Variable)nvs.arg(1).arg(2)).term_, new Variable(nvs.arg(1).arg(1).name()));
00139 nvs = nvs.arg(2);
00140 }
00141
00142
00143 return vars_to_Vars;
00144 } catch (java.lang.ClassCastException e) {
00145 return null;
00146 }
00147 }
00148
00149
00150
00159 public static Term textToTerm(String text) {
00160
00161 Query q = new Query(new Compound("atom_to_term", new Term[] { new Atom(text), new Variable("Term"), new Variable("NVdict")}));
00162 q.open();
00163 Map s = q.getSubstWithNameVars();
00164 if (s != null) {
00165 q.close();
00166 return (Term) s.get("Term");
00167 } else {
00168 return null;
00169 }
00170 }
00171
00172
00183 public static Term textParamsToTerm(String text, Term[] params) {
00184 return Util.textToTerm(text).putParams(params);
00185 }
00186
00194 public static Term stringArrayToList(String[] a) {
00195 Term list = new Atom("[]");
00196 for (int i = a.length - 1; i >= 0; i--) {
00197 list = new Compound(".", new Term[]{new Atom(a[i]), list});
00198 }
00199 return list;
00200 }
00201
00209 public static Term intArrayToList(int[] a) {
00210 Term list = new Atom("[]");
00211 for (int i = a.length - 1; i >= 0; i--) {
00212 list = new Compound(".", new Term[]{new jpl.Integer(a[i]), list});
00213 }
00214 return list;
00215 }
00216
00224 public static Term intArrayArrayToList(int[][] a) {
00225 Term list = new Atom("[]");
00226 for (int i = a.length - 1; i >= 0; i--) {
00227 list = new Compound(".", new Term[]{intArrayToList(a[i]), list});
00228 }
00229 return list;
00230 }
00231 public static int listToLength(Term t) {
00232 int length = 0;
00233 Term head = t;
00234 while (head.hasFunctor(".", 2)) {
00235 length++;
00236 head = head.arg(2);
00237 }
00238 return (head.hasFunctor("[]", 0) ? length : -1);
00239 }
00245 public static Term[] listToTermArray(Term t) {
00246 try {
00247 int len = t.listLength();
00248 Term[] ts = new Term[len];
00249
00250 for (int i = 0; i < len; i++) {
00251 ts[i] = t.arg(1);
00252 t = t.arg(2);
00253 }
00254 return ts;
00255 } catch (JPLException e) {
00256 throw new JPLException("Util.listToTermArray: term is not a proper list");
00257 }
00258 }
00259
00260 public static String[] atomListToStringArray( Term t){
00261 int n = listToLength(t);
00262 String[] a;
00263 if ( n<0){
00264 return null;
00265 } else {
00266 a = new String[n];
00267 }
00268 int i = 0;
00269 Term head = t;
00270 while ( head.hasFunctor(".", 2)){
00271 Term x = head.arg(1);
00272 if ( x.isAtom()){
00273 a[i++]=x.name();
00274 } else {
00275 return null;
00276 }
00277 head = head.arg(2);
00278 }
00279 return (head.hasFunctor("[]", 0) ? a : null );
00280 }
00281 }