This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/Logtalk/examples/threads/birthdays/birthdays.lgt
pmoura 3ac73c8881 Logtalk 2.28.2 files.
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@1713 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
2006-11-07 18:47:24 +00:00

91 lines
2.4 KiB
Plaintext

:- object(agent,
implements(event_handlersp)).
:- info([
version is 1.0,
author is 'Peter Robinson and Paulo Moura',
date is 2006/04/09,
comment is 'Simple multi-threading example with agents and their birthdays.']).
:- threaded.
:- public(new/3).
:- mode(new(+atom, +integer, +atom), one).
:- info(new/3, [
comment is 'Creates a new agent given its name, age, and gender.',
argnames is ['Name', 'Age', 'Gender']]).
:- public(age/1).
:- dynamic(age/1).
:- mode(age(?integer), zero_or_one).
:- info(age/1, [
comment is 'Agent age.']).
:- public(ask_age/2).
:- mode(ask_age(+atom, ?integer), zero_or_one).
:- info(ask_age/2, [
comment is 'Ask a friend his/her age.',
argnames is ['Name', 'Age']]).
:- public(gender/1).
:- dynamic(gender/1).
:- mode(gender(?integer), zero_or_one).
:- info(gender/1, [
comment is 'Agent gender.']).
:- public(birthday/0).
:- mode(birthday, one).
:- info(birthday/0, [
comment is 'Increments an agent age, an unfortunate side-effect of its birthday.']).
:- public(happy_birthday/1).
:- mode(happy_birthday(+object_identifier), one).
:- info(happy_birthday/1, [
comment is 'Happy birthday message from a friend.',
argnames is ['From']]).
:- public(cake_slice/1).
:- mode(cake_slice(+object_identifier), one).
:- info(cake_slice/1, [
comment is 'Offer a slice of birthday cake to a friend.',
argnames is ['From']]).
:- public(new_friend/1).
:- mode(new_friend(+object_identifier), one).
:- info(new_friend/1, [
comment is 'New friend, watch out for his/her birthday.',
argnames is ['Name']]).
new(Name, Age, Gender) :-
this(This),
create_object(Name, [extends(This)], [threaded], [age(Age), gender(Gender)]).
ask_age(Friend, Age) :-
threaded_call(Friend::age(Age)),
threaded_exit(Friend::age(Age)).
birthday :-
::retract(age(Old)),
New is Old + 1,
::assertz(age(New)).
happy_birthday(From) :-
self(Self),
write('Happy birthday from '), write(From), write('!'), nl,
write('Thanks! Here, have a slice of cake, '), write(From), write('.'), nl,
threaded_call(From::cake_slice(Self), [noreply]).
cake_slice(From) :-
write('Thanks for the cake '), write(From), write('!'), nl.
new_friend(Friend) :-
self(Self),
define_events(after, Friend, birthday, _, Self).
after(Friend, birthday, _) :-
self(Self),
threaded_call(Friend::happy_birthday(Self), [noreply]).
:- end_object.