Skip to content

This project is a rule engine that uses AST , this project consists of three apis

Notifications You must be signed in to change notification settings

pavankalyan767/rule_engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rule Engine Project

Overview

The Rule Engine is a powerful Django-based application that enables users to create, combine, and evaluate complex business rules through a REST API. It features a tree-based rule parsing system, user-defined functions, and flexible rule evaluation.

Repository Clone

git clone https://github.com/pavankalyan767/rule_engine.git
cd rule_engine

Prerequisites

  • Python 3.8 or higher
  • Django 4.0 or higher
  • pip (Python package manager)

Installation & Setup

  1. Create and activate virtual environment:
# Windows
python -m venv venv
venv\Scripts\activate
# Linux/Mac
python3 -m venv venv
source venv/bin/activate
  1. Install dependencies:

make sure you are inside rule_engine directory use cd rule_engine to change to it and then execute this command

pip install -r requirements.txt
  1. Run migrations:
python manage.py makemigrations
python manage.py migrate
  1. Start development server:
python manage.py runserver

Running Tests

The project includes comprehensive test suites for all components.

Running Tests

In order to run the tests, make sure your current working directory is:

your_username/rule_engine/
  1. Run all tests:
cd rule_engine
cd rules
```bash
cd tests
python run_tests.py

Project Structure

rule_engine/
│
├── rules/
│   ├── __init__.py
│   ├── models.py          # Database models
│   ├── views.py           # API views
│   ├── serializers.py     # DRF serializers
│   ├── parser.py          # Rule parsing logic
│   ├── evaluator.py       # Rule evaluation logic
│   └── tests/
│       ├── __init__.py
│       ├── combine_tets.py
│       ├── evaluate_tests.py
│       └── evaluate_tests.py
|       └──run_tests.py
|  
│
├── rule_engine/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
│
└── requirements.txt

API Documentation

1. Create Rule API

Create and parse business rules into a tree structure.

Endpoint

  • URL: POST /create-rule/
  • Content-Type: application/json

Request Format

{
    "rule_string": "(age > 18 AND income >= 50000) OR (department == \"IT\" AND spend < 1000)"

}

Response Format

{
    "status": "success",
    "parsed_rule": {
        "operator": "OR",
        "left": {
            "operator": "AND",
            "left": {
                "operator": ">",
                "left": {
                    "name": "age"
                },
                "right": {
                    "value": 18
                }
            },
            "right": {
                "operator": ">=",
                "left": {
                    "name": "income"
                },
                "right": {
                    "value": 50000
                }
            }
        },
        "right": {
            "operator": "AND",
            "left": {
                "operator": "==",
                "left": {
                    "name": "department"
                },
                "right": {
                    "name": "IT"
                }
            },
            "right": {
                "operator": "<",
                "left": {
                    "name": "spend"
                },
                "right": {
                    "value": 1000
                }
            }
        }
    },
    "saved_rule_id": 35,
    "saved_rule_string": "(age > 18 AND income >= 50000) OR (department == \"IT\" AND spend < 1000)",
    "created_at": "2024-10-25T14:30:00Z",
    "version": 1
}

2. Combine Rules API

Combine multiple existing rules into a composite rule. Uses the most frequent operator heuristic to determine which operator to use for combining the rules

Endpoint

  • URL: POST /combine-rules/
  • Content-Type: application/json

Request Format

{
    "rule_ids": [1, 2, 3]
    
}

Response Format

{
    "status": "success",
    "combined_ast": {
        "operator": "AND",
        "left": {
            "operator": "AND",
            "left": {
                "operator": ">",
                "left": {
                    "name": "age"
                },
                "right": {
                    "value": 30
                }
            },
            "right": {
                "operator": "AND",
                "left": {
                    "operator": ">",
                    "left": {
                        "name": "age"
                    },
                    "right": {
                        "value": 30
                    }
                },
                "right": {
                    "operator": "==",
                    "left": {
                        "name": "department"
                    },
                    "right": {
                        "value": "Sales"
                    }
                }
            }
        },
        "right": {
            "name": "COMBINED_RULE_STRING"
        }
    },
    "saved_combined_rule_id": 40,
    "saved_combined_rule_string": "COMBINED_RULE_STRING",
    "created_at": "2024-10-25T14:35:00Z"
}

3. Evaluate Rule API

Evaluate rules against provided data.

Endpoint

  • URL: POST /evaluate-rule/
  • Content-Type: application/json

Request Format

{
    "user_data": {
        "age": 22,
        "income": 70000,
        "department": "Engineering",
        "spend": 500
    },
    "rule_string": "(department == 'Engineering' AND spend < 600) OR (age >= 20 AND income >= 50000)"
}

Response Format

{
    "evaluation_result": "true",
    
}

4. User Defined Functions (UDFs)

4.1 Create UDF

Create a new user-defined function.

Endpoint
  • URL: POST /create-udf/
  • Content-Type: application/json
Request Format
    {
    "name": "discount_eligibility",
    "definition": "def discount_eligibility(age, spend):\n    return age > 25 and spend > 1000"
}

4.2 List UDFs

Get all available UDFs.

Endpoint
  • URL: GET /list-udfs/
Response Format
{
    "name": "discount_eligibility",
    "definition": "def discount_eligibility(age, spend):\n    return age > 25 and spend > 1000"
}

4.3 Update UDF

Modify an existing UDF.

Endpoint
  • URL: PUT /update-udf/
  • Content-Type: application/json
Request Format

Enter name in the first section ex: my_udf without quotes

{
    
    "definition": "return x + 20;"
}
Response Format
{
    
    "message": "UDF updated successfully",
    
}

5 Modify Rule

{"rule_id":1,"new_rule_string":"department=Sales"}

Result

Updated Sucessfully

Error Handling

All APIs return appropriate HTTP status codes and error messages:

{
    "status": "error",
    "error_code": "ERROR_CODE",
    "message": "Human readable error message",
    "details": "Detailed error information"
}

Common Status Codes:

  • 200: Success
  • 400: Bad Request
  • 404: Not Found
  • 500: Internal Server Error

Contributing

  1. Fork the repository
  2. Create a feature branch
git checkout -b feature/your-feature-name
  1. Make your changes and commit
git add .
git commit -m "Add your commit message"
  1. Push to your fork
git push origin feature/your-feature-name
  1. Create a Pull Request

Testing Guidelines

  1. Write tests for new features
  2. Ensure all tests pass before submitting PR
  3. Maintain test coverage above 80%
  4. Follow test naming convention: test_[feature_name]_[scenario]

About

This project is a rule engine that uses AST , this project consists of three apis

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published