Thanks Daniel!
It works!
Le 22 aot 2011 22:49, Daniel Thomas a crit :
Hello,
GNU Prolog is a completely separate implementation to GNU Prolog for Java (though confusion is understandable). I take it that your problem is that you want a term (a+b) but get one which is enclosed in single quotes when this is not the behaviour you were expecting.
This is because you are creating an AtomTerm containing 'a+b' rather than a compound term composed of the atoms a and b bound by the infix function +.
I suspect that you want to use http://www.gnu.org/s/gnuprologjava/api/gnu/prolog/io/TermReader.html#stringToTerm(gnu.prolog.io.ReadOptions, java.lang.String, gnu.prolog.vm.Environment) or http://www.gnu.org/s/gnuprologjava/api/gnu/prolog/io/TermReader.html#stringToTerm(java.lang.String, gnu.prolog.vm.Environment) depending on whether you want to be able to be able to get variables out by name when they are in the string you pass using http://www.gnu.org/s/gnuprologjava/api/gnu/prolog/io/ReadOptions.html#variableNames
I hope that helps,
Daniel
P.S. Sorry for the delay I haven't had much internet recently.
On Sun, 2011-08-07 at 13:57 +0200, Carlos-Manuel LPEZ-ENRQUEZ wrote:
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
<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 Bt D-306, Rue de la Passerelle, 38400 Saint Martin d'Hres, France
Tel (+33) 4 76 82 72 31, Fax (+33) 4 76 82 72 87
LAFMIA - Universidad de las Amricas, Puebla
CENTIA , Ex-hda Sta. Catarina Mrtir, 72820 Cholula, Puebla, Mxico
Tel (+52) 222 2 29 26 06, Fax (+52) 222 2 29 21 38
______________________________________________________
|