-
Notifications
You must be signed in to change notification settings - Fork 482
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation around shared conditions
Add documentation covering the use and advantages of shared conditions. Specifically on the ability to re-use the same condition across multiple rules.
- Loading branch information
Christopher Pardy
committed
Jul 10, 2023
1 parent
b19cff2
commit 2e4ec69
Showing
4 changed files
with
234 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
'use strict' | ||
/* | ||
* This is an advanced example demonstrating rules that passed based off the | ||
* results of other rules by adding runtime facts. It also demonstrates | ||
* accessing the runtime facts after engine execution. | ||
* | ||
* Usage: | ||
* node ./examples/10-shared-conditions.js | ||
* | ||
* For detailed output: | ||
* DEBUG=json-rules-engine node ./examples/10-shared-conditions.js | ||
*/ | ||
|
||
require('colors') | ||
const { Engine } = require('json-rules-engine') | ||
|
||
async function start () { | ||
/** | ||
* Setup a new engine | ||
*/ | ||
const engine = new Engine() | ||
|
||
/** | ||
* Shared condition that will be used to determine if a user likes screwdrivers | ||
*/ | ||
engine.addSharedCondition('screwdriverAficionado', { | ||
all: [ | ||
{ | ||
fact: 'drinksOrangeJuice', | ||
operator: 'equal', | ||
value: true | ||
}, | ||
{ | ||
fact: 'enjoysVodka', | ||
operator: 'equal', | ||
value: true | ||
} | ||
] | ||
}) | ||
|
||
/** | ||
* Rule for identifying people who should be invited to a screwdriver social | ||
* - Only invite people who enjoy screw drivers | ||
* - Only invite people who are sociable | ||
*/ | ||
const inviteRule = { | ||
conditions: { | ||
all: [ | ||
{ | ||
uses: 'screwdriverAficionado' | ||
}, | ||
{ | ||
fact: 'isSociable', | ||
operator: 'equal', | ||
value: true | ||
} | ||
] | ||
}, | ||
event: { type: 'invite-to-screwdriver-social' } | ||
} | ||
engine.addRule(inviteRule) | ||
|
||
/** | ||
* Rule for identifying people who should be invited to the other social | ||
* - Only invite people who don't enjoy screw drivers | ||
* - Only invite people who are sociable | ||
*/ | ||
const otherInviteRule = { | ||
conditions: { | ||
all: [ | ||
{ | ||
not: { | ||
uses: 'screwdriverAficionado' | ||
} | ||
}, | ||
{ | ||
fact: 'isSociable', | ||
operator: 'equal', | ||
value: true | ||
} | ||
] | ||
}, | ||
event: { type: 'invite-to-other-social' } | ||
} | ||
engine.addRule(otherInviteRule) | ||
|
||
/** | ||
* Register listeners with the engine for rule success and failure | ||
*/ | ||
engine | ||
.on('success', async (event, almanac) => { | ||
const accountId = await almanac.factValue('accountId') | ||
console.log( | ||
`${accountId}` + | ||
'DID'.green + | ||
` meet conditions for the ${event.type.underline} rule.` | ||
) | ||
}) | ||
.on('failure', async (event, almanac) => { | ||
const accountId = await almanac.factValue('accountId') | ||
console.log( | ||
`${accountId} did ` + | ||
'NOT'.red + | ||
` meet conditions for the ${event.type.underline} rule.` | ||
) | ||
}) | ||
|
||
// define fact(s) known at runtime | ||
let facts = { | ||
accountId: 'washington', | ||
drinksOrangeJuice: true, | ||
enjoysVodka: true, | ||
isSociable: true | ||
} | ||
|
||
// first run, using washington's facts | ||
await engine.run(facts) | ||
|
||
facts = { | ||
accountId: 'jefferson', | ||
drinksOrangeJuice: true, | ||
enjoysVodka: false, | ||
isSociable: true, | ||
accountInfo: {} | ||
} | ||
|
||
// second run, using jefferson's facts; facts & evaluation are independent of the first run | ||
await engine.run(facts) | ||
} | ||
|
||
start() | ||
|
||
/* | ||
* OUTPUT: | ||
* | ||
* washington DID meet conditions for the invite-to-screwdriver-social rule. | ||
* washington did NOT meet conditions for the invite-to-other-social rule. | ||
* jefferson did NOT meet conditions for the invite-to-screwdriver-social rule. | ||
* jefferson DID meet conditions for the invite-to-other-social rule. | ||
*/ |