| 1 | ================================================================ |
|---|
| 2 | Logtalk - Open source object-oriented logic programming language |
|---|
| 3 | Release 2.35.0 |
|---|
| 4 | |
|---|
| 5 | Copyright (c) 1998-2009 Paulo Moura. All Rights Reserved. |
|---|
| 6 | Logtalk is free software. You can redistribute it and/or modify |
|---|
| 7 | it under the terms of the "Artistic License 2.0" as published by |
|---|
| 8 | The 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 | |
|---|
| 22 | Age = 30 |
|---|
| 23 | yes |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | % the same question could be made via any of its viewpoints: |
|---|
| 27 | |
|---|
| 28 | | ?- joeSportsman::age(Age). |
|---|
| 29 | |
|---|
| 30 | Age = 30 |
|---|
| 31 | yes |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | % now let's tell joe to get older: |
|---|
| 35 | |
|---|
| 36 | | ?- joePerson::getOlder. |
|---|
| 37 | |
|---|
| 38 | yes |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | % we can verify the effect of the above message from any of the viewpoints: |
|---|
| 42 | |
|---|
| 43 | | ?- joeChessPlayer::age(Age). |
|---|
| 44 | |
|---|
| 45 | Age = 31 |
|---|
| 46 | yes |
|---|
| 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 | |
|---|
| 54 | yes |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | % we can check this by asking joe its age: |
|---|
| 58 | |
|---|
| 59 | |
|---|
| 60 | | ?- joePerson::age(Age). |
|---|
| 61 | |
|---|
| 62 | Age = 32 |
|---|
| 63 | yes |
|---|
| 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 | |
|---|
| 76 | Score = 0 |
|---|
| 77 | yes |
|---|
| 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 | |
|---|
| 85 | Score = 0 |
|---|
| 86 | yes |
|---|
| 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 | |
|---|
| 94 | Score = 2200 |
|---|
| 95 | yes |
|---|
| 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 | |
|---|
| 103 | Score = 0 |
|---|
| 104 | yes |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | % the other descendants/viewpoints will continue to share the definition |
|---|
| 108 | % in joePerson: |
|---|
| 109 | |
|---|
| 110 | | ?- joeSportsman::score(Score). |
|---|
| 111 | |
|---|
| 112 | Score = 0 |
|---|
| 113 | yes |
|---|