Subject: [SWIPL] Embedding Deterministic Prolog Predicates in XPCE's function obje cts

From: Fabien Todescato <[email-masked]>
Date: Thu, 10 Jan 2002 09:37:47 +0100

Dear XPCErs,

I have to implement a menu the items of which are dynamically enabled
according to some possibly complex conditions. Such dynamic conditions may
be specified by providing a code object yielding a boolean object as the
'condition' argument of the menu item constructor - see the documentation of
the initialise method for the menu_item class -.

In my case, I need to implement the condition logic in Prolog, and therefore
I am seeking a means to wrap a condition/1 Prolog predicate yielding either
@on or @off, into an XPCE function object.

I can define as follows the 'prologFunction' class wrapping a unary
predicate :

:- use_module(library(pce)).

:- pce_begin_class(prologFunction,object).

  variable(p,prolog_term,both).

  initialise(Self,P:prolog_term) :->
    send_super(Self,initialise)
  , send(Self,p(P))
  .

  apply(Self,Y) :<-
    get(Self,p,P)
  , call(P,Y)
  .

:- pce_end_class.

This way, if F is an instance of the above class, then F?apply is an XPCE
function that yields the result of the Prolog predicate embedded in F.

The example below demonstrates the use of a wrapped unary prolog predicate
always yielding @on.

example :-
  new(F,prologFunction('='(@on))) % F is the wrapped Prolog predicate
yielding @on.
, new(M,message(@prolog,write_ln,F?apply))
, send(M,forward)
.

We get on the console :

?- example.
@on

Yes

Although this seems to be working, the use of of the '?' obtainer with the
apply selector is a little bit clumsy.

My question is :

Would it be possible to define a class derived from the XPCE 'function'
class, so that the 'execute' behaviour is overloaded in order to call the
embedded predicate directly ?

Any hints greatly appreciated. Thanks a lot for your attention.

Fabien TODESCATO