Skip to content

Commit

Permalink
first drop
Browse files Browse the repository at this point in the history
  • Loading branch information
frankthelen committed Jan 22, 2018
1 parent 0576b5f commit bd19e84
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Action.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
class Action {
constructor({
id, name, then, priority, final,
id, name, then, priority, final, activationGroup,
}) {
this.id = id;
this.name = name; // for logging only
this.then = then;
this.priority = priority;
this.final = final;
this.activationGroup = activationGroup;
this.premises = [];
}

Expand Down
18 changes: 14 additions & 4 deletions src/Rools.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ class Rools {
// create agenda for actions
const actionsAgenda = pass === 0 ? memory.actions : premisesAgenda
.reduce((acc, premise) => [...new Set([...acc, ...premise.actions])], [])
.filter(action => !memory.getState(action).fired);
.filter((action) => {
const { fired, discarded } = memory.getState(action);
return !fired && !discarded;
});
this.logger.debug({ message: `actions agenda length ${actionsAgenda.length}` });
// evaluate actions
actionsAgenda.forEach((action) => {
Expand All @@ -78,8 +81,8 @@ class Rools {
});
// create conflict set
const conflictSet = memory.actions.filter((action) => { // all actions not only actionsAgenda!
const { fired, ready } = memory.getState(action);
return ready && !fired;
const { fired, ready, discarded } = memory.getState(action);
return ready && !fired && !discarded;
});
this.logger.debug({ message: `conflict set length ${conflictSet.length}` });
// conflict resolution
Expand All @@ -104,11 +107,18 @@ class Rools {
} finally {
delegator.unset();
}
// check final rule
// final rule
if (action.final) {
this.logger.debug({ message: 'evaluation stop after final rule', rule: action.name });
return false; // done
}
// activation group
if (action.activationGroup) {
this.rules.actionsByActivationGroup[action.activationGroup].forEach((other) => {
const state = memory.getState(other);
state.discarded = !state.fired;
});
}
// continue with next pass
return true;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ const { arrify } = require('./utils');

class Rule {
constructor({
name, when, then, priority = 0, final = false, extend,
name, when, then, priority = 0, final = false, extend, activationGroup,
}) {
this.name = name;
this.when = arrify(when);
this.then = then;
this.priority = priority;
this.final = final;
this.extend = arrify(extend);
this.activationGroup = activationGroup;
this.assert();
}

Expand Down Expand Up @@ -52,6 +53,10 @@ class Rule {
this.extend.reduce((acc, rule) => acc && (rule instanceof Rule), true),
'"extend" must be a Rule or an array of Rules',
);
assert(
!this.activationGroup || _.isString(this.activationGroup),
'"activationGroup" must be a string',
);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/RuleSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class RuleSet {
this.premisesByHash = {};
this.nextActionId = uniqueid('a');
this.nextPremiseId = uniqueid('p');
this.actionsByActivationGroup = {}; // hash
}

register(rule) {
Expand Down Expand Up @@ -49,6 +50,16 @@ class RuleSet {
action.add(premise); // action ->> premises
premise.add(action); // premise ->> actions
});
// activation group
const { activationGroup } = rule;
if (activationGroup) {
let group = this.actionsByActivationGroup[activationGroup];
if (!group) {
group = [];
this.actionsByActivationGroup[activationGroup] = group;
}
group.push(action);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/WorkingMemory.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class WorkingMemory {
this.actionsById = {}; // hash
this.premisesById = {}; // hash
this.actions.forEach((action) => {
this.actionsById[action.id] = { ready: false, fired: false };
this.actionsById[action.id] = { ready: false, fired: false, discarded: false };
});
this.premises.forEach((premise) => {
this.premisesById[premise.id] = { value: undefined };
Expand Down

0 comments on commit bd19e84

Please sign in to comment.