Integration Brief: Client-Side Rule Execution

Use Case
You may be considering the use of InRule® in a web or hybrid mobile application to manage the complex logic around:

  • Validations
  • Notifications and Messages
  • Calculations
  • Control Visibility and Formatting

A common architectural approach is to run all the logic on the server-side when the form is submitted by the user. In some cases, though, it would be ideal to run some or all of the logic directly in the browser or on the device to improve the user experience. For example, in an online loan application it would be better for the user to see that their input is invalid when they tab out of a field rather than find out 20 fields are invalid when they click the next button.

This integration brief covers the basics of running rules using the JavaScript files generated by irDistributionTM, the InRule for JavaScript distribution service.

Basic Example of Calling the JavaScript Rule Engine
When rules are packaged for distribution by irDistribution, the output is a single JavaScript file that contains the rules, rule engine and irSDK® for interacting with the engine.

In this sample application we have rules that calculate the monthly payment and total loan cost based on three input values as shown in the following form:

Now, let’s take a look at the code behind the form.

First, to load the rules, you reference the output like any other JavaScript file via a script tag:

The next step is to create the function that will pass data into the rule engine, execute the rules and map the results back to the output fields on the form. Note in the code below that a simple JavaScript object is passed in and out of the engine. The structure of the object corresponds to the entity model in the rule application. At runtime, the engine operates directly against the passed in JavaScript object, eliminating the need to write code to get the output back out of the engine.

 

 

Using Rule Engine Output
Now that you have a sense of how to call the rule engine and modify your own JavaScript object, let’s look at how you work with the three most commonly used types of rule engine output: the rule execution log, validations and notifications. Further down in the web form you can see we are writing this output to the screen:

When you run the applyRules() function you can save the results off to a variable which is called log in the following code sample.

 

 

 

You can follow a similar pattern for notifications as shown here:

 

Validations work the same way as notifications and are available through session.getActiveValidations().

Working with JavaScript Frameworks

The previous examples, although easy to understand, ignore the fact that most rule applications contain hierarchical data and most modern web applications make use of a JavaScript framework such as Angular or Backbone for data binding. Since InRule works with native JavaScript objects, both of these scenarios are easily supported.

Following is another screen shot of the mortgage calculator form which now displays a payment schedule made up of a collection of 360 members (30 years X 12 months).

 

In the following code Angular has been used to map all the data into a JavaScript object named mortgage. Notice how in both the original applyRules() and the new calculatePaymentSchedule() functions we simply pass the Angular object into the engine and run rules as before. Angular then takes care of mapping the modified object back to the UI.

 

 

For More Information
The samples described in this integration brief are available at: http://bit.ly/InRule-BrowserBasedExecutionSamples.

For additional information please visit inrule.com, contact us or reach out to your InRule Account Executive to get in touch with a pre-sales engineer.

Download PDF of Product Brief