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/viewpoints/SCRIPT

111 lines
1.9 KiB
Plaintext
Raw Normal View History

=================================================================
Logtalk - Object oriented extension to Prolog
Release 2.22.2
Copyright (c) 1998-2004 Paulo Moura. All Rights Reserved.
=================================================================
% start by loading the example:
| ?- logtalk_load(viewpoints(loader)).
...
% we can start by asking joe its age:
| ?- joePerson::age(Age).
Age = 30
yes
% the same question could be made via any of its viewpoints:
| ?- joeSportsman::age(Age).
Age = 30
yes
% now let's tell joe to get older:
| ?- joePerson::getOlder.
yes
% we can verify the effect of the above message from any of the viewpoints:
| ?- joeChessPlayer::age(Age).
Age = 31
yes
% because the growOld/0 and the age/1 predicates are implemented using
% property sharing, we can send the getOlder/0 message to any viewpoint:
| ?- joeEmployee::getOlder.
yes
% we can check this by asking joe its age:
| ?- joePerson::age(Age).
Age = 32
yes
% as you can see, although the modification message have been sent to a
% descendant, its the predicate age/1 in the parent that got updated
% to illustrate value sharing we use a couple of predicates, score/1 and
% setScore/0, defined in joePerson:
| ?- joePerson::score(Score).
Score = 0
yes
% initially, score/1 is only defined for joePerson, so every descendant
% or viewpoint will share its value/definition:
| ?- joeEmployee::score(Score).
Score = 0
yes
% but if we decide to increment the counter by sending the setScore/0
% message to a descendant:
| ?- joeChessPlayer::(setScore(2200), score(Score)).
Score = 2200
yes
% then the descendant will now have a local definition for counter/1,
% independent of the definition in its parent, joePerson:
| ?- joePerson::score(Score).
Score = 0
yes
% the other descendants/viewpoints will continue to share the definition
% in joePerson:
| ?- joeSportsman::score(Score).
Score = 0
yes