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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
//! Rule Conflict Resolution
//!
//! Handles conflicts when multiple rules can derive contradictory or competing facts.
//! Provides priority-based resolution, specificity ordering, and conflict detection.
//!
//! # Features
//!
//! - **Priority-Based Resolution**: Rules with higher priority take precedence
//! - **Specificity Ordering**: More specific rules override general rules
//! - **Conflict Detection**: Identify contradictory derivations
//! - **Resolution Strategies**: Multiple strategies for handling conflicts
//! - **Confidence Scoring**: Score facts based on derivation strength
//!
//! # Example
//!
//! ```rust
//! use oxirs_rule::conflict::{ConflictResolver, ResolutionStrategy, Priority};
//! use oxirs_rule::{Rule, RuleAtom, Term};
//!
//! let mut resolver = ConflictResolver::new();
//!
//! // Add rules with priorities
//! let rule1 = Rule {
//!     name: "general_rule".to_string(),
//!     body: vec![],
//!     head: vec![],
//! };
//!
//! let rule2 = Rule {
//!     name: "specific_rule".to_string(),
//!     body: vec![],
//!     head: vec![],
//! };
//!
//! resolver.set_priority("general_rule", Priority::Low);
//! resolver.set_priority("specific_rule", Priority::High);
//!
//! // Resolve conflicts
//! let strategy = ResolutionStrategy::Priority;
//! resolver.set_strategy(strategy);
//! ```

use crate::{Rule, RuleAtom};
use anyhow::Result;
use std::cmp::Ordering;
use std::collections::HashMap;
use tracing::{debug, info, warn};

/// Rule priority levels
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum Priority {
    /// Lowest priority
    VeryLow = 0,
    /// Low priority
    Low = 1,
    /// Normal priority (default)
    #[default]
    Normal = 2,
    /// High priority
    High = 3,
    /// Highest priority
    VeryHigh = 4,
}

/// Conflict resolution strategy
#[derive(Debug, Clone, PartialEq)]
pub enum ResolutionStrategy {
    /// Use rule priorities
    Priority,
    /// Use rule specificity (more specific rules win)
    Specificity,
    /// Use both priority and specificity
    Combined,
    /// Keep all conflicting derivations
    KeepAll,
    /// Reject all conflicting derivations
    RejectAll,
    /// Use confidence scores
    Confidence,
}

/// Conflict between rules
#[derive(Debug, Clone)]
pub struct Conflict {
    /// Rules involved in the conflict
    pub rules: Vec<String>,
    /// Conflicting facts
    pub facts: Vec<RuleAtom>,
    /// Description of the conflict
    pub description: String,
    /// Severity of the conflict
    pub severity: ConflictSeverity,
}

/// Severity of a conflict
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ConflictSeverity {
    /// Low severity - minor inconsistency
    Low,
    /// Medium severity - potentially problematic
    Medium,
    /// High severity - serious inconsistency
    High,
    /// Critical - logical contradiction
    Critical,
}

/// Conflict resolver
pub struct ConflictResolver {
    /// Rule priorities
    priorities: HashMap<String, Priority>,
    /// Rule specificity scores
    specificity: HashMap<String, f64>,
    /// Active resolution strategy
    strategy: ResolutionStrategy,
    /// Detected conflicts
    conflicts: Vec<Conflict>,
    /// Confidence scores for facts
    confidence_scores: HashMap<RuleAtom, f64>,
}

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

impl ConflictResolver {
    /// Create a new conflict resolver
    pub fn new() -> Self {
        Self {
            priorities: HashMap::new(),
            specificity: HashMap::new(),
            strategy: ResolutionStrategy::Combined,
            conflicts: Vec::new(),
            confidence_scores: HashMap::new(),
        }
    }

    /// Set priority for a rule
    pub fn set_priority(&mut self, rule_name: &str, priority: Priority) {
        self.priorities.insert(rule_name.to_string(), priority);
        debug!("Set priority for rule '{}': {:?}", rule_name, priority);
    }

    /// Get priority for a rule
    pub fn get_priority(&self, rule_name: &str) -> Priority {
        self.priorities.get(rule_name).copied().unwrap_or_default()
    }

    /// Set resolution strategy
    pub fn set_strategy(&mut self, strategy: ResolutionStrategy) {
        info!("Set resolution strategy: {:?}", strategy);
        self.strategy = strategy;
    }

