rust-rule-engine 1.0.2-alpha

A high-performance rule engine for Rust with RETE-UL algorithm (2-24x faster), CLIPS-inspired features (Template System, Defglobal, Deffacts, Test CE, Conflict Resolution), Parallel Execution
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use rust_rule_engine::engine::engine::RustRuleEngine;
use rust_rule_engine::engine::plugin::{PluginHealth, PluginMetadata, PluginState, RulePlugin};
use rust_rule_engine::errors::Result;
use rust_rule_engine::types::Value;
use std::collections::HashMap;

/// AI/ML Integration Plugin for machine learning and AI operations
pub struct AIMLPlugin {
    metadata: PluginMetadata,
}

impl AIMLPlugin {
    pub fn new() -> Self {
        Self {
            metadata: PluginMetadata {
                name: "ai-ml".to_string(),
                version: "1.0.0".to_string(),
                description: "AI/ML integration for predictions, NLP, and data analysis"
                    .to_string(),
                author: "Rust Rule Engine Team".to_string(),
                state: PluginState::Loaded,
                health: PluginHealth::Healthy,
                actions: vec![
                    "PredictValue".to_string(),
                    "ClassifyText".to_string(),
                    "SentimentAnalysis".to_string(),
                    "GenerateEmbedding".to_string(),
                    "DetectAnomalies".to_string(),
                    "RecommendItems".to_string(),
                    "ChatCompletion".to_string(),
                    "ImageRecognition".to_string(),
                    "TranslateText".to_string(),
                ],
                functions: vec![
                    "calculateSimilarity".to_string(),
                    "normalizeScore".to_string(),
                    "extractKeywords".to_string(),
                    "tokenizeText".to_string(),
                    "preprocessData".to_string(),
                    "evaluateModel".to_string(),
                    "featureEngineering".to_string(),
                ],
                dependencies: vec![
                    "candle-core".to_string(),
                    "tokenizers".to_string(),
                    "reqwest".to_string(),
                    "serde_json".to_string(),
                ],
            },
        }
    }
}

impl RulePlugin for AIMLPlugin {
    fn get_metadata(&self) -> &PluginMetadata {
        &self.metadata
    }

