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,
44does some processing and passes it to other rules for processing */
55var 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 */
2828var R = new RuleEngine ( ) ;
2929R . 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 */
3132var fact = {
3233 name : "user4" ,
3334 application : "MOB" ,
3435 transactionTotal : 600 ,
3536 cardType : "Credit" ,
3637} ;
3738R . 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 ) ;
0 commit comments