Welcome to the Rule Engine Project! This project determines user eligibility based on attributes such as age, department, income, spend, etc. The system uses an Abstract Syntax Tree (AST) to represent conditional rules and allows for dynamic creation, combination, and modification of these rules.
- Objective
- Installation
- Usage
- API Endpoints
- Data Structure
- Database
- Testing
- Frontend Structure
- Backend Structure
- Contributing
- License
Develop a simple 3-tier rule engine application (Simple UI, API and Backend, Data) to determine user eligibility based on attributes like age, department, income, spend, etc. The system uses an Abstract Syntax Tree (AST) to represent conditional rules and allows for dynamic creation, combination, and modification of these rules.
- Node.js (version 14.x or later)
- npm (version 6.x or later)
- MongoDB (running instance or cloud service)
-
Clone the repository:
git clone https://github.com/pswitchy/Rule-Engine.git cd rule-engine-project/rule-engine-api -
Install dependencies:
npm install
-
Set up environment variables: Create a
.envfile in the backend directory and add your MongoDB connection string.MONGO_URI=mongodb://localhost:27017/rule-engine -
Start the backend server:
node app.js
The backend should now be running on http://localhost:5000.
-
Navigate to the frontend directory:
cd ../rule-engine-ui -
Install dependencies:
npm install
-
Start the frontend development server:
npm start
The frontend should now be running on http://localhost:3000.
Once both the backend and frontend servers are running, you can open your browser and navigate to http://localhost:3000. You should see the interface for creating and managing rules.
- Endpoint:
POST /api/rules/create_rule - Description: Creates a new rule.
- Request Body:
{ "rule_string": "your_rule_here" } - Response:
{ "rule_id": "generated_rule_id" }
- Endpoint:
POST /api/rules/combine_rules - Description: Combines multiple rules into a single AST.
- Request Body:
{ "rules": ["rule1", "rule2"] } - Response:
{ "combined_rule_ast": {} }
- Endpoint:
POST /api/rules/evaluate_rule - Description: Evaluates a rule against provided user data.
- Request Body:
{ "data": { "age": 35, "department": "Sales", "salary": 60000, "experience": 3 } } - Response:
{ "result": true }
The AST is represented using a Node data structure:
type: String indicating the node type ("operator" for AND/OR, "operand" for conditions)left: Reference to another Node (left child)right: Reference to another Node (right child for operators)value: Optional value for operand nodes (e.g., number for comparisons)
MongoDB is used to store rules and application metadata.
Rule Schema:
const ruleSchema = new mongoose.Schema({
rule_string: {
type: String,
required: true
},
ast: {
type: Object,
required: true
}
});{
"rule1": "((age > 30 AND department = 'Sales') OR (age < 25 AND department = 'Marketing')) AND (salary > 50000 OR experience > 5)",
"rule2": "((age > 30 AND department = 'Marketing')) AND (salary > 20000 OR experience > 5)"
}You can use Postman to test the API endpoints. Import the Postman collection and use it to send requests to the backend.
For unit testing the backend, this project uses Jest. To run the tests, use the following command:
npm testrule-engine-ui/
├── src/
│ ├── components/
│ │ ├── CreateRule.jsx
│ │ ├── UpdateRule.jsx
│ │ ├── DeleteRule.jsx
│ │ ├── CombineRule.jsx
│ │ ├── EvaluateRule.jsx
│ │ └── ui/
│ │ ├── alert.jsx
│ ├── App.js
│ ├── index.css
│ └── index.js
├── package.json
└── README.md
rule-engine-api/
├── config/
│ ├── db.js
│ └── default.json
├── models/
│ ├── Node.js
│ └── Rule.js
├── routes/
│ └── rules.js
├── tests/
│ └── rule.test.js
├── utils/
│ ├── ruleUtils.js
│ └── validation.js
├── app.js
├── package.json
└── README.md
Contributions are welcome! Please fork the repository and submit a pull request.