Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8b9dec976c | |||
| 1493848dc8 | |||
| 17287a9ba3 | |||
| 11921a2864 | |||
| 9035fafdc2 | |||
| 773ae00517 | |||
| e67fbfebf6 | |||
| b2478d79f2 | |||
| 917e630554 | |||
| b3b02fee70 | |||
| 8c257bc0fd | |||
| a4ff5c26e2 | |||
| 7d08f9bdf6 | |||
| 5fabe776f5 | |||
| 7be7617c07 |
8
.vscode/settings.json
vendored
8
.vscode/settings.json
vendored
@@ -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"]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
2
package-lock.json
generated
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrule",
|
"name": "@pushrocks/smartrule",
|
||||||
"version": "1.0.2",
|
"version": "1.0.10",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrule",
|
"name": "@pushrocks/smartrule",
|
||||||
"version": "1.0.2",
|
"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",
|
||||||
|
|||||||
27
test/test.ts
27
test/test.ts
@@ -1,8 +1,33 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartrule from '../ts/index';
|
import * as smartrule from '../ts/index';
|
||||||
|
|
||||||
|
interface ITestMessage {
|
||||||
|
id: string;
|
||||||
|
body: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
let testSmartruleInstance: smartrule.SmartRule<ITestMessage>;
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
tap.test('first test', async () => {
|
||||||
console.log(smartrule.standardExport);
|
testSmartruleInstance = new smartrule.SmartRule<ITestMessage>();
|
||||||
|
testSmartruleInstance.createRule(
|
||||||
|
2,
|
||||||
|
async messageArg => {
|
||||||
|
if (messageArg.body.startsWith('hello')) {
|
||||||
|
return 'apply-stop';
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async messageArg => {
|
||||||
|
console.log(`rule triggered for message with body ${messageArg.body}`);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('make a decision based on an object', async () => {
|
||||||
|
testSmartruleInstance.makeDecision({
|
||||||
|
id: '123456',
|
||||||
|
body: 'hello, there. This is a cool message!'
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
|||||||
@@ -1,3 +1 @@
|
|||||||
import * as plugins from './smartrule.plugins';
|
export * from './smartrule.classes.smartrule';
|
||||||
|
|
||||||
export let standardExport = 'Hi there! :) This is an exported string';
|
|
||||||
|
|||||||
26
ts/smartrule.classes.rule.ts
Normal file
26
ts/smartrule.classes.rule.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import * as plugins from './smartrule.plugins';
|
||||||
|
import { SmartRule } from './smartrule.classes.smartrule';
|
||||||
|
|
||||||
|
export type TTreeActionResult = 'continue' | 'apply-continue' | 'apply-stop' | 'stop';
|
||||||
|
export type TActionFunc = (objectArg) => Promise<any>;
|
||||||
|
|
||||||
|
export type TCheckFunc<T> = (objectArg: T) => Promise<TTreeActionResult>;
|
||||||
|
|
||||||
|
export class Rule<T> {
|
||||||
|
public smartRuleRef: SmartRule<T>;
|
||||||
|
public priority: number;
|
||||||
|
public checkFunction: TCheckFunc<T>;
|
||||||
|
public actionFunction: TActionFunc;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
smartRuleRef: SmartRule<T>,
|
||||||
|
priorityArg: number,
|
||||||
|
checkFunctionArg: TCheckFunc<T>,
|
||||||
|
actionFunctionArg: TActionFunc
|
||||||
|
) {
|
||||||
|
this.smartRuleRef = smartRuleRef;
|
||||||
|
this.priority = priorityArg;
|
||||||
|
this.checkFunction = checkFunctionArg;
|
||||||
|
this.actionFunction = actionFunctionArg;
|
||||||
|
}
|
||||||
|
}
|
||||||
80
ts/smartrule.classes.smartrule.ts
Normal file
80
ts/smartrule.classes.smartrule.ts
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
import * as plugins from './smartrule.plugins';
|
||||||
|
import { Rule, TCheckFunc, TActionFunc, TTreeActionResult } from './smartrule.classes.rule';
|
||||||
|
|
||||||
|
export class SmartRule<T> {
|
||||||
|
public rules: Array<Rule<T>> = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* makes a decision based on the given obect and the given rules
|
||||||
|
* @param objectArg
|
||||||
|
*/
|
||||||
|
public async makeDecision(objectArg: T) {
|
||||||
|
// lets sort the rules
|
||||||
|
this.rules = this.rules.sort((a, b) => {
|
||||||
|
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
|
||||||
|
const runNextBatch = async (startPriority: number, runRulesAmount: number): Promise<void> => {
|
||||||
|
const nextBatch = getNextParallelBatch(startPriority);
|
||||||
|
runRulesAmount = runRulesAmount + nextBatch.length;
|
||||||
|
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!'
|
||||||
|
);
|
||||||
|
|
||||||
|
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';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
if (finalOutcomeOfBatch === 'stop') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (runRulesAmount < this.rules.length) {
|
||||||
|
await runNextBatch(startPriority + 1, runRulesAmount);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
await runNextBatch(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
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