home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / proglang / pie2.arj / OBJECT.PRO < prev    next >
Text File  |  2000-01-01  |  2KB  |  70 lines

  1.  
  2.   % call1/1
  3.   call1("true"):-
  4.     !.
  5.   call1(Method):-
  6.     call(Method).
  7.  
  8.   % get_method/3
  9.   get_method(Message,[FirstMethod|Rest],Method):-
  10.     fact_or_rule(Message,FirstMethod,Method).
  11.   get_method(Message,[_|Rest],Method):-
  12.     get_method(Message,Rest,Method).
  13.  
  14.   % fact_or_rule/3
  15.    fact_or_rule(Message,Message,"true").
  16.    fact_or_rule(Message,(Message :- Body),Body).
  17.  
  18.   % isa/2
  19.   isa(klingon(X_Velocity,Y_Velocity,Mass),spaceship(X_Velocity,Y_Velocity,Mass)).
  20.   isa(enterprise(X_Velocity,Y_Velocity,Mass),spaceship(X_Velocity,Y_Velocity,Mass)).
  21.  
  22.   % isa_hierarchy/2
  23.   isa_hierarchy(Object,Object).
  24.   isa_hierarchy(Object,Object1):-
  25.     isa(Object,Object2),
  26.     isa_hierarchy(Object2,Object1).
  27.  
  28.  
  29.   % send/2
  30.   send(Object,Message):-
  31.     isa_hierarchy(Object,Object1),
  32.     object(Object1,Methodlist),
  33.     get_method(Message,Methodlist,Method),
  34.     call1(Method).
  35.  
  36.   % st/1
  37.   st(E):-
  38.     concat("Example of object"," oriented programming.",E),
  39.     write("Finished method for spaceship."),
  40.     nl.
  41.  
  42.  
  43.   % start/0
  44.   start():-
  45.     write("Sending message to Enterprise..."),
  46.     send(enterprise(20,30,40),kinetic_energy(E)),
  47.     nl,
  48.     write("Energy is "),
  49.     write(E),
  50.     nl,
  51.     write("Sending description message to Klingon..."),
  52.     send(klingon(_,_,_),description(D)),
  53.     nl,
  54.     write("Klingon is "),
  55.     write(D),
  56.     nl,
  57.     send(spaceship(A),ee(A)),
  58.     write(A),
  59.     nl.
  60.  
  61.   % object/2
  62.   object(spaceship(E),[(ee(E):-st(E))]).
  63.   object(spaceship(X_Velocity,Y_Velocity,Mass),
  64.   [(kinetic_energy(E) :- E is 0.5 * Mass * (X_Velocity * X_Velocity + Y_Velocity * Y_Velocity)),
  65.   (speed(S) :- S is sqrt(X_Velocity * X_Velocity + Y_Velocity * Y_Velocity)),
  66.   description("a space ship")]).
  67.   object(enterprise(X_Velocity,Y_Velocity,Mass),[description("a Federation Star Ship")]).
  68.   object(klingon(X_Velocity,Y_Velocity,Mass),[description("an enemy Star Ship")]).
  69.   object(romulan(X,Y,M),[description("an enemy Star Ship")]).
  70.