27 Developing Custom-Defined Calculation Functions

To enhance the calculation functions available for Essbase block storage cubes, you can use Java to develop your own custom-defined functions (CDFs). After you write the functions, install the Java class and then register the functions, either globally with the Essbase Server, or locally to an application.

You can use your custom-defined functions in Essbase calculation scripts.

Essbase does not provide tools for creating Java classes and archives; you must have a supported version of the JDK.

For examples of custom-defined functions, see Java Code Examples.

Custom defined functions are available only for block storage cubes (not relevant to aggregate storage cubes).

To create a custom-defined function, use the following workflow.

  1. Review the requirements for custom-defined functions: Requirements for Validity of 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: Creating and Compiling a Java Class for Custom Defined Functions

  3. Install the Java class: Installing Java Classes on Essbase Server

  4. Register the custom-defined function as a local or global function: Registering Custom-Defined Functions

Requirements for Validity of Custom-Defined Functions

You design your Essbase custom-defined functions as methods in a Java class. For global functions, write methods in one class. For application functions, use separate classes and jar files per application. Test functions locally to one application before registering them globally. Note the supported data types, variables, and naming conventions.

You can create multiple methods in a class for use as a custom-defined function. Typically, Oracle recommends that you create the methods that you plan to use across all applications on an Essbase Server as custom-defined functions in a single class. If, however, you plan to add custom-defined functions that will be used in selective applications on the Essbase Server, create these custom-defined functions in a separate class and add them to Essbase 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 cause methods in the duplicate class not to be recognized, and you cannot register those methods as custom-defined functions.

Using test programs in Java, test the Java classes and methods. When you are satisfied with the output of the methods, install them on Essbase Server and register them in a single test application. Do not register functions globally for testing; doing so makes updating them more difficult if you encounter problems.

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

  • boolean

  • byte

  • char

  • com.hyperion.essbase.calculator.CalcBoolean

  • float, double

  • java.lang.String

  • short, int, long

  • arrays of any of these types

CalcBoolean is an Essbase-specific data type that can include three values—TRUE, FALSE, and #MISSING. For information about the other listed data types, see the JDK documentation.

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:

Essbase does not support double variables returned with infinite or Not-a-Number values. 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. See the entry for the class Double in the JDK documentation.

For creating, deleting, and managing custom-defined functions, Essbase requires these security permissions:

  • Local, application-wide, custom-defined functions: Application Manager or higher

  • Global, server-wide, custom-defined functions: System Administrator

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

Follow these requirements for naming custom-defined functions:

  • Start the name with the @ symbol. The rest of a function name can contain letters, numbers, and the following symbols: @, #, $, and _. Function names cannot contain spaces.

    For example: @MYFUNCTION

  • Start the names of custom-defined functions that are called only by custom-defined macros with “@_”, to distinguish them from general-use functions and macros.

    For example: @_MYFUNCTION

  • Custom-defined functions must have unique names. Function names must be different from each other, from the names of custom-defined macros, and from the names of existing calculation functions.

  • If an Essbase application contains a local function that has the same name as a global function, the local function is used for calculation.

Creating and Compiling a Java Class for Custom Defined Functions

To create and compile a Java class for Essbase custom defined functions, write the class using a text editor or IDE (integrated development environment), and compile it using the javac tool.

Here is a sample workflow for creating a Java class for a custom-defined function:

  1. In a text editor, create a Java class.

    For example:

    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;
      }
    }
    
  2. Save the file with a .java extension.

    For example:

    CalcFunc.java
  3. Navigate to the directory where the .java file resides; at a command prompt, enter this command:
    javac java_filename

    For example:

    javac CalcFunc.java
  4. Resolve any compiling errors until the compiler creates a new file with a .class extension.

    For example:

    CalcFunc.class

Installing Java Classes on Essbase Server

To install the Java classes for your custom defined calculation functions (CDFs) onto the Essbase Server, compile them, copy the jar file to a global or application level udf directory as specified in these instructions, and restart the application or server.

Java classes must be compiled in a JAR file, using the JDK jar tool.

To create a .jar file and install it on an Essbase Server:

  1. Navigate to the directory where the .class file resides; at a command prompt, enter this command:
    jar cf jar_filename class_filename

    For example:

    jar cf CalcFunc.jar CalcFunc.class
  2. On the computer running Essbase Server, copy the .jar file to one of the following directories (if the directory does not exist, create it):
    • For .jar files containing global custom-defined functions:

      ESSBASEPATH/java/udf/
    • For .jar files to be used only with specific applications:

      <Application Directory>/app/appname/udf/

      where appname is the name of the application where the local custom-defined function will be used.

    If you do not know the location of ESSBASEPATH or <Application Directory>, see Environment Variables in the Essbase Platform.

    If the .jar file is subsequently placed in another location, you must modify the CLASSPATH variable to include the full path and filename for the .jar file.

  3. If the functions will be used only by specific applications, restart those applications. Otherwise, restart Essbase Server. See Start, Stop, and Check Servers for independent deployments, or Use Commands to Start, Stop, and View Status of Processes for stack deployment on OCI.

Registering Custom-Defined Functions

Use MaxL to register your custom defined functions with Essbase. The registration task comes after you have written the CDFs within Java classes, compiled the classes, and installed the jar files.

After you have compiled the Java classes for custom-defined functions into .jar files and installed the .jar files on Essbase Server, you must register the custom-defined functions before you can use them in calculation scripts and formulas. See Requirements for Validity of Custom-Defined Functions.

