A library for creating and managing decision trees with smart rules.
Go to file
2024-04-14 18:14:46 +02:00
.vscode fix(core): update 2022-08-07 11:19:43 +02:00
dist_ts BREAKING CHANGE(core): switch to esm 2022-08-07 11:18:02 +02:00
test fix(core): update 2022-08-07 11:19:43 +02:00
ts fix(core): update 2022-08-07 11:19:43 +02:00
.gitignore fix(core): update 2022-08-07 11:19:43 +02:00
.gitlab-ci.yml fix(core): update 2022-08-07 11:19:43 +02:00
license fix(core): update 2020-01-20 08:47:35 +00:00
npmextra.json update tsconfig 2024-04-14 18:14:46 +02:00
package-lock.json 2.0.1 2022-08-07 11:19:43 +02:00
package.json update tsconfig 2024-04-14 18:14:46 +02:00
pnpm-lock.yaml switch to new org scheme 2023-07-11 01:30:23 +02:00
readme.hints.md update tsconfig 2024-04-14 18:14:46 +02:00
readme.md update tsconfig 2024-04-14 18:14:46 +02:00
tsconfig.json update tsconfig 2024-04-01 21:40:36 +02:00

@push.rocks/smartrule

a smart rule library for handling decision trees.

Install

To install @push.rocks/smartrule, use the following command with npm:

npm install @push.rocks/smartrule --save

Or if you prefer using Yarn:

yarn add @push.rocks/smartrule

Ensure you have TypeScript and necessary typings installed in your project. If you haven't, you can add TypeScript and the types for Node.js by running:

npm install typescript @types/node --save-dev

Usage

@push.rocks/smartrule is designed to simplify handling decision trees in your application, allowing you to manage complex business logic based on a set of predefined rules. Below we will walk through using this library to create and manage a simple set of decision rules programmatically.

Getting Started

First, ensure that you import SmartRule & define interfaces or types that you plan to use with it if necessary:

import { SmartRule } from '@push.rocks/smartrule';

interface IMessage {
  id: string;
  content: string;
  userType: 'admin' | 'user';
}

Creating a Decision Rule Instance

To start using smart rules, instantiate SmartRule with your data type:

const messageRule = new SmartRule<IMessage>();

Adding Rules

Rules are added by specifying a priority, a check function, and an action function. The check function decides whether the action function should be executed based on the input object. Let's define rules for our IMessage objects:

// Rule 1: Greet Admins
messageRule.createRule(
  1, // priority
  async (message: IMessage) => {
    if (message.userType === 'admin') {
      return 'apply-continue'; // Apply action and continue evaluating
    }
    return 'continue'; // Continue without applying
  },
  async (message: IMessage) => {
    console.log(`Welcome, admin with ID: ${message.id}`);
  }
);

// Rule 2: Delete messages containing forbidden words
messageRule.createRule(
  2, // priority is higher, so this rule gets evaluated first
  async (message: IMessage) => {
    const forbiddenWords = ['forbidden', 'unallowed'];
    const containsForbiddenWord = forbiddenWords.some((word) =>
      message.content.includes(word)
    );
    return containsForbiddenWord ? 'apply-stop' : 'continue'; // Stops evaluating if forbidden word found
  },
  async (message: IMessage) => {
    console.log(`Message with ID: ${message.id} contained a forbidden word and was deleted.`);
    // Logic to delete the message would go here
  }
);

Making Decisions

To evaluate an object against the defined rules, use the makeDecision method:

const testMessage: IMessage = {
  id: '001',
  content: 'This is a welcome message for an admin containing a forbidden word',
  userType: 'admin'
};

messageRule.makeDecision(testMessage).then(() => {
  console.log('Decision making process completed.');
  // Implement further logic as needed
});

Advanced Usage

  • Rule Prioritization: You can manage the order in which rules are evaluated based on their priority. Rules with lower numbers are evaluated first.
  • Decision Flow Control: Through your check function return value (apply-continue, apply-stop, continue, or stop), you can precisely control how the decision-making process progresses after evaluating each rule.
  • Asynchronous Support: Both check and action functions support asynchronous operations, making it easy to integrate with APIs, databases, or other asynchronous data sources in your rules.

Handling Complex Scenarios

@push.rocks/smartrule is especially useful when you have complex decision-making processes in your application. By segmenting the logic into individual rules, you make your code more organized, maintainable, and scalable. You can easily add new rules or modify existing ones without affecting other parts of your decision tree.


By following this guide, you should now have a basic understanding of how to use @push.rocks/smartrule for managing decision trees in TypeScript using ESM syntax. This library provides a powerful yet simple abstraction for creating and managing complex decision-making logic, allowing you to focus on implementing the business rules specific to your application.

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the license file within this repository.

Please note: The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

Company Information

Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.