UNPKG

1.25 kBJavaScriptView Raw
1var RuleEngine = require('../index');
2/* Here we can see a rule which upon matching its condition,
3does some processing and passes it to other rules for processing */
4var rules = [{
5 "condition": function(R) {
6 R.when(this.application === "MOB");
7 },
8 "consequence": function(R) {
9 this.isMobile = true;
10 R.next();//we just set a value on to fact, now lests process rest of rules
11 }
12}, {
13 "condition": function(R) {
14 R.when(this.cardType === "Debit");
15 },
16 "consequence": function(R) {
17 this.result = false;
18 this.reason = "The transaction was blocked as debit cards are not allowed";
19 R.stop();
20 }
21}];
22/* Creating Rule Engine instance and registering rule */
23var R = new RuleEngine();
24R.register(rules);
25/* Fact with more than 500 as transaction but a Debit card, and this should be blocked */
26var fact = {
27 "name": "user4",
28 "application": "MOB",
29 "transactionTotal": 600,
30 "cardType": "Credit"
31};
32R.execute(fact, function(data) {
33
34 if (data.result) {
35 console.log("Valid transaction");
36 } else {
37 console.log("Blocked Reason:" + data.reason);
38 }
39
40 if(data.isMobile) {
41 console.log("It was from a mobile device too!!");
42 }
43
44});
\No newline at end of file