Begging for Java help :)

Ovid on 2005-01-20T02:45:15

There may not be much Perl content here, but this really is about Perl. I've ported virtually all of this to Perl, right down to the bugs.

As it turns out, my predicate logic code will do the first unification but fails to handle successive unifications. It could be something silly. It could be something profound. I have no idea which.

At first I worked out all of the bugs by carefully stepping through the Java and Perl code side by side, but this particular bug is one that I have in the Java code, too. What's even more irritating is that the Java applet does not have this bug. It's only when I remove it from the applet and try to run it as a stand alone application do things fail. So I have two piece of Java code that should report the same results, but don't and I have no idea why. I'm stumped.

If anyone thinks they find the bug, here's what I have done (twice!). Download the source and strip out the applet code. Then strip it down until you can run the following code:

import java.util.*;

public class Append {
    public static Term        query;
    public static ParseString p;
    public static Engine      eng;

    public static void main(String[] args) {
        try {
            p     = new ParseString("append(X,Y,[a,b,c,d]).");
            query = new Term(p);
        } catch (Exception f) {
            System.out.println("Can't parse query!");
        }
        if (query != null) {
            try {
                eng = new Engine(query,
                    ParseString.consult(
                        "append([],X,X)."
                      + "append([W|X],Y,[W|Z]) :- append(X,Y,Z).",
                        new Hashtable()
                        // the final argument is no longer required
                    )
                );
                eng.start(); // prints first result
                eng.more();  // prints 'No.'
            } catch (Exception f) {
                System.out.println("Can't parse program!");
            }
        }
    }
}

What's really strange is the result of engine.more() prints before the result of engine.start(). I suppose that's a hint, but I still can't figure it out.

As for the applet, click the "append" button and enter append(X,Y,[a,b,c,d]) in the query box. When you click Run Query, it will give the same results as eng.start(), but when you click More? button it keeps returning results until there are no more results.

In fact, just to make it easy, I even have a downloadable version of mine. Just compile and run it.


Thread issue

briac on 2005-01-20T08:12:57

At first glance, it seems related to the fact that the Engine class extends Thread. As I understand it, your main() method calls more() even though start() may not have finished yet.

I guess this is not happenning in the Applet since the more() button is handled by a button.

Re:Thread issue

Ovid on 2005-01-20T16:32:58

As it turns out, this was the problem. Someone over on Live Journal pointed out the issue and the exact spot where the problem was occurring and I fixed it and uploaded a new version. Now I just need to find out why my Perl version is failing :(