Skip to content

Commit ceba0bf

Browse files
Merge pull request #133 from mithunsatheesh/9.0.0
9.1.1
2 parents 37e6d7f + 797fccb commit ceba0bf

8 files changed

Lines changed: 121 additions & 99 deletions

File tree

examples/node.js/1.SimpleRule.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
const { RuleEngine } = require("../../dist/index.js");
1+
const { RuleEngine } = require("node-rules");
22

33
/* Sample Rule to block a transaction if its below 500 */
44
var rule = {
5-
condition: function (R) {
6-
R.when(this.transactionTotal < 500);
5+
condition: function (R, fact) {
6+
R.when(fact.transactionTotal < 500);
77
},
8-
consequence: function (R) {
9-
this.result = false;
10-
this.reason = "The transaction was blocked as it was less than 500";
8+
consequence: function (R, fact) {
9+
fact.result = false;
10+
fact.reason = `The transaction was blocked as the transaction total of ${fact.transactionTotal} was less than threshold 500`;
1111
R.stop();
1212
},
1313
};
14+
1415
/* Creating Rule Engine instance and registering rule */
1516
var R = new RuleEngine();
1617
R.register(rule);
@@ -21,8 +22,9 @@ var fact = {
2122
transactionTotal: 400,
2223
cardType: "Credit Card",
2324
};
25+
2426
R.execute(fact, function (data) {
25-
if (data.result) {
27+
if (data.result !== false) {
2628
console.log("Valid transaction");
2729
} else {
2830
console.log("Blocked Reason:" + data.reason);

examples/node.js/2.MultipleRules.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { RuleEngine } = require("../../dist/index.js");
1+
const { RuleEngine } = require("node-rules");
22

33
/* Set of Rules to be applied
44
First blocks a transaction if less than 500
@@ -8,22 +8,22 @@ Rules will be applied as per their index in the array.
88
If you need to enforce priority manually, then see examples with prioritized rules */
99
var rules = [
1010
{
11-
condition: function (R) {
12-
R.when(this.transactionTotal < 500);
11+
condition: function (R, fact) {
12+
R.when(fact.transactionTotal < 500);
1313
},
14-
consequence: function (R) {
15-
this.result = false;
16-
this.reason = "The transaction was blocked as it was less than 500";
14+
consequence: function (R, fact) {
15+
fact.result = false;
16+
fact.reason = "The transaction was blocked as it was less than 500";
1717
R.stop(); //stop if matched. no need to process next rule.
1818
},
1919
},
2020
{
21-
condition: function (R) {
22-
R.when(this.cardType === "Debit");
21+
condition: function (R, fact) {
22+
R.when(fact.cardType === "Debit");
2323
},
24-
consequence: function (R) {
25-
this.result = false;
26-
this.reason =
24+
consequence: function (R, fact) {
25+
fact.result = false;
26+
fact.reason =
2727
"The transaction was blocked as debit cards are not allowed";
2828
R.stop();
2929
},
@@ -40,7 +40,7 @@ var fact = {
4040
cardType: "Debit",
4141
};
4242
R.execute(fact, function (data) {
43-
if (data.result) {
43+
if (data.result !== false) {
4444
console.log("Valid transaction");
4545
} else {
4646
console.log("Blocked Reason:" + data.reason);

examples/node.js/3.CascadingRules.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
const { RuleEngine } = require("../../dist/index.js");
1+
const { RuleEngine } = require("node-rules");
22

33
/* Here we can see a rule which upon matching its condition,
44
does some processing and passes it to other rules for processing */
55
var rules = [
66
{
7-
condition: function (R) {
8-
R.when(this.application === "MOB");
7+
condition: function (R, fact) {
8+
R.when(fact.application === "MOB");
99
},
10-
consequence: function (R) {
11-
this.isMobile = true;
10+
consequence: function (R, fact) {
11+
fact.isMobile = true;
1212
R.next(); //we just set a value on to fact, now lests process rest of rules
1313
},
1414
},
1515
{
16-
condition: function (R) {
17-
R.when(this.cardType === "Debit");
16+
condition: function (R, fact) {
17+
R.when(fact.cardType === "Debit");
1818
},
19-
consequence: function (R) {
20-
this.result = false;
21-
this.reason =
19+
consequence: function (R, fact) {
20+
fact.result = false;
21+
fact.reason =
2222
"The transaction was blocked as debit cards are not allowed";
2323
R.stop();
2424
},
@@ -27,15 +27,16 @@ var rules = [
2727
/* Creating Rule Engine instance and registering rule */
2828
var R = new RuleEngine();
2929
R.register(rules);
30-
/* Fact with more than 500 as transaction but a Debit card, and this should be blocked */
30+
31+
/* Fact is mobile with Credit card type. This should go through */
3132
var fact = {
3233
name: "user4",
3334
application: "MOB",
3435
transactionTotal: 600,
3536
cardType: "Credit",
3637
};
3738
R.execute(fact, function (data) {
38-
if (data.result) {
39+
if (data.result !== false) {
3940
console.log("Valid transaction");
4041
} else {
4142
console.log("Blocked Reason:" + data.reason);

examples/node.js/4.PrioritizedRules.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
const { RuleEngine } = require("../../dist/index.js");
1+
const { RuleEngine } = require("node-rules");
22

33
/* Set of Rules to be applied */
44
var rules = [
55
{
66
priority: 4,
7-
condition: function (R) {
8-
R.when(this.transactionTotal < 500);
7+
condition: function (R, fact) {
8+
R.when(fact.transactionTotal < 500);
99
},
10-
consequence: function (R) {
11-
this.result = false;
12-
this.reason = "The transaction was blocked as it was less than 500";
10+
consequence: function (R, fact) {
11+
fact.result = false;
12+
fact.reason = "The transaction was blocked as it was less than 500";
1313
R.stop();
1414
},
1515
},
1616
{
1717
priority: 10, // this will apply first
18-
condition: function (R) {
19-
R.when(this.cardType === "Debit");
18+
condition: function (R, fact) {
19+
R.when(fact.cardType === "Debit");
2020
},
21-
consequence: function (R) {
22-
this.result = false;
23-
this.reason =
21+
consequence: function (R, fact) {
22+
fact.result = false;
23+
fact.reason =
2424
"The transaction was blocked as debit cards are not allowed";
2525
R.stop();
2626
},
@@ -38,7 +38,7 @@ var fact = {
3838
};
3939
/* This fact will be blocked by the Debit card rule as its of more priority */
4040
R.execute(fact, function (data) {
41-
if (data.result) {
41+
if (data.result !== false) {
4242
console.log("Valid transaction");
4343
} else {
4444
console.log("Blocked Reason:" + data.reason);

examples/node.js/5.RecurssionWithRules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const { RuleEngine } = require("../../dist/index.js");
1+
const { RuleEngine } = require("node-rules");
22

33
/* Sample Rule to block a transaction if its below 500 */
4+
/* Also validates if the legacy syntax of operating on this is supported in Rule engine */
45
var rule = {
56
condition: function (R) {
67
R.when(this.someval < 10);

0 commit comments

Comments
 (0)