我有以下简单的prolog谓词:
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我有什么错误吗?
发布于 2010-12-11 13:44:53
从Prolog设置在PCE中的值是很容易的,使用send/3或send/4。因此,一种方法是让Prolog谓词调用一个方法,该方法为PCE对象设置一个值。
对象可以包含其他对象,并且在作用域中有类和实例变量。Prolog代码所需要的是对对象的引用:通过使用对象引用调用谓词,谓词可以调用适当的方法来传递值。
下面是一些基于对话框类创建对象的代码。当按下对象中的按钮时,将调用一个谓词(类似于此问题中的谓词),该谓词将根据传入的谓词创建一个值,然后在对话框的实例变量中设置该值。它还会将该值放入text_项控件中。
这个程序源代码是textfield.pl,可以用
swipl -s textfield.pl -g run默认文本:

用户输入

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

演示课程:
:- 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对象的方法中完成的。
https://stackoverflow.com/questions/4391136
复制相似问题