Convert 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); 


});

      

I am new to the rule engine. I am trying to write a rule and I am done in "node-rules". but I learned that "zero" is best to write rules, and that is light weight. but I don't know anything about zeros. I have gone through all the documentation but it confuses me. So please someone can help me by converting this rule that I wrote in "node-rules" for "nools". In advance for your kind information.

+3


source to share


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);
}
}

      



+1


source







All Articles