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

posted on   荣锋亮  阅读(12)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用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 集成使用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示