05-optimizing-runtime-with-fact-priorities.js

node v10.24.1
version: 1.0.0
endpointsharetweet
/* * This is an advanced example that demonstrates using fact priorities to optimize the rules engine. * * Usage: * node ./examples/05-optimize-runtime-with-fact-priority.js * * For detailed output: * DEBUG=json-rules-engine node ./examples/05-optimize-runtime-with-fact-priority.js */ let accountData = { washington: { company: 'microsoft', status: 'terminated', ptoDaysTaken: ['2016-12-25', '2016-04-01'], createdAt: '2012-02-14' }, jefferson: { company: 'apple', status: 'terminated', ptoDaysTaken: ['2015-01-25'], createdAt: '2005-04-03' }, lincoln: { company: 'microsoft', status: 'active', ptoDaysTaken: ['2016-02-21', '2016-12-25', '2016-03-28'], createdAt: '2015-06-26' } } /** * mock api client for retrieving account information */ function getAccountInformation(accountId) { var message = 'loading account information for "' + accountId + '"' console.log(message.dim) return new Promise((resolve, reject) => { setImmediate(() => { resolve(accountData[accountId]) }) }) } let RuleEngine = require('json-rules-engine'); let engine = new RuleEngine.Engine(); /** * Setup a new engine */ /** * - Demonstrates setting high performance (cpu) facts higher than low performing (network call) facts. */ let microsoftRule = { conditions: { all: [{ fact: 'account-information', operator: 'equal', value: true }, { fact: 'date', operator: 'lessThan', value: 1467331200000 // unix ts for 2016-07-01; truthy when current date is prior to 2016-07-01 }] }, event: { type: 'microsoft-employees' } } engine.addRule(microsoftRule) /** * Register listeners with the engine for rule success and failure */ let facts engine .on('success', event => { console.log(facts.accountId + ' DID '.green + 'meet conditions for the ' + event.type.underline + ' rule.') }) .on('failure', event => { console.log(facts.accountId + ' did ' + 'NOT'.red + ' meet conditions for the ' + event.type.underline + ' rule.') }) /** * Low and High Priorities. * Facts that do not have a priority set default to 1 * @type {Integer} - Facts are run in priority from highest to lowest. */ let HIGH = 100 let LOW = 1 /** * 'account-information' fact executes an api call - network calls are expensive, so * we set this fact to be LOW priority; it will only be evaluated after all higher priority facts * evaluate truthy */ engine.addFact('account-information', (params, almanac) => { // this fact will not be evaluated, because the "date" fact will fail first console.log('Checking the "account-information" fact...') // this message will not appear return almanac.factValue('accountId') .then((accountId) => { return getAccountInformation(accountId) }) }, { priority: LOW }) /** * 'date' fact returns the current unix timestamp in ms. * Because this is cheap to compute, we set it to "HIGH" priority */ engine.addFact('date', (params, almanac) => { console.log('Checking the "date" fact...') return Date.now() }, { priority: HIGH }) // define fact(s) known at runtime facts = { accountId: 'washington' } engine.run(facts).catch(console.log) /* * OUTPUT: * * Checking the "date" fact first.. * washington did NOT meet conditions for the microsoft-employees rule. */ /* * NOTES: * * - Notice that the "account-information" fact was never evaluated, saving a network call and speeding up * the engine by an order of magnitude(or more!). Swap the priorities of the facts to see both run. */
Loading…

no comments

    sign in to comment