Skip to content

A dynamic Rule Engine built with Abstract Syntax Tree (AST) for creating, evaluating, and combining complex business rules through an intuitive web interface. Built with React and FastAPI, featuring real-time validation, rule combination, and MongoDB for data storage.

Notifications You must be signed in to change notification settings

AryanBV/rule_engine

Repository files navigation

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:

  • Complex rule creation and evaluation
  • Dynamic rule combinations
  • Real-time data assessment
  • Easy rule management through web interface

✨ Features Implemented

Core Features

  • βœ… Dynamic rule creation and modification
  • βœ… Real-time rule evaluation
  • βœ… Rule combination using AST
  • βœ… MongoDB data persistence
  • βœ… Modern React UI

Enhanced Features

  • βœ… Visual Rule Builder with interactive components
  • βœ… Real-time rule validation
  • βœ… Performance analytics and metrics
  • βœ… Rule templates system
  • βœ… Advanced error handling with detailed feedback

Technical Features

  • βœ… AST implementation
  • βœ… Efficient rule traversal
  • βœ… RESTful API design
  • βœ… Error handling
  • βœ… Input validation
  • βœ… Component-based architecture
  • βœ… Modular code structure
  • βœ… Real-time validation service
  • βœ… Analytics tracking
  • βœ… Advanced UI components

πŸ—οΈ 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

  • Frontend: React, Tailwind CSS, Lucide Icons
  • Backend: FastAPI, Python 3.7+
  • Database: MongoDB
  • Validation: Pydantic
  • Testing: Pytest

πŸš€ 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
  1. Create virtual environment:
python -m venv venv
source venv/bin/activate  # Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure MongoDB:
# Create .env file with:
MONGODB_URL=mongodb://localhost:27017/rule_engine
  1. Start server:
uvicorn app.main:app --reload
  1. 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

  • βœ… Core engine implementation
  • βœ… AST evaluation logic
  • βœ… MongoDB integration
  • βœ… CRUD operations
  • βœ… Modern UI
  • βœ… Enhanced validation
  • βœ… Visual rule builder
  • βœ… Performance analytics
  • βœ… Component architecture
  • βœ… Real-time feedback

In Development

  • πŸ”„ Enhanced error handling
  • πŸ”„ UI/UX improvements
  • πŸ”„ Performance optimizations

Planned

  • πŸ“‹ User authentication
  • πŸ“‹ Rule versioning
  • πŸ“‹ Advanced visualizations
  • πŸ“‹ Batch evaluations

πŸ“š Documentation

  • API Docs: http://localhost:8000/docs
  • Components: app/static/js/components/
  • Core Logic: app/rule_engine.py

🀝 Contributing

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

Test Coverage

  • Unit tests
  • API tests
  • Integration tests
  • Validation tests
  • Component tests

About

A dynamic Rule Engine built with Abstract Syntax Tree (AST) for creating, evaluating, and combining complex business rules through an intuitive web interface. Built with React and FastAPI, featuring real-time validation, rule combination, and MongoDB for data storage.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published