JavaScript InProc

From FlexRule Wiki
(Redirected from Run in JavaScript)
Jump to: navigation, search

FlexRule allows publishing your rules on the JavaScript platform. That means you can run your rules in offline mode, on mobile devices, and so on.

Business Logic

The type of business logic (i.e., business rules, application logic, etc.) that are supported on the JavaScript platform are:

  1. Decision Table
  2. Natural Language

This means when you need to support the offline execution of logic on disconnected devices, you need to choose either Decision table or Natural language.


When you model the logic i.e., Decision table, Natural language, to run these on the JavaScript platform you need to build the JS version of your model. You can do this in two ways using:

Package Builder

Package builder allows you to target the JavaScript Platform


When you do that you will need to fill an Application Namespace.


This is just a simple name that allows you to categories your logic in the web app (e.g., SampleUI.BusinessLogic.LoanRating, SampleUI.BusinessLogic.Approval, etc.).

In the package view, you can add multiple models:


Go back to the builder tab and press Publish

Info2.pngSelect Copy to a Target Folder option for your Method in the Packaging Information

The JS files of your model will be copied to the target folder along with the FlexRule.Runtime.min.js. You will learn to reference these in your web application.

JavaScript Compiler

You can automate the process of building the JS file for your model, or you can make it dynamic by using the Compiler API of FlexRule Runtime. There are two steps to do so:

  1. Loading your model
  2. Calling the compiler to create the JS of your model

Below is sample code to create the JS for a model

// Step 1: Loading the model
var baseFolder=...; //the base folder that has the models and related dependent files (i.e., Business glossaries).
var binaryModel = File.ReadAllBytes(Path.Combine(baseFolder, "your_model_file_name.xml");
var model = LoadAdapterUtility.LoadModel(binaryModel);
// Step 2: Calling the compiler
FlexRule.JavaScript.Compiler compiler = new FlexRule.JavaScript.Compiler("CompanyName.WebUI.MyApp");
var js = compiler.Run(model, baseFolder);



Decision Table

Natural Language

  • for is not supported


Registering Logic

FlexRule.ApplicationsLogic provides a global context for applications to register their logic using namespace and logicName. It's a registry for applications logic. When logic (i.e. Decision table, Natural language) are deployed as JavaScript, they are automatically registered using below function.


Register a logic in the application registry.

FlexRule.ApplicationsLogic.register(namespace, logicName, fnLogic)


Check if a logic already is registered.

FlexRule.ApplicationsLogic.exists(namespace, logicName)

Creating Runtime Engine

FlexRule.RuntimeEngine provides a function to create an engine from registered applications logic. When an engine is create by calling fromJavaScript then the engine instance allows executing the logic by passing values and parameters information.


Create an instance of the runtime engine.

var rule = FlexRule.RuntimeEngine.fromJavaScript(appNamespace, logicName)

RuntimeEngine Members

When an engine is create from FlexRule.RuntimeEngine.fromJavaScript then the engine instance allows executing the logic by passing values and parameters information.


Runs the instance of the engine with the input parameters.

var result =, input2, input3,...)


Runs the instance of the engine that allows selecting scope.

var result = rule.runWithParameters(runParameter, input1, input2, input3,...)
  • runParameter: A JOSN object that has 'name' attribute to define the group/scope name
  • input1, input2...: Values to the input parameters


   var fact1=...; // define fact1 as input
   var fact2=...; // define fact2 as input
   var engine = FlexRule.RuntimeEngine.fromJavaScript("CompanyName.WebUI.MyApp", "calling when with param");
   var res = engine.runWithParameters({name:'scope 1'}, fact1, fact2);


Registering custom functions that allows logic call to the functions.

rule.registerFunction(name, fn)


This property holds the rule descriptions



  • logicName: name of the logic document
  • appNamespace: area of the application that this logic is related to
  • logics: list of all rules inside the logic
  • parameters: parameters (in and out) of the logic document

For example, to retrieve all the available rules in a logic document you can use the code below:




You need to reference the files below in your web application:

  • FlexRule.Runtime.min.js: this is the core component of the JS engine that enables the execution of models
  • the JS files of your models

