It is possible that at some point during development, an rule-based application may require a rulebase to perform a calculation that cannot be done (or cannot be done easily) using the built-in functions of the Oracle Determinations Engine. To handle these situations, a custom function can be written to perform the calculation required.
Custom functions are written in the same language as the application using Oracle Determinations Engine (for example, in Java if using the Oracle Determinations Engine API for Java), and must be contained inside a class that implements the CustomFunctionHandler interface. An instance of this class must be registered with the session using Session.setCustomFunctionHandler() before any inferencing is performed on the session. There can only be only one custom function handler installed on a session, but that custom function handler can handle multiple custom functions.
The CustomFunctionHandler interface has two methods to be implemented: evaluate() and initialize().
Evaluate() receives a CustomFunctionDeclaration object containing the name of the custom function, an EntityInstance object in which the custom function was triggered, and the parameters that were passed to it from the rulebase. The return value of the method can be any of the types that Attribute.setValue() supports, including unknown using null or uncertain using Uncertain.INSTANCE. The EntityInstance allows attribute values to be queried, but should not be used to change them. A custom function should only change attribute values indirectly, by supplying a return value.
The evaluate() method may also throw any runtime exceptions to indicate that an error has occurred while evaluating a custom function.
The method initialize() is never called by Oracle directly. This method is included on the custom function handler interface to provide application developers with a single convenient location to initialize any parameters required for the custom functions provided by the implementation class.
The following sample code shows a custom function handler class that implements the "Maximum" custom function, which returns the maximum number from an arbitrary number of attributes.
For example, if “the first number”=4, “the second number”=200, and “the third number”=88, then the custom function would return 200.
The “Maximum” custom function would be used in Oracle Policy Modeling as in the sample rule below. Note that “n1”, “n2” and “n3” are the public names of “the first number”, “the second number, and “the third number” respectively.
For information on calling a custom function from Oracle policy Modeling, refer to the topic Write a rule that uses a custom function in the Oracle Policy Modeling Help.
Click on the appropriate link to view either the Java or the C# example:
See also