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);
});
我是规则引擎的新手。 我试图编写一个规则,我是"节点规则"中制定的。 但我开始知道"nools"是编写规则的最佳方法,而且重量轻。 但我对Nools一无所知。我研究了整个文档,但这让我感到困惑。所以请谁能帮我转换这个我在"节点规则"中写到"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);
}
}