我有两个Prolog文件。条款和规则如下:
clauses.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),!.
当我创建一个Query以通过以下方式找出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。关于什么可能导致错误的任何想法?
注意: 原来我的文件路径不正确。 更改代码以创建Query,如下所示:
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
答案 0 :(得分:2)
我没有足够的声誉,或者我将此作为评论......
我怀疑问题是isAt()的arity是2,但是查询使用isAt()和arity 3:isAt(john,X,Y)。