Developing Custom-Defined Calculation Functions

This chapter explains how to develop custom-defined functions and use them in Essbase formulas and calculation scripts. Custom-defined functions are written in the Java programming language and enable you to create calculation functions not otherwise supported by the Essbase calculation scripting language.

Essbase does not provide tools for creating Java classes and archives. This chapter assumes that you have a compatible version of the Java Development Kit (JDK) and a text editor installed on the computer you will use to develop custom-defined functions. For more information on compatible versions of Java, see the Essbase Installation Guide.

For more examples of custom-defined functions, see the Technical Reference in the docs directory.

Use these sections to create and use custom-defined functions:

Viewing Custom-Defined Functions

You can find out information about existing custom-defined functions using Application Manager:

  1. Start OLAP Server.
  2. Start Application Manager and connect to OLAP Server.
  3. Select Server > Custom-Defined Functions. Essbase displays the Custom Defined Function Manager. You can see a list of all the functions that have been created for the selected server.

No custom-defined functions are displayed until they have been created and registered. Essbase does not supply sample custom-defined functions.

Tip: You can also view information about custom-defined functions using MaxL. For instructions, see Verifying Custom-Defined Functions.

Creating Custom-Defined Functions

There are several steps required to create a custom-defined function:

  1. Learn the requirements for custom-defined functions:
  2. Write a public Java class that contains at least one public, static method to be used as a custom-defined function. For instructions, see Creating and Compiling the Java Class
  3. Install the Java class created in step 2. For instructions, see Installing Java Classes on OLAP Server
  4. Register the custom-defined function as a local or global function. For instructions, see Registering Custom-Defined Functions

Understanding Java Requirements for Custom-Defined Functions

The basis of a custom-defined function is a Java class and method created by a database administrator or Java programmer to perform a particular type of calculation. Creating and testing these Java classes and methods is the first step toward creating a custom-defined function.

You can create more than one method in a class for use as a custom-defined function. In general, it is recommended that you create all the methods you want to use as custom-defined functions in a single class. However, if you want to add new custom-defined functions that are not going to be used across all applications on OLAP Server, create them in a new class and add them to OLAP Server in a separate .jar file.

When creating multiple Java classes that contain methods for use as custom-defined functions, verify that each class name is unique. Duplicate class names will cause methods in the duplicate class not to be recognized, and you will be unable to register those methods as custom-defined functions.

After creating the Java classes and methods for custom-defined functions, test them using test programs in Java. When you are satisfied with the output of the methods, install them on OLAP Server and register them in a single test application. Do not register functions globally for testing, because this makes it more difficult to update them if you find problems.

Understanding Method Requirements for Custom-Defined Functions

Methods in custom-defined functions can have any combination of the following supported data types as input parameters:

Data Types Allowed As Input Parameters

boolean

byte

char

java.lang.String

short, int, long

com.hyperion.essbase.calculator.CalcBoolean

float, double

arrays of any of these types



CalcBoolean is an Essbase-specific data type that can include three values: TRUE, FALSE, and #MISSING. For more information about the other listed data types, see the documentation for the Java Development Kit.

The method return data type can be void or any of the preceding data types. Returned data types are converted to Essbase-specific data types. Strings are mapped to a string type. Boolean values are mapped to the CalcBoolean data type. All other values are mapped to a double type.

Note: Double variables returned with infinite or Not-a-Number values are not supported by Essbase. If these values are returned from a Java program, they may not be recorded or displayed correctly in Essbase. Double variables should be checked for infinite or Not-a-Number values and set to finite values before being returned to Essbase. For more information, see the entry for the class, Double, in the documentation for the Java Development Kit.

For more examples of Java custom-defined functions, see the MaxL statement create function in the Technical Reference in the docs directory.

Understanding Security and Custom-Defined Functions

Essbase requires that you have these security permissions:

Understanding Scope and Custom-Defined Functions

Custom-defined functions are registered with one of two scopes: local (application) or global (OLAP Server). When you register a local custom-defined function, it is available only in the application in which it was registered. When you register a global custom-defined function, it is available to all applications on the OLAP Server on which it was registered.

When developing and testing custom-defined functions, make sure to register and test new functions locally within a test application. You should never register custom-defined functions globally until you have thoroughly tested them and are ready to use them as part of a production environment.

Naming Custom-Defined Functions

When you register a custom-defined function in Essbase, you give the function a name. This name is used in calculation scripts and formulas and is distinct from the Java class and method name used by the function.

Remember these requirements when you create function names:

For more information about registering custom-defined functions, see Registering Custom-Defined Functions.

Creating and Compiling the Java Class

