简体   繁体   English

如何将Java UI连接到JPL Prolog应用程序?

[英]How do I wire up my Java UI to a JPL Prolog application?

I'm writing an application in Java using JPL provided by SWI-Prolog to call Prolog from Java. 我正在使用SWI-Prolog提供的JPL编写Java应用程序,从Java调用Prolog。

I'm using Eclipse as the IDE. 我正在使用Eclipse作为IDE。 I don't know how to start this example I found online: 我不知道如何启动我在网上找到的这个例子:

Here the java code: 这里是java代码:

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. 如果我直接从Prolog运行Prolog程序,那么当我从Java应用程序调用它时它工作正常。

I can also see the output in the Eclipse console and I can reply to the questions. 我也可以在Eclipse控制台中看到输出,我可以回答问题。

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. 但我想为用户和系统之间的交互构建一个Java UI,但我不知道如何从Java中获取Prolog中的代码并将其放在UI中。

For example how can I capture input from the Java UI and pass this to the Prolog code? 例如,如何从Java UI捕获输入并将其传递给Prolog代码?

Problem is probably that your Prolog text is not written in inverted style, as for example Java UI applications typically are. 问题可能是您的Prolog文本不是以反向样式编写的,例如Java UI应用程序通常是。

So start your Prolog system in a separate thread. 因此,在一个单独的线程中启动您的Prolog系统。 Replace all read/1 and write/1 in your Prolog text by roughly: 大致替换Prolog文本中的所有read / 1和write / 1:

my_read(Prompt,Value) :- set_UI_prompt(Prompt), wait(signal), get_UI_value(Value). my_read(提示,值): - set_UI_prompt(提示),等待(信号),get_UI_value(值)。

my_write(Label,Value) :- set_UI_result(Label, 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). 由于还在第二个单独的线程中运行,在输入值并点击某个按钮时,UI应用程序应该通知(信号)。

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 最好的祝福

PS: 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 PS:如果您的应用程序被颠倒了,您可以轻松地将它设置为几个不同的UI: http//www.jekejeke.ch/idatab/doclet/prod/en/docs/10_pro08/13_press/02_deploy/package.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM