| Crates.io | daa-rules |
| lib.rs | daa-rules |
| version | 0.2.1 |
| created_at | 2025-06-24 13:22:44.159694+00 |
| updated_at | 2025-06-25 12:55:06.59079+00 |
| description | Rules engine for DAA system providing policy enforcement and decision automation |
| homepage | https://daa.hq/ |
| repository | https://github.com/daa-hq/daa-sdk |
| max_upload_size | |
| id | 1724294 |
| size | 133,129 |
π 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.
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.
scripting feature)database feature)βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β 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 β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
use daa_rules::{Rule, RuleCondition, RuleAction, LogLevel};
// Create a simple rule
let rule = Rule::new_with_generated_id(
"Agent Status Check".to_string(),
vec![
RuleCondition::Equals {
field: "agent_status".to_string(),
value: "active".to_string(),
},
RuleCondition::GreaterThan {
field: "performance_score".to_string(),
value: 0.8,
},
],
vec![
RuleAction::Log {
level: LogLevel::Info,
message: "High-performing active agent detected".to_string(),
},
RuleAction::SetField {
field: "eligibility".to_string(),
value: "premium".to_string(),
},
],
);
println!("Created rule: {}", rule.name);
use daa_rules::{RuleEngine, ExecutionContext};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create rules engine
let mut engine = RuleEngine::new();
// Add rule to engine
engine.add_rule(rule).await?;
// Create execution context
let mut context = ExecutionContext::new();
context.set_variable("agent_status".to_string(), "active".to_string());
context.set_variable("performance_score".to_string(), "0.85".to_string());
// Execute rule
let result = engine.execute_rule(&rule, &mut context).await?;
println!("Rule execution result: {}", result);
Ok(())
}
use daa_rules::{RuleCondition, TimeOperator};
use chrono::{Utc, Duration};
// Complex logical condition
let complex_condition = RuleCondition::And {
conditions: vec![
RuleCondition::Or {
conditions: vec![
RuleCondition::Equals {
field: "task_type".to_string(),
value: "compute".to_string(),
},
RuleCondition::Equals {
field: "task_type".to_string(),
value: "storage".to_string(),
},
],
},
RuleCondition::Not {
condition: Box::new(RuleCondition::Equals {
field: "agent_status".to_string(),
value: "suspended".to_string(),
}),
},
RuleCondition::TimeCondition {
field: "last_active".to_string(),
operator: TimeOperator::After,
value: Utc::now() - Duration::hours(24),
},
],
};
// Email validation rule
let email_rule = Rule::new_with_generated_id(
"Email Validation".to_string(),
vec![
RuleCondition::Matches {
field: "email".to_string(),
pattern: r"^[^@]+@[^@]+\.[^@]+$".to_string(),
},
],
vec![
RuleAction::SetField {
field: "email_valid".to_string(),
value: "true".to_string(),
},
],
);
// IP address validation
let ip_rule = Rule::new_with_generated_id(
"IP Address Check".to_string(),
vec![
RuleCondition::Matches {
field: "client_ip".to_string(),
pattern: r"^192\.168\.1\.\d{1,3}$".to_string(),
},
],
vec![
RuleAction::SetField {
field: "network_zone".to_string(),
value: "internal".to_string(),
},
],
);
use daa_rules::{RuleAction, LogLevel, NotificationChannel};
use std::collections::HashMap;
// Logging action
let log_action = RuleAction::Log {
level: LogLevel::Warn,
message: "Unusual activity detected".to_string(),
};
// Field modification
let set_field_action = RuleAction::SetField {
field: "alert_level".to_string(),
value: "high".to_string(),
};
// Notification
let notify_action = RuleAction::Notify {
recipient: "admin@example.com".to_string(),
message: "Security alert triggered".to_string(),
channel: NotificationChannel::Email,
};
// Context modification
let mut modifications = HashMap::new();
modifications.insert("processed".to_string(), "true".to_string());
modifications.insert("processor".to_string(), "security_agent".to_string());
let modify_context_action = RuleAction::ModifyContext {
modifications,
};
// Webhook trigger
let mut headers = HashMap::new();
headers.insert("Authorization".to_string(), "Bearer token".to_string());
headers.insert("Content-Type".to_string(), "application/json".to_string());
let webhook_action = RuleAction::Webhook {
url: "https://api.example.com/alerts".to_string(),
method: "POST".to_string(),
headers,
body: r#"{"alert": "security_event", "level": "high"}"#.to_string(),
};
let performance_rule = Rule::new_with_generated_id(
"Performance Monitoring".to_string(),
vec![
RuleCondition::LessThan {
field: "success_rate".to_string(),
value: 0.7, // Below 70% success rate
},
RuleCondition::GreaterThan {
field: "task_count".to_string(),
value: 10.0, // More than 10 tasks
},
],
vec![
RuleAction::Log {
level: LogLevel::Warn,
message: "Agent performance below threshold".to_string(),
},
RuleAction::SetField {
field: "performance_status".to_string(),
value: "review_required".to_string(),
},
RuleAction::Notify {
recipient: "performance_monitor".to_string(),
message: "Agent requires performance review".to_string(),
channel: NotificationChannel::Internal,
},
],
);
let resource_rule = Rule::new_with_generated_id(
"Resource Allocation".to_string(),
vec![
RuleCondition::In {
field: "agent_tier".to_string(),
values: vec!["premium".to_string(), "enterprise".to_string()],
},
RuleCondition::LessThan {
field: "current_load".to_string(),
value: 0.8, // Below 80% load
},
],
vec![
RuleAction::SetField {
field: "resource_allocation".to_string(),
value: "high".to_string(),
},
RuleAction::SetField {
field: "priority_queue".to_string(),
value: "fast_track".to_string(),
},
],
);
let security_rule = Rule::new_with_generated_id(
"Security Policy".to_string(),
vec![
RuleCondition::And {
conditions: vec![
RuleCondition::Matches {
field: "request_path".to_string(),
pattern: r"/admin/.*".to_string(),
},
RuleCondition::NotEquals {
field: "user_role".to_string(),
value: "administrator".to_string(),
},
],
},
],
vec![
RuleAction::Log {
level: LogLevel::Error,
message: "Unauthorized admin access attempt".to_string(),
},
RuleAction::Abort {
reason: "Insufficient privileges for admin access".to_string(),
},
],
);
use daa_rules::RuleEngine;
// Create engine with default settings
let engine = RuleEngine::new();
[dependencies]
daa-rules = { version = "0.2.0", features = ["database"] }
#[cfg(feature = "database")]
use daa_rules::database::DatabaseStorage;
// Create engine with database storage
let storage = DatabaseStorage::new("sqlite://rules.db").await?;
let mut engine = RuleEngine::with_storage(Box::new(storage));
[dependencies]
daa-rules = { version = "0.2.0", features = ["scripting"] }
#[cfg(feature = "scripting")]
use daa_rules::RuleAction;
let script_action = RuleAction::Script {
script_type: "rhai".to_string(),
script: r#"
let result = performance_score * quality_multiplier;
if result > 0.9 {
set_field("bonus_eligible", "true");
}
"#.to_string(),
};
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[dependencies]
daa-rules = { version = "0.2.0", features = ["full"] }
// Rules can be integrated with blockchain operations
let chain_rule = Rule::new_with_generated_id(
"Blockchain Validation".to_string(),
vec![
RuleCondition::GreaterThan {
field: "transaction_amount".to_string(),
value: 1000.0,
},
],
vec![
RuleAction::Custom {
action_type: "blockchain_verify".to_string(),
parameters: {
let mut params = HashMap::new();
params.insert("verification_level".to_string(), "enhanced".to_string());
params
},
},
],
);
// Economic policy enforcement
let economic_rule = Rule::new_with_generated_id(
"Economic Policy".to_string(),
vec![
RuleCondition::And {
conditions: vec![
RuleCondition::Equals {
field: "transaction_type".to_string(),
value: "reward".to_string(),
},
RuleCondition::GreaterThan {
field: "performance_score".to_string(),
value: 0.95,
},
],
},
],
vec![
RuleAction::Custom {
action_type: "apply_bonus".to_string(),
parameters: {
let mut params = HashMap::new();
params.insert("bonus_multiplier".to_string(), "1.5".to_string());
params
},
},
],
);
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 actionIndividual 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 definitionContext 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 metadataRun the test suite:
# Basic tests
cargo test --package daa-rules
# All features
cargo test --package daa-rules --all-features
# Specific feature
cargo test --package daa-rules --features scripting
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 actionsThe rules engine is designed for high performance:
MIT OR Apache-2.0