To create a Java class for a custom-defined function, use this procedure:

  1. Start a text editor and create a Java class. For example, open a text editor and create this class:
  2. public class CalcFunc {
      public static double sum (double[] data) {
        int i, n = data.length;
        double sum = 0.0d;
        for (i=0; i<n; i++) {
          double d = data [i];
          sum = sum + d;
        }
        return sum;
      }
    } 
    

  3. Save the Java class as a .java file; for example, CalcFunc.java.
  4. Compile the Java class using the JDK javac tool. At the operating system command prompt, move to the directory containing the class you want to compile and use the javac tool. For example, to compile the CalcFunc.java class, type this command:
  5. >javac CalcFunc.java  
    

  6. Resolve any compiling errors and repeat steps 2 and 3 until the compiler creates a new .class file; for example, CalcFunc.class.

Installing Java Classes on OLAP Server

When you install Java classes on OLAP Server, you must first compile the classes into a Java Archive (.jar) file, and then copy the .jar file to a specific location on the computer running OLAP Server.

Note: You must either stop and restart Essbase applications or stop and restart OLAP Server when you install new .jar files.

To create a .jar file from a .class file and install it on an OLAP Server:

  1. At the operating system command prompt, move to the directory containing the class you want to add to a .jar file and use the JDK jar tool. To add CalcFunc.class to a .jar file, type this command:
  2. >jar cf CalcFunc.jar CalcFunc.class  
    

  3. Copy the .jar file to one of the following locations on the computer running OLAP Server:
  4. If the functions will only be used by specific applications, restart those applications in Essbase. Otherwise, restart OLAP Server.

Registering Custom-Defined Functions

After you have compiled the Java classes for custom-defined functions into .jar files and installed the .jar files on OLAP Server, you must register the custom-defined function before you can use them in calculation scripts and formulas.

When you register a global custom-defined function, all of your Essbase applications can use it. Be sure you test custom-defined functions in a single application (and register them only in that application) before making them global functions.

Caution: Do not register global functions for testing, because this makes it more difficult to change them if you find problems.

Registering Custom-Defined Functions Using MaxL

To register a custom-defined function using MaxL, use this procedure:

  1. Start OLAP Server.
  2. Start MaxL Shell at the operating system command prompt and log on to OLAP Server.
  3. Use the create function statement to register the custom-defined function. The exact syntax is different, depending on whether the custom-defined function is global or local:
  4. Refresh the custom-defined function and macro catalog by using the refresh custom definitions statement in MaxL, or by stopping and restarting the application.

For more detailed information on the create function statement grammar, see the MaxL section of the Technical Reference in the docs directory. For rules about naming custom-defined functions, see Naming Custom-Defined Functions.

Registering Custom-Defined Functions Using Application Manager

To register custom-defined functions using Application Manager, use this procedure:

  1. Start the OLAP Server.
  2. Start the Application Manager and connect to the server.
  3. Click Server > Custom-Defined Functions. Essbase displays the Custom Defined Function Manager:
  4. Select a server to which you are connected, where the custom-defined function will be used. If the custom-defined function is global, disregard the Application selection box. If the custom-defined function is not global, select the application where the custom-defined function will be used.
  5. Click New to register a new custom-defined function. Essbase displays the Custom Defined function Editor:
  6. Select the appropriate scope for the custom-defined function: <Global> if the custom-defined function will be available for all applications on the server, or select an application name, such as Sample in this example, if the custom-defined function will be available for only one application.
  7. In the Name text box, leave the "@" symbol as the first character of the custom-defined function name, and then add the rest of the name, for example JSUM. Notice that the corresponding MaxL statements which Application Manager uses to register the custom-defined function are displayed in the MaxL Statement box. Review the contents of this box as you add entries in the editor to ensure accuracy.
  8. Add the correct information to the fields in the Custom Defined Function Editor:
  9. This example uses information about the sample class created in Registering Custom-Defined Functions Using MaxL. If you need help filling in the values, review that section.

  10. When the information is correct, click OK to register the function. Essbase displays the function in the Custom Defined Function Manager.

Verifying Custom-Defined Functions

After registering custom-defined functions, you can determine whether a function has been registered successfully and whether it is registered locally or globally. You can use MaxL or Application Manager.

To check the registration of custom-defined function on OLAP Server using MaxL, use this procedure:

  1. Start OLAP Server.
  2. Start MaxL Shell at the operating system command prompt and log on to OLAP Server.
  3. Use the display function statement to check the registration of the custom-defined function; for example, type this statement:
  4. MAXL> display function Sample; 
    

    This statement displays a list of all functions registered for the named application (Sample) and any registered global functions. The display function statement lists global functions without an application name to indicate that they are global. If the application contains a function with the same name as a global function, only the local function is listed.

