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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2023-11-28 OpenFunction faas 平台
2023-11-28 wasm3 webassebly 解释器以及通用runtime
2023-11-28 emscripten 中c 代码引用外部js 函数
2023-11-28 kore 简单试用
2023-11-28 kore可扩展安全的Web 应用程序框架
2021-11-28 jfilter一个方便的spring rest 响应过滤扩展
2020-11-28 vernemq webhook 集成使用