i downloaded the tuprolog and i'm not able to get the result for the following query, while i get answer in PROL ide.. can someone help?
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).
i need to handle these kind of relations and get the output to process again in java
can anyone please tell me how to integrate prolog and java?
EDIT: i get this error in tuprolog by implementing the code
this code works for associatedWith(X,Y). i.e with 2 arguments, but doesnt work when i give 3 arguments associatedWith(X,Y,Z). what to do.. pls help i get this exception
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
The main tuProlog objects you need to handle in Java are: Theory
, that you can use to represent a Prolog program; Prolog
, that is the interpreter which you will ask queries to; and SolveInfo
, containing data for each solution found by the interpreter.
Let's suppose your Prolog program (i.e. all the code you posted except the last line) is contained in a file named theory.pl
. Roughly speaking (i.e. modulo imports and exceptions), the procedure you need to follow is similar to:
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"));
}
If your query possibly yields more than one solution, use a while
loop to test the existence of open choice points with Prolog.hasOpenAlternatives
and retrieve another solution with Prolog.solveNext
.
Chapter 8 in the manual (a PDF file you should find in the doc
subdirectory of any tuProlog distribution) contains lots of small examples of increasing complexity about interacting with Prolog from Java code (see in particular Section 8.4).