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

任何想法我的错误是什么?

4

1 回答 1

0

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

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

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

这个程序源是textfield.pl并且可以运行

swipl -s textfield.pl -g run

默认文本:
演示文本

用户输入

美国广播公司

程序通过从按钮调用的谓词发送到 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 对象的方法中完成的。

于 2010-12-11T13:44:53.300 回答