2001-06-06 20:40:57 +01:00
|
|
|
=================================================================
|
|
|
|
Logtalk - Object oriented extension to Prolog
|
2007-02-19 18:58:28 +00:00
|
|
|
Release 2.29.4
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2007-01-10 12:46:10 +00:00
|
|
|
Copyright (c) 1998-2007 Paulo Moura. All Rights Reserved.
|
2001-06-06 20:40:57 +01:00
|
|
|
=================================================================
|
|
|
|
|
|
|
|
|
2004-06-13 19:04:28 +01:00
|
|
|
% start by loading the example:
|
|
|
|
|
2004-11-29 20:36:31 +00:00
|
|
|
| ?- logtalk_load(viewpoints(loader)).
|
2004-06-13 19:04:28 +01:00
|
|
|
...
|
|
|
|
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
% 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
|
|
|
|
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
% now let's tell joe to get older:
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
| ?- joePerson::getOlder.
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
|
|
|
% we can verify the effect of the above message from any of the viewpoints:
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
| ?- joeChessPlayer::age(Age).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
Age = 31
|
|
|
|
yes
|
|
|
|
|
|
|
|
|
|
|
|
% because the growOld/0 and the age/1 predicates are implemented using
|
2003-01-09 21:29:22 +00:00
|
|
|
% property sharing, we can send the getOlder/0 message to any viewpoint:
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
| ?- joeEmployee::getOlder.
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
% to illustrate value sharing we use a couple of predicates, score/1 and
|
|
|
|
% setScore/0, defined in joePerson:
|
2001-06-06 20:40:57 +01:00
|
|
|
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
| ?- joePerson::score(Score).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
Score = 0
|
2001-06-06 20:40:57 +01:00
|
|
|
yes
|
|
|
|
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
% initially, score/1 is only defined for joePerson, so every descendant
|
2001-06-06 20:40:57 +01:00
|
|
|
% or viewpoint will share its value/definition:
|
|
|
|
|
2004-04-26 00:57:46 +01:00
|
|
|
| ?- joeEmployee::score(Score).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
Score = 0
|
2001-06-06 20:40:57 +01:00
|
|
|
yes
|
|
|
|
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
% but if we decide to increment the counter by sending the setScore/0
|
2001-06-06 20:40:57 +01:00
|
|
|
% message to a descendant:
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
| ?- joeChessPlayer::(setScore(2200), score(Score)).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
Score = 2200
|
2001-06-06 20:40:57 +01:00
|
|
|
yes
|
|
|
|
|
|
|
|
|
|
|
|
% then the descendant will now have a local definition for counter/1,
|
|
|
|
% independent of the definition in its parent, joePerson:
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
| ?- joePerson::score(Score).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2004-04-26 00:57:46 +01:00
|
|
|
Score = 0
|
2001-06-06 20:40:57 +01:00
|
|
|
yes
|
|
|
|
|
|
|
|
|
|
|
|
% the other descendants/viewpoints will continue to share the definition
|
|
|
|
% in joePerson:
|
|
|
|
|
2003-01-09 21:29:22 +00:00
|
|
|
| ?- joeSportsman::score(Score).
|
2001-06-06 20:40:57 +01:00
|
|
|
|
2004-04-26 00:57:46 +01:00
|
|
|
Score = 0
|
2001-06-06 20:40:57 +01:00
|
|
|
yes
|