/Rule-Engine-Using-AST

A simple 3-tier rule engine application to determine user eligibility based on attributes like age, department, income, spend by using the AST

Primary LanguageHTML

Rule-Engine-Using-AST

Hosted link: https://app1-rule-engine-with-ast.onrender.com/

Overview

This application is a rule engine that determines user eligibility based on attributes such as age, department, salary, and experience. It uses an Abstract Syntax Tree (AST) to represent and manage conditional rules, allowing for dynamic rule creation, combination, and evaluation.

image

Features

  • Create Rules: Define rules using a string format that gets converted into an AST.

    image
  • Combine Rules: Combine multiple rules into a single AST for more complex evaluations.

    image
  • Evaluate Rules: Check if the given data meets the criteria defined by the AST.

    image
  • Tree Visualization: Define or Combine Rule would should show Tree Representation.

Tech Stack

  • Backend: Node.js, Express.js
  • Database: MongoDB

Getting Started

Prerequisites

  • Node.js and npm installed
  • MongoDB installed and running

Installation

  1. Clone the Repository

    git clone "https://github.com/D-D-Roshan/Rule-Engine-Using-AST.git"
    cd rule-engine
  2. Install Backend Dependencies

    npm install
  3. Start MongoDB

    Ensure that MongoDB is running on your local machine:

    mongod
  4. Start the Backend Server

    nodemon server.js
  5. Edit the Mongodb URI

      mongodb+srv://db_name:<db_password>@ast.cscvg.mongodb.net/?retryWrites=true&w=majority&appName=AST
      db_name with your db name
      db_password with your db password

API Endpoints

  1. Create a Rule
    • Endpoint: /api/create_rule

    • Method: POST

    • Body:

      {
        "ruleString": "((age > 30 AND department = 'Sales') OR (age < 25 AND department = 'Marketing')) AND (salary > 50000 OR experience > 5)",
        "ruleName": "Rule 1"
      }

use appropriate spaces in Rules for correct results.

Rule should be in follow format: variable operator value

  • Response:

    {
      "_id": "605c72ef1f4e3a001f4d2e9a",
      "rule_name": "Rule1",
      "rule_ast": { ... }
    }
  1. Combine Rules

    • Endpoint: /api/rules/combine_rules

    • Method: POST

    • Body:

      {
        "ruleIds": ["605c72ef1f4e3a001f4d2e9a", "605c730f1f4e3a001f4d2e9b"]
        "operators: op
      }
    • Response:

      {
        "type": "operator",
        "value": operator,
        "left": { ... },
        "right": { ... }
      }
  2. Evaluate a Rule

    • Endpoint: /api/rules/evaluate_rule

    • Method: POST

    • Body:

      {
        "rule": { ... },
        "data": {
          "age": 35,
          "department": "Sales",
          "salary": 60000,
          "experience": 3
        }
      }
    • Response:

      {
        "result": true
      }

Running Tests

You can add and run tests to ensure everything is working correctly.

created bt: Roshan