To check the registration of custom-defined functions on OLAP Server using Application Manager, use this procedure:

  1. Start OLAP Server.
  2. Start the Application Manager and connect to the server.
  3. Click Server > Custom-Defined Functions. Essbase displays the Custom Defined Function Manager:
  4. If your function has been successfully registered, it will appear in this list.

Using Registered Custom-Defined Functions

After registering custom-defined functions, you can use them like native Essbase calculation commands. Functions you registered locally-using the AppName. prefix on the function name-are only available for use in calculation scripts or formulas within the application in which they were registered. If you registered the custom-defined functions globally, then the functions are available to all calculation scripts and formulas on the OLAP Server where the functions are registered.

For more information about registering custom-defined functions, see Registering Custom-Defined Functions.

To use a registered custom-defined function:

  1. Create a new calculation script or formula, or open an existing calculation script or formula.
  2. Add the custom-defined function to a new or existing calculation script or formula. To use the custom-defined function shown earlier in this chapter, use this calculation script:
  3. "New York" = @JSUM(@LIST(2.3, 4.5, 6.6, 1000.34)); 
    

    Use this calculation script with the Sample Basic sample database, or replace "New York" with the name of a member in a test database.

  4. Save the calculation script or formula, and then run it as usual.

For more information about creating and running calculation scripts, see Developing Calculation Scripts. For more information about creating and running formulas, see Developing Formulas.

Updating Custom-Defined Functions

When you update a custom-defined function, there are two major issues to consider:

The answers to these questions determine the procedure for updating the custom-defined function.

For information on determining whether a custom-defined function is local or global, see Verifying Custom-Defined Functions.

To update a custom-defined function, in most cases, you must replace the .jar file that contains the code for the function, and then re-register the function. However, if the signature of the custom-defined function-the Java class name, method name and input parameters-have not changed and the function has only one set of input parameters (it is not an overloaded method), you can simply replace the .jar file that contains the function. In either situation, you must stop and restart the Essbase application or OLAP Server where the custom-defined function is registered, depending on whether it is a local or global function.

Note: The following procedure is effective only if the signature of the custom-defined function-the Java class name, method name, and input parameters for the custom-defined function-has not changed.

To update a custom-defined function whose signature has not changed:

  1. Make the changes to the Java class for the custom-defined function and use Java test programs to test its output.
  2. Compile the Java classes and encapsulate them in a .jar file, using the same name as the previous .jar file. Make sure to include any other classes and methods for custom-defined functions that were included in the previous .jar file.
  3. Perform one of the following steps:
  4. Copy the new .jar file to OLAP Server over the existing .jar file with the same name.
  5. Restart the applications you shut down in step 3.

Updating Local Custom-Defined Functions

Local custom-defined functions are registered with an AppName. prefix on the function name and can be used only within the application where they were registered. To update a local custom-defined function, you must stop and restart the Essbase application where the function is registered.

To update a local custom-defined function:

  1. Make the changes to the Java class for the custom-defined function and use Java test programs to test its output.
  2. Compile the Java classes and encapsulate them in a .jar file, using the same name as the previous .jar file. Make sure to include any other classes and methods for custom-defined functions that were included in the previous .jar file.
  3. Perform one of the following steps:
  4. Copy the new .jar file to OLAP Server over the existing .jar file with the same name.
  5. Start MaxL Shell at the operating system command prompt and log on to OLAP Server.
  6. Use the create or replace function statement to replace the custom-defined function; for example, type this statement:
  7. MAXL> create or replace function sample.'@JSUM' 
       2> as 'CalcFunc.sum'; 
    

    Note: This step can also be performed by using the Application Manager Custom Defined Function Manager. Start the server, Application Manager and application, and select File > Custom Defined Function Manager from the menu. Select a function from the list displayed and click Edit to make changes.

Updating Global Custom-Defined Functions

Global custom-defined functions are registered without an AppName. prefix on the function name and are available in any application running on the OLAP Server where they are registered. To update a global custom-defined function, you must stop and restart all applications on OLAP Server.

Global custom-defined functions can be used in calculation scripts and formulas across OLAP Server, so you should verify that no calculation scripts or formulas are using a global custom-defined function before updating it.

Caution: Only a database administrator should update global custom-defined functions.

To update a global custom-defined function:

  1. Make the changes to the Java class for the custom-defined function and use Java test programs to test its output.
  2. Compile the Java classes and encapsulate them in a .jar file, using the same name as the previous .jar file. Make sure to include any other classes and methods for custom-defined functions that were included in the previous .jar file.
  3. Shut down all running Essbase applications.
  4. Copy the new .jar file to OLAP Server over the existing .jar file with the same name.
  5. Start MaxL Shell at the operating system command prompt and log on to OLAP Server.
  6. Use the create or replace function statement to replace the custom-defined function; for example, type this statement:
  7. MAXL> create or replace function '@JSUM' 
       2> as 'CalcFunc.sum'; 
    

    Note: This step can also be performed by using the Application Manager Custom Defined Function Manager. Start the server and Application Manager, and select File > Custom Defined Function Manager from the menu. Select a function from the list displayed and click Edit to make changes.