    /// Calculate specificity of a rule
    pub fn calculate_specificity(&mut self, rule: &Rule) -> f64 {
        // Specificity is based on:
        // 1. Number of conditions in body (more = more specific)
        // 2. Number of variables (fewer = more specific)
        // 3. Number of constants (more = more specific)

        let body_size = rule.body.len() as f64;
        let mut variable_count = 0;
        let mut constant_count = 0;

        for atom in &rule.body {
            self.count_terms_in_atom(atom, &mut variable_count, &mut constant_count);
        }

        // Higher score = more specific
        let specificity = body_size + (constant_count as f64 * 2.0) - (variable_count as f64 * 0.5);

        self.specificity.insert(rule.name.clone(), specificity);
        specificity
    }

    /// Count variables and constants in an atom
    fn count_terms_in_atom(&self, atom: &RuleAtom, variables: &mut usize, constants: &mut usize) {
        match atom {
            RuleAtom::Triple {
                subject,
                predicate,
                object,
            } => {
                Self::count_term(subject, variables, constants);
                Self::count_term(predicate, variables, constants);
                Self::count_term(object, variables, constants);
            }
            RuleAtom::Builtin { args, .. } => {
                for arg in args {
                    Self::count_term(arg, variables, constants);
                }
            }
            RuleAtom::NotEqual { left, right }
            | RuleAtom::GreaterThan { left, right }
            | RuleAtom::LessThan { left, right } => {
                Self::count_term(left, variables, constants);
                Self::count_term(right, variables, constants);
            }
        }
    }

    /// Count a single term
    fn count_term(term: &crate::Term, variables: &mut usize, constants: &mut usize) {
        match term {
            crate::Term::Variable(_) => *variables += 1,
            crate::Term::Constant(_) | crate::Term::Literal(_) => *constants += 1,
            crate::Term::Function { args, .. } => {
                for arg in args {
                    Self::count_term(arg, variables, constants);
                }
            }
        }
    }

    /// Resolve conflicts between rules
    pub fn resolve_conflicts(
        &mut self,
        rules: &[Rule],
        facts: &[RuleAtom],
    ) -> Result<Vec<RuleAtom>> {
        info!("Resolving conflicts using strategy: {:?}", self.strategy);

        // Detect conflicts
        self.detect_conflicts(rules, facts)?;

        // Apply resolution strategy
        let resolved = match self.strategy {
            ResolutionStrategy::Priority => self.resolve_by_priority(rules, facts)?,
            ResolutionStrategy::Specificity => self.resolve_by_specificity(rules, facts)?,
            ResolutionStrategy::Combined => self.resolve_combined(rules, facts)?,
            ResolutionStrategy::KeepAll => facts.to_vec(),
            ResolutionStrategy::RejectAll => {
                if !self.conflicts.is_empty() {
                    warn!("Rejecting all facts due to conflicts");
                    vec![]
                } else {
                    facts.to_vec()
                }
            }
            ResolutionStrategy::Confidence => self.resolve_by_confidence(facts)?,
        };

        info!(
            "Conflict resolution complete: {} facts retained",
            resolved.len()
        );
        Ok(resolved)
    }

    /// Detect conflicts in derived facts
    fn detect_conflicts(&mut self, _rules: &[Rule], facts: &[RuleAtom]) -> Result<()> {
        self.conflicts.clear();

        // Check for negation conflicts (simplified)
        // In a full implementation, we would check for:
        // - Contradictory facts (e.g., both P and NOT P)
        // - Inconsistent cardinality constraints
        // - Disjoint class violations
        // etc.

        // For now, just detect duplicate facts with different sources
        let mut fact_sources: HashMap<RuleAtom, Vec<String>> = HashMap::new();

        for fact in facts {
            fact_sources
                .entry(fact.clone())
                .or_default()
                .push("source".to_string());
        }

        for (fact, sources) in fact_sources {
            if sources.len() > 1 {
                self.conflicts.push(Conflict {
                    rules: sources,
                    facts: vec![fact],
                    description: "Multiple derivation paths".to_string(),
                    severity: ConflictSeverity::Low,
                });
            }
        }

        debug!("Detected {} conflicts", self.conflicts.len());
        Ok(())
    }

    /// Resolve conflicts by priority
    fn resolve_by_priority(&self, _rules: &[Rule], facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        // Keep all facts for now
        // In a full implementation, we would track which rule derived each fact
        // and filter based on priority
        Ok(facts.to_vec())
    }

    /// Resolve conflicts by specificity
    fn resolve_by_specificity(&self, _rules: &[Rule], facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        // Keep all facts for now
        // In a full implementation, we would use specificity scores
        Ok(facts.to_vec())
    }

