The Rule Engine with Abstract Syntax Tree (AST) is a simple 3-tier application designed to determine user eligibility based on various attributes such as age, department, income, and spend. The system utilizes AST to represent conditional rules and allows for dynamic creation, combination, and modification of these rules.
- Rule Creation and Modification: Define rules using a simple string format and edit existing rules.
- Combine Multiple Rules: Combine rules into a single rule.
- Rule Evaluation: Evaluate rules against JSON data.
- API Endpoints: Expose functionality through RESTful APIs.
- ⚡ FastAPI for the Python backend API.
- 🧰 SQLModel for the Python SQL database interactions (ORM).
- 🔍 Pydantic, used by FastAPI, for the data validation and settings management.
- 💾 PostgreSQL as the SQL database.
- 🚀 React for the frontend.
- 💻 Using TypeScript, hooks, Vite, and other parts of a modern frontend stack.
- 🎨 Material UI for the frontend components.
1.Clone the Repository
git clone https://github.com/TinkerWizard/Rule-Engine-With-AST.git
cd Rule-Engine-With-AST2.Install Dependencies
cd backend
pip install -r requirements.txtcd frontend
npm install3.Configure the Database
Create and Update the .env file with your database credentials:
DB_USER=your_username
DB_PASSWORD=your_password
Ensure that the database and rules table exist. You may need to create the table manually if it does not exist:
CREATE TABLE rules (
id INT AUTO_INCREMENT PRIMARY KEY,
rule_string TEXT NOT NULL
);4.Run the Application
uvicorn main:app --reloadnpm run dev-
URL:
/save -
Method:
POST -
Description: This endpoint allows users to create a new rule by providing a rule string and generates the AST. Also, we can modify the exisiting rules. We fetch those rules from the database.

-
Request Body:
{ "rule_string": "((age > 30 AND department = 'Sales') OR (age < 25 AND department = 'Marketing')) AND (salary > 50000 OR experience > 5)" } -
Response(AST):
{ "ast": { "type": "operator", "value": "AND", "left": { "type": "operator", "value": "OR", "left": { "type": "operator", "value": "AND", "left": { "type": "operand", "value": "age > 30", "left": null, "right": null }, "right": { "type": "operand", "value": "department = 'Sales'", "left": null, "right": null } }, "right": { "type": "operator", "value": "AND", "left": { "type": "operand", "value": "age < 25", "left": null, "right": null }, "right": { "type": "operand", "value": "department = 'Marketing'", "left": null, "right": null } } }, "right": { "type": "operator", "value": "OR", "left": { "type": "operand", "value": "salary > 50000", "left": null, "right": null }, "right": { "type": "operand", "value": "experience > 5", "left": null, "right": null } } } }
-
URL:
/combine -
Method:
POST -
Description: This endpoint allows users to create a combine rules by providing a multiple rule string and generates the AST.

-
Request Body:
{ "rule_1": "(age > 30 AND department = 'Sales')", "rule_2": "(age < 25 AND department = 'Marketing')", } -
Response(AST):
{ "ast": { "type": "operator", "value": "AND", "left": { "type": "operator", "value": "AND", "left": { "type": "operand", "value": "age > 30", "left": null, "right": null }, "right": { "type": "operand", "value": "department = 'Sales'", "left": null, "right": null } }, "right": { "type": "operator", "value": "AND", "left": { "type": "operand", "value": "age < 25", "left": null, "right": null }, "right": { "type": "operand", "value": "department = 'Marketing'", "left": null, "right": null } } } }
-
URL:
/evaluate -
Method:
POST -
Request Body:
{ "rule_string": "age > 30 AND salary > 50000", "data": { "age": 35, "salary": 60000 } } -
Response(boolean):
{ "result": true }
- Performance: Can handle multiple rules without a hassle.
- Error Handling Invalid rules are not processed and not saved in the database.
