Mastering Custom Corticon.js Operators: Flexibility Beyond Standard Implementations

Mastering Custom Corticon.js Operators: Flexibility Beyond Standard Implementations

Posted on December 17, 2024 0 Comments
Decorative image

When you are creating business rules, you sometimes need to perform operations that are not built natively into the Progress Corticon decision automation platform. For example, you may need a complex mathematical formula. Custom operators provide a mechanism to extend the Corticon platform with your own functions. These custom operators greatly simplify rule-modeling activities as modelers do not need to build complex formulas, which makes the development and maintenance of business rules easier.

Extensibility through custom operators is a key benefit of the Corticon platform. In this blog post, we explore how to create robust, flexible Corticon.js custom operators that go beyond standard type and parameter constraints.

We’ll use the Present Value and Future Value example operators from the samples in GitHub.

These operators calculate the values over time using parameters like interest rates and number of years (duration)—both of which can be represented in many ways. For example, the duration could be just 4 as in 4 years, or it could be 1.5 to represent one and a half years. It could also be a date like 2028-12-01, in which case the duration is the difference between the date and today’s date (assuming today is 2024-12-01, then the duration is 4 years).

Read on to learn how to support such flexible operators in your projects.

Why Custom Operators?

Custom operators allow you to:

  • Extend the built-in functionality of the Corticon platform
  • Implement complex logic not available in standard operators
  • Reuse specialized logic across multiple rulesheets and projects
  • Integrate custom calculations and transformations seamlessly into your decision services

Key Characteristics of Powerful Custom Operators

Flexible Parameter Handling

A custom operator that can handle diverse input types can make life easier for the modeler using the operator.

Consider the following powerful design principles.

Multiple Input Types

A single parameter can accept various data types. In our financial example, the number of years parameter can be:

  • A Decimal number (1.5 years)
  • An Integer (5 years)
  • A String (‘5.5’ years)
  • A Date or Datetime

Check the function checkForValidTypes in GitHub for an illustration of parameters validation with all the supported Corticon types.

Intelligent Default Behavior

Robust operators anticipate and handle various input scenarios:

  • Missing parameters
  • Null values

For example, in the function presentValue, we default the duration to one year if no time period is specified:

... let numberOfYears = params[2]; 

    if (numberOfYears === undefined || numberOfYears === null) 

        numberOfYears = 1; 

Dynamic Type Conversion

Operators should gracefully handle different input representations. For example, a modeler could pass the number of years parameter as a String representing numbers. Fortunately, for the interest rate parameter in our case, we do not need to write conversion code. Corticon.js decimal implementation accepts both JavaScript numbers as well as strings as input parameters so there is no need to explicitly convert a string to a number in the first place.

Comprehensive Error Checking

Implement robust type validation:

  • Prevent unexpected behavior
  • Provide clear error messages
  • Stop execution immediately if inputs are invalid and you cannot have a proper default value

Practical Considerations

When designing custom operators:

  • Don't limit yourself to fixed parameter types
  • Support multiple input representations
  • Implement intelligent type conversion
  • Provide meaningful defaults

Financial Calculation Operator Example

Our financial custom operator:

  • Supports multiple duration representations (Decimal, Date, Datetime) for the number of years parameter
  • Handles missing parameters gracefully by using proper default values
  • Implements comprehensive type checking
  • Provides clear error messaging

Using Custom Operators in Corticon Modeling

One of the most powerful aspects of custom operators is their seamless integration into the Corticon rule modeling environment. Here's how you'll typically use the financial custom operators:

// In your Corticon rule sheet Finance.presentValue = getDecimal('PresentValue', Finance.inputValue1, Finance.interestRate, Finance.years)

Finance.futureValue = getDecimal('FutureValue', Finance.inputValue2, Finance.interestRate, Finance.years)

Breakdown of Operator Usage

  1. getDecimal(): A Corticon function that invokes custom operators and returns a value of type Decimal
  2. First Argument: The name of the custom operator, either 'PresentValue' or 'FutureValue'
  3. Subsequent Arguments: The amount to compute future or present value from:
    • Interest rate
    • The number of years (a flexible duration representation)

Flexibility in Action

The custom operators shine in their ability to handle diverse inputs. To compute the present value of 10,000 at 5.5% over 3 years: Finance.presentValue = getDecimal('PresentValue', 10000, 5.5, 3)

To compute the present value of 10,000 at 5.5% over 3.5 years where the duration is a String: Finance.presentValue = getDecimal('PresentValue', 10000, 5.5, '3.5')

To compute the present value of 10,000 at 6.5% over the duration represented by the difference between a date and today: Finance.presentValue = getDecimal('PresentValue', 10000, 6.5, someDate)

Key Benefits Demonstrated

  1. Type Flexibility: Supports multiple input types
  2. Intelligent Defaults: Handles missing or partial information
  3. Robust Error Handling: Prevents invalid calculations
  4. Seamless Integration: Easily used in Corticon rule modeling

Conclusion

Custom Corticon operators bridge the gap between complex business logic and intuitive rule modeling. By creating flexible, intelligent operators, you empower business analysts to build more sophisticated decision services with less complexity.

To sign up for a trial or demo of the Corticon platform, visit our Getting Started page.

Get Started

Thierry Ciot

Thierry Ciot

Thierry Ciot is a Software Architect on the Corticon Business Rule Management System. Ciot has gained broad experience in the development of products ranging from development tools to production monitoring systems. He is now focusing on bringing Business Rule Management to Javascript and in particular to the serverless world where Corticon will shine. He holds two patents in the memory management space.

Comments

Comments are disabled in preview mode.
Topics

Sitefinity Training and Certification Now Available.

Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.

Learn More
Latest Stories
in Your Inbox

Subscribe to get all the news, info and tutorials you need to build better business apps and sites

Loading animation