Next Previous Up Contents
Next: Server Mode
Up: Advanced Topics
Previous: Instance Methods

10.9.3 Adding User-Defined Functions

The functions provided by default for use with algebraic expressions, while powerful, may not provide all the operations you need. For this reason, it is possible to write your own extensions to the expression language. In this way you can specify abritrarily complicated functions. Note however that this will only allow you to define new columns or subsets where each cell is a function only of the other cells in the same row - it will not allow values in one row to be functions of values in another.

In order to do this, you have to write and compile a (probably short) program in the Java language. A full discussion of how to go about this is beyond the scope of this document, so if you are new to Java and/or programming you may need to find a friendly local programmer to assist (or mail the author). The following explanation is aimed at Java programmers, but may not be incomprehensible to non-specialists.

The steps you need to follow are:

  1. Write and compile a class containing one or more static public methods representing the function(s) required
  2. Make this class available on the application's classpath at runtime as described in Section 3.1
  3. Specify the class's name to the application, as the value of the jel.classes system property (colon-separated if there are several) as described in Section 3.3

Any public static methods defined in the classes thus specified will then be available for use. They should be defined to take and return the relevant primitive or Object types for the function required. For instance a class written as follows would define a three-value average:

   public class AuxFuncs {
       public static double average3(double x, double y, double z) {
           return (x + y + z) / 3.0;
       }
   }
and the command
   stilts tpipe cmd='addcol AVERAGE "average3($1,$2,$3)"'
would add a new column named AVERAGE giving the average of the first three existing columns. Exactly how you would build this is dependent on your system, but it might involve doing something like the following:
  1. Writing a file named AuxFuncs.java containing the above code
  2. Compiling it using a command like "javac AuxFuncs.java"
  3. Running tpipe using the flags "stilts -classpath . -Djel.classes=AuxFuncs tpipe"

Note that (in versions later than STILTS 3.0-6) variable-length argument lists are supported where the final declared argument is an array, so for instance a method declared like:

       public static double sum(double[] values) {
           double total = 0;
           for (int i = 0; i < values.length; i++) {
               total += values[i];
           }
           return total;
       }
can be invoked like "sum(UMAG,GMAG,RMAG,IMAG,ZMAG)". The alternative form "double... values" can be used in the declaration with identical effect.


Next Previous Up Contents
Next: Server Mode
Up: Advanced Topics
Previous: Instance Methods

STILTS - Starlink Tables Infrastructure Library Tool Set
Starlink User Note256
STILTS web page: http://www.starlink.ac.uk/stilts/
Author email: m.b.taylor@bristol.ac.uk
Mailing list: topcat-user@jiscmail.ac.uk