()
| 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 | /** |
| 22 | * Create a rule |
| 23 | */ |
| 24 | engine.addRule({ |
| 25 | // define the 'conditions' for when "hello world" should display |
| 26 | conditions: { |
| 27 | all: [{ |
| 28 | fact: 'displayMessage', |
| 29 | operator: 'equal', |
| 30 | value: true |
| 31 | }] |
| 32 | }, |
| 33 | // define the 'event' that will fire when the condition evaluates truthy |
| 34 | event: { |
| 35 | type: 'message', |
| 36 | params: { |
| 37 | data: 'hello-world!' |
| 38 | } |
| 39 | } |
| 40 | }) |
| 41 | |
| 42 | /** |
| 43 | * Define a 'displayMessage' as a constant value |
| 44 | * Fact values do NOT need to be known at engine runtime; see the |
| 45 | * 03-dynamic-facts.js example for how to pull in data asynchronously during runtime |
| 46 | */ |
| 47 | const facts = { displayMessage: true } |
| 48 | |
| 49 | // engine.run() evaluates the rule using the facts provided |
| 50 | const { events } = await engine.run(facts) |
| 51 | |
| 52 | events.map(event => console.log(event.params.data.green)) |
| 53 | } |
| 54 | |
| 55 | start() |
| 56 | /* |
no test coverage detected
searching dependent graphs…