json-rules-engine
Advanced tools
Comparing version 1.0.0-beta4 to 1.0.0-beta5
@@ -62,6 +62,2 @@ # Engine | ||
engine.run({ userId: 1 }) | ||
// run with all fact caching turned off | ||
async run ({}, { clearfactResultsCache: true }) | ||
``` | ||
@@ -68,0 +64,0 @@ |
{ | ||
"name": "json-rules-engine", | ||
"version": "1.0.0-beta4", | ||
"version": "1.0.0-beta5", | ||
"description": "Rules Engine expressed in simple json", | ||
@@ -62,2 +62,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"babel-polyfill": "~6.5.0", | ||
"debug": "^2.2.0", | ||
@@ -64,0 +65,0 @@ "object-hash": "^1.1.0", |
@@ -199,3 +199,3 @@ # Json Rules Engine | ||
// subscribe to any event emitted by the engine | ||
engine.on('event', function (event, engine) { | ||
engine.on('success', function (event, engine) { | ||
// event: { | ||
@@ -202,0 +202,0 @@ // type: "young-adult-rocky-mnts", |
@@ -41,3 +41,3 @@ 'use strict' | ||
engine.addRule(rule) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
}) | ||
@@ -82,3 +82,3 @@ | ||
engine.addRule(rule) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
}) | ||
@@ -85,0 +85,0 @@ |
@@ -31,3 +31,3 @@ 'use strict' | ||
engine.addFact('age', ageSpy) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
}) | ||
@@ -78,3 +78,3 @@ | ||
engine.addFact('age', ageSpy) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
}) | ||
@@ -81,0 +81,0 @@ |
@@ -32,3 +32,3 @@ 'use strict' | ||
engine.addFact('age', factSpy, factOptions) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
} | ||
@@ -35,0 +35,0 @@ |
@@ -48,3 +48,3 @@ 'use strict' | ||
ageStub.returns(20) // success | ||
engine.on('event', (event, engine) => { | ||
engine.on('success', (event, engine) => { | ||
eventSpy() | ||
@@ -51,0 +51,0 @@ engine.stop() |
@@ -29,3 +29,3 @@ 'use strict' | ||
it('passes the event type and params', (done) => { | ||
engine.on('event', function (a, engine) { | ||
engine.on('success', function (a, engine) { | ||
try { | ||
@@ -70,3 +70,3 @@ expect(a).to.eql(event) | ||
engine.addRule(drinkOrderRule) | ||
engine.on('event', function (a, e) { | ||
engine.on('success', function (a, e) { | ||
try { | ||
@@ -73,0 +73,0 @@ switch (a.type) { |
@@ -29,3 +29,3 @@ 'use strict' | ||
engine.addFact('accountType', accountTypeStub, { priority: 25 }) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
engine.on('failure', failureSpy) | ||
@@ -32,0 +32,0 @@ } |
@@ -44,3 +44,3 @@ 'use strict' | ||
engine.addFact('eligibility', eligibility) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
} | ||
@@ -47,0 +47,0 @@ |
@@ -57,3 +57,3 @@ 'use strict' | ||
engine.addFact(demographicsDataFact) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
}) | ||
@@ -60,0 +60,0 @@ |
@@ -40,3 +40,3 @@ 'use strict' | ||
engine.addFact('age', factDefinition, factOptions) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
} | ||
@@ -43,0 +43,0 @@ |
@@ -45,3 +45,3 @@ 'use strict' | ||
engine.addRule(rule) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
} | ||
@@ -48,0 +48,0 @@ |
@@ -32,3 +32,3 @@ 'use strict' | ||
engine.addFact('age', factSpy) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
} | ||
@@ -35,0 +35,0 @@ |
@@ -18,34 +18,16 @@ 'use strict' | ||
let eventSpy = sinon.spy() | ||
let factSpy = sinon.spy() | ||
beforeEach(() => { | ||
factSpy.reset() | ||
eventSpy.reset() | ||
let factDefinition = () => { | ||
factSpy() | ||
return 24 | ||
} | ||
engine = engineFactory() | ||
let rule = factories.rule({ conditions, event }) | ||
engine.addRule(rule) | ||
engine.addFact('age', factDefinition) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
}) | ||
it('resets the fact cache with each run', async () => { | ||
await engine.run() | ||
await engine.run() | ||
await engine.run() | ||
expect(eventSpy).to.have.been.calledThrice | ||
expect(factSpy).to.have.been.calledThrice | ||
describe('independent runs', () => { | ||
it('treats each run() independently', async () => { | ||
await Promise.all([50, 10, 12, 30, 14, 15, 25].map((age) => engine.run({age}))) | ||
expect(eventSpy).to.have.been.calledThrice | ||
}) | ||
}) | ||
it('does not reset the fact cache if specified', async () => { | ||
await engine.run() | ||
await engine.run({}, { clearFactCache: false }) | ||
await engine.run() | ||
expect(eventSpy).to.have.been.calledThrice | ||
expect(factSpy).to.have.been.calledTwice | ||
}) | ||
}) |
@@ -17,3 +17,2 @@ 'use strict' | ||
expect(engine).to.have.property('addFact') | ||
expect(engine).to.have.property('factValue') | ||
expect(engine).to.have.property('run') | ||
@@ -141,10 +140,5 @@ expect(engine).to.have.property('stop') | ||
it('allows facts to be added when run', () => { | ||
engine.run({modelId: 'XYZ'}) | ||
expect(engine.facts.get('modelId').value).to.equal('XYZ') | ||
}) | ||
it('changes the status to "RUNNING"', () => { | ||
let eventSpy = sinon.spy() | ||
engine.on('event', (event, engine) => { | ||
engine.on('success', (event, engine) => { | ||
eventSpy() | ||
@@ -162,52 +156,2 @@ expect(engine.status).to.equal('RUNNING') | ||
}) | ||
describe('factValue', () => { | ||
describe('arguments', () => { | ||
beforeEach(() => { | ||
engine.addFact('foo', async (params, facts) => { | ||
if (params.userId) return params.userId | ||
return 'unknown' | ||
}) | ||
}) | ||
it('allows parameters to be passed to the fact', async () => { | ||
return expect(engine.factValue('foo')).to.eventually.equal('unknown') | ||
}) | ||
it('allows parameters to be passed to the fact', async () => { | ||
return expect(engine.factValue('foo', { userId: 1 })).to.eventually.equal(1) | ||
}) | ||
it('throws an exception if it encounters an undefined fact', () => { | ||
expect(engine.factValue('foo')).to.be.rejectedWith(/Undefined fact: foo/) | ||
}) | ||
}) | ||
describe('caching', () => { | ||
let factSpy = sinon.spy() | ||
function setup (factOptions) { | ||
factSpy.reset() | ||
engine.addFact('foo', async (params, facts) => { | ||
factSpy() | ||
return 'unknown' | ||
}, factOptions) | ||
} | ||
it('evaluates the fact every time when fact caching is off', () => { | ||
setup({ cache: false }) | ||
engine.factValue('foo') | ||
engine.factValue('foo') | ||
engine.factValue('foo') | ||
expect(factSpy).to.have.been.calledThrice | ||
}) | ||
it('evaluates the fact once when fact caching is on', () => { | ||
setup({ cache: true }) | ||
engine.factValue('foo') | ||
engine.factValue('foo') | ||
engine.factValue('foo') | ||
expect(factSpy).to.have.been.calledOnce | ||
}) | ||
}) | ||
}) | ||
}) |
@@ -124,3 +124,3 @@ 'use strict' | ||
engine.addRule(rule) | ||
engine.on('event', eventSpy) | ||
engine.on('success', eventSpy) | ||
await engine.run() | ||
@@ -127,0 +127,0 @@ expect(eventSpy).to.have.been.calledOnce |
66259
33
1747
4
+ Addedbabel-polyfill@~6.5.0
+ Addedbabel-polyfill@6.5.0(transitive)
+ Addedbabel-regenerator-runtime@6.5.0(transitive)
+ Addedbabel-runtime@5.8.38(transitive)
+ Addedcore-js@1.2.7(transitive)