SWI Prolog Java jpl.PrologException查询不工作
我有两个Prolog文件。条款和规则如下:SWI Prolog Java jpl.PrologException查询不工作,java,prolog,jpl,Java,Prolog,Jpl,我有两个Prolog文件。条款和规则如下: static void test_1() { Variable X = new Variable(); Term args[] = { new Atom( "john" ), X }; Query query = new Query( "isAt", args ); Syste
static void
test_1()
{
Variable X = new Variable();
Term args[] = {
new Atom( "john" ),
X
};
Query query =
new Query(
"isAt",
args );
System.out.println( "iSAt(john, X) = " + query.query() );
}
public static void main(String[] args) throws IOException {
//include the prolog file with clauses to test
File clauseFile = new File ("G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl");
File ruleFile = new File ("G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl");
String clausePath = clauseFile.getAbsolutePath();
String rulePath = ruleFile.getAbsolutePath();
System.out.println("Clause file path: " + clausePath);
System.out.println("Rule file path: " + rulePath);
String t1 = "consult('" + "G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl"+"').";
String t2 = "consult('" + "G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl"+"').";
/*Scanner scan = new Scanner(ruleFile);
while (scan.hasNextLine()){
System.out.println(scan.nextLine());
}*/
jpl.JPL.init();
Term consult_arg[] = {
new Atom( "G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl")
};
Query consult_query =
new Query(
"consult",
consult_arg );
Term consult_arg2[] = {
new Atom( "G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl")
};
Query consult_query2 =
new Query(
"consult",
consult_arg2);
boolean consulted = consult_query.query()&& consult_query2.query();
if ( !consulted ){
System.err.println( "Consult failed" );
System.exit( 1 );
}
test_1();
Variable X = new Variable("_");
Variable Y = new Variable();
Query q = new Query("isAt",new Term[]{new Atom("john"),X});
while (q.hasMoreElements()) {
Hashtable binding = (Hashtable) q.nextElement();
Term t = (Term) binding.get(X);
System.out.println(t);
}
System.out.println(q.toString());
}
第3.pl条
get(mary,milk).
go(sandra,kitchen,1).
get(john,football).
go(john,hallway,1).
go(mary,garden,1).
go(john,kitchen,2).
rules.pl
/* X = person Y=location T,T2= time
This rule finds last location of a person */
isAt(X,Y) :- go(X, Y, T), \+ (go(X,_,T2), T2 > T).
/* This rule finds the last location of an object */
whereIs(Q,R) :- findall(R,(get(P,Q,I),go(P,R,_)),L), last(L,R),!.
当我通过以下方式创建查询以找出John在Java中的位置时:
//include the prolog file with clauses to test
File clauseFile = new File ("clauses_qa2.pl");
File ruleFile = new File ("rules.pl");
String clausePath = clauseFile.getAbsolutePath();
String rulePath = ruleFile.getAbsolutePath();
System.out.println("Clause file path: " + clausePath);
System.out.println("Rule file path: " + rulePath);
String t1 = "consult('" + clausePath + "').";
String t2 = "consult('" + rulePath + "').";
jpl.JPL.init();
Query q1 = new Query(t1);
Query q2 = new Query(t2);
Variable X = new Variable("_");
Variable Y = new Variable();
Query q = new Query("isAt",new Term[]{new Atom("john"),X,Y});
while (q.hasMoreElements()) {
Hashtable binding = (Hashtable) q.nextElement();
Term t = (Term) binding.get(X);
System.out.println(t);
}
System.out.println(q.toString());
我得到以下错误:
Exception in thread "main" jpl.PrologException: PrologException: error(existence_error(procedure, /(isAt, 3)), context(:(system, /('$c_call_prolog', 0)), _1))
at jpl.Query.get1(Query.java:336)
at jpl.Query.hasMoreSolutions(Query.java:258)
at jpl.Query.hasMoreElements(Query.java:472)
但是,如果我删除while循环并简单地打印查询,我将从Prolog获得以下响应:
Clause file path: G:\Natural Language Final Project\PrologTest\clauses_qa2.pl
Rule file path: G:\Natural Language Final Project\PrologTest\rules.pl
isAt( john, _, _0 )
所以我知道,至少查询是从Java进入Prolog的。你知道是什么导致了这个错误吗
注:
结果是我的文件路径不正确。
更改代码以创建查询,如下所示:
static void
test_1()
{
Variable X = new Variable();
Term args[] = {
new Atom( "john" ),
X
};
Query query =
new Query(
"isAt",
args );
System.out.println( "iSAt(john, X) = " + query.query() );
}
public static void main(String[] args) throws IOException {
//include the prolog file with clauses to test
File clauseFile = new File ("G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl");
File ruleFile = new File ("G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl");
String clausePath = clauseFile.getAbsolutePath();
String rulePath = ruleFile.getAbsolutePath();
System.out.println("Clause file path: " + clausePath);
System.out.println("Rule file path: " + rulePath);
String t1 = "consult('" + "G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl"+"').";
String t2 = "consult('" + "G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl"+"').";
/*Scanner scan = new Scanner(ruleFile);
while (scan.hasNextLine()){
System.out.println(scan.nextLine());
}*/
jpl.JPL.init();
Term consult_arg[] = {
new Atom( "G:\\Natural Language Final Project\\PrologTest\\src\\clauses_qa2.pl")
};
Query consult_query =
new Query(
"consult",
consult_arg );
Term consult_arg2[] = {
new Atom( "G:\\Natural Language Final Project\\PrologTest\\src\\rules.pl")
};
Query consult_query2 =
new Query(
"consult",
consult_arg2);
boolean consulted = consult_query.query()&& consult_query2.query();
if ( !consulted ){
System.err.println( "Consult failed" );
System.exit( 1 );
}
test_1();
Variable X = new Variable("_");
Variable Y = new Variable();
Query q = new Query("isAt",new Term[]{new Atom("john"),X});
while (q.hasMoreElements()) {
Hashtable binding = (Hashtable) q.nextElement();
Term t = (Term) binding.get(X);
System.out.println(t);
}
System.out.println(q.toString());
}
结果如下:
iSAt(john, X) = true
null
isAt( john, _ )
这比编译器错误要好,但答案应该是:
isAt(john,X)
X= kitchen
我没有足够的声誉,否则我会留下这个评论
我怀疑问题在于isAt()的算术数是2,但查询使用的是isAt()和算术数3:isAt(john,X,Y)。将算术数更改回2会产生以下错误:线程“main”jpl中的异常。PrologException:PrologException:error(存在错误(过程,/(isAt,2)),上下文(:(系统,/($c_调用_prolog',0)),_1))在jpl.Query.get1(Query.java:336)和jpl.Query.hasMoreSolutions(Query.java:258)中,在jpl.Query.hasMoreElements(Query.java:472)中,通过显示的编辑,您可以得到人们所期望的结果。似乎您已将
X
设置为匿名变量\u
,对吗?你查询了isAt(john,41;,结果没有做任何有用的事情,就成功了。谢谢你,Boris。你说得对。我必须尝试其他方法。只要给它一个自由变量,就像你在Y
中看到的一样,我想?我试过了。由于某些原因,代码示例中的变量无法正常工作,“Variable X=new Variable();”应该生成一个自由变量,该变量可以使用枚举和hashmap处理所有可能的解决方案。在示例中,这是有效的。但当我这样做时,它仍然返回null。这将是一个有趣的问题。据我所见,您似乎对查询进行了评估,结果成功,打印出query.query()
,它将为您提供true
,但没有绑定,您将得到null
。您需要至少显示完整的代码,以及适当的自由变量和您得到的输出。