oxirs-rule 0.2.4

Forward/backward rule engine for RDFS, OWL, and SWRL reasoning
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
//! Advanced Integration Example
//!
//! Demonstrates all Beta.1 features working together in a realistic scenario:
//! - Rule Composition (modules, templates, inheritance)
//! - Custom Rule Language (DSL)
//! - SPARQL Integration (query-driven reasoning)
//! - SHACL Integration (validation with reasoning)
//! - Distributed Reasoning (scaling)
//! - Explanation Support
//! - Conflict Resolution
//! - Transactions
//!
//! # Scenario: E-Commerce Knowledge Graph
//!
//! This example implements a complete e-commerce knowledge graph with:
//! - Product recommendations based on rules
//! - Inventory validation with SHACL
//! - Distributed query processing
//! - Transaction support for order processing
//! - Explanation of recommendations

use crate::{
    composition::{CompositionManager, RuleModule},
    conflict::{ConflictResolver, Priority},
    distributed::{DistributedReasoner, Node, PartitionStrategy},
    explanation::ExplanationEngine,
    shacl_integration::{ShaclRuleIntegration, ShapeConstraint, ValidationMode},
    sparql_integration::{QueryMode, QueryPattern, SparqlRuleIntegration},
    transaction::{IsolationLevel, TransactionManager},
    Rule, RuleAtom, RuleEngine, Term,
};
use anyhow::Result;
use tracing::{debug, info};

/// E-Commerce reasoning system with all Beta.1 features
pub struct EcommerceReasoningSystem {
    /// Main rule engine
    engine: RuleEngine,
    /// Composition manager for rule organization
    composition: CompositionManager,
    /// SPARQL integration for query-driven reasoning
    sparql: SparqlRuleIntegration,
    /// SHACL integration for validation
    shacl: ShaclRuleIntegration,
    /// Distributed reasoner for scaling
    distributed: DistributedReasoner,
    /// Explanation engine for provenance
    explanation: ExplanationEngine,
    /// Conflict resolver for rule priorities
    conflict: ConflictResolver,
    /// Transaction manager for ACID operations
    transactions: TransactionManager,
}

impl EcommerceReasoningSystem {
    /// Create a new e-commerce reasoning system
    pub fn new() -> Self {
        info!("Initializing E-Commerce Reasoning System with all Beta.1 features");

        let engine = RuleEngine::new();
        let composition = CompositionManager::new();
        let sparql = SparqlRuleIntegration::new(RuleEngine::new());
        let shacl = ShaclRuleIntegration::new(RuleEngine::new());
        let distributed = DistributedReasoner::new(PartitionStrategy::LoadBalanced);
        let explanation = ExplanationEngine::new();
        let conflict = ConflictResolver::new();
        let transactions = TransactionManager::new();

        Self {
            engine,
            composition,
            sparql,
            shacl,
            distributed,
            explanation,
            conflict,
            transactions,
        }
    }

