2010-09-18 76 views
1

我下載了tuprolog,我無法獲得以下查詢的結果,而我在PROL ide中得到答案..有人可以幫忙嗎?在tuProlog中執行查詢時出錯

verb(admit-1). 
verb(work-4). 
nsubjpass(admit-1, patient-2). 
agent(admit-1, doctor-3). 
nsubj(work-4, who-5). 
aux(work-4, be-6). 
rcmod(doctor-3, work-4). 
prep_in(work-4, clinic-7). 


aggregation('part of'). 
aggregation('belongs to'). 
aggregation('subdivision of'). 
aggregation('have'). 
aggregation('contain'). 
aggregation('comprise'). 
aggregation('include'). 
aggregation('define'). 
aggregation('consist of'). 
aggregation('compose of'). 
aggregation('denote by'). 
aggregation('identify by'). 
aggregation('make up of'). 
aggregation('record with'). 

attribute('have'). 
attribute('contain'). 
attribute('comprise'). 
attribute('include'). 
attribute('define'). 
attribute('consist of'). 
attribute('compose of'). 
attribute('denote by'). 
attribute('identify by'). 
attribute('make up of'). 
attribute('record with'). 

generalization('be'). 
generalization('kind of'). 
generalization('type of'). 
generalization('classify into'). 
generalization('consider'). 

object(X) :- noun(X). 
relation(X) :- verb(X). 
rel(X,Y) :- nsubjpass(X,Y). 
rel(X,Y) :- agent(X,Y). 
rel(X,Y) :- nsubj(X,Y). 
rel(X,Y) :- aux(X,Y). 
rel(X,Y) :- rcmod(X,Y). 
rel(X,Y) :- prep_in(X,Y). 

associatedWith(X,Y) :- rel(X,Y). 
associatedWith(X,Y,Z) :- verb(Y),associatedWith(X,Y), associatedWith(Y,Z). 
associatedWith(X,Y,Z) :- verb(X),associatedWith(X,Y), associatedWith(X,Z). 
rel_aggregation(X,Y,R):-rel(X,Y).aggregation(R). 

?- associatedWith(X,Y,Z). 

我需要處理這些類型的關係,並得到輸出在Java中再次處理

誰能告訴我如何整合序言和Java?

編輯: 我通過實現代碼

這個代碼適用於associatedWith(X,Y)得到這個錯誤tuprolog。即有2個參數,但不起作用,當我給與3關聯與(X,Y,Z)。做什麼..請幫助我得到這個例外

java.lang.ClassCastException: alice.tuprolog.Var cannot be cast to alice.tuprolog.Struct 
     at alice.tuprolog.lib.BasicLibrary.agent_2(Unknown Source) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
     at java.lang.reflect.Method.invoke(Method.java:597) 
     at alice.tuprolog.PrimitiveInfo.evalAsPredicate(Unknown Source) 
     at alice.tuprolog.StateGoalEvaluation.doJob(Unknown Source) 
     at alice.tuprolog.Engine.run(Unknown Source) 
     at alice.tuprolog.EngineManager.solve(Unknown Source) 
     at alice.tuprolog.Prolog.solve(Unknown Source) 
     at javaapplication9.Main.main(Main.java:26) 
Exception in thread "main" alice.tuprolog.NoSolutionException 
     at alice.tuprolog.SolveInfo.getSolution(Unknown Source) 
     at javaapplication9.Main.main(Main.java:28) 
Java Result: 1 

回答

5

主要tuProlog對象,您需要在Java中處理的:Theory,你可以用它來代表一個Prolog程序; Prolog,這是你會問的解釋器;和SolveInfo,其中包含解釋器找到的每個解決方案的數據。

假設您的Prolog程序(即您發佈的所有代碼除最後一行外)都包含在名爲theory.pl的文件中。粗略地說(即模進口和例外),你需要遵循的過程類似於:

Prolog engine = new Prolog(); 
Theory theory = new Theory(new FileInputStream("theory.pl")); 
engine.setTheory(theory); 
SolveInfo solution = engine.solve("associatedWith(X, Y, Z)."); 
if (solution.isSuccess()) { 
    System.out.println(solution.getTerm("X")); 
    System.out.println(solution.getTerm("Y")); 
    System.out.println(solution.getTerm("Z")); 
} 

如果您的查詢可能產生一個以上的解決方案,使用while循環來測試的開放選擇點的存在性Prolog.hasOpenAlternatives並用Prolog.solveNext檢索另一個解決方案。

手冊中的第8章(您可以在任何tuProlog發行版​​的doc子目錄中找到PDF文件)包含大量關於與Java代碼(參見第8.4節)與Prolog交互複雜性增加的小例子。

+0

此代碼適用於associatedWith(X,Y)。即有2個參數,但不起作用,當我給與3關聯與(X,Y,Z)。該怎麼辦..請幫助 – karthi 2010-09-18 13:31:04

+0

已在我的主要問題中添加了例外..請說說做 – karthi 2010-09-18 13:39:28

3

agent/2是tuProlog中的內置謂詞 - 您可以嘗試重命名您的版本。

+0

謝謝:)它的工作 – karthi 2010-09-18 15:08:23

+0

我被撕裂。這個答案顯然解決了OP的問題,但它沒有被標記爲正確的。另一個答案幫助我理解了tuProlog .. upvotes for allll – ataulm 2013-07-04 17:27:55

+1

@ataulm原來的問題是關於Prolog和Java的集成,我回答了。然後,這個問題被編輯添加一個異常尋求幫助,我忽略了這個答案,但給出了這個答案。 – 2013-07-05 09:08:40