JavaScript InProc

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

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

Business Logic

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

  1. Decision Table
  2. Natural Language

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

Publish

When you modelled the logic i.e. Decision table, Natural language, then to run them in 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 JavaScript Platform

Package-builder-js.png

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

Package-builder.png

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 if you like to:

Package-builder-view-multi-js.png

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 which you will lean to reference them to 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. To doing that there are two steps:

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

Below is the 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 dependant 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);

Support

Expressions

Decision Table

Natural Language

  • for is not supported

API

Creating Runtime Engine

fromJavaScript

Create an instance of runtime engine.

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

RuntimeEngine Members

run

Runs the instance of engine with the input parameters.

var result = rule.run(input1, input2, input3,...)

runWithParameters

Runs the instance of engine that allows selecting scope.

var result = rule.runWithParameters(runParameter, input1, input2, input3,...)

registerFunction

Registering custom functions that allows logic call to the functions.

rule.registerFunction(name, fn)

descriptions

This property hols the rule descriptions

rule.descriptions

Members:

  • 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 below code:

rule.descriptions.logic

Integration

References

You need to reference the blow files to your web application:

  • FlexRule.Runtime.min.js: this is the core component of 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>
  3.  
  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.

Example:

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

Executing Logic

Run

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.

Example:

  1. var result = statesRules.run('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 the .Net instance of the API but with the member descried in the above structure.

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

Example:

  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

Notification is supported in the JS runtime.

Decision Table

Use the Action column and set the Type to Notice.

Duplicate-notice-action-DT.png

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.

Nl-js-notification.png

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

Extensibility

Custom Functions

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

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

Sample

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

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 in the web page on submit.