Skip to content

TinkerWizard/Rule-Engine-With-AST

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rule Engine With AST

Overview

The Rule Engine with Abstract Syntax Tree (AST) is a simple 3-tier application designed to determine user eligibility based on various attributes such as age, department, income, and spend. The system utilizes AST to represent conditional rules and allows for dynamic creation, combination, and modification of these rules.

Features

  • Rule Creation and Modification: Define rules using a simple string format and edit existing rules.
  • Combine Multiple Rules: Combine rules into a single rule.
  • Rule Evaluation: Evaluate rules against JSON data.
  • API Endpoints: Expose functionality through RESTful APIs.

Technology Stack and Features

  • FastAPI for the Python backend API.
    • 🧰 SQLModel for the Python SQL database interactions (ORM).
    • 🔍 Pydantic, used by FastAPI, for the data validation and settings management.
    • 💾 PostgreSQL as the SQL database.
  • 🚀 React for the frontend.
    • 💻 Using TypeScript, hooks, Vite, and other parts of a modern frontend stack.
    • 🎨 Material UI for the frontend components.

Setup

1.Clone the Repository

git clone https://github.com/TinkerWizard/Rule-Engine-With-AST.git
cd Rule-Engine-With-AST

2.Install Dependencies

cd backend
pip install -r requirements.txt
cd frontend
npm install

3.Configure the Database

Create and Update the .env file with your database credentials:

DB_USER=your_username
DB_PASSWORD=your_password

Ensure that the database and rules table exist. You may need to create the table manually if it does not exist:

CREATE TABLE rules (
    id INT AUTO_INCREMENT PRIMARY KEY,
    rule_string TEXT NOT NULL
);

4.Run the Application

uvicorn main:app --reload
npm run dev

API Endpoints

Create / Modify Rule

  • URL: /save

  • Method: POST

  • Description: This endpoint allows users to create a new rule by providing a rule string and generates the AST. Also, we can modify the exisiting rules. We fetch those rules from the database. Create and Modify Invalid rule

  • Request Body:

    {
      "rule_string": "((age > 30 AND department = 'Sales') OR (age < 25 AND department = 'Marketing')) AND (salary > 50000 OR experience > 5)"
    }
  • Response(AST):

    {
      "ast": {
        "type": "operator",
        "value": "AND",
        "left": {
          "type": "operator",
          "value": "OR",
          "left": {
            "type": "operator",
            "value": "AND",
            "left": {
              "type": "operand",
              "value": "age > 30",
              "left": null,
              "right": null
            },
            "right": {
              "type": "operand",
              "value": "department = 'Sales'",
              "left": null,
              "right": null
            }
          },
          "right": {
            "type": "operator",
            "value": "AND",
            "left": {
              "type": "operand",
              "value": "age < 25",
              "left": null,
              "right": null
            },
            "right": {
              "type": "operand",
              "value": "department = 'Marketing'",
              "left": null,
              "right": null
            }
          }
        },
        "right": {
          "type": "operator",
          "value": "OR",
          "left": {
            "type": "operand",
            "value": "salary > 50000",
            "left": null,
            "right": null
          },
          "right": {
            "type": "operand",
            "value": "experience > 5",
            "left": null,
            "right": null
          }
        }
      }
    }

Combine Rules

  • URL: /combine

  • Method: POST

  • Description: This endpoint allows users to create a combine rules by providing a multiple rule string and generates the AST. Combine rules

  • Request Body:

    {
      "rule_1": "(age > 30 AND department = 'Sales')",
      "rule_2": "(age < 25 AND department = 'Marketing')",
    }
  • Response(AST):

    {
      "ast": {
        "type": "operator",
        "value": "AND",
        "left": {
          "type": "operator",
          "value": "AND",
          "left": {
            "type": "operand",
            "value": "age > 30",
            "left": null,
            "right": null
          },
          "right": {
            "type": "operand",
            "value": "department = 'Sales'",
            "left": null,
            "right": null
          }
        },
        "right": {
          "type": "operator",
          "value": "AND",
          "left": {
            "type": "operand",
            "value": "age < 25",
            "left": null,
            "right": null
          },
          "right": {
            "type": "operand",
            "value": "department = 'Marketing'",
            "left": null,
            "right": null
          }
        }
      }
    }

Evaluate Rule

  • URL: /evaluate

  • Method: POST

    • Description: This endpoint allows users to evaluate a rule by providing a multiple rule string and it returns either true or false. Evaluate rules
  • Request Body:

    {
      "rule_string": "age > 30 AND salary > 50000",
      "data": {
        "age": 35,
        "salary": 60000
      }
    }
  • Response(boolean):

    {
      "result": true
    }

Performance

  • Performance: Can handle multiple rules without a hassle.
  • Error Handling Invalid rules are not processed and not saved in the database.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published