2010-12-08 39 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