Skip to content

Commit 0bdccb3

Browse files
author
Cache Hamm
committed
Update rule chaining example with async/await and accountInformation query
1 parent aa9859d commit 0bdccb3

1 file changed

Lines changed: 39 additions & 42 deletions

File tree

examples/07-rule-chaining.js

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
require('colors')
1515
const { Engine } = require('json-rules-engine')
16+
const { getAccountInformation } = require('./support/account-api-client')
1617

1718
/**
1819
* Setup a new engine
@@ -40,12 +41,10 @@ const drinkRule = {
4041
almanac.addRuntimeFact('screwdriverAficionado', true)
4142

4243
// asychronous operations can be performed within callbacks
43-
// execution will not proceed until returned promises are resolved
44-
const zipCode = await almanac.factValue('zipCode')
45-
if (zipCode > 80014 && zipCode < 80642) {
46-
const hometown = 'Denver'
47-
almanac.addRuntimeFact('hometown', hometown) // overrides default 'hometown' fact value
48-
}
44+
// engine execution will not proceed until the returned promises is resolved
45+
const accountId = await almanac.factValue('accountId')
46+
const accountInfo = await getAccountInformation(accountId)
47+
almanac.addRuntimeFact('accountInfo', accountInfo)
4948
},
5049
onFailure: function (event, almanac) {
5150
almanac.addRuntimeFact('screwdriverAficionado', false)
@@ -69,9 +68,10 @@ const inviteRule = {
6968
operator: 'equal',
7069
value: true
7170
}, {
72-
fact: 'hometown',
71+
fact: 'accountInfo',
72+
path: '$.company',
7373
operator: 'equal',
74-
value: 'Denver'
74+
value: 'microsoft'
7575
}]
7676
},
7777
event: { type: 'invite-to-screwdriver-social' },
@@ -82,48 +82,45 @@ engine.addRule(inviteRule)
8282
/**
8383
* Register listeners with the engine for rule success and failure
8484
*/
85-
let facts
8685
engine
87-
.on('success', (event, almanac) => {
88-
console.log(facts.accountId + ' DID '.green + 'meet conditions for the ' + event.type.underline + ' rule.')
86+
.on('success', async (event, almanac) => {
87+
const accountInfo = await almanac.factValue('accountInfo')
88+
const accountId = await almanac.factValue('accountId')
89+
console.log(`${accountId}(${accountInfo.company}) ` + 'DID'.green + ` meet conditions for the ${event.type.underline} rule.`)
8990
})
90-
.on('failure', event => {
91-
console.log(facts.accountId + ' did ' + 'NOT'.red + ' meet conditions for the ' + event.type.underline + ' rule.')
91+
.on('failure', async (event, almanac) => {
92+
const accountId = await almanac.factValue('accountId')
93+
console.log(`${accountId} did ` + 'NOT'.red + ` meet conditions for the ${event.type.underline} rule.`)
9294
})
9395

94-
// define fact(s) known at runtime
95-
facts = { accountId: 'washington', drinksOrangeJuice: true, enjoysVodka: true, isSociable: true, zipCode: 80211, hometown: 'unknown' }
96-
engine
97-
.run(facts) // first run, using washington's facts
98-
.then((results) => {
99-
// access whether washington is a screwdriverAficionado,
100-
// which was determined at runtime via the rules `drinkRules`
101-
return results.almanac.factValue('screwdriverAficionado')
102-
})
103-
.then(isScrewdriverAficionado => {
104-
console.log(`${facts.accountId} ${isScrewdriverAficionado ? 'IS'.green : 'IS NOT'.red} a screwdriver aficionado in Denver`)
105-
})
106-
.then(() => {
107-
facts = { accountId: 'jefferson', drinksOrangeJuice: true, enjoysVodka: false, isSociable: true, zipCode: 80248, hometown: 'unknown' }
108-
return engine.run(facts) // second run, using jefferson's facts; facts & evaluation are independent of the first run
109-
})
110-
.then((results) => {
111-
// access whether jefferson is a screwdriverAficionado,
112-
// which was determined at runtime via the rules `drinkRules`
113-
return results.almanac.factValue('screwdriverAficionado')
114-
})
115-
.then(isScrewdriverAficionado => {
116-
console.log(`${facts.accountId} ${isScrewdriverAficionado ? 'IS'.green : 'IS NOT'.red} a screwdriver aficionado in Denver`)
117-
})
118-
.catch(console.log)
96+
async function run () {
97+
// define fact(s) known at runtime
98+
let facts = { accountId: 'washington', drinksOrangeJuice: true, enjoysVodka: true, isSociable: true, accountInfo: {} }
99+
100+
// first run, using washington's facts
101+
let results = await engine.run(facts)
102+
103+
// isScrewdriverAficionado was a fact set by engine.run()
104+
let isScrewdriverAficionado = results.almanac.factValue('screwdriverAficionado')
105+
console.log(`${facts.accountId} ${isScrewdriverAficionado ? 'IS'.green : 'IS NOT'.red} a screwdriver aficionado`)
106+
107+
facts = { accountId: 'jefferson', drinksOrangeJuice: true, enjoysVodka: false, isSociable: true, accountInfo: {} }
108+
results = await engine.run(facts) // second run, using jefferson's facts; facts & evaluation are independent of the first run
109+
110+
isScrewdriverAficionado = await results.almanac.factValue('screwdriverAficionado')
111+
console.log(`${facts.accountId} ${isScrewdriverAficionado ? 'IS'.green : 'IS NOT'.red} a screwdriver aficionado`)
112+
}
113+
114+
run().catch(console.log)
119115

120116
/*
121117
* OUTPUT:
122118
*
123-
* washington DID meet conditions for the drinks-screwdrivers rule.
124-
* washington DID meet conditions for the invite-to-screwdriver-social rule.
125-
* washington IS a screwdriver aficionado in Denver
119+
* loading account information for "washington"
120+
* washington(microsoft) DID meet conditions for the drinks-screwdrivers rule.
121+
* washington(microsoft) DID meet conditions for the invite-to-screwdriver-social rule.
122+
* washington IS a screwdriver aficionado
126123
* jefferson did NOT meet conditions for the drinks-screwdrivers rule.
127124
* jefferson did NOT meet conditions for the invite-to-screwdriver-social rule.
128-
* jefferson IS NOT a screwdriver aficionado in Denver
125+
* jefferson IS NOT a screwdriver aficionado
129126
*/

0 commit comments

Comments
 (0)