use arrow::array::Float64Array;
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use fuse_rule::{evaluator::DataFusionEvaluator, rule::Rule, state::SledStateStore, RuleEngine};
use std::sync::Arc;
use tempfile::TempDir;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
println!("🔥 FuseRule Programmatic Rules Example\n");
let temp_dir = TempDir::new()?;
let state_path = temp_dir.path().join("state");
let schema = Schema::new(vec![Field::new("price", DataType::Float64, true)]);
let schema = Arc::new(schema);
let evaluator = Box::new(DataFusionEvaluator::new());
let state = Box::new(SledStateStore::new(&state_path)?);
let mut engine = RuleEngine::new(evaluator, state, schema, 1000, 10);
println!("📝 Adding rule programmatically...");
let rule = Rule {
id: "high_price".to_string(),
name: "High Price Alert".to_string(),
predicate: "price > 1000".to_string(),
action: "logger".to_string(),
window_seconds: None,
version: 1,
enabled: true,
};
engine.add_rule(rule).await?;
println!(" ✅ Rule added");
let batch = RecordBatch::try_new(
engine.schema(),
vec![Arc::new(Float64Array::from(vec![1500.0, 500.0]))],
)?;
println!("🔍 Processing batch...");
let traces = engine.process_batch(&batch).await?;
println!(" ✅ Processed {} rules", traces.len());
println!("🔄 Updating rule...");
let updated_rule = Rule {
id: "high_price".to_string(),
name: "High Price Alert (Updated)".to_string(),
predicate: "price > 2000".to_string(),
action: "logger".to_string(),
window_seconds: None,
version: 2,
enabled: true,
};
engine.update_rule("high_price", updated_rule).await?;
println!(" ✅ Rule updated");
println!("🔀 Disabling rule...");
engine.toggle_rule("high_price", false).await?;
println!(" ✅ Rule disabled");
let _traces = engine.process_batch(&batch).await?;
println!(" ✅ Processed (rule disabled, should not fire)");
Ok(())
}