Skip to content

Commit 4ec0051

Browse files
author
Cache Hamm
committed
Update examples to use async/await
1 parent 5c04c6b commit 4ec0051

10 files changed

+626
-619
lines changed

examples/01-hello-world.js

+36-39
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,49 @@
1010
*/
1111

1212
require('colors')
13-
const { Engine, Rule } = require('json-rules-engine')
13+
const { Engine } = require('json-rules-engine')
1414

15-
/**
16-
* Setup a new engine
17-
*/
18-
const engine = new Engine()
15+
async function start() {
16+
/**
17+
* Setup a new engine
18+
*/
19+
const engine = new Engine()
1920

20-
/**
21-
* Create a rule
22-
*/
23-
const rule = new Rule({
24-
// define the 'conditions' for when "hello world" should display
25-
conditions: {
26-
all: [{
27-
fact: 'displayMessage',
28-
operator: 'equal',
29-
value: true
30-
}]
31-
},
32-
// define the 'event' that will fire when the condition evaluates truthy
33-
event: {
34-
type: 'message',
35-
params: {
36-
data: 'hello-world!'
21+
/**
22+
* Create a rule
23+
*/
24+
engine.addRule({
25+
// define the 'conditions' for when "hello world" should display
26+
conditions: {
27+
all: [{
28+
fact: 'displayMessage',
29+
operator: 'equal',
30+
value: true
31+
}]
32+
},
33+
// define the 'event' that will fire when the condition evaluates truthy
34+
event: {
35+
type: 'message',
36+
params: {
37+
data: 'hello-world!'
38+
}
3739
}
38-
}
39-
})
40+
})
4041

41-
// add rule to engine
42-
engine.addRule(rule)
42+
/**
43+
* Define a 'displayMessage' as a constant value
44+
* Fact values do NOT need to be known at engine runtime; see the
45+
* 03-dynamic-facts.js example for how to pull in data asynchronously during runtime
46+
*/
47+
const facts = { displayMessage: true }
4348

44-
/**
45-
* Define a 'displayMessage' as a constant value
46-
* Fact values do NOT need to be known at engine runtime; see the
47-
* 03-dynamic-facts.js example for how to pull in data asynchronously during runtime
48-
*/
49-
const facts = { displayMessage: true }
49+
// engine.run() evaluates the rule using the facts provided
50+
const { successResults } = await engine.run(facts)
5051

51-
// run the engine
52-
engine
53-
.run(facts)
54-
.then(results => { // engine returns an object with a list of events with truthy conditions
55-
results.events.map(event => console.log(event.params.data.green))
56-
})
57-
.catch(console.log)
52+
successResults.map(result => console.log(result.event.params.data.green))
53+
}
5854

55+
start()
5956
/*
6057
* OUTPUT:
6158
*

examples/02-nested-boolean-logic.js

+48-49
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,61 @@
1111

1212
require('colors')
1313
const { Engine } = require('json-rules-engine')
14-
/**
15-
* Setup a new engine
16-
*/
17-
const engine = new Engine()
1814

19-
// define a rule for detecting the player has exceeded foul limits. Foul out any player who:
20-
// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
21-
engine.addRule({
22-
conditions: {
23-
any: [{
24-
all: [{
25-
fact: 'gameDuration',
26-
operator: 'equal',
27-
value: 40
28-
}, {
29-
fact: 'personalFoulCount',
30-
operator: 'greaterThanInclusive',
31-
value: 5
32-
}]
33-
}, {
34-
all: [{
35-
fact: 'gameDuration',
36-
operator: 'equal',
37-
value: 48
15+
async function start() {
16+
/**
17+
* Setup a new engine
18+
*/
19+
const engine = new Engine()
20+
21+
// define a rule for detecting the player has exceeded foul limits. Foul out any player who:
22+
// (has committed 5 fouls AND game is 40 minutes) OR (has committed 6 fouls AND game is 48 minutes)
23+
engine.addRule({
24+
conditions: {
25+
any: [{
26+
all: [{
27+
fact: 'gameDuration',
28+
operator: 'equal',
29+
value: 40
30+
}, {
31+
fact: 'personalFoulCount',
32+
operator: 'greaterThanInclusive',
33+
value: 5
34+
}]
3835
}, {
39-
fact: 'personalFoulCount',
40-
operator: 'greaterThanInclusive',
41-
value: 6
36+
all: [{
37+
fact: 'gameDuration',
38+
operator: 'equal',
39+
value: 48
40+
}, {
41+
fact: 'personalFoulCount',
42+
operator: 'greaterThanInclusive',
43+
value: 6
44+
}]
4245
}]
43-
}]
44-
},
45-
event: { // define the event to fire when the conditions evaluate truthy
46-
type: 'fouledOut',
47-
params: {
48-
message: 'Player has fouled out!'
46+
},
47+
event: { // define the event to fire when the conditions evaluate truthy
48+
type: 'fouledOut',
49+
params: {
50+
message: 'Player has fouled out!'
51+
}
4952
}
50-
}
51-
})
53+
})
5254

53-
/**
54-
* define the facts
55-
* note: facts may be loaded asynchronously at runtime; see the advanced example below
56-
*/
57-
const facts = {
58-
personalFoulCount: 6,
59-
gameDuration: 40
60-
}
55+
/**
56+
* define the facts
57+
* note: facts may be loaded asynchronously at runtime; see the advanced example below
58+
*/
59+
const facts = {
60+
personalFoulCount: 6,
61+
gameDuration: 40
62+
}
6163

62-
// run the engine
63-
engine
64-
.run(facts)
65-
.then(results => { // run() return and object containing events with truthy conditions
66-
results.events.map(event => console.log(event.params.message.red))
67-
})
68-
.catch(console.log)
64+
const { successResults } = await engine.run(facts)
6965

66+
successResults.map(result => console.log(result.event.params.message.red))
67+
}
68+
start()
7069
/*
7170
* OUTPUT:
7271
*

examples/03-dynamic-facts.js

+55-55
Original file line numberDiff line numberDiff line change
@@ -12,69 +12,69 @@
1212

1313
require('colors')
1414
const { Engine } = require('json-rules-engine')
15+
1516
// example client for making asynchronous requests to an api, database, etc
1617
const apiClient = require('./support/account-api-client')
1718

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()
2224

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+
}
5255
}
5356
}
54-
}
55-
engine.addRule(microsoftRule)
57+
engine.addRule(microsoftRule)
5658

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+
})
7669
})
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()
7878

7979
/*
8080
* OUTPUT:

0 commit comments

Comments
 (0)