Chapter 7. Scripts

Table of Contents

Introduction to scripts
Command script
Attribute get script
Attribute set script
Check scripts
attribute_check script
Attribute check script
Command in_check script
Input argument check script
Command out_check script
Output argument check script
Default scripts
Attribute default script
Input argument default script
test script

Introduction to scripts

A script belongs to an object or a service and is used to execute a set of instructions in order to complete a specific task.

A script is composed of

  • input arguments
  • output arguments
  • instructions
  • an optionally appended test script for unit testing

A script can have 0, 1 or more input arguments. Input arguments are objects that are transferred from the script caller to the script. They are created by the script caller and then used within the script. Each input argument is a reference to an object of a defined type, and is accessed through its unique identifier.

A script can also have 0, 1 or more output arguments. Output arguments are objects that are transferred from the script back to the script caller. They are created by the script and then used by the script caller. Each output argument is a reference to an object of a defined type, and is accessed through its unique identifier.

Each script contains 1 or more script instructions that are executed in order to complete the script's task. For more information see Chapter 8, Script instructions

A script can optionally contain a test script used to test the correct execution of the script by checking the results for different input values. Test scripts are Obix's means to unit testing. For more information see Chapter 21, Testing

The following figure shows a schematic representation of a script:

Figure 7.1. Script

Script

Kinds of scripts

There are different categories of scripts, subdivided into different kinds of scripts, and each kind of script is used for a specific purpose, as shown in the following table:

Table 7.1. Categories and kinds of scripts

Script categoryScript kindUsed toArguments implicitly defined
commandcommand scriptexecute a commandno
attribute getter/setterattribute get scriptget (read) an attribute's valueyes
attribute set scriptset (write) an attribute's valueyes
checkcommand in_check scriptcheck several input arguments before executing a commandyes
input argument check scriptcheck one input argument before executing a commandyes
command out_check scriptcheck several output arguments after executing a commandyes
output argument check scriptcheck one output argument after executing a commandyes
attribute list check scriptcheck several attributes of an object or serviceyes
attribute check scriptcheck one attributeyes
default valueinput argument default scriptdefine the default value of a command's input argumentyes
attribute default scriptdefine the default value of an attributeyes
testtest scripttest anyone of the above scriptsyes

As we can see from the last column in the above table, a script's input and output arguments are always implicitly defined, except in case of the command script, whose arguments must be explicitly defined in the source code.

Each kind of script will more thoroughly be explained in subsequent sections.

Executing (calling) a script

A script is executed through a script caller, which is a source code instruction or part of a source code expression that calls the script.

All kinds of scripts, except the command script, are called implicitly. For example, all input check scripts are automatically called before executing a command, and all output check scripts are automatically called after executing a command. However, all scripts can also be explicitly called at any time. For example, a command input argument check script can manually be called from any script to check if a given value would be accepted as input argument.

When a script is called explicitly, it is accessed through its script selector. A script selector is an identifier path (i.e. several identifiers separated by a dot) which is unique within the object or service. The syntax of a script selector depends on the script kind and is explained in the following sections.

The general syntax to call a script is the following:

Table 7.2. Script execution syntax

ProductionSyntaxLinks
script_execution_instruction RSE_script_selector input_assignment_list ? output_assignment_list ? script_execution_tail ? the section called “Script execution instruction”
script_selectorthe section called “Introduction to scripts”
input_assignment_list"(" input_assignment ( ";" ? input_assignment ) * ")" 
output_assignment_list "(" output_assignment ( ";" ? output_assignment ) * ")"

remark: if there are no input arguments assigned then the output argument assignment list must be preceded by () to indicate an empty input argument assignment list

 
input_assignment ( input_argument_id "=" ) ? expression

remark: input_argument_id = can only be omitted if the called script has exactly one input argument

 
output_assignmentassignable_object_reference_selector "=" output_argument_id 

example of calling a script with 2 input and 3 output arguments (from the section called “Examples”):

var string result; file_error
var file_handle log_file

se_logger_7.co_log_message ( &
   i_message = "The sun is shining!!!" &
   i_message_id = "sunshine" ) &
   ( v_result = o_result &
   v_log_file = o_log_file &
   v_file_error = o_file_error )
   
if v_file_error =r void then
   // everything ok
else
   // we have a problem
end if

Implicit variables

Each script has an implicitly defined variable whose id is v_program_error_ and whose type is ty_error_program. This variable holds the last error that occurred in the script. It is used infrequently in case of special error handling. Please refer to Chapter 13, Runtime error handling for more information.