When you register a global custom-defined function, all Essbase applications on the Essbase Server can use it. Test custom-defined functions in a single application (and register them only in that application) before making them global functions.

Use the same process for updating the catalog of functions as for updating the catalog of macros. See Refreshing the Catalog of Custom-Defined Macros.

Caution:

Do not register global functions for testing; doing so makes changing them more difficult if you encounter problems.

To register a custom-defined function, use the create function MaxL statement.

To register a custom-defined function with local scope, include the application name as a prefix. For example, the following MaxL statement registers the custom-defined function, @JSUM, in the CalcFunc class as a local function for use within the Sample application:

create function Sample.'@JSUM'
as 'CalcFunc.sum'
spec '@JSUM(memberRange)'
comment 'adds list of input members';

To register a custom-defined function with global scope, do not include the application name as a prefix. For example, the following MaxL statement registers the custom-defined function, @JSUM, in the CalcFunc class as a global function for use in any application on Essbase Server:

create function '@JSUM'
as 'CalcFunc.sum'
spec '@JSUM(memberRange)'
comment 'adds list of input members';

Note:

Specifying input parameters for the Java method is optional. If you do not specify input parameters, Essbase reads them from the method definition in the Java code. If, however, you are registering multiple custom-defined functions with the same method name but with different parameter sets, you must register each version of the function separately, specifying the parameters for each version of the function.

Using Registered Custom-Defined Functions

You can use registered custom-defined functions in calculation scripts and formulas, the same way you use native Essbase calculation functions.

To use a registered custom-defined function:

  1. Create or open an existing calculation script or formula.
    • If the custom-defined function was registered locally—within a specific application—you must use a calculation script or formula within that application.

    • If the custom-defined function was registered globally, you can use any calculation script or formula on Essbase Server.

  2. Add the custom-defined function to the calculation script or formula.

    For example, to use JSUM, use this calculation script:

    "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.

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

Updating Custom-Defined Functions

To update an Essbase custom defined function, determine if it is local or global in scope, shut down the impacted application(s), replace the .jar file that contains the code for the function, and re-register the function.

The procedure for updating custom-defined functions depends on these conditions:

  • Whether the function is registered locally or globally

  • Whether the signature of the custom-defined function—class name, method name, or input parameters—has been changed in the Java code for the custom-defined function

Typically, to update a custom-defined function, you must replace the .jar file that contains the code for the function, and then re-register the function. If, however, the signature of the custom-defined function has not changed, and the function has only one set of input parameters (it is not an overloaded method), you can replace the .jar file that contains the function.

Note:

Only DBAs should update global custom-defined functions.

To update a custom-defined function:

  1. Determine whether the function is local or global.
  2. Make the changes to the Java class for the custom-defined function and use Java test programs to test its output.
  3. Compile the Java classes and archive them in a new .jar file, using the same name as the previous .jar file.

    Include any other classes and methods for custom-defined functions that were included in the previous .jar file.

  4. Perform an action, depending on whether you are updating a local or global custom-defined function:
    1. Local: Shut down any Essbase applications that use the functions in the .jar file.
    2. Global: Shut down all Essbase applications

    If you are unsure which Essbase applications use which functions in the .jar file, shut down all Essbase applications.

  5. Copy the new .jar file to Essbase Server, replacing the existing .jar file with the same name.
  6. If the signature of the custom-defined function has not changed, skip to step 8.
  7. To replace the custom-defined function, use the create or replace function MaxL statement. For example:
    • Local:

      create or replace function sample.'@JSUM'
      as 'CalcFunc.sum';
    • Global:

      create or replace function '@JSUM'
      as 'CalcFunc.sum';
  8. Restart the applications that you shut down, which updates the catalog.

Viewing Custom-Defined Functions

View a custom-defined function in Essbase to determine whether it has been registered successfully and whether it is local or global in scope. Custom-defined functions are not displayed until they have been created and registered.

To view a custom-defined function:

Use the display function MaxL statement.

For example, use the following MaxL statement to view the custom-defined functions in the Sample application and any registered global functions:

display function Sample;

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.

Removing Custom-Defined Functions

To remove Essbase custom defined functions, first be sure that they are not in use. Then, shut down the application(s) in which the CDFs are defined, remove the CDFs by issuing the MaxL drop function statement, and restart the affected application(s).

The following permissions are required to remove a custom-defined function:

  • Local: At least Application Manager permission for the application

  • Global: System Administrator permission

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 Essbase Server, so you must verify that no calculation scripts or formulas on Essbase Server are using a global custom-defined function before removing it.

Caution:

Remove global custom-defined functions only when users are not accessing Essbase cubes, and calculation routines are not being performed.

To remove a custom-defined function:

  1. Determine whether the function is local or global.
  2. Perform an action, depending on whether you are removing a local or global custom-defined function:
    1. Local: Shut down any Essbase applications that use the functions in the .jar file.
    2. Global: Shut down all Essbase applications.
  3. To remove the custom-defined function, use the drop function MaxL statement. For example:
    • Local:

      drop function Sample.'@JSUM';
    • Global:

      drop function '@JSUM';
  4. Restart the applications that you shut down, which updates the catalog.

Copying Custom-Defined Functions

You can copy custom-defined functions to any Essbase Server and application to which you have appropriate access.

To copy a custom-defined function, use the create or replace function as MaxL statement.

Performance Considerations for Custom-Defined Functions

Because custom-defined functions are implemented as an extension of the Essbase calculator framework, you can expect CDFs to operate less efficiently than native Essbase calc functions.

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