Skip to content

Commit eefafb0

Browse files
authored
Merge pull request #127 from mithunsatheesh/8.0.0
8.0.0
2 parents 96d7c91 + e653b28 commit eefafb0

19 files changed

+1914
-2161
lines changed

README.md

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ A rule will consist of a condition and its corresponding consequence. You can fi
3636

3737
``` js
3838
{
39-
"condition" : function(R) {
40-
R.when(this.transactionTotal < 500);
39+
"condition" : (R, fact) => {
40+
R.when(fact.transactionTotal < 500);
4141
},
42-
"consequence" : function(R) {
43-
this.result = false;
42+
"consequence" : (R, fact) => {
43+
fact.result = false;
4444
R.stop();
4545
}
4646
}
@@ -56,32 +56,33 @@ Facts are those input json values on which the rule engine applies its rule to o
5656

5757
A sample Fact may look like
5858

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+
````
6567

6668
###### 3. Using the Rule Engine
6769

6870
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.
6971

7072
``` js
71-
var RuleEngine = require("node-rules");
73+
import RuleEngine from "node-rules";
7274

7375
/* Creating Rule Engine instance */
74-
var R = new RuleEngine();
76+
const R = new RuleEngine();
7577

7678
/* 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);
8182
},
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";
8586
R.stop();
8687
}
8788
};
@@ -90,20 +91,22 @@ var rule = {
9091
R.register(rule);
9192

9293
/* Add a Fact with less than 500 as transaction, and this should be blocked */
93-
var fact = {
94+
let fact = {
9495
"name": "user4",
9596
"application": "MOB2",
9697
"transactionTotal": 400,
9798
"cardType": "Credit Card"
9899
};
99100

100101
/* Check if the engine blocks it! */
101-
R.execute(fact, function (data) {
102-
if (data.result) {
102+
R.execute(fact, (data) => {
103+
104+
if (data.result !== false) {
103105
console.log("Valid transaction");
104106
} else {
105107
console.log("Blocked Reason:" + data.reason);
106108
}
109+
107110
});
108111
```
109112

__tests__/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var RuleEngine = require('../index');
1+
import RuleEngine from '../';
2+
23
describe("Rules", function() {
34
describe(".init()", function() {
45
it("should empty the existing rule array", function() {
@@ -251,10 +252,10 @@ describe("Rules", function() {
251252

252253
it("should support fact as optional second parameter for es6 compatibility", function() {
253254
var rule = {
254-
"condition": function(R, fact) {
255+
"condition": (R, fact) => {
255256
R.when(fact && (fact.transactionTotal < 500));
256257
},
257-
"consequence": function(R, fact) {
258+
"consequence": (R, fact) => {
258259
fact.result = false;
259260
R.stop();
260261
}

dist/node-rules.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/Dynamic-Control.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,24 @@ The above `prioritize` call will give priority to Rule with id "one" over all th
3030
##### 3. `RuleEngine.register(<rules>)`
3131
We know that we can pass Rules as parameter into the Rule Engine constructor while we create the Rule Engine object like below.
3232

33-
var RuleEngine = new RuleEngine(rules);
33+
const RuleEngine = new RuleEngine(rules);
3434

3535
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.
3636

37-
var RuleEngine = new RuleEngine();
37+
const RuleEngine = new RuleEngine();
3838
RuleEngine.register(newrule);
3939
RuleEngine.register(newrule);
4040

4141

4242
##### 4. `RuleEngine.findRules(<filter>)`
4343
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.
4444

45-
var rules = RuleEngine.findRules({"id": "one"});
45+
const rules = RuleEngine.findRules({"id": "one"});
4646

4747
##### 5. `RuleEngine.init()`
4848
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.
4949

50-
var RuleEngine = new RuleEngine();
50+
const RuleEngine = new RuleEngine();
5151
RuleEngine.register(badrule);
5252
RuleEngine.init();//removes the bad rule and cleans up
5353
RuleEngine.register(newrule);

docs/Examples.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,23 @@ The example below shows how to use the rule engine to apply a sample rule on a s
22

33
``` js
44
//import the package
5-
var RuleEngine = require('node-rules');
5+
import RuleEngine from 'node-rules';
66

77
//define the rules
8-
var rules = [{
9-
"condition": function(R) {
10-
R.when(this && (this.transactionTotal < 500));
8+
const rules = [{
9+
"condition": (R, fact) => {
10+
R.when(fact && (fact.transactionTotal < 500));
1111
},
12-
"consequence": function(R) {
13-
this.result = false;
12+
"consequence": (R, fact) => {
13+
fact.result = false;
1414
R.stop();
1515
}
1616
}];
1717
/*as you can see above we removed the priority
1818
and on properties for this example as they are optional.*/
1919

2020
//sample fact to run the rules on
21-
var fact = {
21+
let fact = {
2222
"userIP": "27.3.4.5",
2323
"name":"user4",
2424
"application":"MOB2",
@@ -28,10 +28,10 @@ var fact = {
2828
};
2929

3030
//initialize the rule engine
31-
var R = new RuleEngine(rules);
31+
const R = new RuleEngine(rules);
3232

3333
//Now pass the fact on to the rule engine for results
34-
R.execute(fact,function(result){
34+
R.execute(fact, (result) => {
3535

3636
if(result.result)
3737
console.log("\n-----Payment Accepted----\n");

docs/Exporting-and-Importing-Rules.md

Lines changed: 0 additions & 80 deletions
This file was deleted.

docs/Rules.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ Lets see how a sample rule will look like and then proceed to explain the differ
66
"name": "transaction minimum",
77
"priority": 3,
88
"on" : true,
9-
"condition": function(R) {
10-
R.when(this.transactionTotal < 500);
9+
"condition": (R, fact) => {
10+
R.when(fact.transactionTotal < 500);
1111
},
12-
"consequence": function(R) {
13-
this.result = false;
12+
"consequence": (R, fact) => {
13+
fact.result = false;
1414
R.stop();
1515
}
1616
}
1717

1818
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.
1919

2020
###### 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.
2222

23-
"condition": function(R) {
23+
"condition": (R, fact) => {
2424
R.when(this.transactionTotal < 500);
2525
}
2626

@@ -29,12 +29,13 @@ As you can see, the we have to pass an expression on to the `R.when` function wh
2929
Its mandatory to have this field.
3030

3131
###### 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.
3333

34-
"consequence": function(R) {
35-
this.result = false;
34+
"consequence": (R, fact) {
35+
fact.result = false;
3636
R.stop();
3737
}
38+
3839
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.
3940

4041
You can read more about flow control API here.

docs/_Sidebar.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@
44
* [Facts](https://github.com/mithunsatheesh/node-rules/wiki/Facts)
55
* [Flow Control API](https://github.com/mithunsatheesh/node-rules/wiki/Flow-Control-API)
66
* [Dynamic control](https://github.com/mithunsatheesh/node-rules/wiki/Dynamic-Control)
7-
* [Export/Import Rules](https://github.com/mithunsatheesh/node-rules/wiki/Exporting-and-Importing-Rules)
87
* [Examples](https://github.com/mithunsatheesh/node-rules/wiki/Examples)

examples/node.js/1.SimpleRule.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var RuleEngine = require('../../index');
1+
import RuleEngine from '../../lib/node-rules.js';
2+
23
/* Sample Rule to block a transaction if its below 500 */
34
var rule = {
45
"condition": function(R) {

examples/node.js/2.MultipleRules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var RuleEngine = require('../../index');
1+
import RuleEngine from '../../lib/node-rules.js';
2+
23
/* Set of Rules to be applied
34
First blocks a transaction if less than 500
45
Second blocks a debit card transaction.*/

examples/node.js/3.CascadingRules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var RuleEngine = require('../../index');
1+
import RuleEngine from '../../lib/node-rules.js';
2+
23
/* Here we can see a rule which upon matching its condition,
34
does some processing and passes it to other rules for processing */
45
var rules = [{

examples/node.js/4.PrioritizedRules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var RuleEngine = require('../../index');
1+
import RuleEngine from '../../lib/node-rules.js';
2+
23
/* Set of Rules to be applied */
34
var rules = [{
45
"priority": 4,

examples/node.js/5.RecurssionWithRules.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var RuleEngine = require('../../index');
1+
import RuleEngine from '../../lib/node-rules.js';
2+
23
/* Sample Rule to block a transaction if its below 500 */
34
var rule = {
45
"condition": function(R) {

0 commit comments

Comments
 (0)