    fn register_actions(&self, engine: &mut RustRuleEngine) -> Result<()> {
        // Predict Value action
        engine.register_action_handler("PredictValue", |params, facts| {
            let model_name = params
                .get("0")
                .map(|v| v.to_string())
                .unwrap_or("default_model".to_string());
            let input_data = params
                .get("1")
                .map(|v| v.to_string())
                .unwrap_or("{}".to_string());
            let result_key = params
                .get("2")
                .map(|v| v.to_string())
                .unwrap_or("prediction".to_string());

            println!("🤖 ML PREDICTION:");
            println!("   Model: {}", model_name);
            println!("   Input: {}", input_data);

            // Simulate prediction based on model type
            let prediction = match model_name.as_str() {
                "fraud_detection" => {
                    println!("   🔍 Fraud Detection Score: 0.15 (Low Risk)");
                    Value::Number(0.15)
                }
                "price_prediction" => {
                    println!("   💰 Predicted Price: $127.45");
                    Value::Number(127.45)
                }
                "customer_churn" => {
                    println!("   📊 Churn Probability: 0.23 (23%)");
                    Value::Number(0.23)
                }
                _ => {
                    println!("   📈 Prediction Score: 0.78");
                    Value::Number(0.78)
                }
            };

            facts.add_value(&result_key, prediction)?;
            facts.add_value("model_confidence", Value::Number(0.92))?;
            Ok(())
        });

        // Classify Text action
        engine.register_action_handler("ClassifyText", |params, facts| {
            let text = params.get("0").map(|v| v.to_string()).unwrap_or_default();
            let categories = params
                .get("1")
                .map(|v| v.to_string())
                .unwrap_or("general".to_string());

            println!("📝 TEXT CLASSIFICATION:");
            println!("   Text: \"{}\"", text);
            println!("   Categories: {}", categories);

            // Simulate classification based on content
            let (category, confidence) =
                if text.to_lowercase().contains("urgent") || text.contains("emergency") {
                    ("urgent", 0.95)
                } else if text.to_lowercase().contains("spam") || text.contains("offer") {
                    ("spam", 0.88)
                } else if text.to_lowercase().contains("question") || text.contains("help") {
                    ("support", 0.76)
                } else if text.to_lowercase().contains("bug") || text.contains("error") {
                    ("technical", 0.84)
                } else {
                    ("general", 0.65)
                };

            println!(
                "   🏷️  Category: {} (confidence: {:.2})",
                category, confidence
            );

            facts.add_value("text_category", Value::String(category.to_string()))?;
            facts.add_value("classification_confidence", Value::Number(confidence))?;
            Ok(())
        });

        // Sentiment Analysis action
        engine.register_action_handler("SentimentAnalysis", |params, facts| {
            let text = params.get("0").map(|v| v.to_string()).unwrap_or_default();

            println!("😊 SENTIMENT ANALYSIS:");
            println!("   Text: \"{}\"", text);

            // Simulate sentiment analysis
            let (sentiment, score) = if text.to_lowercase().contains("love")
                || text.contains("great")
                || text.contains("excellent")
            {
                ("positive", 0.89)
            } else if text.to_lowercase().contains("hate")
                || text.contains("terrible")
                || text.contains("awful")
            {
                ("negative", -0.76)
            } else if text.to_lowercase().contains("okay") || text.contains("fine") {
                ("neutral", 0.12)
            } else {
                ("neutral", 0.05)
            };

            println!("   🎭 Sentiment: {} (score: {:.2})", sentiment, score);

            facts.add_value("sentiment", Value::String(sentiment.to_string()))?;
            facts.add_value("sentiment_score", Value::Number(score))?;
            Ok(())
        });

        // Generate Embedding action
        engine.register_action_handler("GenerateEmbedding", |params, facts| {
            let text = params.get("0").map(|v| v.to_string()).unwrap_or_default();
            let model = params
                .get("1")
                .map(|v| v.to_string())
                .unwrap_or("text-embedding-ada-002".to_string());

            println!("🧠 GENERATE EMBEDDING:");
            println!("   Text: \"{}\"", text);
            println!("   Model: {}", model);

            // Simulate embedding generation
            let embedding_preview = "[0.123, -0.456, 0.789, 0.234, -0.567, ...]";
            println!("   🔢 Embedding (1536 dimensions): {}", embedding_preview);

            facts.add_value("embedding", Value::String(embedding_preview.to_string()))?;
            facts.add_value("embedding_dimensions", Value::Number(1536.0))?;
            Ok(())
        });

        // Detect Anomalies action
        engine.register_action_handler("DetectAnomalies", |params, facts| {
            let data = params
                .get("0")
                .map(|v| v.to_string())
                .unwrap_or("[]".to_string());
            let threshold = params
                .get("1")
                .map(|v| v.to_string())
                .unwrap_or("0.95".to_string());

            println!("🚨 ANOMALY DETECTION:");
            println!("   Data points: {}", data);
            println!("   Threshold: {}", threshold);

            // Simulate anomaly detection
            let anomalies_found = 3;
            let anomaly_score = 0.97;

            println!("   ⚠️  Anomalies found: {} points", anomalies_found);
            println!("   📊 Max anomaly score: {:.2}", anomaly_score);

            facts.add_value("anomalies_count", Value::Number(anomalies_found as f64))?;
            facts.add_value("max_anomaly_score", Value::Number(anomaly_score))?;
            facts.add_value("has_anomalies", Value::Boolean(anomalies_found > 0))?;
            Ok(())
        });

        // Recommend Items action
        engine.register_action_handler("RecommendItems", |params, facts| {
            let user_id = params
                .get("0")
                .map(|v| v.to_string())
                .unwrap_or("user123".to_string());
            let item_type = params
                .get("1")
                .map(|v| v.to_string())
                .unwrap_or("products".to_string());
            let count = params
                .get("2")
                .map(|v| v.to_string())
                .unwrap_or("5".to_string());

            println!("🎯 RECOMMENDATION ENGINE:");
            println!("   User ID: {}", user_id);
            println!("   Item Type: {}", item_type);
            println!("   Count: {}", count);

            // Simulate recommendations based on item type
            let recommendations = match item_type.as_str() {
                "products" => "[{\"id\": 101, \"name\": \"Wireless Headphones\", \"score\": 0.95}]",
                "movies" => "[{\"id\": 201, \"title\": \"Sci-Fi Adventure\", \"score\": 0.88}]",
                "books" => "[{\"id\": 301, \"title\": \"AI Programming Guide\", \"score\": 0.92}]",
                _ => "[{\"id\": 1, \"name\": \"Recommended Item\", \"score\": 0.85}]",
            };

            println!("   ✨ Recommendations generated:");
            println!("      {}", recommendations);

            facts.add_value(
                "recommendations",
                Value::String(recommendations.to_string()),
            )?;
            facts.add_value("recommendation_count", Value::Number(5.0))?;
            Ok(())
        });

        // Chat Completion action
        engine.register_action_handler("ChatCompletion", |params, facts| {
            let prompt = params.get("0").map(|v| v.to_string()).unwrap_or_default();
            let model = params
                .get("1")
                .map(|v| v.to_string())
                .unwrap_or("gpt-3.5-turbo".to_string());

            println!("💬 CHAT COMPLETION:");
            println!("   Prompt: \"{}\"", prompt);
            println!("   Model: {}", model);

            // Simulate AI response based on prompt content
            let response = if prompt.to_lowercase().contains("code") {
                "Here's a code example: `fn hello() { println!(\"Hello!\"); }`"
            } else if prompt.to_lowercase().contains("explain") {
                "Let me explain that concept in simple terms..."
            } else if prompt.to_lowercase().contains("help") {
                "I'd be happy to help you with that!"
            } else {
                "Thank you for your question. Here's my response..."
            };

            println!("   🤖 AI Response: \"{}\"", response);

            facts.add_value("ai_response", Value::String(response.to_string()))?;
            facts.add_value("tokens_used", Value::Number(127.0))?;
            Ok(())
        });

        Ok(())
    }

