How to Deploy TERR Scripts from AMS to TIBCO Streaming

How to Deploy TERR Scripts from AMS to TIBCO Streaming

book

Article ID: KB0072929

calendar_today

Updated On:

Products Versions
TIBCO Streaming 10.x and later

Description

Starting with a script in the TIBCO Streaming TERR operator Default Script operator property, how can I turn this into an R script which I can revise in TIBCO ModelOps or the TIBCO Artifact Management Server (AMS) and push to a running TIBCO Streaming node?

Issue/Introduction

A simple R script needs a function to wrap it for use in the TIBCO Streaming TERR operator when updated externally.

Resolution

As an example, let’s assume you have this simple R script in the TERR operator, StreamBase Properties view, Operator Properties tab, Default Script:
 CalculateMean <<- mean(TradeAmt) Model <<- "MediaAMS"
Example:
Simple R script shown in the TERR operator "Default Script" property

This works correctly, but the new requirement is to turn this script into an asset that can be managed in TIBCO ModelOps or the Artifact Management Server (AMS). 

R script files supplied from the Initialize tab, the AMS tab, or from the "script" field in the input tuple (when the "init" field is set to True) do not replace the Default Script, but instead define R objects which become part of the TERR runtime environment that is available when the Default Script script is run to evaluate new data. The Default Script script is always executed as defined in the static property setting. To alter the output it must instead use something which is updated through deployment (push) of a changed model.

The R objects allowed to be updated are any of a named model, an R data structure, or an R function. It is through an R function that the TERR operator can receive our example’s updates. The function must be called from the Default Script and must alter R global variables using the ' <<-' assignment operator.

Setup:

The revised TERR operator configuration is...

1. The  Default Script becomes a function call:
 calcScript(TradeAmt)
Example:
Default script replaced with single function call

2. We add a "TERR Script" artifact in AMS, in project "models", named "CalculateMean.R":
 calcScript <- function(a) {   CalculateMean <<- mean(a)   Model <<- "MediaAMS" }

3. The Input Tuple defines the input and output fields with names matching R global variable names:
 terrVars <terrSch>   TradeAmt <list(double)>   CalculateMean <double>   Model <string>
Example:
Input Stream schema showing terrVars tuple field with input data field and output global variables

4. Manage deployment of the new script artifact from AMS by setting a reference to the AMS/ModelOps models/CalculateMean.R artifact in the AMS tab of the TERR operator, loading a default script using the Initialize tab, or pushing a script using the "script" field in the input tuple. Then at any time while running you may deploy (push) an updated artifact from AMS using a deployment descriptor.

Explanation:

The way this works is that it does NOT modify the Default Script, but only causes a new R environment initialization with the new function definition. The output variables are defined in the terrVars input tuple field so they are global. To set global variables the function must use the ' <<-' assignment operator syntax to return results to the adapter.

Each field specified in the TERR operator “ Edit Schema” tab represents a global R variable that is retrieved after the R engine execution of an input tuple. The field name is used to match an R variable name to receive the calculated values.

Example:
TERR operator Edit Schema tab showing global variables to emit