Include Paulo Moura's Logtalk OO LP system
git-svn-id: https://yap.svn.sf.net/svnroot/yap/trunk@53 b08c6af1-5177-4d33-ba66-4b1c6b8b522a
This commit is contained in:
22
Logtalk/examples/viewpoints/NOTES
Normal file
22
Logtalk/examples/viewpoints/NOTES
Normal file
@@ -0,0 +1,22 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
To load all objects in this example consult the viewpoints.loader utility
|
||||
file (note that the *.loader files are Prolog files).
|
||||
|
||||
Example adopted from the paper "Classifying Prototype-Based Programming
|
||||
Languages" by Christophe Dony, Jacques Malenfant and Daniel Bardou.
|
||||
|
||||
This prototype programming example ilustrates how we can do both property
|
||||
sharing and value sharing in Logtalk by calling the built-in predicate
|
||||
modification methods asserta/1, assertz/1 and retract/1 either in the
|
||||
context of "this" or in the context of "self".
|
||||
|
||||
In this example we have a prototype named joePerson, containing general data
|
||||
on joe like its age, name or address, and three descendants/viewpoints named
|
||||
joeSportsman, joeEmployee, and joeFilmEnthusiast. Each descendant contains
|
||||
data related to a particular viewpoint about joe.
|
109
Logtalk/examples/viewpoints/SCRIPT
Normal file
109
Logtalk/examples/viewpoints/SCRIPT
Normal file
@@ -0,0 +1,109 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
|
||||
% 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 grow old:
|
||||
|
||||
| ?- joePerson::growOld.
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% we can verify the effect of the above message from any of the viewpoints:
|
||||
|
||||
| ?- joeFilmEnthusiast::age(Age).
|
||||
|
||||
Age = 31
|
||||
yes
|
||||
|
||||
|
||||
% because the growOld/0 and the age/1 predicates are implemented using
|
||||
% property sharing, we can send the growOld message to any viewpoint:
|
||||
|
||||
| ?- joeEmployee::growOld.
|
||||
|
||||
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, counter/1 and
|
||||
% incCounter/0, defined in joePerson:
|
||||
|
||||
|
||||
| ?- joePerson::counter(Counter).
|
||||
|
||||
Counter = 0
|
||||
yes
|
||||
|
||||
|
||||
% initially, counter/1 is only defined for joePerson, so every descendant
|
||||
% or viewpoint will share its value/definition:
|
||||
|
||||
| ?- joeSportsman::counter(Counter).
|
||||
|
||||
Counter = 0
|
||||
yes
|
||||
|
||||
|
||||
% but if we decide to increment the counter by sending the incCounter/0
|
||||
% message to a descendant:
|
||||
|
||||
| ?- joeFilmEnthusiast::incCounter.
|
||||
|
||||
yes
|
||||
|
||||
| ?- joeFilmEnthusiast::counter(Counter).
|
||||
|
||||
Counter = 1
|
||||
yes
|
||||
|
||||
|
||||
% then the descendant will now have a local definition for counter/1,
|
||||
% independent of the definition in its parent, joePerson:
|
||||
|
||||
| ?- joePerson::counter(Counter).
|
||||
|
||||
Counter = 0
|
||||
yes
|
||||
|
||||
|
||||
% the other descendants/viewpoints will continue to share the definition
|
||||
% in joePerson:
|
||||
|
||||
| ?- joeSportsman::counter(Counter).
|
||||
|
||||
Counter = 0
|
||||
yes
|
26
Logtalk/examples/viewpoints/joeEmployee.lgt
Normal file
26
Logtalk/examples/viewpoints/joeEmployee.lgt
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
:- object(joeEmployee,
|
||||
extends(joePerson)).
|
||||
|
||||
|
||||
:- public(worksFor/1).
|
||||
|
||||
:- public(salary/1).
|
||||
:- dynamic(salary/1).
|
||||
|
||||
:- public(giveRaise/1).
|
||||
|
||||
|
||||
worksFor('ToonTown').
|
||||
|
||||
|
||||
salary(1500).
|
||||
|
||||
|
||||
giveRaise(Raise) :-
|
||||
retract(salary(Old)),
|
||||
New is Old + Raise,
|
||||
asserta(salary(New)).
|
||||
|
||||
|
||||
:- end_object.
|
22
Logtalk/examples/viewpoints/joeFilmEnthusiast.lgt
Normal file
22
Logtalk/examples/viewpoints/joeFilmEnthusiast.lgt
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
:- object(joeFilmEnthusiast,
|
||||
extends(joePerson)).
|
||||
|
||||
|
||||
:- public(favActor/1).
|
||||
|
||||
:- public(favFilm/1).
|
||||
|
||||
:- public(favDirector/1).
|
||||
|
||||
|
||||
favActor('Fred Filistone').
|
||||
|
||||
|
||||
favFilm('The Wizard of Oz').
|
||||
|
||||
|
||||
favDirector('Krzystof Kieslowski').
|
||||
|
||||
|
||||
:- end_object.
|
52
Logtalk/examples/viewpoints/joePerson.lgt
Normal file
52
Logtalk/examples/viewpoints/joePerson.lgt
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
:- object(joePerson).
|
||||
|
||||
|
||||
:- public(growOld/0).
|
||||
|
||||
:- public(address/1).
|
||||
|
||||
:- public(age/1).
|
||||
:- dynamic(age/1).
|
||||
|
||||
:- public(name/1).
|
||||
|
||||
:- public(phone/1).
|
||||
|
||||
:- public(counter/1).
|
||||
:- dynamic(counter/1).
|
||||
|
||||
:- public(incCounter/0).
|
||||
|
||||
|
||||
growOld :-
|
||||
retract(age(Old)),
|
||||
New is Old + 1,
|
||||
asserta(age(New)).
|
||||
|
||||
|
||||
address('8 Octave Street').
|
||||
|
||||
|
||||
age(30).
|
||||
|
||||
|
||||
name('John').
|
||||
|
||||
|
||||
phone(11-11-11-11).
|
||||
|
||||
|
||||
counter(0).
|
||||
|
||||
|
||||
incCounter :-
|
||||
(::retract(counter(Old)) ->
|
||||
true
|
||||
;
|
||||
Old = 0),
|
||||
New is Old + 1,
|
||||
::asserta(counter(New)).
|
||||
|
||||
|
||||
:- end_object.
|
17
Logtalk/examples/viewpoints/joeSportsman.lgt
Normal file
17
Logtalk/examples/viewpoints/joeSportsman.lgt
Normal file
@@ -0,0 +1,17 @@
|
||||
|
||||
:- object(joeSportsman,
|
||||
extends(joePerson)).
|
||||
|
||||
|
||||
:- public(stamina/1).
|
||||
|
||||
:- public(weight/1).
|
||||
|
||||
|
||||
stamina(30).
|
||||
|
||||
|
||||
weight(111).
|
||||
|
||||
|
||||
:- end_object.
|
7
Logtalk/examples/viewpoints/viewpoints.loader
Normal file
7
Logtalk/examples/viewpoints/viewpoints.loader
Normal file
@@ -0,0 +1,7 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load([
|
||||
joePerson,
|
||||
joeEmployee,
|
||||
joeFilmEnthusiast,
|
||||
joeSportsman])).
|
Reference in New Issue
Block a user