2022-08-07 09:18:02 +00:00
|
|
|
import * as plugins from './smartrule.plugins.js';
|
|
|
|
import { Rule, TCheckFunc, TActionFunc, TTreeActionResult } from './smartrule.classes.rule.js';
|
2020-01-20 14:41:27 +00:00
|
|
|
|
|
|
|
export class SmartRule<T> {
|
|
|
|
public rules: Array<Rule<T>> = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* makes a decision based on the given obect and the given rules
|
2020-01-24 07:11:25 +00:00
|
|
|
* @param objectArg
|
2020-01-20 14:41:27 +00:00
|
|
|
*/
|
|
|
|
public async makeDecision(objectArg: T) {
|
|
|
|
// lets sort the rules
|
2020-01-24 07:11:25 +00:00
|
|
|
this.rules = this.rules.sort((a, b) => {
|
2020-01-20 14:41:27 +00:00
|
|
|
if (a.priority > b.priority) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// gets the next batch with the same priority
|
|
|
|
const getNextParallelBatch = (priorityStart: number) => {
|
|
|
|
return this.rules.filter(rule => {
|
|
|
|
return rule.priority === priorityStart;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// lets run the checks
|
2020-01-23 15:24:46 +00:00
|
|
|
const runNextBatch = async (startPriority: number, runRulesAmount: number): Promise<void> => {
|
|
|
|
const nextBatch = getNextParallelBatch(startPriority);
|
|
|
|
runRulesAmount = runRulesAmount + nextBatch.length;
|
2020-01-20 14:41:27 +00:00
|
|
|
const outcomes: TTreeActionResult[] = [];
|
|
|
|
for (const rule of nextBatch) {
|
|
|
|
const checkResult = await rule.checkFunction(objectArg);
|
2020-01-24 07:11:25 +00:00
|
|
|
checkResult
|
|
|
|
? null
|
|
|
|
: console.log(
|
|
|
|
'WARNING!!! Please make sure your rule always returns a statement of how to continue!'
|
|
|
|
);
|
2020-01-23 18:09:09 +00:00
|
|
|
|
2020-01-24 07:11:25 +00:00
|
|
|
if (checkResult.startsWith('apply')) {
|
2020-01-23 16:52:22 +00:00
|
|
|
await rule.actionFunction(objectArg); // here the action function is run
|
2020-01-20 14:41:27 +00:00
|
|
|
}
|
|
|
|
outcomes.push(checkResult);
|
2020-01-23 15:11:29 +00:00
|
|
|
}
|
2020-01-20 14:41:27 +00:00
|
|
|
|
2020-01-23 15:11:29 +00:00
|
|
|
if (outcomes.length > 0) {
|
2020-01-24 07:11:25 +00:00
|
|
|
const finalOutcomeOfBatch: TTreeActionResult = outcomes.reduce(
|
|
|
|
(previous, current, index, array) => {
|
|
|
|
if (current.includes('continue') || previous.includes('continue')) {
|
|
|
|
return 'continue';
|
|
|
|
} else {
|
|
|
|
return 'stop';
|
|
|
|
}
|
2020-01-23 15:11:29 +00:00
|
|
|
}
|
2020-01-24 07:11:25 +00:00
|
|
|
);
|
2020-01-23 15:11:29 +00:00
|
|
|
if (finalOutcomeOfBatch === 'stop') {
|
|
|
|
return;
|
2020-01-20 14:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 15:24:46 +00:00
|
|
|
if (runRulesAmount < this.rules.length) {
|
2020-01-24 07:11:25 +00:00
|
|
|
await runNextBatch(startPriority + 1, runRulesAmount);
|
2020-01-23 15:12:55 +00:00
|
|
|
} else {
|
|
|
|
return;
|
2020-01-20 14:41:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-24 07:11:25 +00:00
|
|
|
await runNextBatch(0, 0);
|
2020-01-20 14:41:27 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 07:11:25 +00:00
|
|
|
public createRule(
|
|
|
|
priorityArg: number,
|
|
|
|
checkFunctionArg: TCheckFunc<T>,
|
|
|
|
actionFunctionArg: TActionFunc
|
|
|
|
) {
|
2020-01-20 14:41:27 +00:00
|
|
|
const rule = new Rule<T>(this, priorityArg, checkFunctionArg, actionFunctionArg);
|
|
|
|
this.rules.push(rule);
|
|
|
|
}
|
2020-01-24 07:11:25 +00:00
|
|
|
}
|