Formatter - Sample Code

LatitudeFormatter Example for Custom Formatter Plugins

This example demonstrates a formatter plugin for Oracle Web Determinations.  For more general information on constructing and debugging plugins, see the topic, Create a Plugin

The LatitudeExample.zip archive contains a custom formatter plugin (Java source and built .jar file) and a rulebase that uses it.
The example custom formatter defers to the default formatter for all values except for attribute values where a Number attribute has a custom property IsLatitude set to "True".

For these, it formats and parses numbers as Latitude values eg. 28°32'45.63"S

The example includes a re-usable abstract class, PassthroughFormatter, which delegates all formatting to the default formatter, allowing your custom formatter to concentrate on special cases.

To construct the example:

First create a rulebase which will use the default formatter.

  1. In Oracle Policy Modeling, choose File->NewProject... and name the project, "LatitudeExample".
  2. Right-click the Rules folder, and select, "Add New Word Document"
  3. Name the new word document "rules.doc" and double-click to edit.
  4. Microsoft Word will open. In Word, add the following rules:



  5. Select, "The latitude of the person's primary residence" (either place it occurs) and type alt-G to add a number variable. You should now have something like this:



  6. Compile the rules (alt-R) and close the document, returning to the Oracle Policy Modeling application.

 

Now we want to test that the rulebase is working correctly.

  1. In Oracle Policy Modeling, choose Build->Build and Debug..., With screens.
  2. An interview should open, in which you can determine if an applicant is eligible for the imaginary polar-resident benefits by entering a number, such as "-30" or "4000"
  3. Note that at this point our web application is accepting these bad latitude values!
  4. After determining your eligibility, click on, "Why?" You should get a display like this, showing your input:


 

You should now have a functioning rulebase. If your rulebase is not working correctly so far, you should fix any problems before proceeding to the customization steps.

Build a formatter plugin

The next task is to build a plugin. Our plugin has two classes. Java source code is provided, but the .NET source code is very similar:

  1. Create a Java project and add the following JAR files as Project Libraries (these files can be found inside the Web Determinations folder 'lib' folder, for example: C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\web-determinations\WEB-INF\lib):
    1. determinations-engine.jar
    2. determinations-interview-engine.jar
    3. determinations-utilities.jar
    4. web-determinations.jar

  2. Create the java files with the source code from this page, and compile them to a JAR file in your favorite Java development environment. You will need to tell your java compiler how to find the necessary Oracle Policy Automation dependencies, as detailed in the topic Create a Plugin. Call the resulting JAR file, LatitudeExample.jar.
  3. Copy the LatitudeExample.jar file you have created to your rulebase's plugins directory, where Oracle Web Determinations can find it. If your rulebase is at <...>\LatitudeExample, the correct directory for your plugin is <...>\LatitudeExample\Release\web-determinations\WEB-INF\classes\plugins. If the Release directory does not exist, Build and Debug your rulebase once to create it; your plugin should now be loading, but it won't be doing anything visible yet.

Customize the rulebase to use the plugin

Now we want to invoke our plugin to format our rulebase's "the latitude of the person's primary residence" attribute as a latitude. The plugin is looking for a custom property, "IsLatitude" so we need to create that property and set it on the special attribute.

  1. In Oracle Policy Modeling, choose File->Project Properties..., add a Boolean custom property called "IsLatitude", and click OK:



  2. Right-click the "Properties" folder, and choose "Add New Properties File". Call it "properties"
  3. Open the properties file, right-click inside the empty "Attributes" section for the global entity, and choose, "Generate Public Names..."
  4. Make sure the checkbox is ticked for the attribute, "the latitude of the person's primary residence"
  5. The attribute should now show up in the view. Double-click it to edit its properties:



  6. Click the "Custom Properties" tab at the bottom of the dialog, and add a value of "True":




  7. Click OK, save, and build and debug again. Your custom formatter should now be working! On the latitude question, you may enter a number as before, or you may enter a latitude value such as: 30°12'13"S (Because the ° character can be hard to type, the formatter also accepts a space as in 30 12'13"S )
  8. Whether you enter a decimal number or a properly-formatted latitude, the custom formatter will reject values beyond the north or south pole, and show a properly-formatted latitude on the decision report screen:

Plugin Source Code

 

LatitudeFormatterPlugin.java

 

PassthroughFormatterPlugin.java