The ScriptWrapper Utility

The ScriptWrapper utility enables users to create components using different installed scripting languages. Currently available are Microsoft Active Scripting languages such as VBScript and JScript, an implementation of Java via BeanShell, Jython, and Python. Note that Active Scripting will only be availiable on Microsoft Windows platforms, however BeanShell and Jython are available on all Analysis Server supported platforms. Python is available on all platforms as well, but will require compilation on platforms other than Windows.

Example ScriptWrappers

A ScriptWrapper is very easy to use. The script below shows a ScriptWrapper uses Active Scripting to model a cube.
#
# This is an example of a simple ScriptWrapper.
# Scripts can be used to perform simple calculations
# or to wrap any COM-enabled application.
#
# @description: example Active Scripting ScriptWrapper
# @author: Phoenix Integration
#

# define variables
variable: side double input lowerBound=0
variable: area double output
variable: volume double output

# anything after the following line is interpreted
# as the script
script:
option explicit

' the run routine is invoked each time the component
' is executed. Code outside subroutines is executed
' when the component is first loaded.
sub run
   area = 6 * side^2
   volume = side^3
end sub
The next script is a ScriptWrapper that uses BeanShell to model a cube
#
# This is an example of a simple Java ScriptWrapper.
#
# @description: example BeanShell ScriptWrapper
# @author: Phoenix Integration
#

# define variables
variable: side double input lowerBound=0
variable: area double output
variable: volume double output

# anything after the following line is interpreted
# as the script
script: language="java"

// the run routine is invoked each time the component
// is executed. Code outside subroutines is executed
// when the component is first loaded.
void run()
{
   area.value = 6 * side.value * side.value;
   volume.value = side.value * side.value * side.value;
}
And a final example is a ScriptWrapper that uses Python to model a cube
#
# This is an example of a simple Python ScriptWrapper.
#
# @description: example Python ScriptWrapper
# @author: Phoenix Integration
#

# define variables
variable: side double input lowerBound=0
variable: area double output
variable: volume double output

# anything after the following line is interpreted
# as the script
script: language="python"

# the run routine is invoked each time the component
# is executed. Code outside subroutines is executed
# when the component is first loaded.
def run():
   area.setValue( 6 * side.getValue() * side.getValue() )
   volume.setValue( side.getValue() * side.getValue() * side.getValue() )
A ScriptWrapper is composed of three sections: the header, variable declarations, and the script.

Header Section

The header section is comprised of comment lines that provide additional information about the wrapper. Information specified here is retrieved using the describe command in the Analysis Server. Additional information about header tags can be found in the components section.

Variable Declarations

There are two kinds of variables used in a script: variables only used internally to the script and variables exposed to the Analysis Server. The variables exposed to the Analysis Server are the variables seen by the end user of the wrapper and must be declared in the ScriptWrapper using the variable: statement.

Syntax

variable: <name> <type> <input|output> [default=value]
                                      [description=string][units=value]
                                      [upperBound=value][lowerBound=value]
                                      [enumValues=val1,val2,...,valN]
                                      [enumAliases=val1,val2,...,valN]
                                      [className=value]

Arguments

Name Description
name The name is a string that specifies what the variable will be called.
type
Type Aliases Associated PHXType
doublerealPHXDouble
integerint,longPHXLong
stringstrPHXString
booleanboolPHXBoolean
geometrygeomPHXGeometry (Not available in VBScript)
file PHXRawFile
object PHXScriptObject
All types except PHXGeometry and PHXScriptObject may be made into arrays by adding [] at the end of the type string. In that case, the associated PHX<type>Array class is used to represent a resizable, multi-dimensional array.
input|output Indicates whether the variable is an input or an output

Options

Name Description
default This option can be used to specify a default value. All entries in the variable will be set to this value initially. A default argument will take precedence over the initialization file.
description This option allows the user to write a description for the variable.
units This option allows the user to write a units string for the variable.
upperBound This option allows the user to set an upper bound for the variable.
lowerBound This option allows the user to set a lower bound for the variable.
enumValues This option allows the user to specify the set of possible values for the variable. The enumerated values are specified as a comma separated list of values.
enumAliases This option allows the user to specify a set of string values that may be associated with the enumerated values. If the variable is set to a value in the alias set, the actual value will be set to the corresponding value in the enumValues set. The aliases are specified as a comma separated list of values. There must be the same amount of elements in the enumValues and enumAliases sets.
className This option is used to specify the class type for "object" type variable. It can be relative to the wrapper's folder or absolute path.
  • className="classes/Wing.java#Wing"   --- Relative URL example
  • className="C:/Documents and Settings/tester/Application Data/Phoenix Integration/AServer7/analyses/wrappers/classes/Wing.java#Wing"  --- Absolute URL example
  • className="file://fileserver.phoenix-int.com/tester/classes/Wing.java#Wing"   --- class file on a file server
  • className="https://irome.phoenix-int.com/content/files/Users/tester/classes/Wing.java#Wing"   --- class file on PHX AnalysisLibrary

Example

After a variable has been declared, it is ready for use in the script. Each variable is represented by a variant object. The default property for each variable is it's .value property. This means that you can get and set variables just by accessing the variable. If the variable is an array, the value property is an indexed property meaning that it also requires an index value.

The example below shows how to use several variables from a script. Note that the scripting language displayed is VBScript. Semantics will vary from language to language.

