Test Script Workflow
The Script work item is designed to allow you to design work items without having to program in Java, the native language of the SDX workflow application. The process of interpreting a script is not as fast as executing the native code, but scripts enable great flexibility. You can do almost anything with the Script work item, even recreate all the work items in the work item library.
The scripting language accepted by the Script work item is Python (see http://www.python.org for more information). Because Python is an object-oriented language, its interaction with Java is straightforward, enabling your scripts to easily access the services of the workflow framework.
This example demonstrates how to use the Script work item to log the values of the Name and Address token parameters to the console; this task is similar to that performed by the Logger work item. This example shows you how to get the parameters from the input token and use them inside your script, how to tell the workflow framework that your task has completed, and how to generate the output token.
Figure 4 shows the TestScriptWorkflow. In this example, we point to the task script stored in a file. Alternatively, you could type the script directly into the Task Script property.
![]()
The task script for the Script Work Item in the TestScriptWorkflow is shown in the sample below.
print "Executing script"print "Executing second line"name = inputToken['Name'][0]print "Received token with Name=" + nameprint "And Address=" + inputToken['Address']print "Ending..."complete = 1 #indicate the task is completeresult = 1 #indicate the success of the work itemoutputToken = inputToken #needs to be explicitThis script does not demonstrate how to subscribe and consume events. To subscribe and consume events:
You must import an event descriptor before you can instantiate it. Then you can use the descriptor as if it were a Python object. The use of Java classes inside a Python script is powered by the Jython implementation of Python. See http://www.python.org for a full reference on how Java and Python interact. See Chapter 19, Available Workflow Event Descriptors, for information about available event descriptors.
- Instantiate the event descriptor.
- Configure the event descriptor.
- Call the register() method in this object passing it as argument.
- To consume it, pass the event object you received to the consume() method, that is also called on this object.
As an example of how to subscribe to events, you can change the TestScriptWorkflow by assigning the following scripts to the Script Work Item. The task script is from net.juniper.smgt.workflow.engine.adapters. Import TimerEventDescriptor.
import timeprint "Executing task script"name = inputToken['Name'][0]print "Received token with Name=" + nameprint "And Address=" + inputToken['Address']print "Subscribing to a Timer Event..."desc = TimerEventDescriptor()now = time.time() #current time in secs since epochdue = now + 10 #wants an event 10 secs from nowdesc.setDue(due * 1000) #setDue requires the time in millisecsthis.register(desc) #registering for an eventprint "Ending task..."complete = 1 #indicate the task is completeListing 4: A task script that subscribes to an event.The event script, which is called upon the reception of the Timer event, would be:print "Executing event script"due = event.getDue() / 1000 #in secs againstrdue = time.strftime("%H:%M:%S", time.localtime(due))strnow = time.strftime("%H:%M:%S", time.localtime(time.time()))print "Received timer event at " + strnow + " due at " + strduethis.consume(event) #indicates that this is the expected eventprint "Finishing work item..."complete = 1 #indicate the event is completeresult = 1 #indicate the success of the work itemoutputToken = inputToken #needs to be explicitThese scripts emulate the Logger work item, which receives a token and prints the Name and Address token parameters to the console. Then it subscribes for a Timer event due 10 seconds after the subscription. When the task script finishes, the work item waits for the arrival of an event.
When the Timer event arrives, the event script executes. It first prints to the console a sentence stating the current time and when the Timer was due. Next it consumes the event, which tells the Timer Event Adapter that the event was delivered to the proper consumer, and it does not need to keep it for any other reason. Finally, the output token created is exactly the same as the input one.