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

Function start

examples/12-using-custom-almanac.js:37–84  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

35}
36
37async function start () {
38 const engine = new Engine()
39 .addRule({
40 conditions: {
41 all: [
42 {
43 fact: 'age',
44 params: {
45 // the addOne pipe adds one to the value
46 pipes: ['addOne']
47 },
48 operator: 'greaterThanInclusive',
49 value: 21
50 }
51 ]
52 },
53 event: {
54 type: 'Over 21(ish)'
55 }
56 })
57
58 engine.on('success', async (event, almanac) => {
59 const name = await almanac.factValue('name')
60 const age = await almanac.factValue('age')
61 console.log(`${name} is ${age} years old and ${'is'.green} ${event.type}`)
62 })
63
64 engine.on('failure', async (event, almanac) => {
65 const name = await almanac.factValue('name')
66 const age = await almanac.factValue('age')
67 console.log(`${name} is ${age} years old and ${'is not'.red} ${event.type}`)
68 })
69
70 const createAlmanacWithPipes = () => {
71 const almanac = new PipedAlmanac()
72 almanac.addPipe('addOne', (v) => v + 1)
73 return almanac
74 }
75
76 // first run Bob who is less than 20
77 await engine.run({ name: 'Bob', age: 19 }, { almanac: createAlmanacWithPipes() })
78
79 // second run Alice who is 21
80 await engine.run({ name: 'Alice', age: 21 }, { almanac: createAlmanacWithPipes() })
81
82 // third run Chad who is 20
83 await engine.run({ name: 'Chad', age: 20 }, { almanac: createAlmanacWithPipes() })
84}
85
86start()
87

Callers 1

Calls 4

factValueMethod · 0.95
createAlmanacWithPipesFunction · 0.85
addRuleMethod · 0.80
runMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…