作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
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/
我真的很高兴在 Node 中看到一个规则引擎,也在 Java 世界中查看 Drools 并阅读文档(特别是:http://docs.jboss.org/drools/release/6.1.0.Fin
嗨,我正在使用 Node.js 开发 nools。当我运行该程序时发生错误: throw new Error("Invalid expression '" + expression + "'") In
var RuleEngine = require('node-rules'); var titan = { "product": "Titan", "amount":"500"
我是一名优秀的程序员,十分优秀!