|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* |
| 4 | + * This is an advanced example demonstrating rules that passed based off the |
| 5 | + * results of other rules |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * node ./examples/07-rule-chaining.js |
| 9 | + * |
| 10 | + * For detailed output: |
| 11 | + * DEBUG=json-rules-engine node ./examples/07-rule-chaining.js |
| 12 | + */ |
| 13 | + |
| 14 | +require('colors') |
| 15 | +let Engine = require('../dist').Engine |
| 16 | + |
| 17 | +/** |
| 18 | + * Setup a new engine |
| 19 | + */ |
| 20 | +let engine = new Engine() |
| 21 | + |
| 22 | +/** |
| 23 | + * Rule for identifying people who may like screwdrivers |
| 24 | + */ |
| 25 | +let drinkRule = { |
| 26 | + conditions: { |
| 27 | + all: [{ |
| 28 | + fact: 'drinksOrangeJuice', |
| 29 | + operator: 'equal', |
| 30 | + value: true |
| 31 | + }, { |
| 32 | + fact: 'enjoysVodka', |
| 33 | + operator: 'equal', |
| 34 | + value: true |
| 35 | + }] |
| 36 | + }, |
| 37 | + event: { type: 'drinks-screwdrivers' }, |
| 38 | + priority: 10 // IMPORTANT! Set a higher priority for the drinkRule, so it runs first |
| 39 | +} |
| 40 | +engine.addRule(drinkRule) |
| 41 | + |
| 42 | +/** |
| 43 | + * Rule for identifying people who should be invited to a screwdriver social |
| 44 | + * - Only invite people who enjoy screw drivers |
| 45 | + * - Only invite people who are sociable |
| 46 | + */ |
| 47 | +let inviteRule = { |
| 48 | + conditions: { |
| 49 | + all: [{ |
| 50 | + fact: 'screwdriverAficionado', // this fact value is set when the drinkRule is evaluated |
| 51 | + operator: 'equal', |
| 52 | + value: true |
| 53 | + }, { |
| 54 | + fact: 'isSociable', |
| 55 | + operator: 'equal', |
| 56 | + value: true |
| 57 | + }] |
| 58 | + }, |
| 59 | + event: { type: 'invite-to-screwdriver-social' }, |
| 60 | + priority: 5 // Set a lower priority for the drinkRule, so it runs later (default: 1) |
| 61 | +} |
| 62 | +engine.addRule(inviteRule) |
| 63 | + |
| 64 | +/** |
| 65 | + * Register listeners with the engine for rule success and failure |
| 66 | + */ |
| 67 | +let facts |
| 68 | +engine |
| 69 | + .on('success', (event, almanac) => { |
| 70 | + console.log(facts.accountId + ' DID '.green + 'meet conditions for the ' + event.type.underline + ' rule.') |
| 71 | + almanac.addRuntimeFact('screwdriverAficionado', true) |
| 72 | + }) |
| 73 | + .on('failure', rule => { |
| 74 | + console.log(facts.accountId + ' did ' + 'NOT'.red + ' meet conditions for the ' + rule.event.type.underline + ' rule.') |
| 75 | + }) |
| 76 | + |
| 77 | +// define fact(s) known at runtime |
| 78 | +facts = { accountId: 'washington', drinksOrangeJuice: true, enjoysVodka: true, isSociable: true, screwdriverAficionado: false } |
| 79 | +engine |
| 80 | + .run(facts) // first run, using washington's facts |
| 81 | + .then(() => { |
| 82 | + facts = { accountId: 'jefferson', drinksOrangeJuice: true, enjoysVodka: false, isSociable: true, screwdriverAficionado: false } |
| 83 | + return engine.run(facts) // second run, using jefferson's facts; facts & evaluation are independent of the first run |
| 84 | + }) |
| 85 | + .catch(console.log) |
| 86 | + |
| 87 | +/* |
| 88 | + * OUTPUT: |
| 89 | + * |
| 90 | + * washington DID meet conditions for the drinks-screwdrivers rule. |
| 91 | + * washington DID meet conditions for the invite-to-screwdriver-social rule. |
| 92 | + * jefferson did NOT meet conditions for the drinks-screwdrivers rule. |
| 93 | + * jefferson did NOT meet conditions for the invite-to-screwdriver-social rule. |
| 94 | + */ |
0 commit comments