    /// Resolve conflicts using combined strategy
    fn resolve_combined(&self, rules: &[Rule], facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        // Use both priority and specificity
        // Priority takes precedence, then specificity
        self.resolve_by_priority(rules, facts)
    }

    /// Resolve conflicts by confidence scores
    fn resolve_by_confidence(&self, facts: &[RuleAtom]) -> Result<Vec<RuleAtom>> {
        let mut scored_facts: Vec<(RuleAtom, f64)> = facts
            .iter()
            .map(|f| {
                let confidence = self.confidence_scores.get(f).copied().unwrap_or(1.0);
                (f.clone(), confidence)
            })
            .collect();

        // Sort by confidence (highest first)
        scored_facts.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));

        // Keep facts with confidence above threshold
        let threshold = 0.5;
        let resolved: Vec<RuleAtom> = scored_facts
            .into_iter()
            .filter(|(_, conf)| *conf >= threshold)
            .map(|(fact, _)| fact)
            .collect();

        Ok(resolved)
    }

    /// Set confidence score for a fact
    pub fn set_confidence(&mut self, fact: RuleAtom, confidence: f64) {
        self.confidence_scores
            .insert(fact, confidence.clamp(0.0, 1.0));
    }

    /// Get detected conflicts
    pub fn get_conflicts(&self) -> &[Conflict] {
        &self.conflicts
    }

    /// Check if there are any conflicts
    pub fn has_conflicts(&self) -> bool {
        !self.conflicts.is_empty()
    }

    /// Get statistics
    pub fn get_stats(&self) -> ConflictStats {
        let critical_conflicts = self
            .conflicts
            .iter()
            .filter(|c| c.severity == ConflictSeverity::Critical)
            .count();

        let high_conflicts = self
            .conflicts
            .iter()
            .filter(|c| c.severity == ConflictSeverity::High)
            .count();

        ConflictStats {
            total_conflicts: self.conflicts.len(),
            critical_conflicts,
            high_conflicts,
            rules_with_priority: self.priorities.len(),
            active_strategy: self.strategy.clone(),
        }
    }
}

/// Statistics about conflict resolution
#[derive(Debug, Clone)]
pub struct ConflictStats {
    pub total_conflicts: usize,
    pub critical_conflicts: usize,
    pub high_conflicts: usize,
    pub rules_with_priority: usize,
    pub active_strategy: ResolutionStrategy,
}

impl std::fmt::Display for ConflictStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Conflicts: {} (critical: {}, high: {}), Rules with priority: {}, Strategy: {:?}",
            self.total_conflicts,
            self.critical_conflicts,
            self.high_conflicts,
            self.rules_with_priority,
            self.active_strategy
        )
    }
}

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

    #[test]
    fn test_priority_setting() {
        let mut resolver = ConflictResolver::new();

        resolver.set_priority("rule1", Priority::High);
        assert_eq!(resolver.get_priority("rule1"), Priority::High);
        assert_eq!(resolver.get_priority("rule2"), Priority::Normal);
    }

    #[test]
    fn test_specificity_calculation() {
        let mut resolver = ConflictResolver::new();

        let rule = Rule {
            name: "test".to_string(),
            body: vec![RuleAtom::Triple {
                subject: Term::Variable("X".to_string()),
                predicate: Term::Constant("type".to_string()),
                object: Term::Constant("Person".to_string()),
            }],
            head: vec![],
        };

        let specificity = resolver.calculate_specificity(&rule);
        assert!(specificity > 0.0);
    }

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

        let rules = vec![];
        let facts = vec![RuleAtom::Triple {
            subject: Term::Constant("john".to_string()),
            predicate: Term::Constant("age".to_string()),
            object: Term::Literal("30".to_string()),
        }];

        resolver.detect_conflicts(&rules, &facts)?;
        // Should not detect conflicts for single fact
        assert_eq!(resolver.conflicts.len(), 0);
        Ok(())
    }

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

        let fact = RuleAtom::Triple {
            subject: Term::Constant("john".to_string()),
            predicate: Term::Constant("type".to_string()),
            object: Term::Constant("Person".to_string()),
        };

        resolver.set_confidence(fact.clone(), 0.9);

        let facts = vec![fact];
        resolver.set_strategy(ResolutionStrategy::Confidence);

        let resolved = resolver.resolve_conflicts(&[], &facts)?;
        assert_eq!(resolved.len(), 1);
        Ok(())
    }

    #[test]
    fn test_stats() {
        let resolver = ConflictResolver::new();
        let stats = resolver.get_stats();

        assert_eq!(stats.total_conflicts, 0);
        assert_eq!(stats.rules_with_priority, 0);
    }
}