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.

function runRules() {
// make new JS object to pass to InRule
// you could also assign fields one by one
var mortgage = {};
mortgage.LoanInfo = {};
mortgage.LoanInfo.Principal = document.getElementById('loanAmount').value;
mortgage.LoanInfo.APR = document.getElementById('apr').value;
mortgage.LoanInfo.TermInYears = document.getElementById('termInYears').value;

var session = inrule.createRuleSession();
var entity = session.createEntity('Mortgage', mortgage);

document.getElementById('montlyPayment').value = mortgage.PaymentSummary.MonthlyPayment.toFixed(2);
document.getElementById('totalCost').value = mortgage.PaymentSummary.TotalCost.toFixed(2);



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.

// run rules, entity is not used here, but is available if needed
var session = inrule.createRuleSession();
var entity = session.createEntity('Mortgage', mortgage);
var log = session.applyRules();

// write exec log, notifications and validations to form
document.getElementById('log').value = processLogFile(log);
document.getElementById('notifications').value = processNotifications(session);
document.getElementById('validations').value = processValidations(session);

You can then loop through the values in the log to build up the output:

function processLogFile(log) {
// write all log entries to a string with line breaks
var text = '';
for (var i = 0; i < log.messages.length; i++) {
text += log.messages[i];
text += '\n';
return text;




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

function processNotifications(session) {
// write all notifications to a string with line breaks
var text = '';
var notifications = session.getActiveNotifications();
for (var i = 0; i < notifications.length; i++) {
text += notifications[i].message;
text += '\n';
return text;


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.


var app = angular.module('mortgage', []);

app.controller('mortgageController', ['$scope', '$timeout', function ($scope) {

$scope.applyRules = function () {
var session = inrule.createRuleSession();
session.createEntity('Mortgage', $;

$scope.calculatePaymentSchedule = function () {
var session = inrule.createRuleSession();
var mortgageEntity = session.createEntity('Mortgage', $;
session.applyRules(); // doing this because the engine doesn't support auto-sequential at this time


For More Information
The samples described in this integration brief are available at:

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