🦀 Rust Rule Engine - GRL Edition
A powerful, high-performance rule engine for Rust supporting GRL (Grule Rule Language) syntax with advanced features like method calls, custom functions, object interactions, and both file-based and inline rule management.
🌟 Key Features
- 🔥 GRL-Only Support: Pure Grule Rule Language syntax (no JSON)
- 📄 Rule Files: External
.grlfiles for organized rule management - 📝 Inline Rules: Define rules as strings directly in your code
- 📞 Custom Functions: Register and call user-defined functions from rules
- 🎯 Method Calls: Support for
Object.method(args)and property access - 🧠 Knowledge Base: Centralized rule management with salience-based execution
- 💾 Working Memory: Facts system for complex object interactions
- ⚡ High Performance: Optimized execution engine with cycle detection
- 🛡️ Type Safety: Rust's type system ensures runtime safety
- 🏗️ Builder Pattern: Clean API with
RuleEngineBuilder - 📈 Execution Statistics: Detailed performance metrics and debugging
🚀 Quick Start
Add to your Cargo.toml:
[]
= "0.1.2"
📄 File-Based Rules
Create a rule file rules/example.grl:
rule "AgeCheck" salience 10 {
when
User.Age >= 18 && User.Country == "US"
then
User.setIsAdult(true);
User.setCategory("Adult");
log("User qualified as adult");
}
rule "VIPUpgrade" salience 20 {
when
User.IsAdult == true && User.SpendingTotal > 1000.0
then
User.setIsVIP(true);
log("User upgraded to VIP status");
}
use ;
use HashMap;
📝 Inline String Rules
Define rules directly in your code:
use ;
use HashMap;
🎯 GRL Rule Language Features
Supported Syntax
rule "RuleName" salience 10 {
when
Object.Property > 100 &&
Object.Status == "ACTIVE"
then
Object.setCategory("HIGH_VALUE");
processTransaction(Object.Id, Object.Amount);
log("Rule executed successfully");
}
Operators
- Comparison:
>,>=,<,<=,==,!= - Logical:
&&,|| - Value Types: Numbers, Strings (quoted), Booleans (
true/false)
Actions
- Method Calls:
Object.method(args) - Function Calls:
functionName(args) - Logging:
log("message")
📚 Examples
🛒 E-commerce Rules
rule "VIPCustomer" salience 20 {
when
Customer.TotalSpent > 5000.0 && Customer.YearsActive >= 2
then
Customer.setTier("VIP");
sendWelcomePackage(Customer.Email, "VIP");
applyDiscount(Customer.Id, 15.0);
log("Customer upgraded to VIP");
}
rule "LoyaltyReward" salience 15 {
when
Customer.OrderCount >= 50
then
addLoyaltyPoints(Customer.Id, 500);
log("Loyalty reward applied");
}
🚗 Vehicle Monitoring
rule "SpeedLimit" salience 25 {
when
Vehicle.Speed > Vehicle.SpeedLimit
then
triggerAlert(Vehicle.Id, "SPEED_VIOLATION");
logViolation(Vehicle.Driver, Vehicle.Speed);
Vehicle.setStatus("FLAGGED");
}
rule "MaintenanceDue" salience 10 {
when
Vehicle.Mileage > Vehicle.NextMaintenance
then
scheduleService(Vehicle.Id, Vehicle.Mileage);
notifyDriver(Vehicle.Driver, "Maintenance due");
}
⚡ Performance & Architecture
Benchmarks
Rule Execution Performance:
• Simple conditions: ~10-50 microseconds
• Complex multi-condition rules: ~100-500 microseconds
• Custom function calls: ~50-200 microseconds
• File-based rule loading: ~1-5 milliseconds
Memory Usage:
• Rule storage: ~1KB per rule
• Facts storage: ~100-500 bytes per fact
• Engine overhead: ~10KB base memory
Key Design Decisions
- GRL-Only: Removed JSON support for cleaner, focused API
- Dual Sources: Support both file-based and inline rule definitions
- Custom Functions: Extensible function registry for business logic
- Builder Pattern: Fluent API for easy engine configuration
- Type Safety: Leverages Rust's type system for runtime safety
- Zero-Copy: Efficient string and memory management
🏗️ Development
Building from Source
Running Examples
# File-based rules with custom functions
# Inline rules demonstration
# Complete GRL feature showcase
# Custom function registry
# Builder pattern usage
Testing
# Run all tests
# Run with output
# Run specific test
Publishing
# Check package
# Run all tests
# Build documentation
# Publish dry run
# Publish to crates.io
📋 API Reference
Core Types
// Main engine builder
new
.with_rule_file?
.with_inline_grl?
.with_config
.build
// Value types
Integer
Number
String
Boolean
Object
// Facts management
let facts = new;
facts.add_value?;
facts.get?;
// Execution results
result.rules_fired // Number of rules that executed
result.cycle_count // Number of execution cycles
result.execution_time // Duration of execution
Function Registration
engine.register_function;
🤝 Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Development Setup
- Fork the repository
- Create your feature branch:
git checkout -b feature/amazing-feature - Make your changes and add tests
- Ensure all tests pass:
cargo test - Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
Guidelines
- Follow Rust naming conventions
- Add tests for new features
- Update documentation for API changes
- Ensure examples work with changes
📄 License
This project is licensed under the MIT License - see the LICENSE file for details.
🎯 Roadmap
- Enhanced GRL Support: More operators and complex conditions
- Rule Templates: Reusable rule patterns
- Performance Optimizations: Rule compilation and caching
- Debugging Tools: Rule execution tracing and profiling
- Integration Examples: Database, HTTP APIs, message queues
- Rule Validation: Static analysis and rule conflict detection
📞 Support
- 📚 Documentation: docs.rs/rust-rule-engine
- 🐛 Issues: GitHub Issues
- 💬 Discussions: GitHub Discussions
Built with ❤️ in Rust 🦀