Skip to content

Dynamic Control

Mithun Satheesh edited this page Jan 17, 2015 · 6 revisions

Dynamic Control refers to functions for altering the rules at run time. These are functions that can be used to turn ON or OFF a rule while a Rule is running inside the Rule Engine. Also there is functions to alter the priority of a function at run time.

Below are the various functions available for Dynamic control of rules

Turn

This function is used to dynamically turn ON or OFF a rule. For this to work its essential that you should have a identifier in the rule object we use. It can be any attribute which can be used to uniquely identify the targeted rule or set of rules from others.

Look at the sample code below

var rules = [{
    "condition": function(R) {
        R.when(1);
    },
    "consequence": function(R) {
        R.stop();
    },
    "id": "one",
    "on":true
}, {
    "condition": function(R) {
        R.when(0);
    },
    "consequence": function(R) {
        R.stop();
    },
    "id": "two",
    "on": false
}];
var R = new RuleEngine(rules);
R.turn("OFF", {
   "id": "one"
});

Here the rule with id as "one" was in true state when we passed it to the Rule engine instance. R.turn call above will set its state to false.

Prioritize

This function is used to dynamically change the priority of a rule while rule engine is running. It works similar to the Turn function and just that instead of the ON/OFF state we will be passing a priority number value to the function

See example below

var rules = [{
    "condition": function(R) {
        R.when(1);
    },
    "consequence": function(R) {
        R.stop();
    },
    "id": "one",
    "priority":4
}, {
    "condition": function(R) {
        R.when(0);
    },
    "consequence": function(R) {
        R.stop();
    },
    "id": "two",
    "priority":6
}];
var R = new RuleEngine(rules);
R.prioritize(10, {
   "id": "one"
});

The above prioritize call will give more priority to Rule with id "one" as the other rule will be of priority 6 which is less than 10 once the prioritize call is executed.

Clone this wiki locally