Policy Automation – Custom Java Libraries #2

Policy Automation – Custom Java Libraries #2

In the previous post we looked at the basic setup of your Policy Automation project so that it can receive a custom Java Library containing some functions, and use them in your Policy Automation Documents like standard functions.

Now we can move on to the first example function. We will use a suitable development environment such as NetBeans or your own choice. We create a simple piece of code like the following :-

package com.oracle.determinations.examples;
//Import Custom Functions and EntityInstances
import com.oracle.determinations.engine.CustomFunction;
import com.oracle.determinations.engine.EntityInstance;
import java.util.*;
 * @author Richard Napier
public class ODECalculate extends CustomFunction {
 public Object evaluate(EntityInstance arg0, Object[] arg1) {
 // TODO Auto-generated method stub
 String firstargument = arg1[0].toString();
 String secondargument = arg1[1].toString();
 String combinedargs = firstargument + " " + secondargument;
 StringBuffer bufferedtext = new StringBuffer(combinedargs);
 System.out.println("Reverse String Buffer returns " + bufferedtext.reverse().toString());
return bufferedtext.toString(); }

Let us walk through the code. Firstly our new library imports the necessary:

import com.oracle.determinations.engine.CustomFunction;
import com.oracle.determinations.engine.EntityInstance;

Then we extend CustomFunction, overriding the standard evaluate Method with our own code. This simply extracts the two input arguments we defined in our XML file last time, and concatenates them. Then we use a StringBuffer and the built-in reverse() function to reverse the text. So Policy Automation will become yciloP moitamotuA.

Finally we return that as the output of our custom evaluate method. In the code above we have also written the output to the Console, which we will be able to see in Policy Modelling perhaps for debugging purposes.

OPA - Sample Code

Readers will notice that we have also imported some libraries that we have not used in this example, they will be used in a later post. Now that our code has been entered in our chosen development tool, we also need to Build the code into a Jar File.

OPA - Build JAR file

Once we have obtained our JAR file we can place in the correct folder for our Policy Automation Project, which is a child of the folder where we placed our XML file.

OPA - Folder for JAR file in Policy Automation Folder StructureNow we are ready to write our first line of Policy Automation using our new function. Provided we respect the XML definition, the following will compile successfully.

OPA - Example Function Call

We can then run the Web Determinations on a java-based platform to see the result. The easiest way is to Build and Run Web Determinations to use the embedded version of Tomcat. Of course our interview is not very interactive so far, with only these two questions.  But the result of the interview should be visible in the Browser, and also in the Embedded Web Server Output.

OPA - Console Output OPA - Reversed Text in Web Determination

Of course writing functions in this way must respect certain basic, well documented principles, in order that we do not interfere with the inference engine. The output of your function must be predictable – that is, providing the same inputs must provide reliably the same outputs. This is not a good place to generate a truly random number, for example.

In the next post, we will further customize our function to use the EntityInstance. See you soon!


Richard Napier

Author: Richard Napier

Richard Napier joined Siebel Systems in 1999 and took up the role of managing the nascent Siebel University in Southern Europe. He subsequently was Director of Business Development and Education for InFact Group (now part of Business & Decisions) for 8 years. He now runs his Consulting and mentoring company, On Demand Consulting & Education Ltd and has run ODCE since 2010. Owner of the OPA Hub, he also is Co-Founder of the Siebel Hub.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Logo by Southpaw Projects LLC