Chapter #10 Examples in Oz
% Defined in previous chapters
class FishD
meth getClass(?$) FishD end
meth instanceOf(F ?$) F == FishD end
meth equals(F ?$) {F instanceOf(FishD $)} end
end
class Anchovy from FishD
meth init skip end
meth getClass(?$) Anchovy end
meth instanceOf(F ?$) F == Anchovy orelse FishD,instanceOf(F $) end
meth equals(F ?$) {F instanceOf(Anchovy $)} end
end
class Salmon from FishD
meth init skip end
meth getClass(?$) Salmon end
meth instanceOf(F ?$) F == Salmon orelse FishD,instanceOf(F $) end
meth equals(F ?$) {F instanceOf(Salmon $)} end
end
class Tuna from FishD
meth init skip end
meth getClass(?$) Tuna end
meth instanceOf(F ?$) F == Tuna orelse FishD,instanceOf(F $) end
meth equals(F ?$) {F instanceOf(Tuna $)} end
end
%%%%%%%%%%%%%%%%%%% Chapter - 10 %%%%%%%%%%%%%%%%%%%%%%
% 10.6
class PiemanI
meth addTopping(T $) raise abstract() end end
meth removeTopping(T $) raise abstract() end end
meth substituteTopping(N O $) raise abstract() end end
meth occursTopping(O $) raise abstract() end end
end
% 10.2
class PiemanM from PiemanI
attr P
meth init P := {New Bottom init} end
meth addTopping(T ?$)
P := {New Topping init(T @P)}
{self occursTopping(T $)}
end
meth removeTopping(T ?$)
P := {@P accept({New RemoveV init(T)} $)}
{self occursTopping(T $)}
end
meth substituteTopping(N O ?$)
P := {@P accept({New SubstituteV init(N O)} $)}
{self occursTopping(N $)}
end
meth occursTopping(O ?$)
{@P accept({New OccursV init(O)} $)}
end
end
% 10.8
class PieVisitorI
meth forBottom($) raise abstract() end end
meth forTopping(T R $) raise abstract() end end
end
class PieD
meth accept(Ask ?$) raise abstract() end end
end
class Bottom from PieD
meth init skip end
meth accept(Ask ?$) {Ask forBottom($)} end
end
class Topping from PieD
feat T R
meth init(Ti Ri)
self.T = Ti
self.R = Ri
end
meth accept(Ask ?$) {Ask forTopping(self.T self.R $)} end
end
% 10.9
class OccursV from PieVisitorI
feat A
meth init(Ai) self.A = Ai end
meth forBottom(?$) 0 end
meth forTopping(T R ?$)
if {T equals(self.A $)}
then {R accept(self $)} + 1
else {R accept(self $)}
end
end
end
class SubstituteV from PieVisitorI
feat N O
meth init(Ni Oi)
self.N = Ni
self.O = Oi
end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R ?$)
if {self.O equals(T $)}
then {New Topping init(self.N {R accept(self $)})}
else {New Topping init(T {R accept(self $)})}
end
end
end
% 10.10
class RemoveV from PieVisitorI
feat O
meth init(Oi) self.O = Oi end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R ?$)
if {self.O equals(T $)}
then {R accept(self $)}
else {New Topping init(T {R accept(self $)})}
end
end
end
% 10.11
{Browse 11#{{New PiemanM init} occursTopping({New Anchovy init} $)}}
% 10.14
{Browse 14#{{New PiemanM init} addTopping({New Anchovy init} $)}}
% 10.21
{Browse 21#{{New PiemanM init} addTopping({New Anchovy init} $)}}
% 10.23
Y = {New PiemanM init}
% 10.24
{Browse 24#{Y addTopping({New Anchovy init} $)}}
% 10.25
{Browse 25#{Y substituteTopping({New Tuna init} {New Anchovy init} $)}}
% 10.26
{Browse 26#{Y occursTopping({New Anchovy init} $)}}
% 10.27
YY = {New PiemanM init}
_ = {YY addTopping({New Anchovy init} $)}
_ = {YY addTopping({New Anchovy init} $)}
_ = {YY addTopping({New Salmon init} $)}
% 10.28
_ = {YY addTopping({New Tuna init} $)}
_ = {YY addTopping({New Tuna init} $)}
_ = {YY substituteTopping({New Tuna init} {New Anchovy init} $)}
{Browse 28#{YY occursTopping({New Tuna init} $)}}
% 10.29
{Browse 26#{YY removeTopping({New Tuna init} $)}}
% 10.31
{Browse 31#{YY occursTopping({New Salmon init} $)}}
% 10.32
{Browse 32#{Y occursTopping({New Salmon init} $)}}
% 10.41
class PieVisitor2I
meth forBottom(That $) raise abstract() end end
meth forTopping(That $) raise abstract() end end
end
% 10.42
class Pie2D
meth accept(Ask ?$) raise abstract() end end
end
class Bottom2 from Pie2D
meth init skip end
meth accept(Ask ?$) {Ask forBottom(self $)} end
end
% 10.44
class Topping2 from Pie2D
feat t r
meth init(T R)
self.t = {NewCell T}
self.r = {NewCell R}
end
meth accept(Ask ?$) {Ask forTopping(self $)} end
end
% 10.45
class Occurs2V from PieVisitor2I
feat A
meth init(Ai) self.A = Ai end
meth forBottom(That ?$) 0 end
meth forTopping(That ?$)
if {@(That.t) equals(self.A $)}
then {@(That.r) accept(self $)} + 1
else {@(That.r) accept(self $)}
end
end
end
% 10.50
class Remove2V from PieVisitor2I
feat O
meth init(Oi) self.O = Oi end
meth forBottom(That ?$) {New Bottom2 init} end
meth forTopping(That ?$)
if {self.O equals(@(That.t) $)}
then {@(That.r) accept(self $)}
else {New Topping2 init(@(That.t) {@(That.r) accept(self $)})}
end
end
end
% 10.54
class Substitute2V from PieVisitor2I
feat N O
meth init(Ni Oi)
self.N = Ni
self.O = Oi
end
meth forBottom(That ?$) That end
meth forTopping(That ?$)
if {self.O equals(@(That.t) $)}
then
That.r := self.N
_ = {@(That.r) accept(self $)}
That
else
_ = {@(That.r) accept(self $)}
That
end
end
end
% 10.65
class PointD
feat x y
meth init(X Y)
self.x = {NewCell X}
self.y = {NewCell Y}
end
meth closerTo0(P ?$)
{self distanceTo0($)} =< {P distanceTo0($)}
end
meth minus(P ?$)
{New CartesianPt init(@(self.x)-P.x @(self.y)-P.y)}
end
meth moveBy(Dx Dy ?$)
(self.x) := @(self.x) + Dx
(self.y) := @(self.y) + Dy
{self distanceTo0($)}
end
meth distanceTo0(?$) raise abstract() end end
end
class CartesianPt from PointD
meth init(X Y)
PointD,init(X Y)
end
meth distanceTo0(?$)
{FloatToInt {Sqrt {IntToFloat @(self.x)*@(self.x) + @(self.y)*@(self.y)}}}
end
end
class ManhattanPt from PointD
meth init(X Y)
PointD,init(X Y)
end
meth distanceTo0(?$)
@(self.x) + @(self.y)
end
end
class ShadowedManhattanPt from ManhattanPt
feat Dx Dy
meth init(X Y DX DY)
PointD,init(X Y)
self.Dx = DX
self.Dy = DY
end
meth distanceTo0(?$)
ManhattanPt,distanceTo0($) + self.Dx + self.Dy
end
end
% 10.67
{Browse 67#{New ManhattanPt init(1 4)}}
% 10.70
PtChild = {New ManhattanPt init(1 4)}
{Browse 70#{PtChild distanceTo0($)}}
% 10.71
{Browse 71#{PtChild moveBy(2 8 $)}}
% 10.72
PtChildBalloon = {New ShadowedManhattanPt init(1 4 1 1)}
{Browse 72#{PtChildBalloon distanceTo0($)}}
% 10.73
{Browse 73#{PtChildBalloon moveBy(2 8 $)}}
|