Removing Custom-Defined Functions

When removing a custom-defined function, you must first determine whether the function is registered locally or globally to identify the security permissions required:

Before removing custom-defined functions, you should verify that no calculation scripts or formulas are using them. Global custom-defined functions can be used in calculation scripts and formulas across OLAP Server, so you must verify that no calculation scripts or formulas on OLAP Server are using a global custom-defined function before removing it.

For information on determining whether a custom-defined function is local or global, see Verifying Custom-Defined Functions.

Removing Local Custom-Defined Functions

Local custom-defined functions are registered with an AppName. prefix on the function name and can only be used within the application where they are registered. When you remove a local custom-defined function, the Essbase application where the function is registered must be stopped and restarted.

To remove a local custom-defined function:

  1. Start OLAP Server.
  2. Start MaxL Shell at the operating system command prompt and log on to OLAP Server.
  3. Use the drop function statement to remove the custom-defined function; for example, type the following statement:
  4. MAXL> drop function Sample.'@JSUM'; 
    

    Note: This step can also be performed by using the Application Manager Custom Defined Function Manager. Start the server and Application Manager, and select File > Custom Defined Function Manager from the menu. Select a function from the list displayed and click Delete to remove the custom-defined function.

  5. Restart the application where the function was registered. This refreshes the custom-defined function and macro catalog.

Removing Global Custom-Defined Functions

Global custom-defined functions are registered without an AppName. prefix on the function name and are available in any application running on OLAP Server where they are registered. When you remove a global custom-defined function, all running Essbase applications must be stopped and restarted.

Global custom-defined functions can be used in calculation scripts and formulas across OLAP Server, so you should verify that no calculation scripts or formulas are using a global custom-defined function before removing it.

Caution: Only a database administrator with supervisor permission can remove global custom-defined functions. Removal of global custom-defined functions should only be performed when no users are accessing Essbase databases and no calculation routines are being performed.

To remove a global custom-defined function:

  1. Start OLAP Server.
  2. Start MaxL Shell at the operating system command prompt and log on to OLAP Server.
  3. Use the drop function statement to remove the custom-defined function; for example, type the following statement:
  4. MAXL> drop function '@JSUM'; 
    

    Note: This step can also be performed by using the Application Manager Custom Defined Function Manager. Start the server and Application Manager, and select File > Custom Defined Function Manager from the menu. Select a function from the list displayed and click Delete to remove the custom-defined function.

  5. Restart the application where the function was registered. This refreshes the custom-defined function and macro catalog.

Performance and Memory Considerations for Custom-Defined Functions

The ability to create and run custom-defined functions is provided as an extension to the Essbase calculator framework. When you use custom-defined functions, consider how their use affects memory resources and calculator performance.

Performance Considerations

Because custom-defined functions are implemented as an extension of the Essbase calculator framework, you can expect custom-defined functions to operate less efficiently than functions that are native to the Essbase calculator framework. In tests using a simple addition function running in the Java Runtime Environment 1.3 on the Windows NT 4.0 platform, the Java function ran 1.8 times (about 80%) slower than a similar addition function performed by native Essbase calculation commands.

To optimize performance, limit use of custom-defined functions to calculations that you cannot perform with native Essbase calculation commands, particularly in applications where calculation speed is a critical consideration.

Memory Considerations

Use of the Java Virtual Machine (JVM) and Java API for XML Parsing has an initial effect on the memory required to run Essbase. The memory required to run these additional components is documented in the memory requirements for Essbase. For more information about memory requirements, see the Essbase Installation Guide.

Beyond these start-up memory requirements, the Java programs you develop for custom-defined functions sometimes require additional memory. When started, the JVM for Win32 operating systems immediately allocates 2 MB of memory for programs. This allocation is increased according to the requirements of the programs that are then run by the JVM. The default upper limit of memory allocation for the JVM on Win32 operating systems is 64 MB. If the execution of a Java program exceeds the default upper limit of memory allocation for the JVM, the JVM generates an error. For more information about JVM memory management and memory allocation details for other operating systems, see the Java Development Kit documentation.

Considering the default memory requirements of the JVM and the limitations of the hardware on which you run servers, carefully monitor your use of memory. In particular, developers of custom-defined functions should be careful not to exceed memory limits of the JVM when creating large objects within custom-defined functions.




© 2002 Hyperion Solutions Corporation. All rights reserved.
http://www.hyperion.com