Converting a Rule from Node-Rules Engine to Nools Rule Engine
var RuleEngine = require('node-rules');
var titan = {
      "product": "Titan",
      "amount":"500",
      "base":1,
      "conversation":0.91,
      };
      var oxicash = {

      }

      var rules = [{
        "name": "Product rule",
        "description": "when the product matches Titan",
        "priority": 1,
        "on":1,
        "condition":
            function(fact,cb) {
                cb(fact && (fact.product = "Titan"));
            },
        "consequence":
            function(cb) {
                console.log("Rule 1 matched for "+this.product);
                var a = this.amount;
                var b = this.base;
                var c = this.conversation;

                var result1= (a*c)/b;
                console.log("the promotion cost is:"+result1);
                this.result = true;
                this.process = true;
                cb();
            }
    }];
    var R = new RuleEngine(rules);

R.execute(titan,function(result){ 

    if(result.result) 
        console.log("\n-----u have discount----\n"); 
    else 
        console.log("\n-----u dont have discount----\n");

    console.log(result); 
    console.log(result1); 


});

Am new to rule-engine. am trying to write a rule and i am made in "node-rules". but i came to know that "nools" was the best to write rules and it is light weight. but i don't know anything about nools. i am studied whole documentation but it is confusing me. So please can anyone help me by converting this rule which I have written in "node-rules" to "nools".Thanking you in advance for your kind information.

1 Answer

//server.js file
"use strict";
var nools = require("nools");
 var ruleFilePath = __dirname + "/rule.nools";
var flow = nools.compile(__dirname + "/rule.nools");
var session=flow.getSession();
var Titan = flow.getDefined("titan");



var t = new Titan(),
    session1 = flow.getSession(new Titan({product : 'titan',amount : 500, base : 1, conversation : 0.91}), t),
    s1 = +(new Date());
session1.match().then(function () {

    console.log("%d [%dms]", t.rule3, +(new Date()) - s1);
    //console.log()
    session1.dispose();
    profiler.pause();
});
//rule.nool
define Titan {
product : null,
amount : null,
base : null,
conversation : null
}
rule product_rule {
when {
  t1 : Titan t1.product == 'titan' && t1.amount == 500 && t1.base == 1 && t1.conversation == 0.91 ;
}
then {
var a = t1.amount;
        var b = t1.base;
        var c = t1.conversation;
        console.log(a);
        var v = (a*c)/b;
        console.log(v);
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest