gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 00:01:46 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com