如何在 XPCE 中读取 prolog 谓词

发布于 2024-10-06 12:48:28 字数 532 浏览 2 评论 0原文

我有以下简单的序言谓词:

tst(In, Out) :- Out = In.

这个想法很明确,只需在“Out”中返回与“In”中收到的相同内容即可。好的,现在我想在 XPCE 程序中包含这个 prolog 谓词。我创建了一个窗口并添加了一个按钮,该按钮应该调用此 prolog 谓词,然后显示“Out”中返回的值。我认为完成这项任务会很简单,

send(Dialog, append(button(execute_command, and(
  message(@prolog, tst, InputText?selection, prolog(Output)),
  message(@prolog, write, prolog(Output)),
  message(@prolog, nl))))),

但不幸的是,这并不完全按照我想要的方式工作。相反,它现在打印出“Out”的内部引用。例如:

?- _L204

有什么想法我的错误是什么吗?

I have the following simple prolog predicate:

tst(In, Out) :- Out = In.

The idea is clear, simply return the same in "Out" as was received in "In". Ok, now I want to include this prolog predicate in a XPCE program. I have created a window and added a button that should call this prolog predicate, then display the value returned in "Out". I thought achieving this task would be as simple as

send(Dialog, append(button(execute_command, and(
  message(@prolog, tst, InputText?selection, prolog(Output)),
  message(@prolog, write, prolog(Output)),
  message(@prolog, nl))))),

but unfortunately, that does not work exactly as I want it to. Instead it now prints out the internal reference of "Out". For example:

?- _L204

Any ideas what is my mistake here?

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

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

发布评论

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

评论(1

梦屿孤独相伴 2024-10-13 12:48:28

从 Prolog 在 PCE 中设置值很容易,使用 send/3send/4。因此解决这个问题的一种方法是让 Prolog 谓词调用一个在 PCE 对象上设置值的方法。

对象可以包含其他对象,并且在范围内具有类和实例变量。 Prolog 代码所需要的只是对对象的引用:通过使用对象引用调用谓词,谓词可以调用适当的方法来传递值。

下面是一些基于 dialog 类创建对象的代码。按下对象中的按钮时,将调用一个谓词(类似于本问题中的谓词),该谓词将根据传入的值创建一个值,然后在实例变量中设置该值对话框。它还会将值放入 text_ item 控件中。

该程序源是textfield.pl,可以使用默认文本运行

swipl -s textfield.pl -g run


演示文本

用户输入

 ABC

程序通过从按钮调用的谓词发送到 GUI 的消息来更新文本:

ABC 被星号包围

演示类:

:- use_module(library(pce)).

:- pce_begin_class(demo, dialog).
/* Instance variables*
         name,   type,  access, description */
variable(result, name*, get,    "Result from Prolog Callback").

initialise(Demo) :->
    "Create something that get/4 and send/3 can work with."::
    send(Demo, send_super, initialise, 'Demo'),
    send(Demo, append, new(InputText, text_item(input, 'Demo Text'))),
    send(Demo, append,
         button(execute_command,
            and(message(@prolog,doIt, Demo,InputText?selection),
            message(@prolog,printIt,Demo)))).
:- pce_end_class.

按钮调用的代码:

%%% Create a new value based on the input string
%%% Write the new value back to the 'input' member of the
%%% 'demo' object.  Also put the value int the 'result' slot.
doIt(Demo,Word) :-
    concat_atom(['*** ' , Word, ' *** '] ,WordGotDid),
    format("doIt: Setting result: ~w...~n", [WordGotDid]),
    get(Demo,member,input,InputText),
    send(InputText,selection,WordGotDid),
    send(Demo,slot,result,WordGotDid).

%%% Read and display the 'result' slot.
printIt(Demo) :-
    get(Demo,slot,result,Result),
    write('\nResult: "'),
    write(Result),
    write('"\n').

主程序:

%%% Create an object that has fields that can be mutated.
run :- new(Demo,demo),
    send(Demo,open).

在查看演示并编码后,这是我的观点,一旦我完成 XPCE 的学习,它可能会改变:尽管如此,它有点可以通过来自 XPCE 的消息在 Prolog 中进行编程,查看演示代码,这不是它的完成方式。编程是用 Prolog 或其他语言完成的,并以 Prolog 作为粘合剂,而 XPCE 主要是一个被动的 GUI,有点像 HTML 表单。该程序创建 XPCE 对象,更改其状态并从中读取值。除了一些与 GUI 相关的小型消息传递之外,XPCE 通常不会写入程序;但即便如此,通常也是在 XPCE 对象的方法中完成的。

From Prolog setting values in PCE is easy,using send/3, or send/4. So one way around this is to have the Prolog predicate invoke a method that sets a value on a PCE object.

Objects can contain other objects, and have class and instance variables in scope. All the Prolog code needs is a reference to the object: By calling the predicate with an object reference, the predicate can invoke an appropriate method to communicate a value.

Here is some code that creates an object based on the dialog class. A button in the object, when pressed, will call a predicate (similar to the one in this question) that will create a value based on the one passed-in, then set that value in an instance variable of the dialog. It will also put the value in the text_ item control.

This program source is textfield.pl and can be run with

swipl -s textfield.pl -g run

Default Text:
Demo Text

User Input

ABC

Text updated by the program via messages sent to the GUI from a predicate invoked by the pushbutton:

ABC surrounded by asterisks

The demo class:

:- use_module(library(pce)).

:- pce_begin_class(demo, dialog).
/* Instance variables*
         name,   type,  access, description */
variable(result, name*, get,    "Result from Prolog Callback").

initialise(Demo) :->
    "Create something that get/4 and send/3 can work with."::
    send(Demo, send_super, initialise, 'Demo'),
    send(Demo, append, new(InputText, text_item(input, 'Demo Text'))),
    send(Demo, append,
         button(execute_command,
            and(message(@prolog,doIt, Demo,InputText?selection),
            message(@prolog,printIt,Demo)))).
:- pce_end_class.

Code called by the pushbutton:

%%% Create a new value based on the input string
%%% Write the new value back to the 'input' member of the
%%% 'demo' object.  Also put the value int the 'result' slot.
doIt(Demo,Word) :-
    concat_atom(['*** ' , Word, ' *** '] ,WordGotDid),
    format("doIt: Setting result: ~w...~n", [WordGotDid]),
    get(Demo,member,input,InputText),
    send(InputText,selection,WordGotDid),
    send(Demo,slot,result,WordGotDid).

%%% Read and display the 'result' slot.
printIt(Demo) :-
    get(Demo,slot,result,Result),
    write('\nResult: "'),
    write(Result),
    write('"\n').

Main program:

%%% Create an object that has fields that can be mutated.
run :- new(Demo,demo),
    send(Demo,open).

After looking at the demos and coding this one, this is my opinion which could change, once I finish studying XPCE: Even though, it is somewhat possible to program in Prolog via messages from XPCE, looking at the demo code, this isn't the way it's done. The programming is done in Prolog or some other language and with Prolog as the glue, while XPCE is mostly a passive GUI, sort of like HTML forms. The program creates XPCE objects, changes their state and reads values from them. XPCE doesn't normally write to the program other than some small GUI-related messaging; but even that is usually done in the methods of XPCE objects.

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