TOC PREV NEXT

3.5 Invoking Methods

Every element and subexpression in an expression represents an instance of the Token class in Ptolemy II (or more likely, a class derived from Token). The expression language supports invocation of any method of a given token, as long as the arguments of the method are of type Token and the return type is Token (or a class derived from Token, or something that the expression parser can easily convert to a token, such as a string, double, int, etc.). The syntax for this is (token).methodName(args), where methodName is the name of the method and args is a comma-separated set of arguments. Each argument can itself be an expression. Note that the parentheses around the token are not required, but might be useful for clarity. As an example, the ArrayToken and RecordToken classes have a length() method, illustrated by the following examples:

 
{1, 2, 3}.length() 
{a=1, b=2, c=3}.length() 
 
each of which returns the integer 3.

The MatrixToken classes have three particularly useful methods, illustrated in the following examples:

 
[1, 2; 3, 4; 5, 6].getRowCount() 
 
which returns 3, and
 
[1, 2; 3, 4; 5, 6].getColumnCount() 
 
which returns 2, and
 
[1, 2; 3, 4; 5, 6].toArray() 
 
which returns {1, 2, 3, 4, 5, 6}. The latter function can be particularly useful for creating arrays using MATLAB-style syntax. For example, to obtain an array with the integers from 1 to 100, you can enter:
 
[1:1:100].toArray() 
 
TOC PREV NEXT