home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 June
/
SIMTEL_0692.cdr
/
msdos
/
aijournl
/
ai_oct86.arc
/
OBJECT1.LTG
< prev
next >
Wrap
Text File
|
1986-07-18
|
5KB
|
151 lines
Listing 1
áááááááááááááááááá
An Object-Oriented Prolog System
% object definitionì
add_object(SuperClass,Object,ObjectMethods) :-ì
ááááááááadd_methods(Object,ObjectMethods),ì
áááááááálink(Object,SuperClass).
% definition of a new object - "compiles" object code to Prologì
add_methods(_,[]) :- !.ì
add_methods(Object,[(Head :- Body)|Rest]) :- !,ì
ááááááááHead =.. [Predicate | Args],ì
ááááááááPrologHead =.. [Predicate, Object | Args],ì
ááááááááassert((PrologHead :- Body)),ì
ááááááááfunctor(Object,ObjName,_),ì
ááááááááassert(index(Object,ObjName,(Head :- Body))), % to allow inquiriesì
ááááááááadd_methods(Object,Rest).ì
add_methods(Object,[Method|Rest]) :-ì
ááááááááMethod =.. [Predicate | Args],ì
ááááááááHead =.. [Predicate, Object | Args],ì
ááááááááassert(Head),ì
ááááááááfunctor(Object,ObjName,_),ì
ááááááááassert(index(Object,ObjName,Method)), % to allow inquiriesì
ááááááááadd_methods(Object,Rest).
% create a new isa linkì
link(Object,SuperClass) :-ì
ááááááááclause(isa(Object,SuperClass),true) -> true ; % to avoid redundancyìèááááááááassert(isa(Object,SuperClass)).
create_root :-ì
ááclause(index(obj,obj,_),_) -> true ; % OK if root already thereì
ááadd_methods(obj,ì
áááááááá[description('an object')]).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ì
% execution messageì
send(Object,Message) :-ì
ááááááááMessage =.. [Predicate | Args],ì
ááááááááQuery =.. [Predicate, Object1 | Args],ì
ááááááááisa_chain(Object,Object1),ì
ááááááááclause(Query,Body) -> % override dup methodsì
áááááááácall(Body).
isa_chain(Object, Object). % try the Object itself firstì
isa_chain(Object1,Object3) :- % get ancestorsì
ááááááááisa(Object1,Object2),ì
áááááááá\+Object1=Object2, % to avoid redundancyì
ááááááááisa_chain(Object2,Object3).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% inquiry messages
% what exists?ì
exists(Object) :-ì
ááááááááindex(Object,_,_).
what_exists :-ì
áááááááásetof(Object,exists(Object),Objects),ì
ááááááááwriteList(Objects).
% what objects exist with ObjectName? (in case you forget parameters)ì
object_name(ObjectName) :-ì
áááááááá( index(Object,ObjectName,_),ì
áááááááááááááwrite(Object), nl,ì
ááááááááááááásend(Object,description(What)),ì
ááááááááááááánl, write(What), nl, failì
áááááááá; trueì
áááááááá).
% what are the methods of Object?ì
methods(Object) :-ì
áááááááásetof(Method,ObjName^index(Object,ObjName,Method),Methods),ì
ááááááááwriteList(Methods).
writeList([]) :- !, nl.ì
writeList([Head|Rest]) :-ì
áááááááánl, write(Head), nl,ì
ááááááááwriteList(Rest).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ì
% deletions and unlinkingè
% remove the links for Objectì
unlink(Object) :-ì
áááááááá( retract(isa(Object,_)),ì
áááááááááááááfailì
áááááááá; retract(isa(_,Object)),ì
áááááááááááááfailì
áááááááá; trueì
áááááááá).
% remove a particular linkì
unlink(Object,SuperClass) :-ì
áááááááá( retract(isa(Object,SuperClass)),ì
áááááááááááááfailì
áááááááá; trueì
áááááááá).
% revise the definition of Objectì
redefine_object(SuperClass,Object,Methods) :-ì
ááááááááremove_object(Object),ì
ááááááááadd_object(SuperClass,Object,Methods).
%%% examples:ì
add_circuit_objs :-ì
áácreate_root,ì
ááadd_object(obj,circuit,[]),ì
ááadd_object(circuit,gate,[]),ì
ááadd_object(gate,and_gate(In1,In2),ì
áááááááá[(output(O) :- In1=1, In2=1 -> O=1 ; O=0),ì
áááádescription('an and_gate with Boolean inputs: Input1, Input2') ] ),ì
ááadd_object(gate,or_gate(In1,In2),ì
áááááááá[(output(O) :- In1=0, In2=0 -> O=0 ; O=1),ì
áááádescription('an or_gate with Boolean inputs: Input1, Input2') ] ),ì
ááadd_object(gate,not_gate(In1),ì
áááááááá[(output(O) :- In1=1 -> O=0 ; O=1),ì
áááádescription('a not_gate with Boolean inputs: Input1') ] ),ì
ááadd_object(circuit,circuit1(In1,In2),ì
áááááááá[(output(O) :- send(not_gate(In1),output(Not1)),ì
áááááááááááááááááááááááásend(not_gate(In2),output(Not2)),ì
áááááááááááááááááááááááásend(or_gate(Not1,Not2),output(O)) ),ì
áááádescription('a circuit with Boolean inputs: Input1, Input2') ] ).
/******************* sample log of a Prolog session:
Quintus Prolog Release 2.0 (Sun)ì
Copyright (C) 1986, Quintus Computer Systems, Inc. All rights reserved.
| ?- compile(oops).ì
[compilation completed]ì
[12.600 sec 6632 bytes]ì
| ?- add_circuit_objs.
yesì
| ?- send(circuit1(1,0),output(Out)).
èOut = 1
| ?- send(circuit1(0,1),output(Out)).
Out = 1
| ?- send(circuit1(1,1),output(Out)).
Out = 0
| ?- halt.ì
********************************************************************/