Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 376a8c3113 | |||
| fcd50a780a | |||
| 875c06f86c | |||
| 0a8e4a3517 | |||
| 8b9dec976c | |||
| 1493848dc8 | |||
| 17287a9ba3 | |||
| 11921a2864 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -15,8 +15,6 @@ node_modules/
|
|||||||
|
|
||||||
# builds
|
# builds
|
||||||
dist/
|
dist/
|
||||||
dist_web/
|
dist_*/
|
||||||
dist_serve/
|
|
||||||
dist_ts_web/
|
|
||||||
|
|
||||||
# custom
|
# custom
|
||||||
@@ -12,29 +12,35 @@ stages:
|
|||||||
- release
|
- release
|
||||||
- metadata
|
- metadata
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- npm install -g @shipzone/npmci
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# security stage
|
# security stage
|
||||||
# ====================
|
# ====================
|
||||||
mirror:
|
auditProductionDependencies:
|
||||||
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci git mirror
|
- npmci npm prepare
|
||||||
|
- npmci command npm install --production --ignore-scripts
|
||||||
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=prod --production
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
snyk:
|
auditDevDependencies:
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:snyk
|
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
||||||
stage: security
|
stage: security
|
||||||
script:
|
script:
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci command npm install --ignore-scripts
|
- npmci command npm install --ignore-scripts
|
||||||
- npmci command snyk test
|
- npmci command npm config set registry https://registry.npmjs.org
|
||||||
|
- npmci command npm audit --audit-level=high --only=dev
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
allow_failure: true
|
||||||
|
|
||||||
# ====================
|
# ====================
|
||||||
# test stage
|
# test stage
|
||||||
@@ -49,9 +55,7 @@ testStable:
|
|||||||
- npmci npm test
|
- npmci npm test
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- priv
|
|
||||||
|
|
||||||
testBuild:
|
testBuild:
|
||||||
stage: test
|
stage: test
|
||||||
@@ -62,9 +66,7 @@ testBuild:
|
|||||||
- npmci command npm run build
|
- npmci command npm run build
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
coverage: /\d+.?\d+?\%\s*coverage/
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
|
||||||
- docker
|
- docker
|
||||||
- notpriv
|
|
||||||
|
|
||||||
release:
|
release:
|
||||||
stage: release
|
stage: release
|
||||||
@@ -84,11 +86,12 @@ release:
|
|||||||
codequality:
|
codequality:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
allow_failure: true
|
allow_failure: true
|
||||||
|
only:
|
||||||
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
@@ -108,11 +111,10 @@ trigger:
|
|||||||
pages:
|
pages:
|
||||||
stage: metadata
|
stage: metadata
|
||||||
script:
|
script:
|
||||||
- npmci node install lts
|
- npmci node install stable
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command tsdoc
|
- npmci command npm run buildDocs
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
|||||||
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@@ -2,28 +2,10 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "current file",
|
"command": "npm test",
|
||||||
"type": "node",
|
"name": "Run npm test",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"args": [
|
"type": "node-terminal"
|
||||||
"${relativeFile}"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "test.ts",
|
|
||||||
"type": "node",
|
|
||||||
"request": "launch",
|
|
||||||
"args": [
|
|
||||||
"test/test.ts"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
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", "wcc"]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
dist_ts/index.d.ts
vendored
Normal file
1
dist_ts/index.d.ts
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
export * from './smartrule.classes.smartrule.js';
|
||||||
2
dist_ts/index.js
Normal file
2
dist_ts/index.js
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
export * from './smartrule.classes.smartrule.js';
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxjQUFjLGtDQUFrQyxDQUFDIn0=
|
||||||
11
dist_ts/smartrule.classes.rule.d.ts
vendored
Normal file
11
dist_ts/smartrule.classes.rule.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import { SmartRule } from './smartrule.classes.smartrule.js';
|
||||||
|
export declare type TTreeActionResult = 'continue' | 'apply-continue' | 'apply-stop' | 'stop';
|
||||||
|
export declare type TActionFunc<T = any> = (objectArg: T) => Promise<any>;
|
||||||
|
export declare type TCheckFunc<T> = (objectArg: T) => Promise<TTreeActionResult>;
|
||||||
|
export declare class Rule<T> {
|
||||||
|
smartRuleRef: SmartRule<T>;
|
||||||
|
priority: number;
|
||||||
|
checkFunction: TCheckFunc<T>;
|
||||||
|
actionFunction: TActionFunc;
|
||||||
|
constructor(smartRuleRef: SmartRule<T>, priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc);
|
||||||
|
}
|
||||||
11
dist_ts/smartrule.classes.rule.js
Normal file
11
dist_ts/smartrule.classes.rule.js
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
import './smartrule.plugins.js';
|
||||||
|
import './smartrule.classes.smartrule.js';
|
||||||
|
export class Rule {
|
||||||
|
constructor(smartRuleRef, priorityArg, checkFunctionArg, actionFunctionArg) {
|
||||||
|
this.smartRuleRef = smartRuleRef;
|
||||||
|
this.priority = priorityArg;
|
||||||
|
this.checkFunction = checkFunctionArg;
|
||||||
|
this.actionFunction = actionFunctionArg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRydWxlLmNsYXNzZXMucnVsZS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL3NtYXJ0cnVsZS5jbGFzc2VzLnJ1bGUudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBeUIsd0JBQXdCLENBQUM7QUFDbEQsT0FBMEIsa0NBQWtDLENBQUM7QUFPN0QsTUFBTSxPQUFPLElBQUk7SUFNZixZQUNFLFlBQTBCLEVBQzFCLFdBQW1CLEVBQ25CLGdCQUErQixFQUMvQixpQkFBOEI7UUFFOUIsSUFBSSxDQUFDLFlBQVksR0FBRyxZQUFZLENBQUM7UUFDakMsSUFBSSxDQUFDLFFBQVEsR0FBRyxXQUFXLENBQUM7UUFDNUIsSUFBSSxDQUFDLGFBQWEsR0FBRyxnQkFBZ0IsQ0FBQztRQUN0QyxJQUFJLENBQUMsY0FBYyxHQUFHLGlCQUFpQixDQUFDO0lBQzFDLENBQUM7Q0FDRiJ9
|
||||||
10
dist_ts/smartrule.classes.smartrule.d.ts
vendored
Normal file
10
dist_ts/smartrule.classes.smartrule.d.ts
vendored
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import { Rule, TCheckFunc, TActionFunc } from './smartrule.classes.rule.js';
|
||||||
|
export declare class SmartRule<T> {
|
||||||
|
rules: Array<Rule<T>>;
|
||||||
|
/**
|
||||||
|
* makes a decision based on the given obect and the given rules
|
||||||
|
* @param objectArg
|
||||||
|
*/
|
||||||
|
makeDecision(objectArg: T): Promise<void>;
|
||||||
|
createRule(priorityArg: number, checkFunctionArg: TCheckFunc<T>, actionFunctionArg: TActionFunc): void;
|
||||||
|
}
|
||||||
69
dist_ts/smartrule.classes.smartrule.js
Normal file
69
dist_ts/smartrule.classes.smartrule.js
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
import './smartrule.plugins.js';
|
||||||
|
import { Rule } from './smartrule.classes.rule.js';
|
||||||
|
export class SmartRule {
|
||||||
|
constructor() {
|
||||||
|
this.rules = [];
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* makes a decision based on the given obect and the given rules
|
||||||
|
* @param objectArg
|
||||||
|
*/
|
||||||
|
async makeDecision(objectArg) {
|
||||||
|
// 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) => {
|
||||||
|
return this.rules.filter(rule => {
|
||||||
|
return rule.priority === priorityStart;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
// lets run the checks
|
||||||
|
const runNextBatch = async (startPriority, runRulesAmount) => {
|
||||||
|
const nextBatch = getNextParallelBatch(startPriority);
|
||||||
|
runRulesAmount = runRulesAmount + nextBatch.length;
|
||||||
|
const outcomes = [];
|
||||||
|
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 = 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);
|
||||||
|
}
|
||||||
|
createRule(priorityArg, checkFunctionArg, actionFunctionArg) {
|
||||||
|
const rule = new Rule(this, priorityArg, checkFunctionArg, actionFunctionArg);
|
||||||
|
this.rules.push(rule);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRydWxlLmNsYXNzZXMuc21hcnRydWxlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvc21hcnRydWxlLmNsYXNzZXMuc21hcnRydWxlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQXlCLHdCQUF3QixDQUFDO0FBQ2xELE9BQU8sRUFBRSxJQUFJLEVBQThDLE1BQU0sNkJBQTZCLENBQUM7QUFFL0YsTUFBTSxPQUFPLFNBQVM7SUFBdEI7UUFDUyxVQUFLLEdBQW1CLEVBQUUsQ0FBQztJQTJFcEMsQ0FBQztJQXpFQzs7O09BR0c7SUFDSSxLQUFLLENBQUMsWUFBWSxDQUFDLFNBQVk7UUFDcEMsc0JBQXNCO1FBQ3RCLElBQUksQ0FBQyxLQUFLLEdBQUcsSUFBSSxDQUFDLEtBQUssQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDLEVBQUUsQ0FBQyxFQUFFLEVBQUU7WUFDcEMsSUFBSSxDQUFDLENBQUMsUUFBUSxHQUFHLENBQUMsQ0FBQyxRQUFRLEVBQUU7Z0JBQzNCLE9BQU8sQ0FBQyxDQUFDO2FBQ1Y7aUJBQU07Z0JBQ0wsT0FBTyxDQUFDLENBQUM7YUFDVjtRQUNILENBQU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+1,2 @@
|
|||||||
|
declare const removeme: {};
|
||||||
|
export { removeme };
|
||||||
3
dist_ts/smartrule.plugins.js
Normal file
3
dist_ts/smartrule.plugins.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
const removeme = {};
|
||||||
|
export { removeme };
|
||||||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRydWxlLnBsdWdpbnMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9zbWFydHJ1bGUucGx1Z2lucy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxNQUFNLFFBQVEsR0FBRyxFQUFFLENBQUM7QUFDcEIsT0FBTyxFQUFFLFFBQVEsRUFBRSxDQUFDIn0=
|
||||||
@@ -1,10 +1,11 @@
|
|||||||
{
|
{
|
||||||
"gitzone": {
|
"gitzone": {
|
||||||
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "gitlab.com",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "smartrule",
|
"gitrepo": "smartrule",
|
||||||
"shortDescription": "a smart rule library for handling decision trees.",
|
"description": "a smart rule library for handling decision trees.",
|
||||||
"npmPackagename": "@pushrocks/smartrule",
|
"npmPackagename": "@pushrocks/smartrule",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"projectDomain": "push.rocks"
|
"projectDomain": "push.rocks"
|
||||||
|
|||||||
13547
package-lock.json
generated
13547
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
29
package.json
29
package.json
@@ -1,35 +1,38 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/smartrule",
|
"name": "@pushrocks/smartrule",
|
||||||
"version": "1.0.8",
|
"version": "2.0.1",
|
||||||
"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_ts/index.js",
|
||||||
"typings": "dist/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/)",
|
"test": "(tstest test/)",
|
||||||
"build": "(tsbuild)",
|
"build": "(tsbuild --web)",
|
||||||
"format": "(gitzone format)"
|
"format": "(gitzone format)",
|
||||||
|
"buildDocs": "tsdoc"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.0.22",
|
"@gitzone/tsbuild": "^2.1.65",
|
||||||
"@gitzone/tstest": "^1.0.15",
|
"@gitzone/tstest": "^1.0.73",
|
||||||
"@pushrocks/tapbundle": "^3.0.7",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^10.11.7",
|
"@types/node": "^18.6.4"
|
||||||
"tslint": "^5.11.0",
|
|
||||||
"tslint-config-prettier": "^1.15.0"
|
|
||||||
},
|
},
|
||||||
"dependencies": {},
|
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
"ts_web/**/*",
|
"ts_web/**/*",
|
||||||
"dist/**/*",
|
"dist/**/*",
|
||||||
"dist_web/**/*",
|
"dist_*/**/*",
|
||||||
|
"dist_ts/**/*",
|
||||||
"dist_ts_web/**/*",
|
"dist_ts_web/**/*",
|
||||||
"assets/**/*",
|
"assets/**/*",
|
||||||
"cli.js",
|
"cli.js",
|
||||||
"npmextra.json",
|
"npmextra.json",
|
||||||
"readme.md"
|
"readme.md"
|
||||||
|
],
|
||||||
|
"browserslist": [
|
||||||
|
"last 1 chrome versions"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
21
readme.md
21
readme.md
@@ -8,13 +8,20 @@ a smart rule library for handling decision trees.
|
|||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/smartrule/)
|
* [docs (typedoc)](https://pushrocks.gitlab.io/smartrule/)
|
||||||
|
|
||||||
## Status for master
|
## Status for master
|
||||||
[](https://gitlab.com/pushrocks/smartrule/commits/master)
|
|
||||||
[](https://gitlab.com/pushrocks/smartrule/commits/master)
|
Status Category | Status Badge
|
||||||
[](https://www.npmjs.com/package/@pushrocks/smartrule)
|
-- | --
|
||||||
[](https://snyk.io/test/npm/@pushrocks/smartrule)
|
GitLab Pipelines | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
||||||
[](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
npm | [](https://lossless.cloud)
|
||||||
[](https://prettier.io/)
|
Snyk | [](https://lossless.cloud)
|
||||||
|
TypeScript Support | [](https://lossless.cloud)
|
||||||
|
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
||||||
|
Code Style | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
||||||
|
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
||||||
|
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
||||||
|
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
|
|||||||
10
test/test.ts
10
test/test.ts
@@ -1,5 +1,5 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
import { expect, tap } from '@pushrocks/tapbundle';
|
||||||
import * as smartrule from '../ts/index';
|
import * as smartrule from '../ts/index.js';
|
||||||
|
|
||||||
interface ITestMessage {
|
interface ITestMessage {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -12,12 +12,12 @@ tap.test('first test', async () => {
|
|||||||
testSmartruleInstance = new smartrule.SmartRule<ITestMessage>();
|
testSmartruleInstance = new smartrule.SmartRule<ITestMessage>();
|
||||||
testSmartruleInstance.createRule(
|
testSmartruleInstance.createRule(
|
||||||
2,
|
2,
|
||||||
async messageArg => {
|
async (messageArg) => {
|
||||||
if (messageArg.body.startsWith('hello')) {
|
if (messageArg.body.startsWith('hello')) {
|
||||||
return 'apply-stop';
|
return 'apply-stop';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async messageArg => {
|
async (messageArg) => {
|
||||||
console.log(`rule triggered for message with body ${messageArg.body}`);
|
console.log(`rule triggered for message with body ${messageArg.body}`);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -26,8 +26,8 @@ tap.test('first test', async () => {
|
|||||||
tap.test('make a decision based on an object', async () => {
|
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();
|
||||||
|
|||||||
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@pushrocks/smartrule',
|
||||||
|
version: '2.0.1',
|
||||||
|
description: 'a smart rule library for handling decision trees.'
|
||||||
|
}
|
||||||
@@ -1 +1 @@
|
|||||||
export * from './smartrule.classes.smartrule';
|
export * from './smartrule.classes.smartrule.js';
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
import * as plugins from './smartrule.plugins';
|
import * as plugins from './smartrule.plugins.js';
|
||||||
import { SmartRule } from './smartrule.classes.smartrule';
|
import { SmartRule } from './smartrule.classes.smartrule.js';
|
||||||
|
|
||||||
export type TTreeActionResult = 'continue' | 'apply-continue' | 'apply-stop' | 'stop';
|
export type TTreeActionResult = 'continue' | 'apply-continue' | 'apply-stop' | 'stop';
|
||||||
export type TActionFunc = (objectArg) => Promise<any>;
|
export type TActionFunc<T = any> = (objectArg: T) => Promise<any>;
|
||||||
|
|
||||||
export type TCheckFunc<T> = (objectArg: T) => Promise<TTreeActionResult>;
|
export type TCheckFunc<T> = (objectArg: T) => Promise<TTreeActionResult>;
|
||||||
|
|
||||||
@@ -12,7 +12,12 @@ 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;
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import * as plugins from './smartrule.plugins';
|
import * as plugins from './smartrule.plugins.js';
|
||||||
import { Rule, TCheckFunc, TActionFunc, TTreeActionResult } from './smartrule.classes.rule';
|
import { Rule, TCheckFunc, TActionFunc, TTreeActionResult } from './smartrule.classes.rule.js';
|
||||||
|
|
||||||
export class SmartRule<T> {
|
export class SmartRule<T> {
|
||||||
public rules: Array<Rule<T>> = [];
|
public rules: Array<Rule<T>> = [];
|
||||||
@@ -10,7 +10,7 @@ export class SmartRule<T> {
|
|||||||
*/
|
*/
|
||||||
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 {
|
||||||
@@ -20,7 +20,7 @@ export class SmartRule<T> {
|
|||||||
|
|
||||||
// gets the next batch with the same priority
|
// gets the next batch with the same priority
|
||||||
const getNextParallelBatch = (priorityStart: number) => {
|
const getNextParallelBatch = (priorityStart: number) => {
|
||||||
return this.rules.filter(rule => {
|
return this.rules.filter((rule) => {
|
||||||
return rule.priority === priorityStart;
|
return rule.priority === priorityStart;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
@@ -32,36 +32,48 @@ export class SmartRule<T> {
|
|||||||
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
|
||||||
|
? 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
|
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 (runRulesAmount < this.rules.length) {
|
if (runRulesAmount < this.rules.length) {
|
||||||
await runNextBatch((startPriority + 1), runRulesAmount);
|
await runNextBatch(startPriority + 1, runRulesAmount);
|
||||||
} else {
|
} else {
|
||||||
return;
|
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);
|
const rule = new Rule<T>(this, priorityArg, checkFunctionArg, actionFunctionArg);
|
||||||
this.rules.push(rule);
|
this.rules.push(rule);
|
||||||
}
|
}
|
||||||
|
|||||||
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
||||||
17
tslint.json
17
tslint.json
@@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user