Search code examples
javaprologtuprolog

tuProlog unknown behavior


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?


Solution

  • 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.