Disclaimer: Use the described method at your own risk...
Why did we look for another option to define jython functions that can be used in another automationscript?
We found the other methods complex
What did we come up with:
Define re-usable functions in an automationscript that can then be called from another automationscript
And yes there are other ways but we find this method simple to use
What do we need to make this functionality work?
1. An automationscript with a function that retrieves an automationscript and return the SOURCE-field (see function def GetAutoscript(strAutoscriptName) in the script below)
2. An automationscript containing a function that we want to use in another automationscript:
Lets say you want to perform some simple calculations in a function called iIncrease that has one parameter iIncreaseMe
if iIncreaseMe == 1 you want to add 1
if iInCreaseMe == 2 you want to add 2
otherwise you want to add 10
Writing a jython function for this would look something like:
def iIncrease(iIncreaseMe):
print "Function:iIncrease(" + str(iIncreaseMe) + ") - Start";
if (iIncreaseMe == 1):
iReturnValue = iIncreaseMe + 1;
elif (iIncreaseMe == 2):
iReturnValue = iIncreaseMe + 2;
else:
iReturnValue = iIncreaseMe + 10;
print "Function:iIncrease(" + str(iIncreaseMe) + ") - End - Returning:[" + str(iReturnValue) + "]";
return iReturnValue;
And we defined an automationscript called EH_FUNCTION_01 containing the function as depicted above.
In order to use our function in an automationscript we have to:
1. Read the (source-field of the) automationscript that contains the function we want to use in another automationscript
2. Put the function into memory
3. Use the function
We have therefore defined following automationscript:
----- Script START ----------
import sys
from psdi.server import MXServer
from psdi.security import UserInfo
from psdi.iface.mic import MicService
from org.python.util import PythonInterpreter
ICDServer = MXServer.getMXServer()
svcIntegration = MicService(ICDServer)
svcIntegration.init()
ICDUser = svcIntegration.getNewUserInfo()
# Function:
def GetAutoscript(strAutoscriptName):
strReturnValue = ""
try:
setAUTOSCRIPT = ICDServer.getMboSet("AUTOSCRIPT", ICDUser)
strFWhereClause = "AUTOSCRIPT = '" + strAutoscriptName + "' "
setAUTOSCRIPT.setWhere(strFWhereClause)
recAUTOSCRIPT = setAUTOSCRIPT.moveFirst()
if (recAUTOSCRIPT):
strReturnValue = recAUTOSCRIPT.getString("SOURCE")
setAUTOSCRIPT.close()
setAUTOSCRIPT.cleanup()
except:
strErrorMessage = "Error: " + str(sys.exc_info()[0]) + " - Message: " + str(sys.exc_info()[1])
print "Function:GetAutoscript(" + strAutoscriptName + ") - Failed with: [" + strErrorMessage + "]"
return strReturnValue
print "============================================(1)"
print "Read autoscript EH_FUNCTION_01"
strFunction = GetAutoscript("EH_FUNCTION_01")
print "============================================(2)"
print "strFunction:"
print strFunction
print "============================================(3)"
print "Performing Exec - Read Function into memory"
exec(strFunction)
print "============================================(4)"
print "Use Read Function - by executing iValue = iIncrease(1)"
iValue = iIncrease(1)
print "iValue:"
print str(iValue)
print "============================================(5)"
print "Use Read Function - by executing iValue = iIncrease(2)"
iValue = iIncrease(2)
print "iValue:"
print str(iValue)
print "============================================(6)"
print "Use Read Function - by executing iValue = iIncrease(10)"
iValue = iIncrease(10)
print "iValue:"
print str(iValue)
----- Script END ----------
And when we execute the script by pressing (the re-introdued) run script button we see the following output:
----- Output START ----------
============================================(1)
Read autoscript EH_FUNCTION_01
============================================(2)
strFunction:
def iIncrease(iIncreaseMe):
print "Function:iIncrease(" + str(iIncreaseMe) + ") - Start";
if (iIncreaseMe == 1):
iReturnValue = iIncreaseMe + 1;
elif (iIncreaseMe == 2):
iReturnValue = iIncreaseMe + 2;
else:
iReturnValue = iIncreaseMe + 10;
print "Function:iIncrease(" + str(iIncreaseMe) + ") - End - Returning:[" + str(iReturnValue) + "]";
return iReturnValue;
============================================(3)
Performing Exec - Read Function into memory
============================================(4)
Use Read Function - by executing iValue = iIncrease(1)
Function:iIncrease(1) - Start
Function:iIncrease(1) - End - Returning:[2]
iValue:
2
============================================(5)
Use Read Function - by executing iValue = iIncrease(2)
Function:iIncrease(2) - Start
Function:iIncrease(2) - End - Returning:[4]
iValue:
4
============================================(6)
Use Read Function - by executing iValue = iIncrease(10)
Function:iIncrease(10) - Start
Function:iIncrease(10) - End - Returning:[20]
iValue:
20
============================================(7)
----- Output END ----------
Note: We are using the function exec() to read the function into memory, the exec()-function becomes available by including "from org.python.util import PythonInterpreter"
Let us know what you think...!
#Customizations
------------------------------
Kind Regards,
Erwin Hebing
------------------------------