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

Function start

examples/04-fact-dependency.js:18–128  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16const accountClient = 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 microsoft employees that have been terminated.
26 * - Demonstrates re-using a same fact with different parameters
27 * - Demonstrates calling a base fact, which serves to load data once and reuse later
28 */
29 const microsoftRule = {
30 conditions: {
31 all: [{
32 fact: 'account-information',
33 operator: 'equal',
34 value: 'microsoft',
35 path: '$.company'
36 }, {
37 fact: 'account-information',
38 operator: 'equal',
39 value: 'terminated',
40 path: '$.status'
41 }]
42 },
43 event: { type: 'microsoft-terminated-employees' }
44 }
45 engine.addRule(microsoftRule)
46
47 /**
48 * Rule for identifying accounts older than 5 years
49 * - Demonstrates calling a base fact, also shared by the account-information-field fact
50 * - Demonstrates performing computations on data retrieved by base fact
51 */
52 const tenureRule = {
53 conditions: {
54 all: [{
55 fact: 'employee-tenure',
56 operator: 'greaterThanInclusive',
57 value: 5,
58 params: {
59 unit: 'years'
60 }
61 }]
62 },
63 event: { type: 'five-year-tenure' }
64 }
65 engine.addRule(tenureRule)
66
67 /**
68 * Register listeners with the engine for rule success and failure
69 */
70 let facts
71 engine
72 .on('success', event => {
73 console.log(facts.accountId + ' DID '.green + 'meet conditions for the ' + event.type.underline + ' rule.')
74 })
75 .on('failure', event => {

Callers 1

Calls 4

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

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…