INTERFACE Tutorial;
EXCEPTION DivideByZero
"this error is signalled if the client of the Calculator calls
the Divide method with a value of 0";
TYPE Calculator = OBJECT COLLECTIBLE
DOCUMENTATION "4-function calculator"
METHODS
SetValue (v : REAL) "Set the value of the calculator to `v'",
GetValue () : REAL "Return the value of the calculator",
Add (v : REAL) "Adds `v' to the calculator's value",
Subtract (v : REAL) "Subtracts `v' from the calculator's value",
Multiply (v : REAL) "Multiplies the calculator's value by `v'",
Divide (v : REAL) RAISES DivideByZero END
"Divides the calculator's value by `v'"
END;
TYPE Factory = OBJECT
METHODS
CreateCalculator () : Calculator
END;
import Tutorial, Tutorial__skel, CalculatorImpl
class Factory (Tutorial__skel.Factory):
# have the __init__ method take handle and server args
# so that we can control which ILU kernel server is used,
# and what the instance handle of the Factory object on
# that server is. This allows us to control the object ID
# of the new Factory instance.
def __init__(self, handle=None, server=None):
self.IluInstHandle = handle
self.IluServer = server
def CreateCalculator (self):
return (CalculatorImpl.Calculator())
simple1.py
# simple1.py, a simple program that demonstrates the use of the
# Tutorial true module as a library.
#
# run this with the command "python simple1.py NUMBER [NUMBER...]"
#
import Tutorial, CalculatorImpl, string, sys
# A simple program:
# 1) make an instance of Tutorial.Calculator
# 2) add all the arguments by invoking the Add method
# 3) print the resultant value.
def main (argv):
c = CalculatorImpl.Calculator()
if not c:
error("Couldn't create calculator")
# clear the calculator before using it
c.SetValue (0.0)
# now loop over the arguments, adding each in turn */
for arg in argv[1:]:
v = string.atof(arg)
c.Add (v)
# and print the result
print "the sum is", c.GetValue()
sys.exit(0)
main(sys.argv)
simple2.py
# simple2.py, a simple program that demonstrates the use of the
# Tutorial true module as a library.
#
# run this with the command "python simple1.py NUMBER [NUMBER...]"
#
import Tutorial, CalculatorImpl, string, sys
# A simple program:
# 1) make an instance of Tutorial.Calculator
# 2) add all the arguments by invoking the Add method
# 3) print the resultant value.
def main (argv):
c = CalculatorImpl.Calculator()
if not c:
error("Couldn't create calculator")
# clear the calculator before using it
if (len(sys.argv) < 2):
c.SetValue (0.0)
else:
c.SetValue (string.atof(argv[1]))
# now loop over the arguments, Dividing by each in turn */
try:
for arg in argv[2:]:
v = string.atof(arg)
c.Divide (v)
except:
print 'exception signalled: ' + str(sys.exc_type)
sys.exit(1)
# and print the result
print "the sum is", c.GetValue()
sys.exit(0)
main(sys.argv)
server.py
# server.py -- a program that runs a Tutorial.Calculator server
#
import ilu, FactoryImpl, sys
def main(argv):
if (len(argv) < 2):
print "Usage: python server.py SERVER-ID"
sys.exit(1)
# Create a kernel server with appropriate server ID, which
# is passed in as the first argument
theServer = ilu.CreateServer (argv[1])
# Now create an instance of a Factory object on that server,
# with the instance handle "theFactory"
theFactory = FactoryImpl.Factory ("theFactory", theServer)
# Now make the Factory object "well-known" by publishing it.
theFactory.IluPublish()
# Now we print the string binding handle (the object's name plus
# its location) of the new instance.
print "Factory instance published."
print "Its SBH is '" + theFactory.IluSBH() + "'"
handle = ilu.CreateLoopHandle()
ilu.RunMainLoop (handle)
main(sys.argv)
simple3.py
# simple3.py -- a simple client program that finds the Calculator Factory,
# creates a calculator, and adds up its arguments as before
#
# to run: python simple4.py ARG [ARG...]
import Tutorial, ilu, sys, string
# We define a new routine, "Get_Tutorial_Calculator", which
# finds the tutorial factory, then creates a new Calculator
# object for us.
def Get_Tutorial_Calculator (factoryObjectID):
# We have to call ilu.LookupObject() with the object ID of
# the factory object, and the "type" of the object we're looking
# for, which is always available as MODULE.TYPENAME
f = ilu.LookupObject (factoryObjectID, Tutorial.Factory)
if not f:
print "Can't find Tutorial.Factory instance " + factoryObjectID
sys.exit(1)
c = f.CreateCalculator()
return (c)
def main (argv):
# A simple program:
# 1) make an instance of Tutorial.Calculator
# 2) add all the arguments by invoking the Add method
# 3) print the resultant value.
if (len(argv) < 2):
print "Usage: python simple3.py FACTORY-OBJECT-ID NUMBER [NUMBER...]\n",
sys.exit(1)
c = Get_Tutorial_Calculator(argv[1])
if not c:
print "Couldn't create calculator"
sys.exit(1)
# clear the calculator before using it
c.SetValue (0.0)
# now loop over the arguments, adding each in turn
for arg in argv[2:]:
v = string.atof (arg)
c.Add (v)
# and print the result
print "the sum is " + str(c.GetValue())
sys.exit (0);
main(sys.argv)
Tutorial2.isl
INTERFACE Tutorial2 IMPORTS Tutorial END;
TYPE OpType = ENUMERATION
SetValue, Add, Subtract, Multiply, Divide END;
TYPE Operation = RECORD
op : OpType,
value : REAL,
accumulator : REAL
END;
TYPE RegisterTape = SEQUENCE OF Operation;
TYPE TapeCalculator = OBJECT COLLECTIBLE
SUPERTYPES Tutorial.Calculator END
DOCUMENTATION "4 function calculator with register tape"
METHODS
GetTape () : RegisterTape
END;
TYPE Factory = OBJECT SUPERTYPES Tutorial.Factory END
METHODS
CreateTapeCalculator () : TapeCalculator
END;