2016-08-15 4 views
1

두 개의 Prolog 파일이 있습니다. 조항 및 규칙은 다음과 같습니다 :SWI Prolog Java jpl.PrologException 쿼리가 작동하지 않습니다.

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),!. 

나는 요한은 다음을 통해 자바에있는 곳을 찾기 위해 쿼리를 만들

:

//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) 

그래서 나는 적어도 쿼리가 자바에서 프롤로그에가는 것을 알고있다. 오류의 원인이 될 수있는 아이디어가 있습니까?

참고 : 내 파일 경로가 올바르지 않습니다.

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

표시하는 편집을 사용하면 기대하는 바를 정확히 얻을 수 있습니다. 'X'를 익명 변수'_'로 설정 한 것 같습니다. 그렇지 않습니까? 당신은'isAt (john, _)'을 쿼리하고, 이것은 아무런 도움도주지 않고 성공합니다. –

+0

고마워요, 보리스. 네가 옳아. 나는 다른 것을 시도해야한다. –

+0

그냥 'Y'에있는 것과 같은 무료 변수를 지정하십시오. –

답변

2

나는 평판이 충분하지 않거나 코멘트로 남겨 두겠다. ...

isAt()의 속성이 2이지만 isAt()가 isAt()를 isAt (john, X, Y)와 함께 사용하고있는 것으로 생각됩니다.

+0

다시 2로 변경하면 다음 오류가 발생합니다. 스레드 "main"의 예외 jpl.PrologException : PrologException : 오류 (exist_error (procedure,/(isAt, 2)), 컨텍스트 (:(시스템), ('$ c_call_prolog' 0)) _1)) jpl.Query.get1 (Query.java:336) jpl.Query.hasMoreSolutions (Query.java:258) jpl.Query.hasMoreElements에서 \t에서 \t (Query.java에서 \t : 472) –