rule_engine

Rule Engine with AST Implementation

A sophisticated rule engine using Abstract Syntax Tree (AST) for complex business rule evaluation. This project is developed as part of the Zeotap Intern Assignment, featuring a modern web interface for rule management and evaluation.

๐Ÿ“‹ Table of Contents

๐ŸŽฏ Overview

A modern 3-tier application that enables:

โœจ Features Implemented

Core Features

Enhanced Features

Technical Features

๐Ÿ—๏ธ Architecture

AST Structure

class Node:
    type: str        # "operator" or "comparison"
    left: Node       # Left child node
    right: Node      # Right child for operators
    field: str       # Field name for comparisons
    operator: str    # Comparison operator
    value: Any       # Comparison value

Project Structure

rule_engine/
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ services/
โ”‚   โ”‚   โ”œโ”€โ”€ rule_validation.py
โ”‚   โ”‚   โ””โ”€โ”€ rule_analytics.py
โ”‚   โ”œโ”€โ”€ static/
โ”‚   โ”‚   โ”œโ”€โ”€ css/
โ”‚   โ”‚   โ””โ”€โ”€ js/
โ”‚   โ”‚       โ””โ”€โ”€ components/
โ”‚   โ”‚           โ”œโ”€โ”€ core/
โ”‚   โ”‚           โ”‚   โ”œโ”€โ”€ RuleEngineUI.jsx
โ”‚   โ”‚           โ”‚   โ””โ”€โ”€ RuleBuilder.jsx
โ”‚   โ”‚           โ”œโ”€โ”€ shared/
โ”‚   โ”‚           โ”‚   โ”œโ”€โ”€ AlertMessage.jsx
โ”‚   โ”‚           โ”‚   โ””โ”€โ”€ LoadingSpinner.jsx
โ”‚   โ”‚           โ””โ”€โ”€ tabs/
โ”‚   โ”‚               โ”œโ”€โ”€ CreateTab.jsx
โ”‚   โ”‚               โ”œโ”€โ”€ ManageTab.jsx
โ”‚   โ”‚               โ”œโ”€โ”€ EvaluateTab.jsx
โ”‚   โ”‚               โ””โ”€โ”€ CombineTab.jsx
โ”‚   โ”œโ”€โ”€ routes/
โ”‚   โ”‚   โ””โ”€โ”€ api.py
โ”‚   โ”œโ”€โ”€ main.py
โ”‚   โ”œโ”€โ”€ database.py
โ”‚   โ””โ”€โ”€ rule_engine.py
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ test_api.py
โ”‚   โ”œโ”€โ”€ test_rule_engine.py
โ”‚   โ””โ”€โ”€ test_rule_validation.py
โ”œโ”€โ”€ requirements.txt
โ””โ”€โ”€ README.md

๐Ÿ› ๏ธ Technology Stack

๐Ÿš€ Setup

Prerequisites

- Python 3.7+
- MongoDB
- Node.js and npm

Installation

  1. Clone repository:
    git clone https://github.com/AryanBV/rule_engine.git
    cd rule_engine
    
  2. Create virtual environment:
    python -m venv venv
    source venv/bin/activate  # Windows: venv\Scripts\activate
    
  3. Install dependencies:
    pip install -r requirements.txt
    
  4. Configure MongoDB:
    # Create .env file with:
    MONGODB_URL=mongodb://localhost:27017/rule_engine
    
  5. Start server:
    uvicorn app.main:app --reload
    
  6. Host name:
    http://localhost:8000/static/index.html
    

๐Ÿ“– Usage

Rule Creation Example

# Simple Rule
age > 30 AND department = 'Sales'

# Complex Rule
((age > 30 AND department = 'Marketing')) AND (salary > 20000 OR experience > 5)

Data Evaluation Example

{
  "age": 35,
  "department": "Sales",
  "salary": 75000,
  "experience": 7
}

๐Ÿ“ก API Reference

Rule Management

POST   /api/rules/                    # Create rule
GET    /api/rules/                    # List rules
GET    /api/rules/{rule_id}           # Get rule
PUT    /api/rules/{rule_id}           # Update rule
DELETE /api/rules/{rule_id}           # Delete rule

Rule Operations

POST   /api/rules/evaluate/{rule_id}  # Evaluate rule
POST   /api/rules/combine             # Combine rules
POST   /api/rules/validate            # Validate rule syntax
GET    /api/rules/{rule_id}/analytics # Get rule analytics

๐Ÿงช Testing

Run tests:

pytest

๐Ÿ“ˆ Current Progress

Completed

In Development

Planned

๐Ÿ“š Documentation

๐Ÿค Contributing

  1. Fork repository
  2. Create feature branch
  3. Commit changes
  4. Push to branch
  5. Submit pull request

Test Coverage