|
From: | Daniel Thomas |
Subject: | Re: [Info-gnuprologjava] How to instance a Term with a functor? |
Date: | Mon, 22 Aug 2011 21:49:19 +0100 |
Good morning,
I just started using the API gnuprologjava-0.2.6.jar. I have a parser of a language written in prolog and implemented by defining new operators with 'op/3'. Therefore, I take advantage of natural treatment of functors already built in prolog. This parser works well and I want to use it from a java program.
I made a first exercise for referencing a simple _expression_ (a+b) to a variable term A by using the '=/2' operator (i.e., A=(a+b) ) in the same way as in the gprolog interface: gprolog.png
This is a basic requirement for parsing the language expressions. I have coded a simple method parser(String) which constructs the terms and execute a goal. I have tried to use AtomTerm and VariableTerm classes to unify with an _expression_ but I don't get the same result as in gprolog.
In the case of the code using AtomTerm:
public void parser(String exp){
exp = "("+exp.trim()+")";
AtomTerm t_exp = AtomTerm.get(exp);
VariableTerm at_A = new VariableTerm("A");
Term[] args = {at_A,t_exp};
CompoundTerm goalTerm = new CompoundTerm("=", args);
System.out.println("? "+goalTerm.toString());
Goal goal = interpreter.prepareGoal(goalTerm);
try {
int rc=-1;
do{
rc=interpreter.execute(goal);
System.out.println("! "+at_A.name+" = "+at_A.value);
}while(rc==0);
} catch (PrologException e1) {
e1.printStackTrace();
}
}
I get the output:
? A = '(a+b)'
! A = '(a+b)'
… but AtomTerm instances my _expression_ as an atom in quotes.
I tried also with VariableTerm class:
public void parser(String exp){
exp = "("+exp.trim()+")";
Term t_exp = new VariableTerm(exp);
VariableTerm at_A = new VariableTerm("A");
Term[] args = {at_A,t_exp};
CompoundTerm goalTerm = new CompoundTerm("=", args);
System.out.println("? "+goalTerm.toString());
Goal goal = interpreter.prepareGoal(goalTerm);
try {
int rc=-1;
do{
rc=interpreter.execute(goal);
System.out.println("! "+at_A.name+" = "+at_A.value);
}while(rc==0);
} catch (PrologException e1) {
e1.printStackTrace();
}
}
with the output:
? A = (a+b)
! A = _A0
…but it doesn't works either.
Do you know if I can instance some gnu.prolog.term.* with an _expression_ (actually a functor) and how? or maybe you have another alternative?
Best regards and thanks in advance,
--
Carlos-Manuel
______________________________________________________
PhD Student at
______________________________________________________
LIG Laboratory - HADAS group
681 Bât D-306, Rue de la Passerelle, 38400 Saint Martin d'Hères, France
Tel (+33) 4 76 82 72 31, Fax (+33) 4 76 82 72 87
LAFMIA - Universidad de las Américas, Puebla
CENTIA , Ex-hda Sta. Catarina Mártir, 72820 Cholula, Puebla, México
Tel (+52) 222 2 29 26 06, Fax (+52) 222 2 29 21 38
______________________________________________________
signature.asc
Description: This is a digitally signed message part
[Prev in Thread] | Current Thread | [Next in Thread] |