Skip to content

Commit

Permalink
added unit tests on refraction
Browse files Browse the repository at this point in the history
  • Loading branch information
frankthelen committed Jan 13, 2018
1 parent 80d4cbe commit 09f86d6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Rools.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class Rools {
this.logger.debug({ message: `evaluate pass ${pass}` });
// create agenda for premises
const premisesAgenda = pass === 0 ? this.premises : memory.getDirtyPremises();
this.logger.debug({ message: `premises agenda length ${premisesAgenda.length}` });
// evaluate premises
premisesAgenda.forEach((premise) => {
try {
Expand All @@ -86,6 +87,7 @@ class Rools {
const actionsAgenda = pass === 0 ? this.actions : premisesAgenda
.reduce((acc, premise) => [...new Set([...acc, ...premise.actions])], [])
.filter(action => !memory.getState(action).fired);
this.logger.debug({ message: `actions agenda length ${actionsAgenda.length}` });
// evaluate actions
actionsAgenda.forEach((action) => {
memory.getState(action).ready =
Expand All @@ -96,6 +98,7 @@ class Rools {
const { fired, ready } = memory.getState(action);
return ready && !fired;
});
this.logger.debug({ message: `conflict set length ${conflictSet.length}` });
// conflict resolution
const action = this.select(conflictSet);
if (!action) {
Expand Down
40 changes: 40 additions & 0 deletions test/refraction.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Rools = require('..');
require('./setup');

describe('Rules.evaluate() / refraction', () => {
const spy = sinon.spy();

const rule1 = {
name: 'rule1',
when: facts => facts.fact1,
then: (facts) => { facts.fact1 = false; facts.fact1 = true; spy(); },
priority: 10,
};

const rule2 = {
name: 'rule2',
when: facts => facts.fact2,
then: (facts) => { facts.fact1 = false; facts.fact1 = true; },
};

const facts = {
fact1: true,
fact2: true,
};

it('should fire each rule only once / recursive', async () => {
spy.resetHistory();
const rools = new Rools();
await rools.register(rule1);
await rools.evaluate(facts);
expect(spy.calledOnce).to.be.equal(true);
});

it('should fire each rule only once / transitive', async () => {
spy.resetHistory();
const rools = new Rools();
await rools.register(rule1, rule2);
await rools.evaluate(facts);
expect(spy.calledOnce).to.be.equal(true);
});
});

0 comments on commit 09f86d6

Please sign in to comment.