Oracle SOA Suite Online Training

Interested in learning Oracle SOA Suite 12c?
Learn from the author of this blog!
A complete and comprehensive course on the #1 platform on SOA - Oracle SOA Suite

Click here to find the complete course details
Click here to check the first session on Oracle SOA Suite 12c


Functions in Oracle Business Rule

This sample demonstrates creation of a custom function in Oracle Business Rules, using it in the RuleSet and calling it form a BPEL Process

High-level details

This example involves the following
a.      Implementation of a custom function in Oracle Business Rule
This is a simple function that adds the input date by 1 day and returns. This function will be further enhanced in later examples by extending the logic.
b.      Calling the custom function from the Business Rule If-Else Decision Service
c.      Calling the Business Rule from BPEL


·        Create a schema for the input and output

·        Create a SOA Process with a BPEL Process using these input and output

·        Create a new Business Rule Component in the same component using the same input and output. In this example, since the usage of BPEL process is just to call the Business Rule, the schema elements are same but these could be any depending on your business requirement.

·        Business Rule Implementation
a.      Creating Globals - for constants

a.      Creating Function
First, define the signature of the function
Click on the '+' icon, give the name of the function, returnType, and description
In the Arguments section, define the input arguments of this function

You'll see a warning as the function is not implemented yet
Pseudo Code for the function
Initialize the variables
Convert the input  string into XMLGeorgianCalendar format (This has many date related functions, so used this type)
Add 1 day to this converted date
Convert the resulting value to String and return

a.      Defining Ruleset
               This is a simple ruleset to call the just defined function

The final Ruleset looks like this

Now that the Business Rule is implemented successfully, the only thing left is to use this in the BPEL Process

·        Using the Business Rule inside BPEL Process
Inside BPEL, drag and drop the Business Rule component from the SOA Components section in the Resource Pallette
Input & Output Association in BPEL

Your final BPEL Process should look like this

Thats it!
Deploy & Test the Process

If-Else in Assign Activity

There are scenarios often that need if-else conditions in your BPEL. For this, you will usually use a switch or if activities.

But if the condition is so simple, and you do not want to use a switch activity, then you might consider the 'translate' xpath function provided in the Assign activity.

This is how the translate function works

translate(arg1, arg2, arg3)

arg1 : Is your value that you want to operate on
arg2 : What characters are to be picked for replacement
arg3 : Character-by-Character replacement

translate funciton replaces the content character by character.
For example, if you want to replace 1 with A, 2 with B, 3 with C, and so on, your translate function would look like

translate(arg1, 'ABCDEFGH','12345678')
Example 1 :
If arg1 = BDGHFCDGA,
then the result would be 247863471

The above example is just for explanation on how it works, but a real world application would be as follows

lets say you get a requestId from input and you want to check if the request id is null or not, and if not null, result value is Y, otherwise N

Psuedo code :
Step 1
First, you will check if the input contains numbers, and replace all the numbers with 'Y' (you can use any letter for that matter in this step)
    So, if the input is 54695, then after translation, your output will be YYYYY
Step 2
Then, you will check if the result in step 1 contains 'Y' using the 'contains' xpath function
    In this step, your output will be either true or false
Step 3
Next, if the output of the previous step is true, replace the first letter 't' with 'Y' and ignore the rest

Actual Code
(These are mappings in assign activity)
Step 1
translate('../ns0:requestId','0123456789','YYYYYYYYYY') ------> $varReqId
Step 2 & 3
translate(contains($varReqId, 'Y'), 'truefals', 'Y') -----------> $varReqId

Explanation :
Lets say varReqId = 3246
Step 1 result will be YYYY (As 3 is replaced with Y, 2 is replaced with Y, 4 is replaced with Y, 6 is replaced with Y)
Next, Contains (YYYY,Y) results in true
Next, translate(true, truefals,Y) will replace t with Y and ignore the rest.
So, the first letter in true will be replaced with Y and the rest is ignored.

If you are not comfortable with this approach, or if its confusing, you always have the switch or if activity in BPEL, but your BPEL will become huge if the number of such conditions are more.

Hope this helps.