|
| 1 | +'use strict' |
| 2 | + |
| 3 | +/* |
| 4 | + * This is a basic example demonstrating how to leverage the metadata supplied by rule results |
| 5 | + * |
| 6 | + * Usage: |
| 7 | + * node ./examples/09-rule-results.js |
| 8 | + * |
| 9 | + * For detailed output: |
| 10 | + * DEBUG=json-rules-engine node ./examples/09-rule-results.js |
| 11 | + */ |
| 12 | +require('colors') |
| 13 | +let Engine = require('../dist').Engine |
| 14 | + |
| 15 | +/** |
| 16 | + * Setup a new engine |
| 17 | + */ |
| 18 | +let engine = new Engine() |
| 19 | + |
| 20 | +// rule for determining honor role student atheletes (student has GPA >= 3.5 AND is an athlete) |
| 21 | +engine.addRule({ |
| 22 | + conditions: { |
| 23 | + all: [{ |
| 24 | + fact: 'athlete', |
| 25 | + operator: 'equal', |
| 26 | + value: true |
| 27 | + }, { |
| 28 | + fact: 'GPA', |
| 29 | + operator: 'greaterThanInclusive', |
| 30 | + value: 3.5 |
| 31 | + }] |
| 32 | + }, |
| 33 | + event: { // define the event to fire when the conditions evaluate truthy |
| 34 | + type: 'honor-roll', |
| 35 | + params: { |
| 36 | + message: 'Student made the athletics honor-roll' |
| 37 | + } |
| 38 | + } |
| 39 | +}) |
| 40 | + |
| 41 | +function render (message, ruleResult) { |
| 42 | + // if rule succeeded, render success message |
| 43 | + if (ruleResult.result) { |
| 44 | + return console.log(`${message}`.green) |
| 45 | + } |
| 46 | + // if rule failed, iterate over each failed condition to determine why the student didn't qualify for athletics honor roll |
| 47 | + let detail = ruleResult.conditions.all.filter(condition => !condition.result) |
| 48 | + .map(condition => { |
| 49 | + switch (condition.operator) { |
| 50 | + case 'equal': |
| 51 | + return `was not an ${condition.fact}` |
| 52 | + case 'greaterThanInclusive': |
| 53 | + return `${condition.fact} of ${condition.factResult} was too low` |
| 54 | + } |
| 55 | + }).join(' and ') |
| 56 | + console.log(`${message} ${detail}`.red) |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * On success, retrieve the student's username for display purposes, and render |
| 61 | + */ |
| 62 | +engine.on('success', (event, almanac, ruleResult) => { |
| 63 | + almanac.factValue('username').then(username => { |
| 64 | + render(`${username.bold} succeeded! ${event.params.message}`, ruleResult) |
| 65 | + }) |
| 66 | +}) |
| 67 | + |
| 68 | +/** |
| 69 | + * On failure, retrieve the student's username for display purposes, and render |
| 70 | + */ |
| 71 | +engine.on('failure', (event, almanac, ruleResult) => { |
| 72 | + almanac.factValue('username').then(username => { |
| 73 | + render(`${username.bold} failed - `, ruleResult) |
| 74 | + }) |
| 75 | +}) |
| 76 | + |
| 77 | +// Run the engine for 5 different students |
| 78 | +Promise.all([ |
| 79 | + engine.run({ athlete: false, GPA: 3.9, username: 'joe' }), |
| 80 | + engine.run({ athlete: true, GPA: 3.5, username: 'larry' }), |
| 81 | + engine.run({ athlete: false, GPA: 3.1, username: 'jane' }), |
| 82 | + engine.run({ athlete: true, GPA: 4.0, username: 'janet' }), |
| 83 | + engine.run({ athlete: true, GPA: 1.1, username: 'sarah' }) |
| 84 | +]) |
| 85 | + |
| 86 | +/* |
| 87 | + * OUTPUT: |
| 88 | + * |
| 89 | + * joe failed - was not an athlete |
| 90 | + * larry succeeded! Student made the athletics honor-roll |
| 91 | + * jane failed - was not an athlete and GPA of 3.1 was too low |
| 92 | + * janet succeeded! Student made the athletics honor-roll |
| 93 | + * sarah failed - GPA of 1.1 was too low |
| 94 | + */ |
0 commit comments