1

I've been using jpl for calling prolog from java. I'm using the following code in java to get the value of X from prolog.

String t4 = "myNumber(X)";
Query q4 = new Query(t4);
System.out.println( "first solution of " + t4 + ": X = " + q4.oneSolution().get("X"));

And my solution is--

first solution of myNumber(X): X = '.'(2, [])--which is true.

What i wanted to do now is to obtain the value 2 from the solution and double the number. Can anyone help me how to handle that?

Venkata
  • 31
  • 2
  • 7
  • You should consider another title for the question. At least I don't see any connection to JPanel and JMenuBar. – chs Oct 11 '11 at 15:38

1 Answers1

0

oneSolution() returns a hashtable of variablename-to-term bindings (they say). Then you have to inspect to the term (untested):

Term listTerm = q4.oneSolution().get("X");
Term firstListItem = listTerm.arg(1);
double value = firstListItem.doubleValue(); // alternatively, use intValue() or so

Also check Term's documentation.

Edit: fixed mistakes

chs
  • 652
  • 4
  • 17
  • I meant listTerm.arg(1), of course. Remember you should accept answers of your questions. You haven't accepted any of your old questions. You won't get many answers unless you do that. – chs Oct 12 '11 at 13:15
  • i'm new to this site..so did not know that i should accept answers..sorry for that – Venkata Oct 12 '11 at 19:16
  • In the same way if the solution is **myNumber(X): X = '.'(2, '.'(3, []))** how should i pass the arguments to obtain all the values 2,3, etc... – Venkata Oct 12 '11 at 20:30
  • `void toList(Term t, List xs) { if (t.arity() == 2) { xs.add(t.arg(1).doubleValue()); toList(t.arg(2), xs); } }` or you might wanna use Term.toTermArray(), I suspect – chs Oct 13 '11 at 13:18
  • i've tried using Term.toTermArray() but was not successful. My part of code is `public Term[] toTermArray(Object k1) { try { int len = this.listLength(k1); Term[] ts = new Term[len]; Term t = this; for (int i = 0; i < len; i++) { ts[i] = t.arg(1); t = t.arg(2); } return ts; } catch (JPLException e) { throw new JPLException("Term.toTermArray: term is not a proper list"); } }` i'm calling this function by 'toTermArray(k);` where k is a object which contains the list. Can you please help me with this – Venkata Oct 20 '11 at 06:29