|
| 1 | +'use strict' |
| 2 | +/* |
| 3 | + * This is an advanced example demonstrating rules that passed based off the |
| 4 | + * results of other rules by adding runtime facts. It also demonstrates |
| 5 | + * accessing the runtime facts after engine execution. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node ./examples/10-shared-conditions.js |
| 9 | + * |
| 10 | + * For detailed output: |
| 11 | + * DEBUG=json-rules-engine node ./examples/10-shared-conditions.js |
| 12 | + */ |
| 13 | + |
| 14 | +require('colors') |
| 15 | +const { Engine } = require('json-rules-engine') |
| 16 | + |
| 17 | +async function start () { |
| 18 | + /** |
| 19 | + * Setup a new engine |
| 20 | + */ |
| 21 | + const engine = new Engine() |
| 22 | + |
| 23 | + /** |
| 24 | + * Shared condition that will be used to determine if a user likes screwdrivers |
| 25 | + */ |
| 26 | + engine.addSharedCondition('screwdriverAficionado', { |
| 27 | + all: [ |
| 28 | + { |
| 29 | + fact: 'drinksOrangeJuice', |
| 30 | + operator: 'equal', |
| 31 | + value: true |
| 32 | + }, |
| 33 | + { |
| 34 | + fact: 'enjoysVodka', |
| 35 | + operator: 'equal', |
| 36 | + value: true |
| 37 | + } |
| 38 | + ] |
| 39 | + }) |
| 40 | + |
| 41 | + /** |
| 42 | + * Rule for identifying people who should be invited to a screwdriver social |
| 43 | + * - Only invite people who enjoy screw drivers |
| 44 | + * - Only invite people who are sociable |
| 45 | + */ |
| 46 | + const inviteRule = { |
| 47 | + conditions: { |
| 48 | + all: [ |
| 49 | + { |
| 50 | + uses: 'screwdriverAficionado' |
| 51 | + }, |
| 52 | + { |
| 53 | + fact: 'isSociable', |
| 54 | + operator: 'equal', |
| 55 | + value: true |
| 56 | + } |
| 57 | + ] |
| 58 | + }, |
| 59 | + event: { type: 'invite-to-screwdriver-social' } |
| 60 | + } |
| 61 | + engine.addRule(inviteRule) |
| 62 | + |
| 63 | + /** |
| 64 | + * Rule for identifying people who should be invited to the other social |
| 65 | + * - Only invite people who don't enjoy screw drivers |
| 66 | + * - Only invite people who are sociable |
| 67 | + */ |
| 68 | + const otherInviteRule = { |
| 69 | + conditions: { |
| 70 | + all: [ |
| 71 | + { |
| 72 | + not: { |
| 73 | + uses: 'screwdriverAficionado' |
| 74 | + } |
| 75 | + }, |
| 76 | + { |
| 77 | + fact: 'isSociable', |
| 78 | + operator: 'equal', |
| 79 | + value: true |
| 80 | + } |
| 81 | + ] |
| 82 | + }, |
| 83 | + event: { type: 'invite-to-other-social' } |
| 84 | + } |
| 85 | + engine.addRule(otherInviteRule) |
| 86 | + |
| 87 | + /** |
| 88 | + * Register listeners with the engine for rule success and failure |
| 89 | + */ |
| 90 | + engine |
| 91 | + .on('success', async (event, almanac) => { |
| 92 | + const accountId = await almanac.factValue('accountId') |
| 93 | + console.log( |
| 94 | + `${accountId}` + |
| 95 | + 'DID'.green + |
| 96 | + ` meet conditions for the ${event.type.underline} rule.` |
| 97 | + ) |
| 98 | + }) |
| 99 | + .on('failure', async (event, almanac) => { |
| 100 | + const accountId = await almanac.factValue('accountId') |
| 101 | + console.log( |
| 102 | + `${accountId} did ` + |
| 103 | + 'NOT'.red + |
| 104 | + ` meet conditions for the ${event.type.underline} rule.` |
| 105 | + ) |
| 106 | + }) |
| 107 | + |
| 108 | + // define fact(s) known at runtime |
| 109 | + let facts = { |
| 110 | + accountId: 'washington', |
| 111 | + drinksOrangeJuice: true, |
| 112 | + enjoysVodka: true, |
| 113 | + isSociable: true |
| 114 | + } |
| 115 | + |
| 116 | + // first run, using washington's facts |
| 117 | + await engine.run(facts) |
| 118 | + |
| 119 | + facts = { |
| 120 | + accountId: 'jefferson', |
| 121 | + drinksOrangeJuice: true, |
| 122 | + enjoysVodka: false, |
| 123 | + isSociable: true, |
| 124 | + accountInfo: {} |
| 125 | + } |
| 126 | + |
| 127 | + // second run, using jefferson's facts; facts & evaluation are independent of the first run |
| 128 | + await engine.run(facts) |
| 129 | +} |
| 130 | + |
| 131 | +start() |
| 132 | + |
| 133 | +/* |
| 134 | + * OUTPUT: |
| 135 | + * |
| 136 | + * washington DID meet conditions for the invite-to-screwdriver-social rule. |
| 137 | + * washington did NOT meet conditions for the invite-to-other-social rule. |
| 138 | + * jefferson did NOT meet conditions for the invite-to-screwdriver-social rule. |
| 139 | + * jefferson DID meet conditions for the invite-to-other-social rule. |
| 140 | + */ |
0 commit comments