The Rule Engine is a simple 3-tier application with a frontend UI, API backend, and a database for storing rules and application metadata. The purpose of this system is to dynamically create, combine, and evaluate rules to determine user eligibility based on attributes like age, department, income, spend, etc. The rules are represented as Abstract Syntax Trees (AST).
- Dynamic Rule Creation: Create rules using a string input that will be parsed into an AST.
- Rule Combination: Combine two rules with logical operators like AND/OR.
- Rule Evaluation: Evaluate if a user matches the rule based on a set of attributes (age, income, etc.).
- Rule Modification: Modify existing rules (e.g., changing operators or conditions).
- Frontend Interface: A simple UI that allows interaction with the system for rule creation, evaluation, and combination.
- Each rule is represented as an AST with nodes that represent either an operand (a condition) or an operator (AND/OR).
Example of a node in the AST: json { "type": "operator", "operator": "AND", "left": { "type": "operand", "field": "age", "comparator": ">", "value": 25 }, "right": { "type": "operand", "field": "salary", "comparator": ">=", "value": 50000 } }
- type: Either
"operator"
(e.g., AND/OR) or"operand"
(conditions likeage > 25
). - left: Reference to another node (left child, for binary operators).
- right: Reference to another node (right child, for binary operators).
- value: For operand nodes, the value used for comparison (e.g., 25 for
age > 25
).
{
"ruleName": "example_rule",
"astString": "{\"type\":\"operator\",\"left\":{\"type\":\"operand\",\"field\":\"age\",\"comparator\":\">\",\"value\":30},\"right\": {\"type\":\"operand\",\"field\":\"salary\",\"comparator\":\">\",\"value\":50000},\"operator\":\"AND\"}"
}
-
create_rule(rule_string):
- This function takes a string representing a rule (e.g.,
age > 30 AND salary > 50000
) and converts it into an Abstract Syntax Tree (AST). - The rule string is parsed into a
Node
structure that can be stored and evaluated.
Example:
-
Input:
"age > 30 AND salary > 50000"
-
Output:
{ "type": "operator", "operator": "AND", "left": { "type": "operand", "field": "age", "comparator": ">", "value": 30 }, "right": { "type": "operand", "field": "salary", "comparator": ">", "value": 50000 } }
- This function takes a string representing a rule (e.g.,
-
combine_rules(rules):
- Takes two or more rules and combines them using a logical operator (e.g., AND/OR).
- The combined rules form a new AST that minimizes redundant checks.
- Returns the root node of the combined AST.
Example:
-
Input: Two rules
"age > 30"
and"salary > 50000"
with anAND
operator. -
Output: Combined AST
{ "type": "operator", "operator": "AND", "left": { "type": "operand", "field": "age", "comparator": ">", "value": 30 }, "right": { "type": "operand", "field": "salary", "comparator": ">", "value": 50000 } }
-
evaluate_rule(JSON data):
-
Takes the combined rule’s AST and a JSON object representing user data.
-
Example of input data:
{ "age": 35, "department": "Sales", "salary": 60000, "experience": 3 }
-
Evaluates the rule against the user’s attributes.
-
Returns
True
if the user meets the rule criteria, otherwise returnsFalse
.
-
-
update_rule(JSON data):
- Takes a rule name and new rule to be inserted as the input, and inserts the updated rule into DB.
- Example of input data:
{ "ruleName" : "Rule2" , "updatedRule" : "age<30"
}
- Ensure that you have Node.js installed on your system .
- Install MongoDB on your local machine or have access to a MongoDB cloud instance.
-
Clone the Repository
git clone https://github.com/abhilashasupe/ast_rule_engine.git
-
Install Backend Dependencies
npm install
-
Configure MongoDB and put values in .env
- Ensure MongoDB is running locally
-
MONGO_URI = mongodb://0.0.0.0:27017/rule_engine PORT = 3333
-
Run the Backend
-
Start the server by going to the src folder and running the following command:
nodemon main.js
-
The backend server should now be running on
http://localhost:PORT
.
-
-
Open the Frontend
- Open the
index.html
file in your browser using the Live Server extension in Visual Studio Code or by manually opening it in a browser. - The frontend will interact with the backend API running on
http://localhost:3333
.
- Open the
- POST /create-rule: Insert a new rule into the system.
- POST /combine-rules: Combine two rules using an operator (AND/OR).
- POST /evaluate-rule: Evaluate user data against a specific rule.
- POST /update-rule: Update an existing rule in the system.