How to Implement a User-Defined Single-Row Function

User-Defined Functions 13-5 public Class? getReturnType { return Float.class; } public AggregationFunction newAggregationFunction { return new Variance; } public void releaseAggregationFunctionAggregationFunction function { } public Object handleMinusObject[] params { if params = null params.length == 1 { Integer param = Integer params[0]; count--; sum -= param; sumSquare -= param param; } if count == 0 { return null; } else { return getVariance; } } public Object handlePlusObject[] params { if params = null params.length == 1 { Integer param = Integer params[0]; count++; sum += param; sumSquare += param param; } if count == 0 { return null; } else { return getVariance; } } public Float getVariance { float avg = sum float count; float avgSqr = avg avg; float var = sumSquare floatcount - avgSqr; return var; } public void initialize { count = 0; sum = 0.0F; sumSquare = 0.0F; } } 2. Compile the user-defined function Java implementation class and register the class in your Oracle CEP application assembly file as Example 13–5 shows. Example 13–5 Aggregate User Defined Function for an Oracle CQL Processor wlevs:processor id=testProcessor wlevs:listener ref=providerCache wlevs:listener ref=outputCache wlevs:cache-source ref=testCache 13-6 Oracle Complex Event Processing CQL Language Reference wlevs:function function-name=var bean class=com.bea.wlevs.test.functions.Variance wlevs:function wlevs:processor For more information, see wlevs:function in the Oracle Complex Event Processing Developers Guide for Eclipse. 3. Invoke your user-defined function in the select list of a SELECT statement or the condition of a WHERE clause as Example 13–6 shows. Example 13–6 Accessing a User-Defined Aggregate Function in Oracle CQL ... query id=uda6[CDATA[ select varc2 from S4[range 3] ]]query ... At run-time, when the user-defined aggregate is executed, and a new event becomes active in the window of interest, the aggregations will have to be recomputed since the set over which the aggregations are defined has a new member. To do so, Oracle CEP passes only the new event rather than the entire active set to the appropriate handler context by invoking the appropriate handlePlus method in Example 13–4 . This state can now be updated to include the new event. Thus, the aggregations have been recomputed in an incremental fashion. Similarly, when an event expires from the window of interest, the aggregations will have to be recomputed since the set over which the aggregations are defined has lost a member. To do so, Oracle CEP passes only the expired event rather than the entire active set to the appropriate handler context by invoking the appropriate handleMinus method in Example 13–4 . As before, the state in the handler context can be incrementally updated to accommodate expiry of the event in an incremental fashion.