1
import java.util.Hashtable;
import jpl.*;
import jpl.Query;

public class Family {
    public static void main( String argv[] ) {

      String t1 = "consult('C:/Users/Kostas/Desktop/family.pl')";
      Query  q1 = new Query(t1);

      System.out.println( t1 + " " + (q1.hasSolution() ? "succeeded" : "failed"));

     //---

      String t2 = "child_of(joe, ralf)";
      Query  q2 = new Query(t2);

      System.out.println(q2.hasSolution());

     //---

      String t3 = "descendent_of(X, ralf)";
      Query  q3 = new Query(t3);

      java.util.Hashtable[] s3 = q3.allSolutions();

      System.out.println("all solutions of " + t3);

      for (int i = 0; i < s3.length; i++) {
        System.out.println("X = " + s3[i].get("X"));
      }  
   }
}

输出:

consult('C:/Users/Kostas/Desktop/family.pl') succeeded
true
all solutions of descendent_of(X, ralf)   
X = null
X = null
X = null

% 家庭.pl

child_of(joe, ralf).
child_of(mary, joe).
child_of(steve, joe).

descendent_of(X, Y) :-
   child_of(X, Y).
descendent_of(X, Y) :-
   child_of(Z, Y),
   descendent_of(X, Z).

你好,

我在 Eclipse 上运行上面的代码。通常没关系,除非我使用变量。在上述情况下,它应该打印出:

X = joe
X = mary
X = steve

相反,它打印'X = null'。

之前在这里问过同样的问题: https ://stackoverflow.com/questions/30495077/swi-prolog-returns-null-but-hassolutions-true-and-also-allsolutions-is-true

问这个问题的人说这是由于jpl.jar 文件。如果这是问题所在,我在哪里可以找到合适的 jpl.jar 版本来解决我的问题?请注意,最近我下载了最新版本的 SWI-Prolog,其中可能还包括最新版本的 jpl.jar 库文件。这是我使用的版本。

谢谢。

4

0 回答 0