The Rule Engine Application is a 3-tier system designed to determine user eligibility based on various attributes like age, department, and income. It uses an Abstract Syntax Tree (AST) to represent conditional rules and supports dynamic rule creation, combination, and modification.
- Dynamic Rule Creation: Input custom rule strings and convert them into ASTs.
- Rule Evaluation: Evaluate user eligibility based on provided attributes (e.g., age, department, income).
- Rule Combination: Combine multiple rules using logical operators (AND/OR) for complex eligibility conditions.
The rules are represented as nodes in an AST with the following properties:
- type: A 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., numbers for comparisons).
The application stores rules and metadata in a NoSQL database (MongoDB). Each rule is associated with its root AST node, and nodes are stored recursively to facilitate easy modification and combination.
-
Node:
type
: Stringoperator
: String (e.g., AND, OR)field
: String (attribute name, e.g., age)value
: String/Number (e.g., 30 for age)left
: Reference to left child noderight
: Reference to right child node
-
Rule:
name
: Stringdescription
: StringrootNode
: Reference to root node of the AST
((age > 30 AND department = 'Sales') OR (age < 25 AND department = 'Marketing')) AND (salary > 50000 OR experience > 5)
((age > 30 AND department = 'Marketing')) AND (salary > 20000 OR experience > 5)
-
Create Rule
- Endpoint:
/api/rules
- Method:
POST
- Description: Create a new rule by providing a rule string, which is parsed into an AST.
- Request:
{ "name": "Rule 1", "description": "Sample rule", "ruleString": "(age > 30 AND department = 'Sales') OR (salary > 50000)" }
- Endpoint:
-
Evaluate Rule
- Endpoint:
/api/rules/evaluate
- Method:
POST
- Description: Evaluate a rule against a user's data.
- Request:
{ "ruleId": "123", "data": { "age": 35, "department": "Sales", "salary": 60000 } }
- Response:
{ "result": true }
- Endpoint:
-
Combine Rules
- Endpoint:
/api/rules/combine
- Method:
POST
- Description: Combine multiple rules into a single AST using the OR operator.
- Request:
{ "ruleIds": ["123", "456"] }
- Endpoint:
The frontend provides a user-friendly interface for creating, viewing, and evaluating rules. It communicates with the backend API to manage rule logic.
- RuleCreation: Form to input rule strings and submit them to the backend.
- RuleListing: Displays all created rules with options to evaluate or combine them.
- RuleEvaluation: Interface to input user data and evaluate eligibility against existing rules.
- Frontend: React
- Backend: Node.js, Express.js
- Database: MongoDB
- Other Libraries: Mongoose (for MongoDB modeling), AST parsing libraries
Test the application by creating, combining, and evaluating rules. Handle cases where:
- Rules are invalid or contain missing operators.
- Data does not meet rule criteria.
- User-defined functions: Extend the rule language to support custom functions for advanced conditions.
- Advanced error handling: Implement comprehensive validation for invalid rules and data formats.
- Clone the repository:
git clone https://github.com/vijay098567/rule-engine.git
- Install Dependencies:
npm install
- Run the application:
npm start
The application is live at https://eng-rule.netlify.app/