10 Commits

Author SHA1 Message Date
8b9dec976c 1.0.10 2020-01-24 07:11:26 +00:00
1493848dc8 fix(core): update 2020-01-24 07:11:25 +00:00
17287a9ba3 1.0.9 2020-01-23 18:09:10 +00:00
11921a2864 fix(core): update 2020-01-23 18:09:09 +00:00
9035fafdc2 1.0.8 2020-01-23 16:52:22 +00:00
773ae00517 fix(core): update 2020-01-23 16:52:22 +00:00
e67fbfebf6 1.0.7 2020-01-23 15:24:47 +00:00
b2478d79f2 fix(core): update 2020-01-23 15:24:46 +00:00
917e630554 1.0.6 2020-01-23 15:12:55 +00:00
b3b02fee70 fix(core): update 2020-01-23 15:12:55 +00:00
8 changed files with 52 additions and 25 deletions

View File

@@ -11,7 +11,13 @@
}, },
"gitzone": { "gitzone": {
"type": "object", "type": "object",
"description": "settings for gitzone" "description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm"]
}
}
} }
} }
} }

View File

@@ -1,5 +1,6 @@
{ {
"gitzone": { "gitzone": {
"projectType": "npm",
"module": { "module": {
"githost": "gitlab.com", "githost": "gitlab.com",
"gitscope": "pushrocks", "gitscope": "pushrocks",

2
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartrule", "name": "@pushrocks/smartrule",
"version": "1.0.5", "version": "1.0.10",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@@ -1,6 +1,6 @@
{ {
"name": "@pushrocks/smartrule", "name": "@pushrocks/smartrule",
"version": "1.0.5", "version": "1.0.10",
"private": false, "private": false,
"description": "a smart rule library for handling decision trees.", "description": "a smart rule library for handling decision trees.",
"main": "dist/index.js", "main": "dist/index.js",

View File

@@ -11,7 +11,7 @@ let testSmartruleInstance: smartrule.SmartRule<ITestMessage>;
tap.test('first test', async () => { tap.test('first test', async () => {
testSmartruleInstance = new smartrule.SmartRule<ITestMessage>(); testSmartruleInstance = new smartrule.SmartRule<ITestMessage>();
testSmartruleInstance.createRule( testSmartruleInstance.createRule(
0, 2,
async messageArg => { async messageArg => {
if (messageArg.body.startsWith('hello')) { if (messageArg.body.startsWith('hello')) {
return 'apply-stop'; return 'apply-stop';
@@ -27,7 +27,7 @@ tap.test('make a decision based on an object', async () => {
testSmartruleInstance.makeDecision({ testSmartruleInstance.makeDecision({
id: '123456', id: '123456',
body: 'hello, there. This is a cool message!' body: 'hello, there. This is a cool message!'
}) });
}); });
tap.start(); tap.start();

View File

@@ -1 +1 @@
export * from './smartrule.classes.smartrule'; export * from './smartrule.classes.smartrule';

View File

@@ -12,10 +12,15 @@ export class Rule<T> {
public checkFunction: TCheckFunc<T>; public checkFunction: TCheckFunc<T>;
public actionFunction: TActionFunc; 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.smartRuleRef = smartRuleRef;
this.priority = priorityArg; this.priority = priorityArg;
this.checkFunction = checkFunctionArg; this.checkFunction = checkFunctionArg;
this.actionFunction = actionFunctionArg; this.actionFunction = actionFunctionArg;
} }
} }

View File

@@ -6,11 +6,11 @@ export class SmartRule<T> {
/** /**
* makes a decision based on the given obect and the given rules * makes a decision based on the given obect and the given rules
* @param objectArg * @param objectArg
*/ */
public async makeDecision(objectArg: T) { public async makeDecision(objectArg: T) {
// lets sort the rules // lets sort the rules
this.rules = this.rules.sort((a,b) => { this.rules = this.rules.sort((a, b) => {
if (a.priority > b.priority) { if (a.priority > b.priority) {
return 1; return 1;
} else { } else {
@@ -26,40 +26,55 @@ export class SmartRule<T> {
}; };
// lets run the checks // lets run the checks
const runNextBatch = async (startPriority: number) => { const runNextBatch = async (startPriority: number, runRulesAmount: number): Promise<void> => {
const nextBatch = getNextParallelBatch(0); const nextBatch = getNextParallelBatch(startPriority);
runRulesAmount = runRulesAmount + nextBatch.length;
const outcomes: TTreeActionResult[] = []; const outcomes: TTreeActionResult[] = [];
for (const rule of nextBatch) { for (const rule of nextBatch) {
const checkResult = await rule.checkFunction(objectArg); const checkResult = await rule.checkFunction(objectArg);
if (checkResult.startsWith("apply")) { checkResult
await rule.actionFunction(objectArg); ? null
: console.log(
'WARNING!!! Please make sure your rule always returns a statement of how to continue!'
);
if (checkResult.startsWith('apply')) {
await rule.actionFunction(objectArg); // here the action function is run
} }
outcomes.push(checkResult); outcomes.push(checkResult);
} }
if (outcomes.length > 0) { if (outcomes.length > 0) {
const finalOutcomeOfBatch: TTreeActionResult = outcomes.reduce((previous, current, index, array) => { const finalOutcomeOfBatch: TTreeActionResult = outcomes.reduce(
if (current.includes('continue') || previous.includes('continue')) { (previous, current, index, array) => {
return 'continue'; if (current.includes('continue') || previous.includes('continue')) {
} else { return 'continue';
return 'stop'; } else {
return 'stop';
}
} }
}); );
if (finalOutcomeOfBatch === 'stop') { if (finalOutcomeOfBatch === 'stop') {
return; return;
} }
} }
if (startPriority < this.rules[this.rules.length-1].priority) { if (runRulesAmount < this.rules.length) {
runNextBatch(startPriority++); await runNextBatch(startPriority + 1, runRulesAmount);
} else {
return;
} }
}; };
await runNextBatch(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); const rule = new Rule<T>(this, priorityArg, checkFunctionArg, actionFunctionArg);
this.rules.push(rule); this.rules.push(rule);
} }
} }