Library scripts are good for encapsulating some re-usable logic. From Maximo 7.6 onwards, you can write those reusable blocks as
library scripts, which are just simple scripts with no launch points. You can then leverage the
service variable to invoke those library scripts from another script.
Library scripts have undergone some improvements over the last few releases.
The following simple example showcases the change in usage:
This is a script that multiplies two variables -
b and sets the result to another variable
Assume that you are invoking this script from another script and you want to pass in
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 HashMapmap = HashMap()map.put("a",2)map.put("b",3)service.invokeScript("CALC",map)res = map.get("r")
The calling script leverages the implicit
service variable (available to all scripts) to invoke the library script named CALC.
It also passes in the
b as a Map object to the script. The response
r is recovered from that same Map object.
While this way works, it has a few downsides.
- It does not allow the library script to have multiple functions, becoming a true utility script. In this model you will be forced to create many such library scripts - each for one utility.
- The calling script spends a lot of code to setup 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*bdef 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 reduces the number of code lines significantly and removes the java Map imports, instead leveraging the native language (py) to do it all.
It also helps make the code more readable. These are just samples meant to demonstrate the library script concept and how
service is leveraged to invoke those.
The real life examples would certainly include reusable business logic inside the library scripts.
For the script, there was no need to import or make any new py/js modules - it is all natively handled by Maximo. The library scripts are no different that any other scripts in terms of storage, caching, compilation, and other aspects.