RuleCraft is a flexible rule engine designed to evaluate user eligibility based on various attributes such as age, department, and income. Utilizing Abstract Syntax Trees (AST), this application supports a modern 3-tier architecture that enables efficient creation, combination, and evaluation of complex business rules.
- Key Features
- Technologies Used
- Architecture
- Implementation Details
- Getting Started
- API Endpoints
- Data Structures
- Testing with Postman
- Screenshots
- Performance
- Contributing
- License
- Support
- Dynamic Rule Creation: Build complex rules using an intuitive UI
- AST-Based Evaluation: Efficient rule processing using Abstract Syntax Trees
- Rule Combination: Merge multiple rules with smart optimization
- Real-time Validation: Instant feedback on rule syntax and structure
- Attribute Catalog: Pre-defined set of valid attributes for rule creation
- User-Friendly Interface: Modern, responsive design for easy rule management
- Frontend: React, TypeScript, Tailwind CSS
- Backend: Node.js, Express.js
- Database: MongoDB
- Testing: Postman
RuleCraft follows a robust 3-tier architecture:
βββ Frontend (React + TypeScript)
β βββ Rule Builder UI
β βββ Rule Visualization
β βββ Results Dashboard
β
βββ API Layer (Express.js)
β βββ Rule Management
β βββ Evaluation Engine
β βββ Combination Logic
β
βββ Data Layer (MongoDB)
βββ Rule Storage
βββ User Data
βββ Evaluation History
Repository Structure
RuleCraft/
βββ server/ # Backend code
β βββ models/ # Database models
β β βββ Rule.js
β βββ routes/ # API routes
β β βββ rules.js
β βββ controllers/ # Request handling logic
β βββ utils/ # Utility functions
β β βββ ruleEngine.js
β βββ config/ # Configuration files
β βββ index.js # Entry point
β
βββ src/ # Frontend code
β βββ components/ # React components
β β βββ Dashboard.tsx
β β βββ Navbar.tsx
β β βββ RuleList.tsx
β β βββ RuleCreator.tsx
β β βββ RuleEvaluator.tsx
β β βββ RuleCombiner.tsx
β βββ pages/ # Page components
β βββ utils/ # Frontend utilities
β βββ App.tsx # Main application
β βββ index.tsx # Entry point
β
βββ .gitignore # Git ignore rules
βββ package.json # Dependencies
βββ tsconfig.json # TypeScript config
βββ vite.config.ts # Vite configuration
βββ README.md # Documentation
const RuleSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
},
expression: {
type: String,
required: true,
},
ast: {
type: Object,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
updatedAt: {
type: Date,
default: Date.now,
}
});
// Create a new rule
export function createRule(ruleString) {
const cleanedString = ruleString.replace(/\s+/g, ' ').trim();
const jsExpression = cleanedString
.replace(/AND/g, '&&')
.replace(/OR/g, '||')
.replace(/(\w+)\s*=\s*'(\w+)'/g, "$1 === '$2'");
const ast = parse(jsExpression);
return ast.body[0].expression;
}
// Combine multiple rules
export function combineRules(rules) {
return rules.reduce((combined, rule) => ({
type: 'LogicalExpression',
operator: '&&',
left: combined,
right: rule
}));
}
// Evaluate a rule
export function evaluateRule(ast, data) {
function evaluate(node) {
switch (node.type) {
case 'BinaryExpression':
const left = evaluate(node.left);
const right = evaluate(node.right);
return performOperation(node.operator, left, right);
case 'LogicalExpression':
return evaluateLogical(node);
case 'Identifier':
return getData(node.name);
case 'Literal':
return node.value;
}
}
return evaluate(ast);
}
function App() {
return (
<Router>
<div className="min-h-screen bg-gray-100">
<Navbar />
<div className="container mx-auto px-4 py-8">
<Routes>
<Route path="/" element={<Dashboard />} />
<Route path="/rules" element={<RuleList />} />
<Route path="/create" element={<RuleCreator />} />
<Route path="/evaluate" element={<RuleEvaluator />} />
<Route path="/combine" element={<RuleCombiner />} />
</Routes>
</div>
</div>
</Router>
);
}
- Node.js (v18 or higher)
- MongoDB (v6.0 or higher)
- npm or yarn
-
Clone the repository:
git clone https://github.com/Satyamkumarnavneet/rulecraft.git cd rulecraft
-
Install dependencies:
npm install
-
Start the Backend Server:
node server/index.js
-
Start the Frontend Development Server:
npm run dev
-
Open or click on the link:
http://127.0.0.1:5173/
// Get all rules
GET /api/rules
// Create a new rule
POST /api/rules
Body: {
"name": "SalesRule",
"expression": "(age > 30 AND department = 'Sales') OR salary > 50000"
}
// Delete a rule
DELETE /api/rules/:id
// Evaluate a rule
POST /api/evaluate
Body: {
"ruleId": "rule123",
"userData": {
"age": 35,
"department": "Sales",
"salary": 60000
}
}
// Combine rules
POST /api/combine-rules
Body: {
"ruleIds": ["rule1ID", "rule2ID"],
"name": "CombinedRule"
}
interface RuleNode {
type: "operator" | "condition";
operator?: "AND" | "OR" | ">" | "<" | "=" | ">=";
field?: string;
value?: any;
left?: RuleNode;
right?: RuleNode;
}
// Rule 1: Sales and Marketing Rule
const rule1 = "((age > 30 AND department = 'Sales') OR (age < 25 AND department = 'Marketing')) AND (salary > 50000 OR experience > 5)";
// Rule 2: Marketing Experience Rule
const rule2 = "((age > 30 AND department = 'Marketing')) AND (salary > 20000 OR experience > 5)";
- Rule Templates: Pre-defined rule patterns for common use cases
- Version Control: Track changes in rules over time
- Batch Evaluation: Process multiple records efficiently
- Custom Functions: Support for user-defined evaluation functions
- Rule Export/Import: Share rules across systems
You can find the Postman documentation for the RuleCraft API https://documenter.getpostman.com/view/16476251/2sAY4rDPzX.
- Optimized AST traversal
- Caching for frequently used rules
- Batch processing capabilities
- Response time < 100ms for standard rules
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the Apache License - see the LICENSE file for details.
For support, email navneetsatyamkumar@gmail.com
.