node.js - 将规则从 Node 规则引擎转换为 nools 规则引擎

标签 node.js nools

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


});

我是规则引擎的新手。我正在尝试编写规则,而我是在“Node 规则”中制定的。但我开始知道“nools”是编写规则的最佳选择,而且重量轻。但我对nools一无所知。 我研究了整个文档,但它让我感到困惑。所以请任何人帮助我转换这个 我在“Node 规则”中写到“nools”的规则。提前感谢您提供的信息。

最佳答案

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

关于node.js - 将规则从 Node 规则引擎转换为 nools 规则引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27649743/

相关文章:

javascript - 从服务器上的 html 发送数据 (node.js)

node.js - 数据存储区使用 Node 获取最后插入的 id

node.js - 如何使用node.js和express.js为网站启用TLS和https请求?

node.js - 使用规则引擎和 Node.js 来处理权限的设计模式

node.js - Jade : Links inside a paragraph