    /// Initialize the system with product recommendation rules
    pub fn initialize_product_rules(&mut self) -> Result<()> {
        info!("Initializing product recommendation rules");

        // Create a rule module for product recommendations
        let mut product_module = RuleModule::new("product_recommendations".to_string());
        product_module.set_description("Rules for product recommendations".to_string());

        // Add rule: customers who bought X also bought Y
        let also_bought_rule = Rule {
            name: "also_bought".to_string(),
            body: vec![
                RuleAtom::Triple {
                    subject: Term::Variable("customer1".to_string()),
                    predicate: Term::Constant("purchased".to_string()),
                    object: Term::Variable("product1".to_string()),
                },
                RuleAtom::Triple {
                    subject: Term::Variable("customer2".to_string()),
                    predicate: Term::Constant("purchased".to_string()),
                    object: Term::Variable("product1".to_string()),
                },
                RuleAtom::Triple {
                    subject: Term::Variable("customer2".to_string()),
                    predicate: Term::Constant("purchased".to_string()),
                    object: Term::Variable("product2".to_string()),
                },
                RuleAtom::NotEqual {
                    left: Term::Variable("customer1".to_string()),
                    right: Term::Variable("customer2".to_string()),
                },
                RuleAtom::NotEqual {
                    left: Term::Variable("product1".to_string()),
                    right: Term::Variable("product2".to_string()),
                },
            ],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("customer1".to_string()),
                predicate: Term::Constant("recommended".to_string()),
                object: Term::Variable("product2".to_string()),
            }],
        };

        product_module.add_rule(also_bought_rule);

        // Set high priority for recommendation rules
        self.conflict.set_priority("also_bought", Priority::High);

        // Register the module
        self.composition.register_module(product_module)?;

        Ok(())
    }

    /// Initialize inventory validation rules using SHACL
    pub fn initialize_inventory_validation(&mut self) -> Result<()> {
        info!("Initializing inventory validation with SHACL");

        // Create SHACL constraints for inventory
        let _min_stock_constraint =
            ShapeConstraint::new("min_stock".to_string(), "sh:minCount".to_string());

        // Create repair rule for low stock
        let low_stock_repair = Rule {
            name: "reorder_low_stock".to_string(),
            body: vec![
                RuleAtom::Triple {
                    subject: Term::Variable("product".to_string()),
                    predicate: Term::Constant("stock".to_string()),
                    object: Term::Variable("quantity".to_string()),
                },
                RuleAtom::LessThan {
                    left: Term::Variable("quantity".to_string()),
                    right: Term::Literal("10".to_string()),
                },
            ],
            head: vec![RuleAtom::Triple {
                subject: Term::Variable("product".to_string()),
                predicate: Term::Constant("needsReorder".to_string()),
                object: Term::Constant("true".to_string()),
            }],
        };

        self.shacl
            .register_repair_rule("sh:minCount".to_string(), low_stock_repair);
        self.shacl.set_mode(ValidationMode::Full);

        Ok(())
    }

    /// Setup distributed reasoning with multiple nodes
    pub fn setup_distributed_reasoning(&mut self) -> Result<()> {
        info!("Setting up distributed reasoning infrastructure");

        // Register reasoning nodes
        let node1 =
            Node::new("node1".to_string(), "localhost:8001".to_string()).with_capacity(1000);
        let node2 =
            Node::new("node2".to_string(), "localhost:8002".to_string()).with_capacity(1500);
        let node3 =
            Node::new("node3".to_string(), "localhost:8003".to_string()).with_capacity(2000);

        self.distributed.register_node(node1)?;
        self.distributed.register_node(node2)?;
        self.distributed.register_node(node3)?;

        info!(
            "Registered {} reasoning nodes",
            self.distributed.node_count()
        );

        Ok(())
    }

    /// Process customer order with full transaction support
    pub fn process_order(&mut self, customer: &str, product: &str) -> Result<()> {
        info!(
            "Processing order for customer '{}' - product '{}'",
            customer, product
        );

        // Begin transaction
        let tx_id = self
            .transactions
            .begin_transaction(IsolationLevel::Serializable)?;
        debug!("Started transaction {}", tx_id);

        // Add purchase fact
        let purchase_fact = RuleAtom::Triple {
            subject: Term::Constant(customer.to_string()),
            predicate: Term::Constant("purchased".to_string()),
            object: Term::Constant(product.to_string()),
        };

        self.transactions.add_fact(tx_id, purchase_fact.clone())?;

        // Record explanation
        self.explanation.record_assertion(purchase_fact.clone());

        // Apply recommendation rules
        let derived = self.engine.forward_chain(&[purchase_fact])?;
        debug!("Derived {} recommendations", derived.len());

        // Commit transaction
        self.transactions.commit(tx_id)?;
        info!("Order processed successfully");

        Ok(())
    }

    /// Query recommendations using SPARQL integration
    pub fn query_recommendations(&mut self, customer: &str) -> Result<Vec<String>> {
        info!("Querying recommendations for customer '{}'", customer);

        // Set query mode to hybrid (forward + backward)
        self.sparql.engine_mut().add_facts(self.engine.get_facts());
        self.sparql.set_mode(QueryMode::Hybrid);

        // Create query pattern for recommendations
        let pattern = QueryPattern::new(
            Some(customer.to_string()),
            Some("recommended".to_string()),
            None, // Match any product
        );

        let results = self.sparql.query_with_reasoning(&[pattern])?;

        // Extract product names
        let products: Vec<String> = results
            .iter()
            .filter_map(|atom| {
                if let RuleAtom::Triple {
                    object: Term::Constant(product),
                    ..
                } = atom
                {
                    Some(product.clone())
                } else {
                    None
                }
            })
            .collect();

        info!("Found {} recommendations", products.len());
        Ok(products)
    }

    /// Validate inventory using SHACL
    pub fn validate_inventory(&mut self, inventory_facts: &[RuleAtom]) -> Result<bool> {
        info!("Validating inventory with {} facts", inventory_facts.len());

        let constraint =
            ShapeConstraint::new("inventory_check".to_string(), "sh:minCount".to_string());

        let report = self
            .shacl
            .validate_with_reasoning(&[constraint], inventory_facts)?;

        if !report.conforms {
            info!(
                "Validation failed: {} violations, {} warnings",
                report.violation_count(),
                report.warning_count()
            );
        } else {
            info!("Validation passed");
        }

        Ok(report.conforms)
    }

    /// Execute distributed reasoning for large datasets
    pub fn execute_distributed(&mut self, facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        info!(
            "Executing distributed reasoning across {} nodes",
            self.distributed.node_count()
        );

        let rules = self.composition.get_all_rules();
        let results = self.distributed.execute_distributed(&rules, facts)?;

        info!(
            "Distributed execution complete: {} facts derived",
            results.len()
        );
        Ok(results)
    }

    /// Get explanation for a recommendation
    pub fn explain_recommendation(&self, customer: &str, product: &str) -> Result<String> {
        let fact = RuleAtom::Triple {
            subject: Term::Constant(customer.to_string()),
            predicate: Term::Constant("recommended".to_string()),
            object: Term::Constant(product.to_string()),
        };

        let explanation = self.explanation.explain_why(&fact)?;
        Ok(format!("{}", explanation))
    }

    /// Get comprehensive statistics
    pub fn get_statistics(&self) -> SystemStatistics {
        SystemStatistics {
            total_rules: self.composition.get_all_rules().len(),
            total_modules: self.composition.get_stats().total_modules,
            total_nodes: self.distributed.node_count(),
            sparql_queries: self.sparql.get_stats().total_queries,
            shacl_validations: self.shacl.get_stats().total_validations,
            transactions: self.transactions.get_stats().total_transactions,
            explanations: self.explanation.get_stats().total_derivations,
        }
    }
}

