Oracle9i OLAP Services Developer's Guide to the Oracle OLAP API
Release 1 (9.0.1)

Part Number A88756-01
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table of Contents
Contents
Go To Index
Index

Master Index

Feedback

Go to previous page Go to beginning of chapter Go to next page

Performing Calculations, 6 of 7


Creating Your own Numerical Functions

Creating parameters

The alias method can be used to create parameters. "Example: Creating a function" shows how to create a new function using the alias method. You can only create cell or row calculation functions in this way. To create client aggregation or position-based functions you use the extract method.

Example: Creating a function

The following function takes a number and multiplies it by 1.05. The function has one parameter, called param, which is created by calling the alias method on the fundamental Source representing the Number OLAP API data type which is the set of all numbers. Note how the value method is used to make the parameter an input of the function.

//Get the Source that represents the number data type
NumberSource number =(NumberSource)dataProvider
.getFundamentalDefinitionProvider()
.getNumberDataType()
.getSource();
//Create a parameter
NumberSource param = (NumberSource)number.alias();
//Create a function
NumberSource function = ((NumberSource)param.value()).times(1.05);

The function created in this way is effectively the same as the built-in functions provided by the OLAP API. It can be used by joining the function to the parameter and the required parameter expression as shown below. You can then apply the function to a Source named sales as shown below.

//Use the function
NumberSource sales = ...;
NumberSource fsales = function.join(param, sales);

Example: Creating a parameterized selection

Assume you want to create a product selection defined to be the set of all products for which the unitsSold measure is greater than the value specified by a parameter. The parameter must be specified before data can be fetched from this Source. You can create this parameter using the following code.

//Get the Source that represents the number data type
NumberSource number = dataProvider
.getFundamentalDefinitionProvider()
.getNumberDataType()
.getSource();
//Create a parameter
NumberSource param = (NumberSource)number.alias();
//Create a parameterized selection
Source products = ...;
NumberSource unitsSold = ...;
Source productSelection = products.select(unitsSold.gt(param.value()));

To set the value of the parameter to 100, you write the following code.

Source unitsSoldGT100 = productSelection.join(param, 100);

Example: Creating an aggregation function

Assume that you want to create a weighted average function. To do so, you write the following code.

//Define an aggregation function
NumberSource weight = ...;
//Create a parameter
NumberSource param = (NumberSource) number.alias();
//Create a function
NumberSource weightedAverage = param.extract().times(weight).average();

As with the example of a standard function "Example: Creating a function", this code first creates a parameter named param for the function to use. However, since this is an aggregation function, the code uses the extract() method with param when it calculates the final result.

To use this function, you issue the following code.

//Use the aggregation function
NumberSource sales = ...;
NumberSource paramSales = dp.createConstantSource(param.selectValues(sales));
Source weightedSales = weightedAverage.join(paramSales);

Go to previous page Go to beginning of chapter Go to next page
Oracle
Copyright © 1996-2001, Oracle Corporation.

All Rights Reserved.
Go To Documentation Library
Home
Go To Product List
Book List
Go To Table of Contents
Contents
Go To Index
Index

Master Index

Feedback