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:
26
Logtalk/examples/bricks/NOTES
Normal file
26
Logtalk/examples/bricks/NOTES
Normal file
@@ -0,0 +1,26 @@
|
||||
=================================================================
|
||||
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 bricks.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.
|
||||
|
||||
This folder contains an example of representation and handling of
|
||||
relations using events. We have instances of class brick and a binary
|
||||
brick_stack relation between the bricks. Every time we move a brick, we
|
||||
want the bricks on top of it to move along. If we break the stack by
|
||||
moving a middle brick, we want to automatically destroy the
|
||||
corresponding relation tuple.
|
||||
|
||||
It's instructive to use the debugger to better understand this example.
|
||||
Set spy points in all brick instances and then activate the debugger.
|
225
Logtalk/examples/bricks/SCRIPT
Normal file
225
Logtalk/examples/bricks/SCRIPT
Normal file
@@ -0,0 +1,225 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
|
||||
% create four bricks, all standing on the "ground" (use your imagination... ;-)
|
||||
|
||||
| ?- brick::new(a, [position-(8, 1)]).
|
||||
yes
|
||||
|
||||
| ?- brick::new(b, [position-(6, 1)]).
|
||||
yes
|
||||
|
||||
| ?- brick::new(c, [position-(4, 1)]).
|
||||
yes
|
||||
|
||||
| ?- brick::new(d, [position-(2, 1)]).
|
||||
yes
|
||||
|
||||
% set up ascii stack monitor so we can watch the bricks moving
|
||||
|
||||
| ?- after_event_registry::set_monitor(_, move(_,_), _, stack_monitor).
|
||||
yes
|
||||
|
||||
|
||||
% make the stack
|
||||
|
||||
| ?- brick_stack::add_tuple([c,d]).
|
||||
|.c......
|
||||
|.d...b.a
|
||||
---------
|
||||
yes
|
||||
|
||||
| ?- brick_stack::add_tuple([b,c]).
|
||||
|.b......
|
||||
|.c......
|
||||
|.d.....a
|
||||
---------
|
||||
yes
|
||||
|
||||
| ?- brick_stack::add_tuple([a,b]).
|
||||
|.a
|
||||
|.b
|
||||
|.c
|
||||
|.d
|
||||
---
|
||||
yes
|
||||
|
||||
|
||||
% check results
|
||||
|
||||
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
|
||||
[c,d]
|
||||
[b,c]
|
||||
[a,b]
|
||||
no
|
||||
|
||||
| ?- before_event_registry::monitors(Mb), after_event_registry::monitors(Ma).
|
||||
Ma = [brick_stack, stack_monitor]
|
||||
Mb = [brick_stack]
|
||||
yes
|
||||
|
||||
|
||||
% move all stack to new position by moving bottom brick; check results
|
||||
|
||||
| ?- d::move(9, 1).
|
||||
|.a.......
|
||||
|.b.......
|
||||
|.c.......
|
||||
|........d
|
||||
----------
|
||||
|.a.......
|
||||
|.b.......
|
||||
|........c
|
||||
|........d
|
||||
----------
|
||||
|.a.......
|
||||
|........b
|
||||
|........c
|
||||
|........d
|
||||
----------
|
||||
|........a
|
||||
|........b
|
||||
|........c
|
||||
|........d
|
||||
----------
|
||||
yes
|
||||
|
||||
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
|
||||
Xa = 9,
|
||||
Xb = 9,
|
||||
Xc = 9,
|
||||
Xd = 9,
|
||||
Ya = 4,
|
||||
Yb = 3,
|
||||
Yc = 2,
|
||||
Yd = 1
|
||||
yes
|
||||
|
||||
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
|
||||
[c,d]
|
||||
[b,c]
|
||||
[a,b]
|
||||
no
|
||||
|
||||
|
||||
% break stack in half by moving b to the "ground"; check results
|
||||
|
||||
| ?- b::move(3, 1).
|
||||
|........a
|
||||
|.........
|
||||
|........c
|
||||
|..b.....d
|
||||
----------
|
||||
|..a.....c
|
||||
|..b.....d
|
||||
----------
|
||||
yes
|
||||
|
||||
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
|
||||
Xa = 3,
|
||||
Xb = 3,
|
||||
Xc = 9,
|
||||
Xd = 9,
|
||||
Ya = 2,
|
||||
Yb = 1,
|
||||
Yc = 2,
|
||||
Yd = 1
|
||||
yes
|
||||
|
||||
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
|
||||
[c,d]
|
||||
[a,b]
|
||||
no
|
||||
|
||||
|
||||
% create new brick_stack tuple ; check results
|
||||
|
||||
| ?- brick_stack::add_tuple([d, a]).
|
||||
|..d......
|
||||
|..a.....c
|
||||
|..b......
|
||||
----------
|
||||
|..c
|
||||
|..d
|
||||
|..a
|
||||
|..b
|
||||
----
|
||||
yes
|
||||
|
||||
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
|
||||
Xa = 3,
|
||||
Xb = 3,
|
||||
Xc = 3,
|
||||
Xd = 3,
|
||||
Ya = 2,
|
||||
Yb = 1,
|
||||
Yc = 4,
|
||||
Yd = 3
|
||||
yes
|
||||
|
||||
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
|
||||
[c,d]
|
||||
[a,b]
|
||||
[d,a]
|
||||
no
|
||||
|
||||
|
||||
% move all stack to new position by moving bottom brick; check results
|
||||
|
||||
| ?- b::move(5, 1).
|
||||
|..c..
|
||||
|..d..
|
||||
|..a..
|
||||
|....b
|
||||
------
|
||||
|..c..
|
||||
|..d..
|
||||
|....a
|
||||
|....b
|
||||
------
|
||||
|..c..
|
||||
|....d
|
||||
|....a
|
||||
|....b
|
||||
------
|
||||
|....c
|
||||
|....d
|
||||
|....a
|
||||
|....b
|
||||
------
|
||||
yes
|
||||
|
||||
| ?- a::position(Xa, Ya), b::position(Xb, Yb), c::position(Xc, Yc), d::position(Xd, Yd).
|
||||
Xa = 5,
|
||||
Xb = 5,
|
||||
Xc = 5,
|
||||
Xd = 5,
|
||||
Ya = 2,
|
||||
Yb = 1,
|
||||
Yc = 4,
|
||||
Yd = 3
|
||||
yes
|
||||
|
||||
| ?- brick_stack::tuple(Tuple), write(Tuple), nl, fail.
|
||||
[c,d]
|
||||
[a,b]
|
||||
[d,a]
|
||||
no
|
||||
|
||||
|
||||
% clean up instances, tuples and monitors
|
||||
|
||||
| ?- brick_stack::remove_all_tuples.
|
||||
yes
|
||||
|
||||
| ?- after_event_registry::del_monitors(_, _, _, stack_monitor).
|
||||
yes
|
||||
|
||||
| ?- brick::delete_all.
|
||||
yes
|
76
Logtalk/examples/bricks/brick.lgt
Normal file
76
Logtalk/examples/bricks/brick.lgt
Normal file
@@ -0,0 +1,76 @@
|
||||
|
||||
:- object(brick,
|
||||
instantiates(class),
|
||||
specializes(object)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.1,
|
||||
date is 2000/10/31,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Two-dimensional brick (or should I say square?) class.']).
|
||||
|
||||
|
||||
:- public(position/2).
|
||||
|
||||
:- mode(position(?integer, ?integer), zero_or_one).
|
||||
|
||||
:- info(position/2, [
|
||||
comment is 'Brick current position.',
|
||||
argnames is ['X', 'Y']]).
|
||||
|
||||
|
||||
:- private(position_/2).
|
||||
:- dynamic(position_/2).
|
||||
|
||||
:- mode(position_(?integer, ?integer), zero_or_one).
|
||||
|
||||
:- info(position_/2, [
|
||||
comment is 'Stores brick current position.',
|
||||
argnames is ['X', 'Y']]).
|
||||
|
||||
|
||||
:- public(move/2).
|
||||
|
||||
:- mode(move(+integer, +integer), one).
|
||||
|
||||
:- info(move/2, [
|
||||
comment is 'Moves a brick to a new position.',
|
||||
argnames is ['X', 'Y']]).
|
||||
|
||||
|
||||
position(X, Y) :-
|
||||
::position_(X, Y).
|
||||
|
||||
|
||||
move(X, Y) :-
|
||||
::retractall(position_(_, _)),
|
||||
::assertz(position_(X, Y)).
|
||||
|
||||
|
||||
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).
|
||||
|
||||
|
||||
valid_init_option(position-(X, Y)) :-
|
||||
!,
|
||||
integer(X),
|
||||
integer(Y).
|
||||
|
||||
valid_init_option(Option) :-
|
||||
^^valid_init_option(Option).
|
||||
|
||||
|
||||
instance_base_name(b).
|
||||
|
||||
|
||||
:- end_object.
|
59
Logtalk/examples/bricks/brick_stack.lgt
Normal file
59
Logtalk/examples/bricks/brick_stack.lgt
Normal file
@@ -0,0 +1,59 @@
|
||||
|
||||
:- object(brick_stack,
|
||||
instantiates(constrained_relation)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 1998/3/23,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Stack of bricks as a constrained binary relation.']).
|
||||
|
||||
|
||||
descriptor_([top, bottom]).
|
||||
|
||||
|
||||
domain_(top, brick).
|
||||
domain_(bottom, brick).
|
||||
|
||||
|
||||
key_([top, bottom]).
|
||||
|
||||
|
||||
cardinality_(top, 0, 1).
|
||||
cardinality_(bottom, 0, 1).
|
||||
|
||||
|
||||
delete_option_(top, cascade).
|
||||
delete_option_(bottom, restrict).
|
||||
|
||||
|
||||
add_tuple([A, B]) :-
|
||||
B::position(Xb, Yb),
|
||||
Ya2 is Yb + 1,
|
||||
{A::move(Xb, Ya2)},
|
||||
^^add_tuple([A, B]).
|
||||
|
||||
|
||||
activ_points_(top, before, []).
|
||||
activ_points_(top, after, [move(_, _)]).
|
||||
|
||||
activ_points_(bottom, before, []).
|
||||
activ_points_(bottom, after, [move(_, _)]).
|
||||
|
||||
|
||||
propagate(after, move(X, Y), Top, top, [Top, Bottom]) :-
|
||||
!,
|
||||
Y2 is Y - 1,
|
||||
(Bottom::position(X, Y2) ->
|
||||
true
|
||||
;
|
||||
::remove_tuple([Top, Bottom])).
|
||||
|
||||
propagate(after, move(X, Y), Bottom, bottom, [Top, Bottom]) :-
|
||||
!,
|
||||
Y2 is Y + 1,
|
||||
{Top::move(X, Y2)}.
|
||||
|
||||
|
||||
:- end_object.
|
6
Logtalk/examples/bricks/bricks.loader
Normal file
6
Logtalk/examples/bricks/bricks.loader
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load([
|
||||
brick,
|
||||
brick_stack,
|
||||
stack_monitor])).
|
38
Logtalk/examples/bricks/stack_monitor.lgt
Normal file
38
Logtalk/examples/bricks/stack_monitor.lgt
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
:- object(stack_monitor,
|
||||
implements(event_handlersp)).
|
||||
|
||||
|
||||
:- info([
|
||||
version is 1.0,
|
||||
date is 1998/3/23,
|
||||
authors is 'Paulo Moura',
|
||||
comment is 'Monitor for brick movements printing an ascii representation of each brick position.']).
|
||||
|
||||
|
||||
:- uses(loop).
|
||||
:- uses(list).
|
||||
|
||||
|
||||
after(_, move(_, _), _) :-
|
||||
findall(
|
||||
(Brick, X, Y),
|
||||
(instantiates_class(Brick, brick), Brick::position(X, Y)),
|
||||
Bricks),
|
||||
setof(X, Brick^Y^ (list::member((Brick,X,Y), Bricks)), Xs),
|
||||
list::last(Xs, Xmax),
|
||||
setof(Y, Brick^X^ (list::member((Brick,X,Y), Bricks)), Ys),
|
||||
list::last(Ys, Ymax),
|
||||
loop::fordownto(Y, Ymax, 1,
|
||||
(write('|'),
|
||||
loop::forto(X, 1, Xmax,
|
||||
(list::member((Brick, X, Y), Bricks) ->
|
||||
write(Brick)
|
||||
;
|
||||
write('.'))),
|
||||
nl)),
|
||||
write('-'),
|
||||
loop::forto(X, 1, Xmax, write('-')), nl.
|
||||
|
||||
|
||||
:- end_object.
|
Reference in New Issue
Block a user