Converting a rule from node-rules engine to nools rule engine

1.8k views Asked by At
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

There are 1 answers

0
A.H On BEST 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);
}
}