2

I'm using tuProlog to integrate Prolog with Java, to do so I have defined a prolog.pl file with the following code:

go:-write('hello world!'),nl. 

Then, in my Java project I Have the Main Class that invokes this:

Prolog engine = new Prolog();
 
Theory theory = new Theory(new FileInputStream("prolog.pl"));
 
try {
           engine.setTheory(theory);
        } catch (InvalidTheoryException ex) {
            
        }
SolveInfo solution = engine.solve("go.");
 
if (solution.isSuccess()) {
  
    System.out.println(solution.getSolution());
}

This code should output 'hello world', however, it outputs 'go', any ideas of why this behavior?

WSD
  • 3,243
  • 26
  • 38

3 Answers3

2

Finally found that the behavior was not erratic at all :)

The solution is to add this code just before calling the Solve Method.

engine.addOutputListener(new OutputListener() {
        @Override
        public void onOutput(OutputEvent e) {
            finalResult += e.getMsg();

        }
});

finalResult is just a Global variable that contains the returned String produced by Prolog Write instruction.

WSD
  • 3,243
  • 26
  • 38
1

Your solution it's (correctly) the succeded Prolog query (go/0), what you expect ('hello world!') it's the output of a builtin, as such you should inspect the 'stdout' of your Java engine.

Otherwise, code your program to 'return' info in variables.

go(X) :- X = 'hello world!'.

Then tuProlog will provide the methods to access instanced variables.

CapelliC
  • 59,646
  • 5
  • 47
  • 90
0

I don't know about tuProlog/Java, but when calling Swi-Prolog from PHP, I must put 'halt' as the final statement of the predicate to tell Prolog to exit and return control back to php.

go:-write('hello world!'),nl, halt.
magus
  • 1,347
  • 7
  • 13