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.
git clone https://github.com/pavankalyan767/rule_engine.git
cd rule_engine
- Python 3.8 or higher
- Django 4.0 or higher
- pip (Python package manager)
- Create and activate virtual environment:
# Windows
python -m venv venv
venv\Scripts\activate
# Linux/Mac
python3 -m venv venv
source venv/bin/activate
- 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
- Run migrations:
python manage.py makemigrations
python manage.py migrate
- Start development server:
python manage.py runserver
The project includes comprehensive test suites for all components.
In order to run the tests, make sure your current working directory is:
your_username/rule_engine/
- Run all tests:
cd rule_engine
cd rules
```bash
cd tests
python run_tests.py
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
Create and parse business rules into a tree structure.
- URL:
POST /create-rule/
- Content-Type:
application/json
{
"rule_string": "(age > 18 AND income >= 50000) OR (department == \"IT\" AND spend < 1000)"
}
{
"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
}
Combine multiple existing rules into a composite rule. Uses the most frequent operator heuristic to determine which operator to use for combining the rules
- URL:
POST /combine-rules/
- Content-Type:
application/json
{
"rule_ids": [1, 2, 3]
}
{
"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"
}
Evaluate rules against provided data.
- URL:
POST /evaluate-rule/
- Content-Type:
application/json
{
"user_data": {
"age": 22,
"income": 70000,
"department": "Engineering",
"spend": 500
},
"rule_string": "(department == 'Engineering' AND spend < 600) OR (age >= 20 AND income >= 50000)"
}
{
"evaluation_result": "true",
}
Create a new user-defined function.
- URL:
POST /create-udf/
- Content-Type:
application/json
{
"name": "discount_eligibility",
"definition": "def discount_eligibility(age, spend):\n return age > 25 and spend > 1000"
}
Get all available UDFs.
- URL:
GET /list-udfs/
{
"name": "discount_eligibility",
"definition": "def discount_eligibility(age, spend):\n return age > 25 and spend > 1000"
}
Modify an existing UDF.
- URL:
PUT /update-udf/
- Content-Type:
application/json
Enter name in the first section ex: my_udf without quotes
{
"definition": "return x + 20;"
}
{
"message": "UDF updated successfully",
}
{"rule_id":1,"new_rule_string":"department=Sales"}
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
- Fork the repository
- Create a feature branch
git checkout -b feature/your-feature-name
- Make your changes and commit
git add .
git commit -m "Add your commit message"
- Push to your fork
git push origin feature/your-feature-name
- Create a Pull Request
- Write tests for new features
- Ensure all tests pass before submitting PR
- Maintain test coverage above 80%
- Follow test naming convention:
test_[feature_name]_[scenario]