You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+25-22Lines changed: 25 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -36,11 +36,11 @@ A rule will consist of a condition and its corresponding consequence. You can fi
36
36
37
37
```js
38
38
{
39
-
"condition":function(R) {
40
-
R.when(this.transactionTotal<500);
39
+
"condition":(R, fact) => {
40
+
R.when(fact.transactionTotal<500);
41
41
},
42
-
"consequence":function(R) {
43
-
this.result=false;
42
+
"consequence":(R, fact) => {
43
+
fact.result=false;
44
44
R.stop();
45
45
}
46
46
}
@@ -56,32 +56,33 @@ Facts are those input json values on which the rule engine applies its rule to o
56
56
57
57
A sample Fact may look like
58
58
59
-
{
60
-
"name":"user4",
61
-
"application":"MOB2",
62
-
"transactionTotal":400,
63
-
"cardType":"Credit Card",
64
-
}
59
+
```json
60
+
{
61
+
"name":"user4",
62
+
"application":"MOB2",
63
+
"transactionTotal":400,
64
+
"cardType":"Credit Card",
65
+
}
66
+
````
65
67
66
68
###### 3. Using the Rule Engine
67
69
68
70
The example below shows how to use the rule engine to apply a sample rule on a specific fact. Rules can be fed into the rule engine as Array of rules or as an individual rule object.
69
71
70
72
``` js
71
-
var RuleEngine =require("node-rules");
73
+
import RuleEngine from "node-rules";
72
74
73
75
/* Creating Rule Engine instance */
74
-
varR=newRuleEngine();
76
+
const R = new RuleEngine();
75
77
76
78
/* Add a rule */
77
-
var rule = {
78
-
"condition":function(R) {
79
-
console.log(this);
80
-
R.when(this.transactionTotal<500);
79
+
const rule = {
80
+
"condition": (R, fact) => {
81
+
R.when(fact.transactionTotal < 500);
81
82
},
82
-
"consequence":function(R) {
83
-
this.result=false;
84
-
this.reason="The transaction was blocked as it was less than 500";
83
+
"consequence": (R, fact) => {
84
+
fact.result = false;
85
+
fact.reason = "The transaction was blocked as it was less than 500";
85
86
R.stop();
86
87
}
87
88
};
@@ -90,20 +91,22 @@ var rule = {
90
91
R.register(rule);
91
92
92
93
/* Add a Fact with less than 500 as transaction, and this should be blocked */
Copy file name to clipboardExpand all lines: docs/Dynamic-Control.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,24 +30,24 @@ The above `prioritize` call will give priority to Rule with id "one" over all th
30
30
##### 3. `RuleEngine.register(<rules>)`
31
31
We know that we can pass Rules as parameter into the Rule Engine constructor while we create the Rule Engine object like below.
32
32
33
-
var RuleEngine = new RuleEngine(rules);
33
+
const RuleEngine = new RuleEngine(rules);
34
34
35
35
Where `rules` can be either an array of rule objects or a single array. But what if we need to add some rules later to the Rule Engine. Register can be used any time to append new rules into the Rule Engine. It can be used like.
36
36
37
-
var RuleEngine = new RuleEngine();
37
+
const RuleEngine = new RuleEngine();
38
38
RuleEngine.register(newrule);
39
39
RuleEngine.register(newrule);
40
40
41
41
42
42
##### 4. `RuleEngine.findRules(<filter>)`
43
43
This function is used to retrieve the Rules which are registered on the Rule engine which matches the filter we pass as its parameter. A sample usage can be like below.
This function is used to remove all the rules registered on the Rule Engine. This is mostly used for rule clean up purposes by internal functions. A sample usage can be like below.
49
49
50
-
var RuleEngine = new RuleEngine();
50
+
const RuleEngine = new RuleEngine();
51
51
RuleEngine.register(badrule);
52
52
RuleEngine.init();//removes the bad rule and cleans up
Copy file name to clipboardExpand all lines: docs/Rules.md
+10-9Lines changed: 10 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,21 +6,21 @@ Lets see how a sample rule will look like and then proceed to explain the differ
6
6
"name": "transaction minimum",
7
7
"priority": 3,
8
8
"on" : true,
9
-
"condition": function(R) {
10
-
R.when(this.transactionTotal < 500);
9
+
"condition": (R, fact) => {
10
+
R.when(fact.transactionTotal < 500);
11
11
},
12
-
"consequence": function(R) {
13
-
this.result = false;
12
+
"consequence": (R, fact) => {
13
+
fact.result = false;
14
14
R.stop();
15
15
}
16
16
}
17
17
18
18
Above is a sample rule which has mandatory as well as optional parameters. You can choose to use which all attributes you need to use while defining your rule. Now lets look into the attributes one by one.
19
19
20
20
###### 1. condition
21
-
Condition is a function where the user can do the checks on the fact provided. The fact variable will be available in `this` context of the condition function. Lets see a sample condition below.
21
+
Condition is a function where the user can do the checks on the fact provided. The fact variable will be available in `this` context of the condition function or as second function argument incase you are using arrow functions. Lets see a sample condition below.
22
22
23
-
"condition": function(R) {
23
+
"condition": (R, fact) => {
24
24
R.when(this.transactionTotal < 500);
25
25
}
26
26
@@ -29,12 +29,13 @@ As you can see, the we have to pass an expression on to the `R.when` function wh
29
29
Its mandatory to have this field.
30
30
31
31
###### 2. consequence
32
-
The consequence is the part where we define what happens when the condition evaluates to true for a particular fact. Just like in condition, fact variable will be available in `this` context. You may utilize it to add extra result attributes if needed.
32
+
The consequence is the part where we define what happens when the condition evaluates to true for a particular fact. Just like in condition, fact variable will be available in `this` context or as second function argument incase you are using arrow functions. You may utilize it to add extra result attributes if needed.
33
33
34
-
"consequence": function(R) {
35
-
this.result = false;
34
+
"consequence": (R, fact) {
35
+
fact.result = false;
36
36
R.stop();
37
37
}
38
+
38
39
In the above example we use an additional parameter `result` to communicate to the code outside the rule engine that the fact has succeeded. Also the Rule API provides a number of functions here to control the flow of the rule engine. They are `R.stop()`, `R.restart()` and `R.next()`. Stop refers to stop processing the rule engine. Restart tells the rule engine to start applying all the rules again to the fact. Next is to instruct the rule engine to continue applying the rest of the rules to the fact before stopping. Check [Flow Control API](https://github.com/mithunsatheesh/node-rules/wiki/Flow-Control-API) in wiki to read more about this.
0 commit comments