开发者

gnuprologjava problem

I am using http://www.gnu.org/software/gnuprologjava/ to perform prolog stuff from Java application.

In this context I am lodaing the following prolog file:

    /* ghoul is a prolog fact representing the goal state in flat notation */
ghoul(cube('WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE', 'WHITE','WHITE',    /*White*/
           'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN', 'GREEN','GREEN',    /*Green*/
           'RED', 'RED', 'RED', 'RED', 'RED', 'RED', 'RED', 'RED','RED',    /*Red*/
           'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE', 'BLUE','BLUE',    /*Blue*/
           'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE', 'ORANGE','ORANGE',    /*Orange, Yellow*/
           'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW', 'YELLOW','YELLOW')).  

/* pieces converts from flat to pieces notation and vice versa*/        
pieces(cube(X01, X02, X03, X04, X05, X06, X07, X08, X09,  /*White*/
            X10, X11, X12, X13, X14, X15, X16, X17, X18,  /*Green*/
            X19, X20, X21, X22, X23, X24, X25, X26, X27,  /*Red*/
            X28, X29, X30, X31, X32, X33, X34, X35, X36,  /*Blue*/
            X37, X38, X39, X40, X41, X42, X43, X44, X45,  /*Orange*/
            X46, X47, X48, X49, X50, X51, X52, X53, X54), /*Yellow*/
       [p(X05), p(X14), p(X23), p(X32), p(X41),p(X50),
        p(X11, X08), p(X15, X22), p(X17, X47), p(X13, X42), /* Green edges */
        p(X38, X04), p(X44, X49),                           /* Orange edges */
        p(X20, X06), p(X26, X51),                           /* Red edges */
        p(X29, X02), p(X33, X40), p(X35, X53), p(X31, X24), /* Blue edges */
        p(X10, X07, X39), p(X12, X19, X09), p(X18, X48, X25), p(X16, X45, X46), /* Green coins */
        p(X28, X03, X21), p(X30, X37, X01), p(X36, X52, X43), p(X34, X27, X54)]).


/* Move of white face */
mov(w, cube(X01, X02, X03, X04, X05, X06, X07, X08, X09,  /*White*/
            X10, X11, X12, X13, X14, X15, X16, X17, X18,  /*Green*/
            X19, X20, X21, X22, X23, X24, X25, X26, X27,  /*Red*/
            X28, X29, X30, X31, X32, X33, X34, X35, X36,  /*Blue*/
            X37, X38, X39, X40, X41, X42, X43, X44, X45,  /*Orange*/
            X46, X47, X48, X49, X50, X51, X52, X53, X54), 
       cube(X03, 开发者_运维百科X06, X09, X02, X05, X08, X01, X04, X07,  /*White*/
            X19, X20, X21, X13, X14, X15, X16, X17, X18,  /*Green*/
            X28, X29, X30, X22, X23, X24, X25, X26, X27,  /*Red*/
            X37, X38, X39, X31, X32, X33, X34, X35, X36,  /*Blue*/
            X10, X11, X12, X40, X41, X42, X43, X44, X45,  /*Orange*/
            X46, X47, X48, X49, X50, X51, X52, X53, X54)).

/* Opposite Moves */        
move(+M, OldState, NewState):- mov(M, OldState, NewState). 
move(-M, OldState, NewState):- mov(M, NewState, OldState).

Why the following Java Code always return -1 (false in prolog interpreter langage) where as the interpreter is able to compute the NewState value:

    try {
            URL url = FileLocator.find(Activator.getDefault().getBundle(),
                    new Path("cube.pl"), null);
            url = FileLocator.toFileURL(url);
            Environment env = new Environment();


        // Run the initialization
        env.runInitialization(interpreter);
            env.ensureLoaded(AtomTerm.get(url.getFile()));

   Interpreter interpreter = env.createInterpreter();

            VariableTerm cube = new VariableTerm("cube");
            Term[] args = { cube };
            CompoundTerm goalTerm = new CompoundTerm(AtomTerm.get("ghoul"),
                    args);
            interpreter.runOnce(goalTerm);

            VariableTerm newCube = new VariableTerm("NewCube");
            Term[] args2 = { AtomTerm.get("+w"), cube,
                    newCube };
            CompoundTerm move = new CompoundTerm(AtomTerm.get("move"), args2);
            int runOnce = interpreter.runOnce(move);
            System.out.println(runOnce);
        } catch (Exception e) {
            e.printStackTrace();
        }


Shouldn't you be calling ensureLoaded before you call runInitialization? The javadoc says: "You must use runInitialization(Interpreter) after using this and before expecting answers."

Also, is the interpreter that you pass to runInitialization the same as the one you create for running the query?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