5
votes

I'm writing an application in Java using JPL provided by SWI-Prolog to call Prolog from Java.

I'm using Eclipse as the IDE. I don't know how to start this example I found online:

Here the java code:

package prolog;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import jpl.Atom;
import jpl.Compound;
import jpl.Variable;
import jpl.Term;
import jpl.Query;
import jpl.JPL;


@SuppressWarnings({ "unchecked", "deprecation", "serial" })
public class JavaProlog extends JFrame {

 JButton  startButton = new JButton("Start");
 JTextArea  textArea = new JTextArea("A Diagnostic Expert System \n" +
           "for respiratory diseases and lung.");

 /**
  */
 JavaProlog(){
  Container cp=getContentPane();
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  setLocation (200,200);
  setSize  (300,200);
  setLayout (new FlowLayout());


  startButton.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    startDiagnose();
   }
  });

  cp.add(textArea);
  cp.add(startButton);

  setVisible(true); 
 }

 private void startDiagnose(){
  Term consult_arg[] = { 
          new Atom( "C://Users//i_vista//workspace//mdc.pl" ) 
      };
      Query consult_query = 
          new Query( 
              "consult", 
              consult_arg );

      boolean consulted = consult_query.query();

      if ( !consulted ){
          System.err.println( "Consult failed" );
          System.exit( 1 );
      }
 }

 public static void main( String argv[] ){
  JPL.init();
  JavaProlog jpTest = new JavaProlog();

}

If I run the Prolog program directly from Prolog it works fine and the same when I call it from the Java application.

I can also see the output in the Eclipse console and I can reply to the questions.

But I would like to build a Java UI for the interaction between the user and the system but I don't know how to take the code from Prolog in Java and put it in the UI.

For example how can I capture input from the Java UI and pass this to the Prolog code?

1

1 Answers

1
votes

Problem is probably that your Prolog text is not written in inverted style, as for example Java UI applications typically are.

So start your Prolog system in a separate thread. Replace all read/1 and write/1 in your Prolog text by roughly:

my_read(Prompt,Value) :- set_UI_prompt(Prompt), wait(signal), get_UI_value(Value).

my_write(Label,Value) :- set_UI_result(Label, Value).

Since also running in a second separate thread, upon entering a value and hitting some button, the UI application should notify(signal).

Or rewrite the logic of the expert system, so that the inferences leading to a query or answer can be called from the outside in a step wise fashion. But also then spawning a thread is recommended, since the inference might take time.

Best Regards

P.S.: If youre application were inverted, you could easily make it a couple of different UIs: http://www.jekejeke.ch/idatab/doclet/prod/en/docs/10_pro08/13_press/02_deploy/package.html