MCPcopy
hub / github.com/CacheControl/json-rules-engine / start

Function start

examples/07-rule-chaining.js:18–112  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16const { getAccountInformation } = require('./support/account-api-client')
17
18async function start () {
19 /**
20 * Setup a new engine
21 */
22 const engine = new Engine()
23
24 /**
25 * Rule for identifying people who may like screwdrivers
26 */
27 const drinkRule = {
28 conditions: {
29 all: [{
30 fact: 'drinksOrangeJuice',
31 operator: 'equal',
32 value: true
33 }, {
34 fact: 'enjoysVodka',
35 operator: 'equal',
36 value: true
37 }]
38 },
39 event: { type: 'drinks-screwdrivers' },
40 priority: 10, // IMPORTANT! Set a higher priority for the drinkRule, so it runs first
41 onSuccess: async function (event, almanac) {
42 almanac.addFact('screwdriverAficionado', true)
43
44 // asychronous operations can be performed within callbacks
45 // engine execution will not proceed until the returned promises is resolved
46 const accountId = await almanac.factValue('accountId')
47 const accountInfo = await getAccountInformation(accountId)
48 almanac.addFact('accountInfo', accountInfo)
49 },
50 onFailure: function (event, almanac) {
51 almanac.addFact('screwdriverAficionado', false)
52 }
53 }
54 engine.addRule(drinkRule)
55
56 /**
57 * Rule for identifying people who should be invited to a screwdriver social
58 * - Only invite people who enjoy screw drivers
59 * - Only invite people who are sociable
60 */
61 const inviteRule = {
62 conditions: {
63 all: [{
64 fact: 'screwdriverAficionado', // this fact value is set when the drinkRule is evaluated
65 operator: 'equal',
66 value: true
67 }, {
68 fact: 'isSociable',
69 operator: 'equal',
70 value: true
71 }, {
72 fact: 'accountInfo',
73 path: '$.company',
74 operator: 'equal',
75 value: 'microsoft'

Callers 1

Calls 4

addRuleMethod · 0.95
runMethod · 0.95
addFactMethod · 0.45
factValueMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…