Most Recent

Blog Post

Creating a custom Function rule in Pega

Creating a custom Function rule in Pega

Keep It Simple Stupid

The world around us is becoming increasingly more complex. And so is the digital world. Cryptocurrencies have been soaring, but it looks as if the tipping point is near now Bitcoin, Ethereum and the likes as well as stock markets are plunging. Some people call it The Next Dot-Com Bubble. Something that could throw us back to simpler times. The times we appreciated to elegance of understandable solutions.

In Pega, I try to keep things simple as well. This means leverage the right rule type as well as standard functions. I sometimes encounter applications favoring Decision Tables over regular Data Types with a table for local storage. Even when those tables contain no decision logic at all — other than selecting a single entry by some key. Some architects might fancy the Excel import/export feature and the ability to delegate that rule type; where it could just represent a ordinary string table with key-value pairs (like we had in Visual C). That´s not quite right as it might complicate matters down the road. I.e. how would you report on those records?

My take on this boils down to the fact that working with data that needs referential integrity checked (foreign keys) is ridiculously more difficult for Decision Tables than (a set of common) related Data Tables. The primary reason is that all of the decision table´s definition is packed in that single rule of class Rule-Declare-DecisionTable whereas you would normally store different rows as separate records. Nonetheless, working on our hybrid solution I got to appreciate the introduction of a few custom but simple helper functions to divide and conquer the challenge at hand. This is why I´d like to share with you next, my recipe for create a Function rule in Pega.

What are Function rules?

A Function rule in Pega allows you to write small pieces of reusable Java code. When the challenge at hand is hard to solve with any of the other rule types consider the Function rule. Such functions can be called with inline syntax from an Activity or even a Data Transform. This keeps them readable and easier to understand at the expense of some custom code. But if you keep them small enough an native Java than you have little to no maintenance in practice. So use "low code" wisely. That´s it for the warning.

Writing a Function

Let get into an exemplary function. Let´s count the number of occurrences of a specific keyword in some text strophe. The following steps will result in this new library function:

  1. Create a Library rule if not already done so previously. It makes sense to have at least a common library on enterprise of framework level holding reusable utility functions. An maybe one for application specific functions as well. I´d like to keep names short just like a proper API would be designed.
  2. Create a new Function rule and carefully define the input parameter names and Java types since this signature cannot be change later on(!) For our CountInString function we define parameter list of type (String, String).
  3. Pega Function Parameters

  4. Next, you typically describe the purpose and usage as a reminder of what this function should accomplish. Be descriptive.
  5. More important even is to set the appropriate output parameter type. For formatting functions you would typically have String. But for our CountInString calculation we set it to return an int.
  6. Now, switch to the Java tab where your code can be written to implement this function.
  7. Reminder: The Java import lines are instead configured on the separate Imports & Exceptions tabs.
  8. As an example I have written the following:
  9. Pattern pattern = Pattern.compile(Keyword);
    Matcher matcher = pattern.matcher(Text);
    int count = 0;
    while(matcher.find()) {
           count++;
    }
    return count;
    

    Pega Function Java Code

    This piece of Java code simply relies on the Pattern and Matcher classes for regular expressions to find and count the occurrences of the given Keyword in some input Text.
  10. Next, switch to the Imports & Exceptions tab to declare the two required import lines for these Java classes.
  11. Pega Function with Import packages

  12. When you finish writing your function you should mark the checkbox that reads "Function ready to be compile?" on the Java tab. This will activate the rule for compilation and execution by PRPC; otherwise the system will just ignore your draft rule.
  13. On the History tab, describe (what it does) and document the usage details (how to use it) to assist others that might like to reuse and call your function. Note that the usage part will be displayed in the pop-up window when hovering over the function name in Pega´s Expression Builder.
  14. Now Save (and check-in) the Function rule to prepare for testing and compilation on the Pega server
  15. Back on the Java tab press the Test Function Compilation button to make sure your code is valid and compiles.
  16. Finally, hit the Generate Function button to start the actual byte-code generation.

Good luck extending PRPC! [Edgar] (2/2/2018 4:04:09 PM)

 

Twitter Feed

@edgarverburg

Bio

About Edgar

Edgar is a software engineer with experience in TIBCO Middleware and Pega Case Managemement. He holds a master's degree in Computer Science with a specialization in Data Visualization & Computer Graphics.

In his spare time Edgar reads SOS and Empire, mixes house music, blogs and writes film reviews or goes running.


Currently employed by SynTouch he is specifically looking for a PRPC project. Feel free to contact him for challenging assignments through LinkedIn.