()
| 13 | const { Engine } = require('json-rules-engine') |
| 14 | |
| 15 | async function start () { |
| 16 | /** |
| 17 | * Setup a new engine |
| 18 | */ |
| 19 | const engine = new Engine() |
| 20 | |
| 21 | // define a rule for detecting the player has exceeded foul limits. Foul out any player who: |
| 22 | // (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes) |
| 23 | engine.addRule({ |
| 24 | conditions: { |
| 25 | any: [{ |
| 26 | all: [{ |
| 27 | fact: 'gameDuration', |
| 28 | operator: 'equal', |
| 29 | value: 40 |
| 30 | }, { |
| 31 | fact: 'personalFoulCount', |
| 32 | operator: 'greaterThanInclusive', |
| 33 | value: 5 |
| 34 | }], |
| 35 | name: 'short foul limit' |
| 36 | }, { |
| 37 | all: [{ |
| 38 | fact: 'gameDuration', |
| 39 | operator: 'equal', |
| 40 | value: 48 |
| 41 | }, { |
| 42 | not: { |
| 43 | fact: 'personalFoulCount', |
| 44 | operator: 'lessThan', |
| 45 | value: 6 |
| 46 | } |
| 47 | }], |
| 48 | name: 'long foul limit' |
| 49 | }] |
| 50 | }, |
| 51 | event: { // define the event to fire when the conditions evaluate truthy |
| 52 | type: 'fouledOut', |
| 53 | params: { |
| 54 | message: 'Player has fouled out!' |
| 55 | } |
| 56 | } |
| 57 | }) |
| 58 | |
| 59 | /** |
| 60 | * define the facts |
| 61 | * note: facts may be loaded asynchronously at runtime; see the advanced example below |
| 62 | */ |
| 63 | const facts = { |
| 64 | personalFoulCount: 6, |
| 65 | gameDuration: 40 |
| 66 | } |
| 67 | |
| 68 | const { events } = await engine.run(facts) |
| 69 | |
| 70 | events.map(event => console.log(event.params.message.red)) |
| 71 | } |
| 72 | start() |
no test coverage detected
searching dependent graphs…