DAA Rules
π FULL IMPLEMENTATION - This is the complete, production-ready implementation of the DAA Rules Engine, not a placeholder.
A comprehensive rules engine for the Decentralized Autonomous Agents (DAA) system, providing policy enforcement, decision automation, and governance capabilities.
Overview
DAA Rules provides a flexible and powerful rules engine that enables autonomous agents to make decisions based on predefined policies and conditions. The engine supports complex logical operations, pattern matching, time-based conditions, and various action types.
Features
Core Rule Engine
- Rule Definition: Flexible rule creation with conditions and actions
- Condition Evaluation: Support for equality, comparison, pattern matching, and logical operations
- Action Execution: Set fields, log messages, send notifications, and custom actions
- Context Management: Execution context with variables and metadata
Advanced Features
- Complex Logic: AND, OR, NOT operations for sophisticated rule conditions
- Pattern Matching: Regular expression support for text matching
- Time-based Rules: Temporal conditions and scheduling
- Priority System: Rule execution order based on priority levels
- Validation: Comprehensive rule and condition validation
Optional Features
- Scripting Support: Execute custom scripts within rules (with
scriptingfeature) - Database Storage: Persistent rule storage (with
databasefeature) - Custom Extensions: Pluggable condition and action types
Architecture
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β RuleEngine β βConditionEvaluatorβ β ActionExecutor β
β β β β β β
β - Rule Storage βββββΊβ - Equals/CompareβββββΊβ - SetField β
β - Execution β β - Pattern Match β β - Log Message β
β - Validation β β - Logic Ops β β - Notifications β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
βExecutionContext β β RuleStorage β β Scripting β
β β β β β (Optional) β
β - Variables β β - InMemory β β - Rhai Engine β
β - Metadata β β - Database β β - Custom Scriptsβ
β - Timestamp β β - File System β β - Extensions β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
Usage
Basic Rule Creation
use ;
// Create a simple rule
let rule = new_with_generated_id;
println!;
Rules Engine Setup
use ;
async
Complex Conditions
use ;
use ;
// Complex logical condition
let complex_condition = And ;
Pattern Matching
// Email validation rule
let email_rule = new_with_generated_id;
// IP address validation
let ip_rule = new_with_generated_id;
Action Types
use ;
use HashMap;
// Logging action
let log_action = Log ;
// Field modification
let set_field_action = SetField ;
// Notification
let notify_action = Notify ;
// Context modification
let mut modifications = new;
modifications.insert;
modifications.insert;
let modify_context_action = ModifyContext ;
// Webhook trigger
let mut headers = new;
headers.insert;
headers.insert;
let webhook_action = Webhook ;
Rule Definition Examples
Agent Performance Monitoring
let performance_rule = new_with_generated_id;
Resource Allocation
let resource_rule = new_with_generated_id;
Security Policy
let security_rule = new_with_generated_id;
Configuration
Basic Configuration
use RuleEngine;
// Create engine with default settings
let engine = new;
With Database Storage
[]
= { = "0.2.0", = ["database"] }
use DatabaseStorage;
// Create engine with database storage
let storage = new.await?;
let mut engine = with_storage;
With Scripting Support
[]
= { = "0.2.0", = ["scripting"] }
use RuleAction;
let script_action = Script ;
Features
The crate supports several feature flags:
default: Basic rules engine functionalitybasic: Core features only (same as default)scripting: Enables Rhai scripting support for custom actionsdatabase: Adds SQLite database storage for rulesfull: Includes all features
[]
= { = "0.2.0", = ["full"] }
Integration with DAA System
With DAA Chain
// Rules can be integrated with blockchain operations
let chain_rule = new_with_generated_id;
With DAA Economy
// Economic policy enforcement
let economic_rule = new_with_generated_id;
API Reference
RuleEngine
Main rules engine for executing policies.
Key Methods:
new()- Create new rules engineadd_rule(rule)- Add rule to engineexecute_rule(rule, context)- Execute specific ruleevaluate_condition(condition, context)- Evaluate conditionexecute_action(action, context)- Execute action
Rule
Individual rule definition with conditions and actions.
Key Methods:
new(id, name, conditions, actions)- Create new rulenew_with_generated_id(name, conditions, actions)- Create with auto-generated IDis_valid()- Validate rule definition
ExecutionContext
Context for rule execution containing variables and metadata.
Key Methods:
new()- Create new contextset_variable(key, value)- Set context variableget_variable(key)- Get context variableset_metadata(key, value)- Set metadata
Testing
Run the test suite:
# Basic tests
# All features
# Specific feature
Examples
See the /examples directory for comprehensive usage examples:
basic_rules.rs- Simple rule creation and executioncomplex_conditions.rs- Advanced logical conditionspattern_matching.rs- Regular expression usagetime_based_rules.rs- Temporal conditionscustom_actions.rs- Extending with custom actions
Performance
The rules engine is designed for high performance:
- Fast Evaluation: Optimized condition evaluation with short-circuiting
- Memory Efficient: Minimal memory overhead for rule storage
- Concurrent Safe: Thread-safe execution with async support
- Scalable: Handles thousands of rules efficiently
License
MIT OR Apache-2.0