In an enterprise setup, business logic (i.d. business rules, decisions, flows…) should be extracted out of applications, services and processes and be host across multiple channels by use of a Business Rule Server. This platform enables you to share business rules and decisions for execution across any type of applications and any processes as REST API service. A very common scenario is when decisions are shared across many other applications and systems. So you possible do not want to re-write but want to reuse that decision within you mobile, JavaScript or any type of web applications. Therefore after hosting the decision as a service, a REST API interface can provide you a simple interface to execute the decisions.
However, if you use the JavaScript client library, it is even much more easier!

In this post we are going to call this decision which is host as a service.

Setup

The client library, requires jQuery, so make sure you have it references, in your HTML, and let’s put together a very simple html page that grabs ‘car’ information such as bellow:






html-form

In the button we simply are going to call a javascript function named callFlexRule.


Client Library

When the ‘javascript decision as a service’ client library is loaded, there will be an global object call FlexRuleJS which you can:

  • Step 1: Configure the master server address
  • Step 2: Acquire an access token
  • Step 3: Execute the decision
function callFlexRule() {
    // step 1: Set the execution server address (Master Server)
    FlexRuleJS.executionClient.configuration.masterServerAddress = "http://localhost:9000";

    // Loads the input data from view
    var inputs = getInputs();

    // step 2: grab an authentication token
    var token = FlexRuleJS.executionClient.acquireToken({
        client_id: "app1",
        client_secret: "123123",
    });

    // step 3: run the decision
    FlexRuleJS.executionClient.run({
            // authentication token, if authorization is disabled, this line can be deleted
            token: token,
            // A correlation identifier for the request, so you know what requests was for what reason
            // This can be deleted also, if you don't want to track requests.
            correlationId: 'abcd123',

            // values for input parameters of decision
            inputs: inputs,

            // Address of decision for execution
            service: 'Calculator',
            packageVersion: '1',
            packageName: 'Car Insurance',

           // strategy to select a service within a package
           useLatestVersion = true,

            // handlers when execution is finished
            success: function (response) {
                // retrieve the result form output parameters.
                // The parameter is named 'car' in this example.
                var car = response.getValue('car');
                updateView(car);
            },
            error: function(response) {
                alert(response.statusText);
            }
        }
    );
}

Download

This sample web application and the client library that defines FlexRuleJS are available as part of FlexRule Server installer.

Read More

  1. Synchronize Decision Service
  2. .NET Decision as a Service – REST API Client for .Net
  3. SOA for Business Rules and Logic
  4. Manage Online Business Rules
  5. Decision as a Service
  6. JavaScript Decision as a Service – REST API Client for HTML/JS