[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Info-gnuprologjava] problems running a simple quicksort example
From: |
Daniel Thomas |
Subject: |
Re: [Info-gnuprologjava] problems running a simple quicksort example |
Date: |
Thu, 11 Nov 2010 18:54:49 +0000 |
Hello,
I have tried running your code on my machine and the results were as
follows:
address@hidden:~$ vim Test.java #I removed the line numbers and edited the path
to quicksort.pl
address@hidden:~$ vim quicksort.pl
address@hidden:~$ javac -cp
'/home/daniel/dev/gnuprolog/releases/0.2.5/gnuprologjava-0.2.5/gnuprologjava-0.2.5.jar'
Test.java
address@hidden:~$ java -cp
'/home/daniel/dev/gnuprolog/releases/0.2.5/gnuprologjava-0.2.5/gnuprologjava-0.2.5.jar:.'
Test
ergebnis
0
.(5,8,1,7,3)
What exception do you get? I think that ".(5,8,1,7,3)" is probably the
wrong answer as it doesn't appear to be particularly sorted.
There is one bug I have found in your prolog:
teilen(Element, [Kopf|Rset], Kleiner, [Kopf|Groesser]):-teilen(Element,
Rest, Kleiner, Groesser).
"[Kopf|Rset]" should probably be "[Kopf|Rest]"
That doesn't fix the missorting though.
Does this help you? There is a demo program in the demo directory of the
release zip file.
In 0.3.0 the return codes of Interpreter.execute(Goal) etc. will be
changed to be an enum which will make System.out.println(rc); print
something slightly more useful than 0;
Daniel
On Wed, 2010-11-10 at 14:51 +0100, Benjamin Martin Brunzel wrote:
> Hi Folks,
>
> I am currently trying to run a simple quicksort example using gnu prolog
> for java. I get an exception every time I use a prolog file with more
> then one rule in it.
> I think the problem might be the representation of lists in a CompoundTerm
> object.
> Does anyone know what is wrong with it?
> Is there a simple example program available?
>
> The Java Class:
>
> 1 import gnu.prolog.term.*;
> 2 import gnu.prolog.vm.Environment;
> 3 import gnu.prolog.vm.Interpreter;
> 4 import gnu.prolog.vm.PrologException;
> 5
> 6
> 7 public class Test {
> 8
> 9 /**
> 10 * @param args
> 11 * @throws PrologException
> 12 */
> 13 public static void main(String[] args) throws PrologException {
> 14
> 15 Environment e = new Environment();
> 16
> e.ensureLoaded(AtomTerm.get("C:\\Path\\to\\prolog\\files\\quicksort.pl"));
> 17 Interpreter i = e.createInterpreter();
> 18 e.runInitialization(i);
> 19
> 20 VariableTerm ergebnis = new VariableTerm("ergebnis");
> 21 IntegerTerm[] elemente = new IntegerTerm[5];
> 22 elemente[0] = new IntegerTerm(5);
> 23 elemente[1] = new IntegerTerm(8);
> 24 elemente[2] = new IntegerTerm(1);
> 25 elemente[3] = new IntegerTerm(7);
> 26 elemente[4] = new IntegerTerm(3);
> 27
> 28 CompoundTerm liste = new CompoundTerm(".", elemente);
> 29
> 30 Term[] argumente = {ergebnis, liste};
> 31
> 32 CompoundTerm anfrage = new CompoundTerm("quicksort",
> argumente);
> 33
> 34 System.out.println(ergebnis);
> 35
> 36 Interpreter.Goal goal = i.prepareGoal(anfrage);
> 37 int rc = i.execute(goal);
> 38 System.out.println(rc);
> 39
> 40 System.out.println(ergebnis);
> 41
> 42 }
> 43
> 44 }
>
> The Prolog File:
>
> quicksort(A, A).
> quicksort([Kopf|Rest], SortierteListe):- teilen(Kopf, Rest, Kleiner,
> Groesser),quicksort(Kleiner, Kleiner_Sortiert),quicksort(Groesser,
> Groesser_Sortiert),append(Kleiner_Sortiert , [Kopf|Groesser_Sortiert],
> SortierteListe).
> teilen(_, [], [], []).
> teilen(Element, [Kopf|Rest], [Kopf|Kleiner], Groesser):-Kopf < Element,
> !,teilen(Element, Rest, Kleiner, Groesser).
> teilen(Element, [Kopf|Rset], Kleiner, [Kopf|Groesser]):-teilen(Element,
> Rest, Kleiner, Groesser).
>
>
> I used the version 0.2.5
>
> Please note that I am not on the mailing list so please cc me on your
> answers.
>
> Thanks for your support.
>
>
>
signature.asc
Description: This is a digitally signed message part