json-rules-engine almanac 简单说明
almanac 实际上就是一个上下文对象,可以对于engine 中依赖的一些fact 进行计算处理,同时可以方便的进行动态fact维护,同时还会复用以前的一些计算fact
官方参考使用
官方有一个比较完整的介绍例子
/*
* Base fact for retrieving account data information.
* Engine will automatically cache results by accountId
*/
let accountInformation = new Fact('account-information', function(params, almanac) {
return request
.get({ url: `http://my-service/account/${params.accountId}`)
.then(function (response) {
return response.data
})
})
/*
* Calls the account-information fact with the appropriate accountId.
* Receives a promise w/results unique to the accountId
*/
let isFundedAccount = new Fact('is-funded-account', function(params, almanac) {
return almanac.factValue('account-information', { accountId: params.accountId }).then(info => {
return info.funded === true
})
})
/*
* Calls the account-information fact with the appropriate accountId.
* Receives a promise w/results unique to the accountId
*/
let accountBalance = new Fact('account-balance', function(params, almanac) {
return almanac.factValue('account-information', { accountId: params.accountId }).then(info => {
return info.balance
})
})
/*
* Add the facts the the engine
*/
engine.addFact(accountInformation)
engine.addFact(isFundedAccount)
engine.addFact(accountBalance)
/*
* Run the engine.
* account-information will be loaded ONCE for the account, regardless of how many
* times is-funded-account or account-balance are mentioned in the rules
*/
engine.run({ accountId: 1 })
almanac 使用场景
获取fact,动态设置fact基于event 获取fact 数据信息,进行fact 依赖处理
参考资料
https://github.com/CacheControl/json-rules-engine/blob/master/docs/almanac.md
https://github.com/CacheControl/json-rules-engine/blob/master/examples/04-fact-dependency.js