Did you know ... | Search Documentation: |
Pack logtalk -- logtalk-3.86.0/examples/viewpoints/SCRIPT.txt |
This file is part of Logtalk https://logtalk.org/ SPDX-FileCopyrightText: 1998-2023 Paulo Moura <pmoura@logtalk.org> SPDX-License-Identifier: Apache-2.0
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
% start by loading the example:
| ?- logtalk_load(viewpoints(loader))
.
...
% we can start by asking joe its age:
| ?- joe_person::age(Age)
.
Age = 30 yes
% the same question could be made via any of its viewpoints:
| ?- joe_sportsman::age(Age)
.
Age = 30 yes
% now let's tell joe to get older:
| ?- joe_person::grow_older.
yes
% we can verify the effect of the above message from any of the viewpoints:
| ?- joe_chess_player::age(Age)
.
Age = 31 yes
% because the growOld/0 and the age/1 predicates are implemented using % property sharing, we can send the grow_older/0 message to any viewpoint:
| ?- joe_employee::grow_older.
yes
% we can check this by asking joe its age:
| ?- joe_person::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 % set_score/0, defined in joe_person:
| ?- joe_person::score(Score)
.
Score = 0 yes
% initially, score/1 is only defined for joe_person, so every descendant % or viewpoint will share its value/definition:
| ?- joe_employee::score(Score)
.
Score = 0 yes
% but if we decide to increment the counter by sending the set_score/0 message to a descendant % (don't use message broadcasting syntax in order to workaround a XSB parser bug):
| ?- joe_chess_player::set_score(2200)
, joe_chess_player::score(Score)
.
Score = 2200 yes
% then the descendant will now have a local definition for counter/1, % independent of the definition in its parent, joe_person:
| ?- joe_person::score(Score)
.
Score = 0 yes
% the other descendants/viewpoints will continue to share the definition % in joe_person:
| ?- joe_sportsman::score(Score)
.
Score = 0 yes