variable: inputs double[] input
variable: average double output

script: language=VBScript

sub run
   dim i
   dim sum
   sum = 0

   for i = 0 to inputs.size-1
      sum = sum + inputs(i)
   next

   average = sum / inputs.size
end sub
Note the use of the size property for array types. This property can be retrieved to get the size of an array, or set to change the size.  In this case the initial size is not specified, which means it is up to the client to specify the array size.  You can also use the "getNumDimensions" and "setDimensions" functions to handle multi-dimensional arrays.

Group Declarations

Variables can be separated into different groups with the "setGroup" statement.

Syntax

setGroup "groupName"

Arguments


Name Description
groupName The name of the group you would like to put subsequently declared variables into.  For nested groups, separate names with ".", i.e. "Outputs.Frame.Costing". To stop putting subsequent variables into groups, do a setGroup command with "" as the groupName.

Example

The example below shows how to use the SetGroup statement in a script.


setGroup "Inputs"
variable: inputs double[] input
setGroup "Outputs"
variable: average double output
setGroup ""
variable: sum double output

Method Declarations

Methods can be declared with the "method:" statement.

Syntax

method: methodName ["Full Name"] [downloadInputs]

Arguments


Name Description
methodName The name of the method that is to be published as available.  Must match with the name of a method declared in the script code.
NOTE: Some names are reserved, such as "run" and "end"
Full Name
A human readable name for the method which can be put in a drop-down menu for the component
downloadInputs
Added in v4.1.  If this optional argument is set to 'true', then a hint is passed to clients that they should re-download the values of all the component's inputs after running the method.  The client may ignore this value.  If you are using ModelCenter or ModelRunner, you must be using version 6.1 in order for this value to take effect.

Example

The example below shows how to use the Method statement in a script. The example uses VBScript, the exact semantics of the example would be different for different languages.

variable: inputs double[] input
variable: average double output
method: aMethod "Method Number One"

script: language=VBScript

sub run
   dim i
   dim sum
   sum = 0

   for i = 0 to inputs.size-1
      sum = sum + inputs(i)
   next

   average = sum / inputs.size
end sub

sub aMethod
   MsgBox "aMethod Called"
end sub

The Script

The actual script begins with the script: statement. All lines following this line are handled by the script interpreter directly.

Syntax

script: [language="value"][timeout="value"][timeoutVarName="value"]

Arguments

None.

Options

OptionDescription

language The scripting language to be used. The Analysis Server includes VBScript, JScript and BeanShell's Java scripting languages. Other Active Scripting languages such as PerlScript and PythonScript may be installed by the user. For more information on installing Active Scripting languages, see msdn.microsoft.com/scripting
timeout This currently does not work with Java, Python, or Jython based scripts. The amount of time (in seconds) that the script should be allowed to run before an error is generated. The default, -1, indicates infinity.
timeoutVarName This currently does not work with Java, Python, or Jython based scripts. Use this to specify a string that is a ModelCenter variable name that can be used to control the script timeout. The value of the variable will be used as the timeout value. If both "timeout" and "timeoutVarName" are specified here, "timeoutVarName" will attempt to be used and if it is, "timeout" is ignored.
note: It's important that the variable whose name is specified here is created in the script itself as a seperate variable and not part of any group. The variable also should be of type "double". Here's an example of a timeout variable line:
variable: myTimeout double input description="used to specify the timeout"

Script Methods

Code within the script is executed at various times. Global code, i.e. code not within subroutines is executed when the script is loaded. This type of code is only executed once in the lifetime of the component and is useful for initializing the component. Code within the run() subroutine is executed each time the component is executed. This is the primary work method for the script and must be defined. The onEnd() method, if defined, is called when the component ends. This method should be used to close any resources in use by the component.

Available APIs

The ScriptWrapper defines a global object for all scripts called wrapper which supplies basic functionality for handling special cases with variables and methods.  In addition there are objects which represent variables of various types.  If you are using the Java scripting language, there is an additional File Parsing API which allows you to easily take advantage of the FileWrapper's robust file parsing capabilities programatically. 

This API is basically the same across all possible languages, subject to constraints imposed by the languages themselves. 

File Parsing API

Users can now take advantage of the FileWrapper's robust file parsing capability in the ScriptWrapper by using the File Parsing API(new in Analysis Server 3.1). To use this feature, users must use the BeanShell implementation of Java as the language for the ScriptWrapper since the File Parsing API is pure Java. The File Parsing API is closely mapped to FileWrapper statements.  More information on available methods can be found in Analysis Server's Java API in the com.phoenix_int.aserver.util.scriptwrapper.api package.

nD Arrays

Arrays in ScriptWrapper are handled through special variable classes which represent the array. These arrays are named after the base type of data that they hold, such as PHXDoubleArray. The array classes automatically handle arrays with any number of dimensions. By default arrays are "auto resizable", which means that if you have an array that is an input to your ScriptWrapper, the client can change the number of dimensions or length of the array at will. Ideally your ScriptWrapper should check the number of dimensions and length of the arrays before trying to use the input array.

Equally, your may change the size of your output arrays at any time by using the "setDimensions" method.

See the API documentation above for the language of your choice for more information on using the array classes.

Example ScriptWrappers

Visual Basic

Java

Python

See also Analysis Server