を働いていません。条項および規則を次のようにSWI PrologのJavaのjpl.PrologExceptionクエリは、私は2つのプロローグファイルを持っている
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),!.
私はジョンが次を経由して、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)
しかし、:
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からプロローグになっていることを知っています。何が原因でエラーが発生する可能性がありますか?
注: は私のファイルパスが正しくなかったことが判明。次の出力で
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
を は、次のようにクエリを作成するためのコードを変更します
あなたが表示する編集では、何を期待するのかが正確にわかります。あなたの 'X'を匿名変数' _'にセットしたようですね、そうではありませんか?あなたは 'isAt(john、_)'を照会し、これは役に立たない何もせずに成功します。 –
ありがとう、ボリス。あなたが正しい。私は他の何かを試さなければならない。 –
ちょうどあなたが 'Y'に持っているような無料の変数を与えるでしょうか? –