The above event handlers will not work as you can only monitor messages sent using the ::/2 control construct; the predicate test/3 is called directly and is thus invisible as far as events are concerned. In this case, you will need to drop the predicate test1/3 and make test/3 public.Victor NOEL wrote:I understand more now, so I applied your advices and wrote a little example like that :
Code: Select all
:- category(ctg, implements(monitoring)). :- public(test1/3). test1(X, Y, Z) :- test(X, Y, Z). test(X, Y, Z) :- a(1, X), a(2, Y), a(3, Z). after(_, test(X, Y, Z), _) :- writeq(X), write(' '), writeq(Y), write(' '), writeq(Z), nl. a(1, a). a(2, b). a(3, c). :- end_category. :- object(obj, implements(monitoring), imports(ctg)). :- threaded. after(Object, Message, Sender) :- ::after(Object, Message, Sender). :- end_object.
I would be worried if you did not get the error above as I made a typo in my previous message! Sorry about that (I've already edited the post and corrected the typo). You get an "out of stack" error because you're using the "message to self" control construct ::/1 and self in this case is the object receiving the message that happens to define the after/3 event handler as a call to... self. The solution in this case is to use the :/1 control construct (see the manual for details):Victor NOEL wrote:And if I activate the event handler, I get a out of local stack error :I guess there is something wrong in what I wroteCode: Select all
?- obj::test1(X, Y, Z). X = a, Y = b, Z = c. ?- define_events(after, _, _, _, obj). true. ?- obj::test1(X, Y, Z). ERROR: Out of local stack
Maybe the after event (in obj) is called every time the after event (in ctg) is called ?
Code: Select all
:- category(ctg, implements(monitoring)).
:- public(test/3).
test(X, Y, Z) :-
a(1, X), a(2, Y), a(3, Z).
after(_, test(X, Y, Z), _) :- !,
writeq(X), write(' '), writeq(Y), write(' '), writeq(Z), nl.
after(_, _, _).
a(1, a).
a(2, b).
a(3, c).
:- end_category.
:- object(obj,
implements(monitoring),
imports(ctg)).
:- threaded.
after(Object, Message, Sender) :-
:after(Object, Message, Sender).
:- end_object.
Code: Select all
?- logtalk_load(noel, [events(on)]).
<<< loading source file noel...
>>> compiling source file noel...
compiling category ctg... compiled
compiling object obj... compiled
>>> noel source file compiled
% noel.pl compiled 0.01 sec, 5,860 bytes
<<< noel source file loaded
(0 warnings)
true.
?- obj::test(X, Y, Z).
X = a,
Y = b,
Z = c.
?- define_events(after, _, _, _, obj).
true.
?- obj::test(X, Y, Z).
a b c
X = a,
Y = b,
Z = c.
Paulo