0

我正在尝试通过 Java 与 GNU 解释器一起使用 Prolog,但我遇到了一个大问题。

如果对变量进行查询,我总是会返回变量的名称。

所以这里有一些代码:

node(1,3,3).
node(2,14,1).
node(3,19,5).
node(4,10,7).
node(5,15,8).

connection(1,2).
connection(1,2).

connection(2,3).
connection(3,2).

connection(1,3).
connection(3,1).

connection(2,4).
connection(4,2).

connection(4,5).
connection(5,4).

connection(3,5).
connection(5,3).

connection(1,4).
connection(4,1).

route(ID1,ID2,List) :- route(ID1,ID2,List,[]).
route(ID1,ID2,[ID1,ID2],List) :- connection(ID1,ID2),
                          not(member(ID1,List)).
route(ID1,ID2,[ID1|ID1s],List) :-   connection(ID1,ID3),
                                not(member(ID1,List)),      
                                route(ID3,ID2,ID1s,[ID1|List]).

findPath(Sum,Result) :- route(1,1,Result),
                    X is Sum+1,
                    length(Result, X).

此代码在 GNU Prolog 控制台和 SWIPL 中运行良好,但在 Java 中

    VariableTerm lengthTerm = new VariableTerm("Length");
    Term[] arg_list = {new IntegerTerm(5), lengthTerm };
    CompoundTerm(AtomTerm.get("findPath"), arg_list);
    CompoundTerm goalTerm = new CompoundTerm(AtomTerm.get("findPath"), arg_list);

    Goal goal = interpreter.prepareGoal(goalTerm);

    interpreter.runOnce(goalTerm);  
    System.out.println("Length: " + lengthTerm.dereference());      

长度的结果是“长度”。所以我认为内存中没有保存真正的结果。如果我尝试类似if (rc == PrologCode.SUCCESS || rc == PrologCode.SUCCESS_LAST)并捕获 NoAnswerException,则会引发异常。我能做些什么?我试图从几个小时开始解决这个问题。请帮助:)

4

0 回答 0