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:
23
Logtalk/examples/reflection/NOTES
Normal file
23
Logtalk/examples/reflection/NOTES
Normal file
@@ -0,0 +1,23 @@
|
||||
=================================================================
|
||||
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 reflection.loader utility
|
||||
file (note that the *.loader files are Prolog files). Run this example with
|
||||
no other examples loaded at the same time.
|
||||
|
||||
This folder contains an example that shows how to implement a reflective
|
||||
class-based system. There are three main classes:
|
||||
|
||||
object - root of the inheritance graph
|
||||
class - default metaclass for all instantiable classes
|
||||
abstract_class - default metaclass for all abstract classes
|
||||
|
||||
Each class inherit all the methods form the other two classes and from
|
||||
itself (without any inheritance loops of course ;-).
|
||||
|
||||
You can find more sophisticated versions of these classes in the roots
|
||||
example.
|
129
Logtalk/examples/reflection/SCRIPT
Normal file
129
Logtalk/examples/reflection/SCRIPT
Normal file
@@ -0,0 +1,129 @@
|
||||
=================================================================
|
||||
Logtalk - Object oriented extension to Prolog
|
||||
Release 2.8.4
|
||||
|
||||
Copyright (c) 1998-2001 Paulo Moura. All Rights Reserved.
|
||||
=================================================================
|
||||
|
||||
|
||||
% print the (public and protected) interface of each class:
|
||||
|
||||
| ?- (object, abstract_class, class)::print.
|
||||
|
||||
Object: object
|
||||
|
||||
interface:
|
||||
new/1
|
||||
delete/1
|
||||
instances/1
|
||||
metaclass/0
|
||||
abstract_class/0
|
||||
strict_instance/0
|
||||
print/0
|
||||
|
||||
Object: abstract_class
|
||||
|
||||
interface:
|
||||
new/1
|
||||
delete/1
|
||||
instances/1
|
||||
metaclass/0
|
||||
abstract_class/0
|
||||
strict_instance/0
|
||||
print/0
|
||||
|
||||
Object: class
|
||||
|
||||
interface:
|
||||
new/1
|
||||
delete/1
|
||||
instances/1
|
||||
metaclass/0
|
||||
abstract_class/0
|
||||
strict_instance/0
|
||||
print/0
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% class is the metaclass of all classes:
|
||||
|
||||
| ?- class::instances(Instances), class::metaclass.
|
||||
|
||||
Instances = [class,abstract_class,object]
|
||||
yes
|
||||
|
||||
|
||||
% create an abstract_class, check it and print its interface:
|
||||
|
||||
| ?- abstract_class::new(ac), ac::abstract_class, ac::print.
|
||||
|
||||
Object: ac
|
||||
|
||||
interface:
|
||||
metaclass/0
|
||||
abstract_class/0
|
||||
strict_instance/0
|
||||
print/0
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% try to create an instance of the abstract class:
|
||||
|
||||
| ?- ac::new(i).
|
||||
|
||||
uncaught exception: error(existence_error(predicate_declaration,new(i)),ac::new(i),user)
|
||||
|
||||
|
||||
% create a new instantiable class and print its interface:
|
||||
|
||||
| ?- class::new(c), c::print.
|
||||
|
||||
Object: c
|
||||
|
||||
interface:
|
||||
new/1
|
||||
delete/1
|
||||
instances/1
|
||||
metaclass/0
|
||||
abstract_class/0
|
||||
strict_instance/0
|
||||
print/0
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% create an instance of the new class:
|
||||
|
||||
| ?- c::new(i), c::instances(Instances).
|
||||
|
||||
Instances = [i]
|
||||
yes
|
||||
|
||||
|
||||
% because c does not declare any predicates, its instances have no interface:
|
||||
|
||||
| ?- i::current_predicate(Predicate).
|
||||
|
||||
no
|
||||
|
||||
|
||||
% create an instance of object, root of the inheritance graph, and print its interface:
|
||||
|
||||
| ?- object::new(j), j::print.
|
||||
|
||||
Object: j
|
||||
|
||||
interface:
|
||||
strict_instance/0
|
||||
print/0
|
||||
|
||||
yes
|
||||
|
||||
|
||||
% delete the dynamic objects that we created:
|
||||
|
||||
| ?- c::delete(i), class::delete(c), abstract_class::delete(ac), object::delete(j).
|
||||
|
||||
yes
|
38
Logtalk/examples/reflection/abstract_class.lgt
Normal file
38
Logtalk/examples/reflection/abstract_class.lgt
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
:- object(abstract_class,
|
||||
instantiates(class),
|
||||
specializes(object)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Default metaclass for all abstract classes.']).
|
||||
|
||||
|
||||
:- public(metaclass/0).
|
||||
:- mode(metaclass, zero_or_one).
|
||||
|
||||
|
||||
:- public(abstract_class/0).
|
||||
:- mode(abstract_class, zero_or_one).
|
||||
|
||||
|
||||
abstract_class :-
|
||||
self(Self),
|
||||
Self \= abstract_class.
|
||||
|
||||
|
||||
metaclass :-
|
||||
self(Self),
|
||||
once((
|
||||
instantiates_class(Class, Self),
|
||||
Class::current_predicate(abstract_class/0))).
|
||||
|
||||
|
||||
strict_instance :-
|
||||
fail.
|
||||
|
||||
|
||||
:- end_object.
|
46
Logtalk/examples/reflection/class.lgt
Normal file
46
Logtalk/examples/reflection/class.lgt
Normal file
@@ -0,0 +1,46 @@
|
||||
|
||||
:- object(class,
|
||||
instantiates(class),
|
||||
specializes(abstract_class)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Instantiation root and default metaclass for all classes.']).
|
||||
|
||||
|
||||
:- public(new/1).
|
||||
:- mode(new(+object), zero_or_one).
|
||||
|
||||
:- public(delete/1).
|
||||
:- mode(delete(+object), zero_or_one).
|
||||
|
||||
:- public(instances/1).
|
||||
:- mode(instances(-list), one).
|
||||
|
||||
|
||||
new(Object) :-
|
||||
self(Self),
|
||||
create_object(Object, [instantiates(Self)], [], []).
|
||||
|
||||
|
||||
delete(Object) :-
|
||||
self(Self),
|
||||
instantiates_class(Object, Self),
|
||||
\+ instantiates_class(_, Object),
|
||||
\+ specializes_class(_, Object),
|
||||
abolish_object(Object).
|
||||
|
||||
|
||||
instances(Instances) :-
|
||||
self(Self),
|
||||
findall(Instance, instantiates_class(Instance, Self), Instances).
|
||||
|
||||
|
||||
abstract_class :-
|
||||
fail.
|
||||
|
||||
|
||||
:- end_object.
|
33
Logtalk/examples/reflection/object.lgt
Normal file
33
Logtalk/examples/reflection/object.lgt
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
:- object(object,
|
||||
instantiates(class)).
|
||||
|
||||
|
||||
:- info([
|
||||
authors is 'Paulo Moura',
|
||||
version is 1.0,
|
||||
date is 2000/4/22,
|
||||
comment is 'Inheritance root for all objects.']).
|
||||
|
||||
|
||||
:- public(strict_instance/0).
|
||||
:- mode(strict_instance, zero_or_one).
|
||||
|
||||
:- public(print/0).
|
||||
:- mode(print, one).
|
||||
|
||||
|
||||
strict_instance.
|
||||
|
||||
|
||||
print :-
|
||||
self(Self),
|
||||
write('Object: '), writeq(Self), nl, nl,
|
||||
write(' interface:'), nl,
|
||||
forall(
|
||||
::current_predicate(Predicate),
|
||||
(write(' '), writeq(Predicate), nl)),
|
||||
nl.
|
||||
|
||||
|
||||
:- end_object.
|
6
Logtalk/examples/reflection/reflection.loader
Normal file
6
Logtalk/examples/reflection/reflection.loader
Normal file
@@ -0,0 +1,6 @@
|
||||
|
||||
:- initialization(
|
||||
logtalk_load([
|
||||
class,
|
||||
abstract_class,
|
||||
object])).
|
Reference in New Issue
Block a user