如何将 Java UI 连接到 JPL Prolog 应用程序?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题可能是您的 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