# # 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 subThe 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.
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]
| Name | Description | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| name | The name is a string that specifies what the variable will be called. | ||||||||||||||||||||||||
| type |
|
||||||||||||||||||||||||
| input|output | Indicates whether the variable is an input or an output |
| 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.
|
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.setGroup "groupName"
| 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. |
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: methodName ["Full Name"] [downloadInputs]
| 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. |
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
script: [language="value"][timeout="value"][timeoutVarName="value"]
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" |
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.
See also Analysis Server