for example:

  1. <!--FlexRule JavaScript BRE Reference-->
  2. <script src="js/FlexRule.Runtime.min.js"></script>
  4. <!--List of business rules logic-->
  5. <script src="js/Rules/States.xml.js"></script>
  6. <script src="js/Rules/Validation.xml.js"></script>

Creating Runtime Engine

Once you have the references in your application, you can create an instance of the runtime for your logic using FlexRule.RuntimeEngine.fromJavaScript.


  1. var statesRules = FlexRule.RuntimeEngine.fromJavaScript("SampleUI.BusinessLogic", "States");

Executing Logic


When you want to run the logic, you need to call either:

  • run
  • runWithParameters

on the runtime instance of your logic that is created by fromJavaScript.


  1. var result ='Australia');

In the above example, the statesRules accepts one parameter as the country name.

Retrieve Results

When you execute the run method, it returns the RuntimeResult.

RuntimeResult {
    context {
           notifications{ ... },
           variableContainer{ ... }

The RuntimeResult behaviour is similar to the .Net instance of the API, but with the members described in the above structure.

To retrieve the results you can use the variableContainer which has all the output parameters.


  1. // get the output parameter of the rule which is the list of states for the country
  2. var states = result.context.variableContainer.states;

In the above example, member states on variableContainer is the output of the actual model.


Notification is supported in the JS runtime. Using context.notifications on result of run method give application access to notifications.

result.context.notifications: where result is return object of run:

  1. getNotification(groupName): returns the notification for a group
  2. getAllGroups(): returns all the groups in the notification
  3. default: returns the default notification group
  1.  // executing the rules using input parameter
  2.  var res =;
  4.  // listing all the notification groups
  5.  var allGroups = res.context.notifications.getAllGroups();
  7.  // accessing notification with a groupName
  8.  var notifications = res.context.notifications.getNotification("g2");
  10.  // todo: do what is needed with notifications

Decision Table

Use the Action column and set the Type to Notice.


In the above example, you can set the Notice Type or leave it blank for Error notices as the default behaviour.

Natural Language

Use $context to write notifications in Natural Language.


In the above example, logic Write Error {msg} is used to write a notification error during execution.


Custom Functions

In the JavaScript engine, you can introduce custom functions and use them as part of your logic.

Using Function Declaration

When custom functions are required, to ensure the function can be used in both .Net and JavaScript platform Using command with static method signature to register a function must be used.

For example, in below NL IsParticipant is a custom function:

  1. <Natural>
  2.  <Declaration>
  3.      <Using name="IsParticipant" function="true" path="NS.MyType.IsParticipant(object, object)" assembly="CustomAssembly.dll" />
  4.  </Declaration>
  5.  <Dsl>
  6.      input user, agreement
  7.      @name calling custom func
  8.       when R1
  9.          IsParticipant(user, agreement)
  10.      end
  11.  </Dsl>
  12. </Natural>

Register Custom Function

Now in your JavaScript register the function:

  1. // get an instance of a runtime engine for a rule/logic
  2. var rule1 = FlexRule.RuntimeEngine.fromJavaScript("CompanyName.WebUI.MyApp", "calling custom func");
  4. // register a custom function (i.e., IsParticipant)
  5. rule1.registerFunction("IsParticipant",
  6.         function(u, a) {
  7.    = 'aaa';
  8.    = 'bbb';
  9.             return true;
  10.         }
  11. );
  13. // Running the rule by passing the input parameters
  14. var user = {};
  15. var agreement = {};
  16. var res =, agreement);
  18. assert.equal(, 'aaa', "");
  19. assert.equal(, 'bbb', "");


When you install FlexRule Runtime have a look at the path below:

FlexRule\Samples\Client JavaScript Rules

In this folder you can find:

  • Rule project that has the models
    • Decision Table: rules for filtering
    • Natural Language: rules for validating a registration form
  • Angular JS web application

In the JavaScript Angular JS app, two behaviours are implemented:

  1. Filtering: The rule file is called 'States.xml'. It filters the list of states based on a selected country on the form.
  2. Form Validation: The rule file is called 'Validation.xml' It validates the missing and incorrect information on the web page on submission.