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

发布于 2024-08-18 04:08:47 字数 2077 浏览 8 评论 0原文

我正在使用 Java 提供的 JPL 编写一个应用程序SWI-Prolog 从 Java 调用 Prolog。

我使用 Eclipse 作为 IDE。我不知道如何开始这个我在网上找到的例子:

这里是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();

}

如果我直接从Prolog运行Prolog程序,它工作得很好,并且当我从Java应用程序调用它时也是一样。

我还可以在 Eclipse 控制台中看到输出,并且可以回答问题。

但我想为用户和系统之间的交互构建一个Java UI,但我不知道如何从Java中的Prolog中获取代码并将其放入UI中。

例如,如何捕获来自 Java UI 的输入并将其传递给 Prolog 代码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别理我 2024-08-25 04:08:47

问题可能是您的 Prolog 文本不是用
倒置样式,例如 Java UI 应用程序通常就是这样。

因此,在单独的线程中启动 Prolog 系统。替换所有读/1和写/1
在 Prolog 文本中大致如下:

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

my_write(标签,值):- set_UI_result(标签,值)。

由于也在第二个单独的线程中运行,因此在输入值时
点击某个按钮,UI 应用程序应该发出通知(信号)。

或者重写专家系统的逻辑,使得推论
导致查询或答案可以在步骤中从外部调用
明智的时尚。但也建议生成一个线程,因为
推断可能需要时间。

最好的问候

P.S.:如果您的申请被倒置,您可以轻松地使
它有几个不同的用户界面:
http://www.jekejeke.ch /idatab/doclet/prod/en/docs/10_pro08/13_press/02_deploy/package.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文