fix(core): update
This commit is contained in:
@ -1 +1 @@
|
||||
export * from './smartrule.classes.smartrule';
|
||||
export * from './smartrule.classes.smartrule';
|
||||
|
@ -12,10 +12,15 @@ export class Rule<T> {
|
||||
public checkFunction: TCheckFunc<T>;
|
||||
public actionFunction: TActionFunc;
|
||||
|
||||
constructor(smartRuleRef: SmartRule<T>, priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc) {
|
||||
constructor(
|
||||
smartRuleRef: SmartRule<T>,
|
||||
priorityArg: number,
|
||||
checkFunctionArg: TCheckFunc<T>,
|
||||
actionFunctionArg: TActionFunc
|
||||
) {
|
||||
this.smartRuleRef = smartRuleRef;
|
||||
this.priority = priorityArg;
|
||||
this.checkFunction = checkFunctionArg;
|
||||
this.actionFunction = actionFunctionArg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,11 @@ export class SmartRule<T> {
|
||||
|
||||
/**
|
||||
* makes a decision based on the given obect and the given rules
|
||||
* @param objectArg
|
||||
* @param objectArg
|
||||
*/
|
||||
public async makeDecision(objectArg: T) {
|
||||
// lets sort the rules
|
||||
this.rules = this.rules.sort((a,b) => {
|
||||
this.rules = this.rules.sort((a, b) => {
|
||||
if (a.priority > b.priority) {
|
||||
return 1;
|
||||
} else {
|
||||
@ -32,39 +32,49 @@ export class SmartRule<T> {
|
||||
const outcomes: TTreeActionResult[] = [];
|
||||
for (const rule of nextBatch) {
|
||||
const checkResult = await rule.checkFunction(objectArg);
|
||||
checkResult ? null : console.log('WARNING!!! Please make sure your rule always returns a statement of how to continue!');
|
||||
checkResult
|
||||
? null
|
||||
: console.log(
|
||||
'WARNING!!! Please make sure your rule always returns a statement of how to continue!'
|
||||
);
|
||||
|
||||
if (checkResult.startsWith("apply")) {
|
||||
if (checkResult.startsWith('apply')) {
|
||||
await rule.actionFunction(objectArg); // here the action function is run
|
||||
}
|
||||
outcomes.push(checkResult);
|
||||
}
|
||||
|
||||
if (outcomes.length > 0) {
|
||||
const finalOutcomeOfBatch: TTreeActionResult = outcomes.reduce((previous, current, index, array) => {
|
||||
if (current.includes('continue') || previous.includes('continue')) {
|
||||
return 'continue';
|
||||
} else {
|
||||
return 'stop';
|
||||
const finalOutcomeOfBatch: TTreeActionResult = outcomes.reduce(
|
||||
(previous, current, index, array) => {
|
||||
if (current.includes('continue') || previous.includes('continue')) {
|
||||
return 'continue';
|
||||
} else {
|
||||
return 'stop';
|
||||
}
|
||||
}
|
||||
});
|
||||
);
|
||||
if (finalOutcomeOfBatch === 'stop') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (runRulesAmount < this.rules.length) {
|
||||
await runNextBatch((startPriority + 1), runRulesAmount);
|
||||
await runNextBatch(startPriority + 1, runRulesAmount);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
await runNextBatch(0,0);
|
||||
await runNextBatch(0, 0);
|
||||
}
|
||||
|
||||
public createRule(priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc) {
|
||||
public createRule(
|
||||
priorityArg: number,
|
||||
checkFunctionArg: TCheckFunc<T>,
|
||||
actionFunctionArg: TActionFunc
|
||||
) {
|
||||
const rule = new Rule<T>(this, priorityArg, checkFunctionArg, actionFunctionArg);
|
||||
this.rules.push(rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user