Scripting in Modeler is an extremely powerful way to automate processes and execution of streams. Just about any action that you can perform manually can be automated using the Jython scripting API. This is convenient if you’re running the stream from within Modeler client, and extremely important if you’re running the stream outside of Modeler client (Modeler Batch/command line/C&DS). Note that Modeler scripts are written in Jython – this mean that in addition to modeler-specific API functions you’ll have all the features of Python available as well!

In this article, we’ll talk through some basic scripting functions to get started and run nodes in order.

Starting a Script

To navigate to the scripting window, you can click the pencil icon at the top of the screen to open the stream properties window and navigate to the Execution tab:

For any script, the first thing you’ll want to do is import the modeler API library. This can be done with a simple ‘import’ command:

import modeler.api

This will enable the script to use modeler-specific functions. You can also add any other Python libraries here by separating them with commas.

Next, you’ll want to define your stream object – or give the script a way to reference the modeler stream itself. This can be done in a single command:

stream = modeler.script.stream()

With this combination of loading the modeler API and defining the reference to the stream, you’ll be able to run stream-specific functions like running nodes!

Running a Node

The command to run a node is broken into two pieces – 1. Finding a node and 2. Running it. Finding a node simply gives you a way to refer to a specific node and perform operations on it. This might include setting node properties, connecting it to other nodes, or running it. The most common ‘find’ function is called findByType. This allows you to find a node based on its type (database source, filler, etc.), or its name. In this example, we’ll find two nodes by their names and run them in order.

When scripting, it’s recommended to give each node a unique name using the Annotations tab. In this stream, we’ve got two branches of source nodes to export nodes called ‘Export_1’ and ‘Export_2’:

We can then use this unique name in the findByType function as follows:

exportNode1 = stream.findByType(None,’Export_1′)

This function takes two arguments: a node type (database source, filler, etc.) and a node name. If you’re referring to a unique name, as we are in this example, you can pass in a value of None into the type argument and find the node solely on its name.

This will create a variable called exportNode1 that is a direct reference to the node named “Export_1”.

To run the node, we can call the run command:

exportNode1.run([])

We can then follow the same process for the second node, and run both nodes with a complete script of:

import modeler.api

stream = modeler.script.stream()

exportNode1 = stream.findByType(None,’Export_1′)

exportNode2 = stream.findByType(None,’Export_2′)

exportNode1.run([])

exportNode2.run([])

Scripts always run from top to bottom, so exportNode1 will run and complete before running exportNode2.

Note: Just like when running from the GUI, you’ll only need to reference and run the terminal node of any branch.

Using this methodology you can script your streams to run any number of nodes in order, just pass the name into the find function. Note that by default, the run option on the script is to be ignored:

If you click ‘Run this script’, then hitting the Run button on the stream will run the script.

IBM’s documentation can be extremely useful to expand your knowledge of scripting and automation of streams, you can find it here:

ftp://public.dhe.ibm.com/software/analytics/spss/documentation/modeler/18.1/en/ModelerScriptingAutomation.pdf