js-rule-engine

0.0.9 • Public • Published

js-rule-engine

整体思路

  • 整体使用Lokijs做为内存数据库,存储fact,rule,fact-rule关系
  • fact collection 只有一条数据,格式为{factId: factValue}
  • rule collection 存储了转化为mongo-like的查询语句,相应的定时器Id
  • fact-rule collection 存储了 fact 对应的 ruleName数组,fact的数据类型
  • 当addFact时,在fact-rule表中找到可能会触发rule,将rule表中查找到的rule依次执行sql查询。返回查询结果

使用

  • 使用示例
 const Engine = require('js-rule-engine')
 
 const engine = new Engine()
 // addRule
 engine.addRule('r1', {
   conditions: {
     all: [{
         fact: `s2`,
         operator: 'equal',
         value: true,
         path: '.test'
       },{
         fact: `s3`,
         operator: 'equal',
         value: true
       }]
   },
   timers: [
     {
       id: 's124',
       type: 'CONDITION',
       rule: "00 44 10 ? * MON,TUE,WED,THU,FRI,SAT,SUN *"
     }, {
         id: 's124',
         type: 'LIMIT',
         recurrence: {
           range: ['10:50', '11:0'],
           dayofWeek: [0 ,1, 2, 3, 4, 5, 6]
         }
       }
   ]
 })
 // addFact and run
 const ret = engine.addFact([
   { factId: 's2', value: { test: true } },
   { factId: 's3', value: true }
 ])
 // ret = ['r1']
 
 // deleteRule
 engine.deleteRule('r1') 
  • 关于定时器,需要监听Engine实例上的timer事件,参数为触发的ruleName数组
engine.on('timer', (data) => {
 console.log(`engine on timer ${data}`)
})

关于cron

  • 支持两种模式6位和7位
*    *    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    │    └ year
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)

Package Sidebar

Install

npm i js-rule-engine

Weekly Downloads

0

Version

0.0.9

License

ISC

Unpacked Size

29.8 kB

Total Files

10

Last publish

Collaborators

  • leo_