- Notifications
You must be signed in to change notification settings - Fork113
Node-rules is a light weight forward chaining rule engine that can be used in JavaScript and TypeScript based projects.
License
mithunsatheesh/node-rules
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Node-rules is a light weight forward chaining Rule Engine, written in JavaScript for both browser and node.js environments.
npm install node-rulesYou can see this in action in a node.js environment usingthis RunKit example. If you are interested to use it in the browser, usethis JSFiddle for a quick start.
Node-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.
A rule will consist of a condition and its corresponding consequence. You can find the explanation for various mandatory and optional parameters of a rule inthis wiki.
{"condition" :(R,fact)=>{R.when(fact.transactionTotal<500);},"consequence" :(R,fact)=>{fact.result=false;R.stop();}}
Here 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 ruleR.when evaluates the condition expression andR.stop used to stop further processing of the fact as we have arrived at a result.
The functionsR.stop,R.when,R.next,R.restart are part of the Flow Control API which allows user to control the Engine Flow. Read more aboutFlow Controls inwiki.
Facts 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.
A sample Fact may look like
{"name":"user4","application":"MOB2","transactionTotal":400,"cardType":"Credit Card"}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.
const{ RuleEngine}=require("node-rules");/* Creating Rule Engine instance */constR=newRuleEngine();/* Add a rule */construle={condition:(R,fact)=>{R.when(fact.transactionTotal<500);},consequence:(R,fact)=>{fact.result=false;fact.reason="The transaction was blocked as it was less than 500";R.stop();},};/* Register Rule */R.register(rule);/* Add a Fact with less than 500 as transaction, and this should be blocked */letfact={name:"user4",application:"MOB2",transactionTotal:400,cardType:"Credit Card",};/* Check if the engine blocks it! */R.execute(fact,(data)=>{if(data.result!==false){console.log("Valid transaction");}else{console.log("Blocked Reason:"+data.reason);}});
If you are looking for ways to specify the order in which the rules get applied on a fact, it can be done via using thepriority parameter. Read more about it in theRule wiki. 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 inDynamic Control Wiki.
To read more about storing rules running on the engine to an external DB, refer thiswiki article.
To read more about the Rule engine functions, please readthe wiki here!. To find more examples of implementation please look in theexamples folder.
Got issues with the implementation?. Feel free to open an issuehere.
Node rules is distributed under the MIT License.
The JSON friendly rule formats used in version 2.x.x of this module were initially based on the node modulejools.The screencast image shown in this page is taken fromnmotv.in which has a pretty nice article on how to use this module!
About
Node-rules is a light weight forward chaining rule engine that can be used in JavaScript and TypeScript based projects.
Topics
Resources
License
Code of conduct
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Contributors13
Uh oh!
There was an error while loading.Please reload this page.
