root/trunk/examples/viewpoints/SCRIPT.txt

Revision 4662, 2.1 KB (checked in by pmoura, 6 days ago)

Updated copyright notice.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1================================================================
2Logtalk - Open source object-oriented logic programming language
3Release 2.35.0
4
5Copyright (c) 1998-2009 Paulo Moura.        All Rights Reserved.
6Logtalk is free software.  You can redistribute it and/or modify
7it under the terms of the "Artistic License 2.0" as published by
8The Perl Foundation. Consult the "LICENSE.txt" file for details.
9================================================================
10
11
12% start by loading the example:
13
14| ?- logtalk_load(viewpoints(loader)).
15...
16
17
18% we can start by asking joe its age:
19
20| ?- joePerson::age(Age).
21
22Age = 30
23yes
24
25
26% the same question could be made via any of its viewpoints:
27
28| ?- joeSportsman::age(Age).
29
30Age = 30
31yes
32
33
34% now let's tell joe to get older:
35
36| ?- joePerson::getOlder.
37
38yes
39
40
41% we can verify the effect of the above message from any of the viewpoints:
42
43| ?- joeChessPlayer::age(Age).
44
45Age = 31
46yes
47
48
49% because the growOld/0 and the age/1 predicates are implemented using
50% property sharing, we can send the getOlder/0 message to any viewpoint:
51
52| ?- joeEmployee::getOlder.
53
54yes
55
56
57% we can check this by asking joe its age:
58
59
60| ?- joePerson::age(Age).
61
62Age = 32
63yes
64
65
66% as you can see, although the modification message have been sent to a
67% descendant, its the predicate age/1 in the parent that got updated
68
69
70% to illustrate value sharing we use a couple of predicates, score/1 and
71% setScore/0, defined in joePerson:
72
73
74| ?- joePerson::score(Score).
75
76Score = 0
77yes
78
79
80% initially, score/1 is only defined for joePerson, so every descendant
81% or viewpoint will share its value/definition:
82
83| ?- joeEmployee::score(Score).
84
85Score = 0
86yes
87
88
89% but if we decide to increment the counter by sending the setScore/0
90% message to a descendant:
91
92| ?- joeChessPlayer::(setScore(2200), score(Score)).
93
94Score = 2200
95yes
96
97
98% then the descendant will now have a local definition for counter/1,
99% independent of the definition in its parent, joePerson:
100
101| ?- joePerson::score(Score).
102
103Score = 0
104yes
105
106
107% the other descendants/viewpoints will continue to share the definition
108% in joePerson:
109
110| ?- joeSportsman::score(Score).
111
112Score = 0
113yes
Note: See TracBrowser for help on using the browser.