SCORE- and RANK-functions

Creating SCORE- and RANK-functions

To get an overview of SCORE- and RANK-functions see Syntax#Score and Syntax#Numerical ranking.

Syntax

Syntax for creating a score function

CREATE [OR REPLACE] SCOREFUNCTION '<name>' AS BEGIN
[IMPORT
<javacode: imports>
#IMPORT]
GETMAXSCORE:
<javacode: getMaximumScore() method>
#GETMAXSCORE
DOMAINTYPE:
<BOOLEAN>| <STRING> | <SET> | <DOUBLE>
#DOMAINTYPE
SCORE:
<javacode: score(Object) method>
#SCORE
END;

Syntax for creating a rank function:

CREATE [OR REPLACE] RANKFUNCTION '<name>' AS BEGIN 
[IMPORT
<javacode: imports>
#IMPORT]
RANK:
<javacode: rank method>
#RANK
END;

Example

As a use case we want to represent a customer interested in buying a relative new, inexpensive car. More precisely his favor for a cheap car is three times more important than the age of the vehicle. For the price, a difference of 100 rupees doesn't matter. As for the age, we like to use a regression function like the square root so that newer cars can more likely be differentiated whereas for older cars the age difference won't really matter.

To represent this preference, we can use a rank function with the predefined sqrtScoreF scoring function for the age:

Thus we can write:

CREATE RANKFUNCTION 'carRankF' AS BEGIN 
IMPORT:
import java.util.Iterator;
import java.util.List;
#IMPORT
RANK:
Iterator<RankFunctionParameter> par = getParameters().iterator();
double price = par.next().getFunctionValue(objPref, prefContext);
double age = par.next().getFunctionValue(objPref, prefContext);
return (price*3+age)/4;
#RANK
END;

It remains to define the scoring functions for the price:

CREATE SCOREFUNCTION 'priceScoreF' AS BEGIN 
IMPORT:
import preference.dataconnector.MinMax;
#IMPORT
GETMAXSCORE:
MinMax minmax = getDataConnector().getMinMaxValues(getTableName(), getAttributeName());
return minmax.getMax()/100.;
#GETMAXSCORE
DOMAINTYPE:
double
#DOMAINTYPE
SCORE:
double value = Double.parseDouble(A.toString()); // we have A : Object and cast it to double
return value/100.;
#SCORE
END;

Finally, we want to search with our new rank function by:

SELECT * 
FROM car
PREFERRING (price SCORE 'priceScoreF' | age SCORE 'sqrtScoreF') RANK 'carRankF';

Predefined SCORE-functions

  • absScoreF: Returns absolute values
  • identityScoreF: Returns unmodified score-values
  • negScoreF: Negates the values
  • powThreeScoreF: Values to the power of 3
  • powTwoScoreF: Values to the power of 2
  • sqrtScoreF: Square root of the values

Predefined RANK-functions

  • sumRankF: Does a summation or a weighted summation, depending on the syntax; for a standard summation just use it as any other rank-function, for weighted summation use it as with the following addendum:

    The number of attributes (here: 2, age and mileage) have to match the number of arguments (here: 2; 2, and 3).

    SELECT * 
    FROM CAR
    PREFERRING (age SCORE 'identityScoreF' | mileage SCORE 'identityScoreF') RANK 'sumRankF' '2,3';
  • avgRankF: Returns the average or the weighted average, for latter see syntax for "sumRankF"
  • deviationRankF: Calculates the deviation
  • maxRankF: Returns the highest value
  • minRankF: Returns the lowest value
  • productRankF: Multiplies all values and returns the arithmetic product
  • varRankF: Calculates the variance

Function Management

The following command shows all selfdefined (e.g. dynamic) score and rank functions:

PRINT SCORERANKFUNCTIONS; 

The next command shows the generated Java-source code of a score/rank function:

PRINT SCORERANKFUNCTION <label> 

The last command deletes a score/rank function:

DELETE SCORERANKFUNKTION <label>;