impl Default for EcommerceReasoningSystem {
    fn default() -> Self {
        Self::new()
    }
}

/// System statistics
#[derive(Debug, Clone)]
pub struct SystemStatistics {
    pub total_rules: usize,
    pub total_modules: usize,
    pub total_nodes: usize,
    pub sparql_queries: usize,
    pub shacl_validations: usize,
    pub transactions: usize,
    pub explanations: usize,
}

impl std::fmt::Display for SystemStatistics {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "E-Commerce Reasoning System Statistics:")?;
        writeln!(f, "  Rules: {}", self.total_rules)?;
        writeln!(f, "  Modules: {}", self.total_modules)?;
        writeln!(f, "  Distributed Nodes: {}", self.total_nodes)?;
        writeln!(f, "  SPARQL Queries: {}", self.sparql_queries)?;
        writeln!(f, "  SHACL Validations: {}", self.shacl_validations)?;
        writeln!(f, "  Transactions: {}", self.transactions)?;
        writeln!(f, "  Explanations Generated: {}", self.explanations)?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ecommerce_system_creation() {
        let system = EcommerceReasoningSystem::new();
        assert_eq!(system.distributed.node_count(), 0);
    }

    #[test]
    fn test_initialize_product_rules() -> Result<(), Box<dyn std::error::Error>> {
        let mut system = EcommerceReasoningSystem::new();
        system.initialize_product_rules()?;

        let stats = system.get_statistics();
        assert!(stats.total_modules > 0);
        Ok(())
    }

    #[test]
    fn test_setup_distributed_reasoning() -> Result<(), Box<dyn std::error::Error>> {
        let mut system = EcommerceReasoningSystem::new();
        system.setup_distributed_reasoning()?;

        assert_eq!(system.distributed.node_count(), 3);
        Ok(())
    }

    #[test]
    fn test_process_order() -> Result<(), Box<dyn std::error::Error>> {
        let mut system = EcommerceReasoningSystem::new();
        system.initialize_product_rules()?;

        let result = system.process_order("customer1", "product1");
        assert!(result.is_ok());

        let stats = system.get_statistics();
        assert!(stats.transactions > 0);
        Ok(())
    }

    #[test]
    fn test_validate_inventory() -> Result<(), Box<dyn std::error::Error>> {
        let mut system = EcommerceReasoningSystem::new();
        system.initialize_inventory_validation()?;

        let inventory_facts = vec![RuleAtom::Triple {
            subject: Term::Constant("product1".to_string()),
            predicate: Term::Constant("stock".to_string()),
            object: Term::Literal("100".to_string()),
        }];

        let valid = system.validate_inventory(&inventory_facts)?;
        assert!(valid);
        Ok(())
    }

    #[test]
    fn test_full_integration_workflow() -> Result<(), Box<dyn std::error::Error>> {
        let mut system = EcommerceReasoningSystem::new();

        // Initialize all components
        system.initialize_product_rules()?;
        system.initialize_inventory_validation()?;
        system.setup_distributed_reasoning()?;

        // Process some orders
        system.process_order("alice", "laptop")?;
        system.process_order("bob", "laptop")?;
        system.process_order("bob", "mouse")?;

        // Query recommendations (alice should get mouse recommended)
        let recommendations = system.query_recommendations("alice").unwrap_or_default();

        // Get statistics
        let stats = system.get_statistics();
        assert!(stats.total_modules > 0);
        assert!(stats.total_nodes == 3);

        println!("\n{}", stats);
        println!("Recommendations for alice: {:?}", recommendations);
        Ok(())
    }

    #[test]
    fn test_statistics_display() {
        let system = EcommerceReasoningSystem::new();
        let stats = system.get_statistics();

        let display = format!("{}", stats);
        assert!(display.contains("E-Commerce Reasoning System Statistics"));
    }
}