|
12 | 12 |
|
13 | 13 | require('colors')
|
14 | 14 | const { Engine } = require('json-rules-engine')
|
| 15 | + |
15 | 16 | // example client for making asynchronous requests to an api, database, etc
|
16 | 17 | const apiClient = require('./support/account-api-client')
|
17 | 18 |
|
18 |
| -/** |
19 |
| - * Setup a new engine |
20 |
| - */ |
21 |
| -const engine = new Engine() |
| 19 | +async function start() { |
| 20 | + /** |
| 21 | + * Setup a new engine |
| 22 | + */ |
| 23 | + const engine = new Engine() |
22 | 24 |
|
23 |
| -/** |
24 |
| - * Rule for identifying microsoft employees taking pto on christmas |
25 |
| - * |
26 |
| - * the account-information fact returns: |
27 |
| - * { company: 'XYZ', status: 'ABC', ptoDaysTaken: ['YYYY-MM-DD', 'YYYY-MM-DD'] } |
28 |
| - */ |
29 |
| -const microsoftRule = { |
30 |
| - conditions: { |
31 |
| - all: [{ |
32 |
| - fact: 'account-information', |
33 |
| - operator: 'equal', |
34 |
| - value: 'microsoft', |
35 |
| - path: '$.company' // access the 'company' property of "account-information" |
36 |
| - }, { |
37 |
| - fact: 'account-information', |
38 |
| - operator: 'in', |
39 |
| - value: ['active', 'paid-leave'], // 'status'' can be active or paid-leave |
40 |
| - path: '$.status' // access the 'status' property of "account-information" |
41 |
| - }, { |
42 |
| - fact: 'account-information', |
43 |
| - operator: 'contains', |
44 |
| - value: '2016-12-25', |
45 |
| - path: '$.ptoDaysTaken' // access the 'ptoDaysTaken' property of "account-information" |
46 |
| - }] |
47 |
| - }, |
48 |
| - event: { |
49 |
| - type: 'microsoft-christmas-pto', |
50 |
| - params: { |
51 |
| - message: 'current microsoft employee taking christmas day off' |
| 25 | + /** |
| 26 | + * Rule for identifying microsoft employees taking pto on christmas |
| 27 | + * |
| 28 | + * the account-information fact returns: |
| 29 | + * { company: 'XYZ', status: 'ABC', ptoDaysTaken: ['YYYY-MM-DD', 'YYYY-MM-DD'] } |
| 30 | + */ |
| 31 | + const microsoftRule = { |
| 32 | + conditions: { |
| 33 | + all: [{ |
| 34 | + fact: 'account-information', |
| 35 | + operator: 'equal', |
| 36 | + value: 'microsoft', |
| 37 | + path: '$.company' // access the 'company' property of "account-information" |
| 38 | + }, { |
| 39 | + fact: 'account-information', |
| 40 | + operator: 'in', |
| 41 | + value: ['active', 'paid-leave'], // 'status'' can be active or paid-leave |
| 42 | + path: '$.status' // access the 'status' property of "account-information" |
| 43 | + }, { |
| 44 | + fact: 'account-information', |
| 45 | + operator: 'contains', |
| 46 | + value: '2016-12-25', |
| 47 | + path: '$.ptoDaysTaken' // access the 'ptoDaysTaken' property of "account-information" |
| 48 | + }] |
| 49 | + }, |
| 50 | + event: { |
| 51 | + type: 'microsoft-christmas-pto', |
| 52 | + params: { |
| 53 | + message: 'current microsoft employee taking christmas day off' |
| 54 | + } |
52 | 55 | }
|
53 | 56 | }
|
54 |
| -} |
55 |
| -engine.addRule(microsoftRule) |
| 57 | + engine.addRule(microsoftRule) |
56 | 58 |
|
57 |
| -/** |
58 |
| - * 'account-information' fact executes an api call and retrieves account data, feeding the results |
59 |
| - * into the engine. The major advantage of this technique is that although there are THREE conditions |
60 |
| - * requiring this data, only ONE api call is made. This results in much more efficient runtime performance. |
61 |
| - */ |
62 |
| -engine.addFact('account-information', function (params, almanac) { |
63 |
| - return almanac.factValue('accountId') |
64 |
| - .then(accountId => { |
65 |
| - return apiClient.getAccountInformation(accountId) |
66 |
| - }) |
67 |
| -}) |
68 |
| - |
69 |
| -// define fact(s) known at runtime |
70 |
| -const facts = { accountId: 'lincoln' } |
71 |
| -engine |
72 |
| - .run(facts) |
73 |
| - .then(results => { |
74 |
| - if (!results.events.length) return |
75 |
| - console.log(facts.accountId + ' is a ' + results.events.map(event => event.params.message)) |
| 59 | + /** |
| 60 | + * 'account-information' fact executes an api call and retrieves account data, feeding the results |
| 61 | + * into the engine. The major advantage of this technique is that although there are THREE conditions |
| 62 | + * requiring this data, only ONE api call is made. This results in much more efficient runtime performance. |
| 63 | + */ |
| 64 | + engine.addFact('account-information', function (params, almanac) { |
| 65 | + return almanac.factValue('accountId') |
| 66 | + .then(accountId => { |
| 67 | + return apiClient.getAccountInformation(accountId) |
| 68 | + }) |
76 | 69 | })
|
77 |
| - .catch(err => console.log(err.stack)) |
| 70 | + |
| 71 | + // define fact(s) known at runtime |
| 72 | + const facts = { accountId: 'lincoln' } |
| 73 | + const { successResults } = await engine.run(facts) |
| 74 | + |
| 75 | + console.log(facts.accountId + ' is a ' + successResults.map(result => result.event.params.message)) |
| 76 | +} |
| 77 | +start() |
78 | 78 |
|
79 | 79 | /*
|
80 | 80 | * OUTPUT:
|
|
0 commit comments