prologjpl

accessing prolog list term


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?


Solution

  • 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