Library scripts

Library scripts are good for encapsulating reusable logic. You can write those reusable blocks as library scripts, which are simple scripts with no launch points. You can then use the service variable to run those library scripts from another script. The following script is a simple example.
r=a*b
This script multiplies two variables--a and b--and sets the result to another variable, r. Assume that you are invoking this script from another script and you want to pass in a and b and then get the r value back. The following script shows a sample that calls this library script (named CALC):
from java.util import HashMap

map = HashMap()
map.put("a",2)
map.put("b",3)
service.invokeScript("CALC",map)
res = map.get("r")
The calling script uses the implicit service variable, which is available to all scripts, to run the library script named CALC. It also passes in the a and b as a map object to the script. The response r is recovered from that same map object. While this way works, it has some downsides:
  • It does not allow the library script to have multiple functions, becoming a true utility script. In this model you are forced to create many such library scripts, each for one utility.
  • The calling script uses much code to set up the library script call dealing with Java™ imports and Java data structures.
The following example shows a simpler way to rewrite the CALC script, while helping the library scripts to be function-based:
def mult(a,b):
  return a*b
  
 def add(a,b):
  return a+b
Now, it is possible to accommodate multiple utility modules in one library script. The following calling script can be used:
res = service.invokeScript("CALC","mult",[2,3])

This method reduces the number of code lines significantly and removes the Java map imports, instead using the native language Python to do it all. It also helps make the code more readable. These scripts are just samples that are meant to demonstrate the library script concept and how service is used to invoke those scripts. Actual examples would include reusable business logic inside the library scripts.

For the script, there was no need to import or make any new Python or JavaScript modules. It is all handled by IBM® Maximo® Manage. The library scripts are no different from any other scripts in terms of storage, caching, compilation, and other aspects.