This project implements a Rule Engine that uses Abstract Syntax Trees (AST) to evaluate complex rules and conditions based on user-defined attributes. This engine allows for dynamic rule creation, combination, modification, and evaluation for various use cases, such as determining user eligibility based on attributes like age, department, income, and experience. The application follows a 3-tier architecture, comprising a backend API, data storage, and a simple frontend for interaction.
The frontend provides a simple UI to create, view, combine, and evaluate rules.
- Create rules using simple expressions like
js(age > 30 && salary > 50000). - Combine rules logically (e.g., using 'AND' or 'OR').
- Evaluate rules against user data to check eligibility.
- Update rules by modifying existing expressions.
- Validation for valid attributes and handling of invalid rules.
Rules are stored as ASTs, making evaluation and modification efficient.
- The backend uses Mongoose for defining models and interacting with MongoDB.
- Tech stack: MERN stack.
- Error handling both on the server and on the client
- Ensure you have Node.js and npm installed.
- Node.js (v18.12.1)
- Express (v4.x)
- Axios (v1.x)
- Mongoose (v6.x)
- dotenv (v16.x)
- Nodemon (v2.x) (for development)
- React (v18.x)
- Axios (v1.x)
- Supertest (v7.x)
- Jest (v29.x)
Here all the enclosed braces indicate the version of these Tech Stacks used.
- Clone the repository:
git clone https://github.com/siddharthgupta5/Rule_Engine_AST.git- Change Directory:
cd backend- Install dependencies:
npm install- Create the .env file with your values for PORT and USERNAME and PASSWORD for the MONGO_URI (for the MongoDB database or add your MONGODB_URL in the MONGO_URI).
PORT=...
MONGO_URI=mongodb+srv://<USERNAME>:<PASSWORD>@cluster0.lw6c7.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0- Start the development server:
npm start- Open another terminal. Navigate into the project directory:
cd frontend- Install dependencies:
npm install- Start the frontend server:
npm start- Navigate back to the backend directory:
cd backend- Run tests:
npm tests- Create a Rule
{
"ruleString": "(age > 28 && department == 'Marketing')",
"name": "Marketing Eligibility"
}- Evaluate a Rule
{
"ruleId": "<ruleId>",
"data": {
"age": 30,
"department": "Marketing",
"salary": 50000
}
}- Create a Rule:
js POST /api/rules/create - Combine Rules:
js POST /api/rules/combine - Evaluate Rule:
js POST /api/rules/evaluate - Get ALL Rules:
js GET /api/rules - Evaluate Rule:
js POST /api/rules/update/:id