    fn register_functions(&self, engine: &mut RustRuleEngine) -> Result<()> {
        // Calculate Similarity function
        engine.register_function("calculateSimilarity", |args, _facts| {
            let text1 = args.get(0).map(|v| v.to_string()).unwrap_or_default();
            let text2 = args.get(1).map(|v| v.to_string()).unwrap_or_default();

            // Simulate cosine similarity calculation
            let similarity = if text1.is_empty() || text2.is_empty() {
                0.0
            } else if text1 == text2 {
                1.0
            } else {
                // Simple word overlap similarity
                let words1: std::collections::HashSet<_> = text1.split_whitespace().collect();
                let words2: std::collections::HashSet<_> = text2.split_whitespace().collect();
                let intersection = words1.intersection(&words2).count();
                let union = words1.union(&words2).count();
                intersection as f64 / union as f64
            };

            println!("🔍 Similarity calculated: {:.3}", similarity);

            Ok(Value::Number(similarity))
        });

        // Extract Keywords function
        engine.register_function("extractKeywords", |args, _facts| {
            let text = args.get(0).map(|v| v.to_string()).unwrap_or_default();
            let max_keywords = args.get(1).map(|v| v.to_string()).unwrap_or("5".to_string());
            
            // Simulate keyword extraction
            let keywords = if text.to_lowercase().contains("ai") || text.contains("machine learning") {
                "artificial intelligence, machine learning, neural networks, deep learning, automation"
            } else if text.to_lowercase().contains("database") || text.contains("sql") {
                "database, sql, query, data, storage"
            } else if text.to_lowercase().contains("web") || text.contains("http") {
                "web, http, api, rest, server"
            } else {
                "general, content, text, analysis, processing"
            };
            
            println!("🏷️  Keywords extracted ({}): {}", max_keywords, keywords);
            
            Ok(Value::String(keywords.to_string()))
        });

        // Tokenize Text function
        engine.register_function("tokenizeText", |args, _facts| {
            let text = args.get(0).map(|v| v.to_string()).unwrap_or_default();

            // Simulate tokenization
            let word_count = text.split_whitespace().count();
            let tokens = format!(
                "[\"{}\"]",
                text.split_whitespace().collect::<Vec<_>>().join("\", \"")
            );

            println!(
                "🔤 Tokenized {} words: {}",
                word_count,
                if tokens.len() > 100 {
                    format!("{}...", &tokens[..100])
                } else {
                    tokens.clone()
                }
            );

            Ok(Value::String(tokens))
        });

        // Normalize Score function
        engine.register_function("normalizeScore", |args, _facts| {
            let score = args
                .get(0)
                .and_then(|v| v.to_string().parse::<f64>().ok())
                .unwrap_or(0.0);
            let min_val = args
                .get(1)
                .and_then(|v| v.to_string().parse::<f64>().ok())
                .unwrap_or(0.0);
            let max_val = args
                .get(2)
                .and_then(|v| v.to_string().parse::<f64>().ok())
                .unwrap_or(1.0);

            // Normalize score to 0-1 range
            let normalized = if max_val == min_val {
                0.5
            } else {
                (score - min_val) / (max_val - min_val)
            }
            .max(0.0)
            .min(1.0);

            println!("📊 Score normalized: {} -> {:.3}", score, normalized);

            Ok(Value::Number(normalized))
        });

        // Evaluate Model function
        engine.register_function("evaluateModel", |args, _facts| {
            let model_name = args
                .get(0)
                .map(|v| v.to_string())
                .unwrap_or("model".to_string());
            let metric = args
                .get(1)
                .map(|v| v.to_string())
                .unwrap_or("accuracy".to_string());

            // Simulate model evaluation
            let score = match metric.as_str() {
                "accuracy" => 0.94,
                "precision" => 0.91,
                "recall" => 0.88,
                "f1_score" => 0.89,
                "auc" => 0.96,
                _ => 0.85,
            };

            println!("📈 Model {} {} score: {:.3}", model_name, metric, score);

            Ok(Value::Number(score))
        });

        Ok(())
    }

    fn unload(&mut self) -> Result<()> {
        self.metadata.state = PluginState::Unloaded;
        println!("🤖 AI/ML Plugin unloaded");
        Ok(())
    }

    fn health_check(&mut self) -> PluginHealth {
        println!("🏥 AI/ML health check: Models loaded and ready");
        PluginHealth::Healthy
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rust_rule_engine::engine::knowledge_base::KnowledgeBase;
    use rust_rule_engine::Facts;

    #[test]
    fn test_aiml_plugin() {
        let kb = KnowledgeBase::new("AIMLTest");
        let mut engine = RustRuleEngine::new(kb);
        let facts = Facts::new();

        let plugin = AIMLPlugin::new();

        // Test plugin registration
        assert!(plugin.register_actions(&mut engine).is_ok());
        assert!(plugin.register_functions(&mut engine).is_ok());

        // Test function availability
        assert!(engine.has_function("calculateSimilarity"));
        assert!(engine.has_function("extractKeywords"));
        assert!(engine.has_function("normalizeScore"));

        // Test action availability
        assert!(engine.has_action_handler("PredictValue"));
        assert!(engine.has_action_handler("ClassifyText"));
        assert!(engine.has_action_handler("SentimentAnalysis"));
    }
}