【问题标题】:Howto read from a prolog predicate in XPCE如何从 XPCE 中的序言谓词中读取
【发布时间】:2010-12-08 18:56:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: swi-prolog xpce


    【解决方案1】:

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

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

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

    这个程序源是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 对象的方法中完成的。

    【讨论】:

    • 谢谢,我会检查一下——现在我通过在 prolog 中使用 send/3 解决了我的问题,正如你在第一行中所建议的那样......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多