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:
67
Logtalk/examples/points/NOTES
Normal file
67
Logtalk/examples/points/NOTES
Normal file
@@ -0,0 +1,67 @@
|
||||
=================================================================
|
||||
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 points.loader utility
|
||||
file (note that the *.loader files are Prolog files).
|
||||
|
||||
You will need to load the objects in the roots and relations
|
||||
examples (consulting the corresponding roots.loader and relations.loader
|
||||
files).
|
||||
|
||||
You will also need to consult the following files in the library directory:
|
||||
events.loader, types.loader, metapredicates.loader, and hierarchies.loader.
|
||||
|
||||
You can find the original description of this example (and a solution using
|
||||
SICStus Objects) at the URL:
|
||||
http://www.sics.se/ps/sicstus/sicstus_32.html#SEC254
|
||||
|
||||
Suppose you wish to represent one point in a two-dimensional space. The
|
||||
protocol you will have to define consists on the operation move/2, wish
|
||||
allows you to move one point to a new position, and on the operation
|
||||
print/0, which prints the point position. From the base class point,
|
||||
which contains the indicated operations, we wish to build three
|
||||
variants. One class bounded_point in which one point can only move in a
|
||||
restricted area in space. One class history_point, characterized from
|
||||
the particularity that each point recalls its previous positions.
|
||||
Finally, a class bounded_history_point combining the functionality of
|
||||
classes bounded_point and history_point.
|
||||
|
||||
At first sight, this looks like the kind of ideal problem to illustrate
|
||||
the advantages of the multiple inheritance mechanisms. This type of
|
||||
solution holds, however, several problems. If the methods move/2 and
|
||||
print/0 are inherited by bounded_history_point of classes
|
||||
history_point and bounded_point simultaneously, then one point will be
|
||||
moved and shown twice. If the inheritance is carried out, for each
|
||||
method, only from one of the superclasses (assuming that it is possible
|
||||
to do so, only by breaking the apparent problem symmetry), then the
|
||||
interfaces of classes history_point and bounded_point will have to
|
||||
contain separately the necessary operations to verify the limits (in the
|
||||
case of bounded_point), or to recall the previous positions (in the
|
||||
case of history_point). This way, the class bounded_history_point could
|
||||
build its own versions of methods move/2 and print/0, adding to the
|
||||
inherited definitions of one of the superclasses the calling of the
|
||||
operation missing in the other superclass. This is the solution adopted
|
||||
in the SICStus Objects. However, this solution also implies a few
|
||||
problems. Let's suppose that method move/2 is inherited from class
|
||||
history_point. Then, any changing operated in the definition of the same
|
||||
method in class bounded_point is ignored by bounded_history_point. The
|
||||
problem can be unnoticed, once the symmetry suggested by the use of
|
||||
multiple inheritance does not reflect on the present implementation.
|
||||
|
||||
The solution just suggested is, in short, a generalization of the
|
||||
problem previously described. Instead of using multiple inheritance,
|
||||
let's use composition mechanisms. In order to do so, let's separate the
|
||||
operations on one point, while an object state, of the classes
|
||||
representing each one of the point types. This can be achieved through
|
||||
the definition of two new categories, bounded_coordinate and
|
||||
point_history, that will define the operations associated both to the
|
||||
memorization of previous values, and to the verification of feasible
|
||||
limits for a coordinate value. Each one of the point, bounded_point,
|
||||
history_point and bounded_history_point classes will import this
|
||||
category, using his operations so as to define the methods affecting the
|
||||
solutions that use multiple inheritance.
|
81
Logtalk/examples/points/SCRIPT
Normal file
81
Logtalk/examples/points/SCRIPT
Normal file
@@ -0,0 +1,81 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
% let's start with a simple point:
|
||||
|
||||
| ?- point::new(Point,[position-(1, 3)]), Point::(print, move(7, 4), print).
|
||||
|
||||
p1 @ (1, 3)
|
||||
p1 @ (7, 4)
|
||||
|
||||
Point = p1 ?
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% same problem but with bounds on coordinate values:
|
||||
|
||||
| ?- bounded_point::new(Point,[position-(1, 3), bounds(x)-(0, 13), bounds(y)-(-7, 7)]), Point::(print, move(7, 4), print).
|
||||
|
||||
bounds(x) : 0,13
|
||||
bounds(y) : -7,7
|
||||
bp2 @ (1, 3)
|
||||
bounds(x) : 0,13
|
||||
bounds(y) : -7,7
|
||||
bp2 @ (7, 4)
|
||||
|
||||
Point = bp2 ?
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% same problem but storing the history of coordinate values:
|
||||
|
||||
| ?- history_point::new(Point,[position-(1, 3)]), Point::(print, move(7, 4), print).
|
||||
|
||||
location history: []
|
||||
hp3 @ (1, 3)
|
||||
location history: [(1,3)]
|
||||
hp3 @ (7, 4)
|
||||
|
||||
Point = hp3 ?
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% same problem but with bounds on coordinate values and storing past values:
|
||||
|
||||
| ?- bounded_history_point::new(Point,[position-(1, 3), bounds(x)-(0, 13), bounds(y)-(-7, 7)]), Point::(print, move(7, 4), print).
|
||||
|
||||
bounds(x) : 0,13
|
||||
bounds(y) : -7,7
|
||||
location history: []
|
||||
bhp4 @ (1, 3)
|
||||
bounds(x) : 0,13
|
||||
bounds(y) : -7,7
|
||||
location history: [(1,3)]
|
||||
bhp4 @ (7, 4)
|
||||
|
||||
Point = bhp4 ?
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% clean up instances:
|
||||
|
||||
| ?- point::delete_all.
|
||||
yes
|
||||
|
||||
| ?- bounded_point::delete_all.
|
||||
yes
|
||||
|
||||
| ?- history_point::delete_all.
|
||||
yes
|
||||
|
||||
| ?- bounded_history_point::delete_all.
|
||||
yes
|
70
Logtalk/examples/points/bounded_coordinate.lgt
Normal file
70
Logtalk/examples/points/bounded_coordinate.lgt
Normal file
@@ -0,0 +1,70 @@
|
||||
|
||||
:- category(bounded_coordinate).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 1998/3/23,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Point coordinate bounds management predicates.',
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
:- public(set_bounds/3).
|
||||
:- mode(set_bounds(+atom, +integer, +integer), one).
|
||||
|
||||
:- public(clear_bounds/1).
|
||||
:- mode(clear_bounds(+atom), one).
|
||||
|
||||
:- public(bounds/3).
|
||||
:- mode(bounds(?atom, ?integer, ?integer), zero_or_more).
|
||||
|
||||
:- public(check_bounds/2).
|
||||
:- mode(check_bounds(+atom, +integer), zero_or_one).
|
||||
|
||||
:- public(print_bounds/1).
|
||||
:- mode(print_bounds(?atom), zero_or_more).
|
||||
|
||||
:- public(valid_value/2).
|
||||
:- mode(valid_value(+atom, +integer), zero_or_one).
|
||||
|
||||
:- private(bounds_/3).
|
||||
:- dynamic(bounds_/3).
|
||||
:- mode(bounds_(?atom, ?integer, ?integer), zero_or_more).
|
||||
|
||||
|
||||
set_bounds(Coordinate, Min, Max) :-
|
||||
::retractall(bounds_(Coordinate, _, _)),
|
||||
::assertz(bounds_(Coordinate, Min, Max)).
|
||||
|
||||
|
||||
clear_bounds(Coordinate) :-
|
||||
::retractall(bounds_(Coordinate, _, _)).
|
||||
|
||||
|
||||
bounds(Coordinate, Min, Max) :-
|
||||
::bounds_(Coordinate, Min, Max).
|
||||
|
||||
|
||||
check_bounds(Coordinate, Value) :-
|
||||
::bounds_(Coordinate, Min, Max),
|
||||
Value >= Min,
|
||||
Value =< Max.
|
||||
|
||||
|
||||
print_bounds(Coordinate) :-
|
||||
::bounds_(Coordinate, Min, Max),
|
||||
writeq(bounds(Coordinate)),
|
||||
write(' : '),
|
||||
write((Min, Max)),
|
||||
nl.
|
||||
|
||||
|
||||
valid_value(Coordinate, Value) :-
|
||||
::bounds_(Coordinate, Min, Max) ->
|
||||
Value >= Min, Value =< Max
|
||||
;
|
||||
true.
|
||||
|
||||
|
||||
:- end_category.
|
54
Logtalk/examples/points/bounded_history_point.lgt
Normal file
54
Logtalk/examples/points/bounded_history_point.lgt
Normal file
@@ -0,0 +1,54 @@
|
||||
|
||||
:- object(bounded_history_point,
|
||||
imports(bounded_coordinate, point_history),
|
||||
instantiates(class),
|
||||
specializes(point)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.1,
|
||||
date is 2000/10/31,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Two dimensional point moving in a constrained area and remembering past point positions.',
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
move(X, Y) :-
|
||||
::check_bounds(x, X),
|
||||
::check_bounds(y, Y),
|
||||
::position(OldX, OldY),
|
||||
^^move(X, Y),
|
||||
::add_to_history((OldX, OldY)).
|
||||
|
||||
|
||||
print :-
|
||||
::print_bounds(x),
|
||||
::print_bounds(y),
|
||||
::print_history,
|
||||
^^print.
|
||||
|
||||
|
||||
instance_base_name(bhp).
|
||||
|
||||
|
||||
default_init_option(history-[]).
|
||||
|
||||
default_init_option(bounds(x)-(-10, 10)).
|
||||
|
||||
default_init_option(bounds(y)-(-10, 10)).
|
||||
|
||||
default_init_option(Default) :-
|
||||
^^default_init_option(Default).
|
||||
|
||||
|
||||
process_init_option(history-History) :-
|
||||
::init_history(History).
|
||||
|
||||
process_init_option(bounds(Coordinate)-(Min, Max)) :-
|
||||
::set_bounds(Coordinate, Min, Max).
|
||||
|
||||
process_init_option(Option) :-
|
||||
^^process_init_option(Option).
|
||||
|
||||
|
||||
:- end_object.
|
49
Logtalk/examples/points/bounded_point.lgt
Normal file
49
Logtalk/examples/points/bounded_point.lgt
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
:- object(bounded_point,
|
||||
imports(bounded_coordinate),
|
||||
instantiates(class),
|
||||
specializes(point)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.1,
|
||||
date is 2000/10/31,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Two dimensional point moving in a constrained area.',
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
:- uses(list).
|
||||
|
||||
|
||||
move(X, Y) :-
|
||||
::check_bounds(x, X),
|
||||
::check_bounds(y, Y),
|
||||
^^move(X, Y).
|
||||
|
||||
|
||||
print :-
|
||||
::print_bounds(x),
|
||||
::print_bounds(y),
|
||||
^^print.
|
||||
|
||||
|
||||
instance_base_name(bp).
|
||||
|
||||
|
||||
default_init_option(bounds(x)-(-10, 10)).
|
||||
|
||||
default_init_option(bounds(y)-(-10, 10)).
|
||||
|
||||
default_init_option(Default) :-
|
||||
^^default_init_option(Default).
|
||||
|
||||
|
||||
process_init_option(bounds(Coordinate)-(Min, Max)) :-
|
||||
::set_bounds(Coordinate, Min, Max).
|
||||
|
||||
process_init_option(Option) :-
|
||||
^^process_init_option(Option).
|
||||
|
||||
|
||||
:- end_object.
|
43
Logtalk/examples/points/history_point.lgt
Normal file
43
Logtalk/examples/points/history_point.lgt
Normal file
@@ -0,0 +1,43 @@
|
||||
|
||||
:- object(history_point,
|
||||
imports(point_history),
|
||||
instantiates(class),
|
||||
specializes(point)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.1,
|
||||
date is 2000/10/31,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Two dimensional point remembering past positions.',
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
move(X, Y) :-
|
||||
::position(OldX, OldY),
|
||||
^^move(X, Y),
|
||||
::add_to_history((OldX, OldY)).
|
||||
|
||||
|
||||
print :-
|
||||
::print_history,
|
||||
^^print.
|
||||
|
||||
|
||||
instance_base_name(hp).
|
||||
|
||||
|
||||
default_init_option(history-[]).
|
||||
|
||||
default_init_option(Default) :-
|
||||
^^default_init_option(Default).
|
||||
|
||||
|
||||
process_init_option(history-History) :-
|
||||
::init_history(History).
|
||||
|
||||
process_init_option(Option) :-
|
||||
^^process_init_option(Option).
|
||||
|
||||
|
||||
:- end_object.
|
57
Logtalk/examples/points/point.lgt
Normal file
57
Logtalk/examples/points/point.lgt
Normal file
@@ -0,0 +1,57 @@
|
||||
|
||||
:- object(point,
|
||||
instantiates(class),
|
||||
specializes(object)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.1,
|
||||
date is 2000/10/31,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Two dimensional point class.',
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
:- public(move/2).
|
||||
:- mode(move(+integer, +integer), zero_or_one).
|
||||
|
||||
:- public(position/2).
|
||||
:- mode(position(?integer, ?integer), one).
|
||||
|
||||
:- private(position_/2).
|
||||
:- dynamic(position_/2).
|
||||
:- mode(position_(?integer, ?integer), one).
|
||||
|
||||
|
||||
move(X, Y) :-
|
||||
::retractall(position_(_, _)),
|
||||
::assertz(position_(X, Y)).
|
||||
|
||||
|
||||
position(X, Y) :-
|
||||
::position_(X, Y).
|
||||
|
||||
|
||||
print :-
|
||||
self(Self),
|
||||
::position_(X, Y),
|
||||
writeq(Self), write(' @ '), write((X, Y)), nl.
|
||||
|
||||
|
||||
default_init_option(position-(0, 0)).
|
||||
|
||||
default_init_option(Default) :-
|
||||
^^default_init_option(Default).
|
||||
|
||||
|
||||
process_init_option(position-(X, Y)) :-
|
||||
::assertz(position_(X, Y)).
|
||||
|
||||
process_init_option(Option) :-
|
||||
^^process_init_option(Option).
|
||||
|
||||
|
||||
instance_base_name(p).
|
||||
|
||||
|
||||
:- end_object.
|
51
Logtalk/examples/points/point_history.lgt
Normal file
51
Logtalk/examples/points/point_history.lgt
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
:- category(point_history).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 1998/3/23,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Point position history management predicates.',
|
||||
source is 'Example adopted from the SICStus Objects documentation.']).
|
||||
|
||||
|
||||
:- public(add_to_history/1).
|
||||
:- mode(add_to_history(+nonvar), one).
|
||||
|
||||
:- public(init_history/1).
|
||||
:- mode(init_history(+list), one).
|
||||
|
||||
:- public(history/1).
|
||||
:- mode(history(-list), zero_or_one).
|
||||
|
||||
:- public(print_history/0).
|
||||
:- mode(print_history, zero_or_one).
|
||||
|
||||
:- private(history_/1).
|
||||
:- dynamic(history_/1).
|
||||
:- mode(history_(-list), zero_or_one).
|
||||
|
||||
|
||||
add_to_history(Location) :-
|
||||
::retract(history_(History)),
|
||||
::assertz(history_([Location| History])).
|
||||
|
||||
|
||||
init_history(History) :-
|
||||
::retractall(history_(_)),
|
||||
::assertz(history_(History)).
|
||||
|
||||
|
||||
history(History) :-
|
||||
::history_(History).
|
||||
|
||||
|
||||
print_history :-
|
||||
::history_(History),
|
||||
write('location history: '),
|
||||
write(History),
|
||||
nl.
|
||||
|
||||
|
||||
:- end_category.
|
9
Logtalk/examples/points/points.loader
Normal file
9
Logtalk/examples/points/points.loader
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load([
|
||||
bounded_coordinate,
|
||||
bounded_history_point,
|
||||
bounded_point,
|
||||
history_point,
|
||||
point,
|
||||
point_history])).
|
Reference in New Issue
Block a user