2010-12-08 48 views
0

我有以下简单的序言断言从序言谓词阅读:HOWTO在XPCE

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

的想法很明确,简单地返回相同的“输出”,如“在”被接收到。好的,现在我想把这个prolog谓词包含在XPCE程序中。我创建了一个窗口并添加了一个应该调用此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 

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

回答

0

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

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

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

此程式的来源是textfield。PL并且可以与

swipl -s textfield.pl -g run 

默认文本运行:
Demo Text

用户输入

ABC

文本经由来自谓词发送到GUI的消息由程序更新调用按下按钮:

ABC surrounded by asterisks

演示类:

%%% 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). 

看演示和编码之后由按钮叫

:- 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. 

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

+0

谢谢,我会检查一下 - 现在我通过在prolog中使用send/3解决了我的问题,正如您在第一行中所建议的那样... – Matthias 2010-12-12 18:05:16