UNPKG

5.31 kBMarkdownView Raw
1[![Build Status](https://api.travis-ci.org/mithunsatheesh/node-rules.svg?branch=master)](https://travis-ci.org/mithunsatheesh/node-rules)
2[![npm](https://img.shields.io/npm/l/express.svg?style=flat-square)]()
3[![npm version](https://badge.fury.io/js/node-rules.svg)](http://badge.fury.io/js/node-rules)
4[![Coverage Status](https://coveralls.io/repos/github/mithunsatheesh/node-rules/badge.svg?branch=4.0.0)](https://coveralls.io/github/mithunsatheesh/node-rules?branch=4.0.0)
5[![Known Vulnerabilities](https://snyk.io/test/npm/node-rules/badge.svg)](https://snyk.io/test/npm/node-rules)
6
7
8Node Rules
9=====
10
11Node-rules is a light weight forward chaining Rule Engine, written in JavaScript.
12
13
14#### Installation
15
16 npm install node-rules
17
18![Sample Screencast](https://raw.githubusercontent.com/mithunsatheesh/node-rules/gh-pages/images/screencast.gif "See it in action")
19
20#### Try This Out!
21
22You can see it in action in [this RunKit example](https://runkit.com/mithunsatheesh/runkit-npm-node-rules).
23
24#### Overview
25
26Node-rules takes rules written in JSON friendly format as input. Once the rule engine is running with rules registered on it, you can feed it facts and the rules will be applied one by one to generate an outcome.
27
28###### 1. Defining a Rule
29
30A rule will consist of a condition and its corresponding consequence. You can find the explanation for various mandatory and optional parameters of a rule in [this wiki](https://github.com/mithunsatheesh/node-rules/wiki/Rules).
31
32``` js
33{
34 "condition" : function(R) {
35 R.when(this.transactionTotal < 500);
36 },
37 "consequence" : function(R) {
38 this.result = false;
39 R.stop();
40 }
41}
42```
43
44Here priority is an optional parameter which will be used to specify priority of a rule over other rules when there are multiple rules running. In the above rule `R.when` evaluates the condition expression and `R.stop` used to stop further processing of the fact as we have arrived at a result.
45
46The functions `R.stop`, `R.when`, `R.next`, `R.restart` are part of the Flow Control API which allows user to control the Engine Flow. Read more about [Flow Controls](https://github.com/mithunsatheesh/node-rules/wiki/Flow-Control-API) in [wiki](https://github.com/mithunsatheesh/node-rules/wiki).
47
48
49###### 2. Defining a Fact
50Facts are those input json values on which the rule engine applies its rule to obtain results. A fact can have multiple attributes as you decide.
51
52A sample Fact may look like
53
54 {
55 "name":"user4",
56 "application":"MOB2",
57 "transactionTotal":400,
58 "cardType":"Credit Card",
59 }
60
61###### 3. Using the Rule Engine
62
63The 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.
64
65``` js
66var RuleEngine = require("node-rules");
67
68/* Creating Rule Engine instance */
69var R = new RuleEngine();
70
71/* Add a rule */
72var rule = {
73 "condition": function(R) {
74 console.log(this);
75 R.when(this.transactionTotal < 500);
76 },
77 "consequence": function(R) {
78 this.result = false;
79 this.reason = "The transaction was blocked as it was less than 500";
80 R.stop();
81 }
82};
83
84/* Register Rule */
85R.register(rule);
86
87/* Add a Fact with less than 500 as transaction, and this should be blocked */
88var fact = {
89 "name": "user4",
90 "application": "MOB2",
91 "transactionTotal": 400,
92 "cardType": "Credit Card"
93};
94
95/* Check if the engine blocks it! */
96R.execute(fact, function (data) {
97 if (data.result) {
98 console.log("Valid transaction");
99 } else {
100 console.log("Blocked Reason:" + data.reason);
101 }
102});
103```
104
105###### 4. Controlling Rules running on the Rule Engine
106If you are looking for ways to specify the order in which the rules get applied on a fact, it can be done via using the `priority` parameter. Read more about it in the [Rule wiki](https://github.com/mithunsatheesh/node-rules/wiki/Rules). If you need to know about how to change priority of rules or remove add new rules to a Running Rule Engine, you may read more about it in [Dynamic Control Wiki](https://github.com/mithunsatheesh/node-rules/wiki/Dynamic-Control).
107
108###### 5. Exporting Rules to an external storage
109To read more about storing rules running on the engine to an external DB, refer this [wiki article](https://github.com/mithunsatheesh/node-rules/wiki/Exporting-and-Importing-Rules).
110
111
112#### Wiki
113To read more about the Rule engine functions, please read [the wiki here](https://github.com/mithunsatheesh/node-rules/wiki)!. To find more examples of implementation please look in the [examples](https://github.com/mithunsatheesh/node-rules/tree/master/examples) folder.
114
115#### Issues
116Got issues with the implementation?. Feel free to open an issue [here](https://github.com/mithunsatheesh/node-rules/issues/new).
117
118#### Licence
119Node rules is distributed under the MIT License.
120
121#### External References
122* https://ieeexplore.ieee.org/document/7968566
123
124#### Credits
125The JSON friendly rule formats used in version 2.x.x of this module were initially based on the node module [jools](https://github.com/tdegrunt/jools).
126The screencast image shown in this page is taken from [nmotv.in](http://nmotw.in/node-rules/) which has a pretty nice article on how to use this module!