oxirs-chat 0.2.4

RAG chat API with LLM integration and natural language to SPARQL translation
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Advanced Reasoning Module for OxiRS Chat RAG System
//!
//! Implements sophisticated reasoning capabilities including:
//! - Multi-step logical inference
//! - Causal reasoning chains
//! - Probabilistic reasoning with uncertainty quantification
//! - Analogical reasoning for pattern matching
//! - Temporal reasoning for time-sensitive queries

use crate::rag::AssembledContext;
use anyhow::{anyhow, Result};
use chrono::{DateTime, Utc};
use oxirs_core::model::triple::Triple;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use tracing::{debug, info};

/// Configuration for advanced reasoning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningConfig {
    pub max_inference_depth: usize,
    pub confidence_threshold: f64,
    pub enable_causal_reasoning: bool,
    pub enable_temporal_reasoning: bool,
    pub enable_analogical_reasoning: bool,
    pub uncertainty_quantification: bool,
}

impl Default for ReasoningConfig {
    fn default() -> Self {
        Self {
            max_inference_depth: 5,
            confidence_threshold: 0.7,
            enable_causal_reasoning: true,
            enable_temporal_reasoning: true,
            enable_analogical_reasoning: true,
            uncertainty_quantification: true,
        }
    }
}

/// Types of reasoning patterns
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ReasoningType {
    /// Direct logical inference (A → B, B → C, therefore A → C)
    Deductive,
    /// Pattern-based inference from examples
    Inductive,
    /// Cause-and-effect reasoning
    Causal,
    /// Time-based sequential reasoning
    Temporal,
    /// Similarity-based reasoning
    Analogical,
    /// Probabilistic inference with uncertainty
    Probabilistic,
}

/// A single reasoning step in a chain
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningStep {
    pub step_id: String,
    pub reasoning_type: ReasoningType,
    pub premise_triples: Vec<Triple>,
    pub conclusion_triple: Option<Triple>,
    pub confidence: f64,
    pub explanation: String,
    pub timestamp: DateTime<Utc>,
}

/// A complete reasoning chain from premise to conclusion
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningChain {
    pub chain_id: String,
    pub query: String,
    pub steps: Vec<ReasoningStep>,
    pub final_conclusion: Option<Triple>,
    pub overall_confidence: f64,
    pub reasoning_time_ms: u64,
    pub alternative_chains: Vec<AlternativeChain>,
}

/// Alternative reasoning paths with different conclusions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AlternativeChain {
    pub chain_id: String,
    pub steps: Vec<ReasoningStep>,
    pub conclusion: Option<Triple>,
    pub confidence: f64,
    pub divergence_point: usize,
}

/// Result of reasoning analysis
#[derive(Debug, Clone)]
pub struct ReasoningResult {
    pub primary_chain: ReasoningChain,
    pub supporting_evidence: Vec<Triple>,
    pub contradicting_evidence: Vec<Triple>,
    pub uncertainty_factors: Vec<UncertaintyFactor>,
    pub reasoning_quality: ReasoningQuality,
}

/// Factors contributing to reasoning uncertainty
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UncertaintyFactor {
    pub factor_type: UncertaintyType,
    pub description: String,
    pub impact_score: f64,
    pub mitigation_strategy: Option<String>,
}

/// Types of uncertainty in reasoning
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum UncertaintyType {
    /// Insufficient evidence for conclusion
    InsufficientEvidence,
    /// Conflicting evidence exists
    ConflictingEvidence,
    /// Temporal inconsistencies
    TemporalInconsistency,
    /// Causal chain gaps
    CausalGaps,
    /// Statistical uncertainty
    StatisticalUncertainty,
}

/// Quality assessment of reasoning process
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReasoningQuality {
    pub logical_consistency: f64,
    pub evidence_strength: f64,
    pub chain_completeness: f64,
    pub temporal_coherence: f64,
    pub overall_quality: f64,
}

/// Advanced reasoning engine
pub struct AdvancedReasoningEngine {
    config: ReasoningConfig,
    reasoning_patterns: HashMap<String, ReasoningPattern>,
    causal_knowledge: CausalKnowledgeBase,
    temporal_model: TemporalReasoningModel,
    analogical_matcher: AnalogicalMatcher,
}

/// Reasoning pattern template
#[derive(Debug, Clone)]
struct ReasoningPattern {
    pattern_id: String,
    pattern_type: ReasoningType,
    premise_template: String,
    conclusion_template: String,
    confidence_modifier: f64,
}

/// Causal knowledge base for cause-effect reasoning
#[derive(Debug, Clone)]
struct CausalKnowledgeBase {
    causal_relations: HashMap<String, Vec<CausalRelation>>,
}

/// Temporal reasoning model
#[derive(Debug, Clone)]
struct TemporalReasoningModel {
    temporal_relations: HashMap<String, TemporalRelation>,
    time_constraints: Vec<TimeConstraint>,
}

/// Analogical pattern matcher
#[derive(Debug, Clone)]
struct AnalogicalMatcher {
    similarity_patterns: HashMap<String, Vec<AnalogicalPattern>>,
}

#[derive(Debug, Clone)]
struct CausalRelation {
    cause: String,
    effect: String,
    strength: f64,
    conditions: Vec<String>,
}

#[derive(Debug, Clone)]
struct TemporalRelation {
    relation_type: String,
    before_entity: String,
    after_entity: String,
    time_interval: Option<std::time::Duration>,
}

#[derive(Debug, Clone)]
struct TimeConstraint {
    constraint_type: String,
    entities: Vec<String>,
    temporal_bound: DateTime<Utc>,
}

#[derive(Debug, Clone)]
struct AnalogicalPattern {
    source_domain: String,
    target_domain: String,
    mapping_strength: f64,
    structural_similarity: f64,
}

impl AdvancedReasoningEngine {
    /// Create a new advanced reasoning engine
    pub fn new(config: ReasoningConfig) -> Self {
        Self {
            config,
            reasoning_patterns: Self::initialize_reasoning_patterns(),
            causal_knowledge: CausalKnowledgeBase {
                causal_relations: HashMap::new(),
            },
            temporal_model: TemporalReasoningModel {
                temporal_relations: HashMap::new(),
                time_constraints: Vec::new(),
            },
            analogical_matcher: AnalogicalMatcher {
                similarity_patterns: HashMap::new(),
            },
        }
    }

    /// Initialize standard reasoning patterns
    fn initialize_reasoning_patterns() -> HashMap<String, ReasoningPattern> {
        let mut patterns = HashMap::new();

        // Deductive reasoning patterns
        patterns.insert(
            "modus_ponens".to_string(),
            ReasoningPattern {
                pattern_id: "modus_ponens".to_string(),
                pattern_type: ReasoningType::Deductive,
                premise_template: "If {P} then {Q}; {P} is true".to_string(),
                conclusion_template: "Therefore {Q} is true".to_string(),
                confidence_modifier: 0.95,
            },
        );

        // Causal reasoning patterns
        patterns.insert(
            "causal_chain".to_string(),
            ReasoningPattern {
                pattern_id: "causal_chain".to_string(),
                pattern_type: ReasoningType::Causal,
                premise_template: "{A} causes {B}; {B} causes {C}".to_string(),
                conclusion_template: "{A} causes {C}".to_string(),
                confidence_modifier: 0.8,
            },
        );

        // Temporal reasoning patterns
        patterns.insert(
            "temporal_sequence".to_string(),
            ReasoningPattern {
                pattern_id: "temporal_sequence".to_string(),
                pattern_type: ReasoningType::Temporal,
                premise_template: "{A} happens before {B}; {B} happens before {C}".to_string(),
                conclusion_template: "{A} happens before {C}".to_string(),
                confidence_modifier: 0.9,
            },
        );

        patterns
    }

    /// Perform advanced reasoning on assembled context
    pub async fn reason(
        &mut self,
        query: &str,
        context: &AssembledContext,
    ) -> Result<ReasoningResult> {
        let start_time = std::time::Instant::now();
        info!("Starting advanced reasoning for query: {}", query);

        // Build reasoning chains from different perspectives
        let mut reasoning_chains = Vec::new();

        // Deductive reasoning chain
        if let Some(deductive_chain) = self.build_deductive_chain(query, context).await? {
            reasoning_chains.push(deductive_chain);
        }

        // Causal reasoning chain
        if self.config.enable_causal_reasoning {
            if let Some(causal_chain) = self.build_causal_chain(query, context).await? {
                reasoning_chains.push(causal_chain);
            }
        }

        // Temporal reasoning chain
        if self.config.enable_temporal_reasoning {
            if let Some(temporal_chain) = self.build_temporal_chain(query, context).await? {
                reasoning_chains.push(temporal_chain);
            }
        }

        // Analogical reasoning chain
        if self.config.enable_analogical_reasoning {
            if let Some(analogical_chain) = self.build_analogical_chain(query, context).await? {
                reasoning_chains.push(analogical_chain);
            }
        }

        // Select the best reasoning chain
        let primary_chain = self.select_best_chain(reasoning_chains)?;

        // Gather supporting and contradicting evidence
        let (supporting_evidence, contradicting_evidence) =
            self.gather_evidence(&primary_chain, context).await?;

        // Quantify uncertainty if enabled
        let uncertainty_factors = if self.config.uncertainty_quantification {
            self.quantify_uncertainty(&primary_chain, context).await?
        } else {
            Vec::new()
        };

        // Assess reasoning quality
        let reasoning_quality = self
            .assess_reasoning_quality(&primary_chain, context)
            .await?;

        let reasoning_time = start_time.elapsed().as_millis() as u64;
        info!("Advanced reasoning completed in {}ms", reasoning_time);

        Ok(ReasoningResult {
            primary_chain,
            supporting_evidence,
            contradicting_evidence,
            uncertainty_factors,
            reasoning_quality,
        })
    }

    /// Build deductive reasoning chain
    async fn build_deductive_chain(
        &self,
        query: &str,
        context: &AssembledContext,
    ) -> Result<Option<ReasoningChain>> {
        debug!("Building deductive reasoning chain");

        let mut steps = Vec::new();
        let mut current_premises = context
            .semantic_results
            .iter()
            .map(|r| r.triple.clone())
            .collect::<Vec<_>>();

        // Apply modus ponens pattern iteratively
        for depth in 0..self.config.max_inference_depth {
            if let Some(new_conclusion) = self.apply_modus_ponens(&current_premises)? {
                let step = ReasoningStep {
                    step_id: format!("deductive_step_{depth}"),
                    reasoning_type: ReasoningType::Deductive,
                    premise_triples: current_premises.clone(),
                    conclusion_triple: Some(new_conclusion.clone()),
                    confidence: 0.9 - (depth as f64 * 0.1),
                    explanation: format!("Applied deductive inference at depth {depth}"),
                    timestamp: Utc::now(),
                };
                steps.push(step);
                current_premises.push(new_conclusion);
            } else {
                break;
            }
        }

        if steps.is_empty() {
            return Ok(None);
        }

        let overall_confidence = steps
            .iter()
            .map(|s| s.confidence)
            .fold(1.0, |acc, conf| acc * conf);

        Ok(Some(ReasoningChain {
            chain_id: uuid::Uuid::new_v4().to_string(),
            query: query.to_string(),
            steps,
            final_conclusion: current_premises.last().cloned(),
            overall_confidence,
            reasoning_time_ms: 0, // Will be set by caller
            alternative_chains: Vec::new(),
        }))
    }

    /// Build causal reasoning chain
    async fn build_causal_chain(
        &self,
        query: &str,
        context: &AssembledContext,
    ) -> Result<Option<ReasoningChain>> {
        debug!("Building causal reasoning chain");

        // Look for causal relationships in the context
        let causal_triples = context
            .semantic_results
            .iter()
            .filter(|r| self.is_causal_relation(&r.triple))
            .map(|r| r.triple.clone())
            .collect::<Vec<_>>();

        if causal_triples.is_empty() {
            return Ok(None);
        }

        let mut steps = Vec::new();
        let mut causal_chain = Vec::new();

        // Build causal chain step by step
        for (i, triple) in causal_triples.iter().enumerate() {
            let step = ReasoningStep {
                step_id: format!("causal_step_{i}"),
                reasoning_type: ReasoningType::Causal,
                premise_triples: vec![triple.clone()],
                conclusion_triple: None, // Will be derived from causal inference
                confidence: 0.8,
                explanation: format!("Identified causal relationship: {}", triple.object()),
                timestamp: Utc::now(),
            };
            steps.push(step);
            causal_chain.push(triple.clone());
        }

        let overall_confidence = 0.8_f64.powi(steps.len() as i32);

        Ok(Some(ReasoningChain {
            chain_id: uuid::Uuid::new_v4().to_string(),
            query: query.to_string(),
            steps,
            final_conclusion: causal_chain.last().cloned(),
            overall_confidence,
            reasoning_time_ms: 0,
            alternative_chains: Vec::new(),
        }))
    }

    /// Build temporal reasoning chain
    async fn build_temporal_chain(
        &self,
        query: &str,
        context: &AssembledContext,
    ) -> Result<Option<ReasoningChain>> {
        debug!("Building temporal reasoning chain");

        // Look for temporal relationships
        let temporal_triples = context
            .semantic_results
            .iter()
            .filter(|r| self.is_temporal_relation(&r.triple))
            .map(|r| r.triple.clone())
            .collect::<Vec<_>>();

        if temporal_triples.is_empty() {
            return Ok(None);
        }

        // Sort by temporal order if possible
        let mut sorted_triples = temporal_triples;

        // Implement temporal sorting based on timestamps and sequential relationships
        sorted_triples.sort_by(|a, b| {
            // Try to extract temporal information from the triple objects
            let a_temporal_score = self.extract_temporal_score(a);
            let b_temporal_score = self.extract_temporal_score(b);

            a_temporal_score
                .partial_cmp(&b_temporal_score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        let mut steps = Vec::new();
        for (i, triple) in sorted_triples.iter().enumerate() {
            let step = ReasoningStep {
                step_id: format!("temporal_step_{i}"),
                reasoning_type: ReasoningType::Temporal,
                premise_triples: vec![triple.clone()],
                conclusion_triple: None,
                confidence: 0.85,
                explanation: format!("Temporal sequence element: {}", triple.object()),
                timestamp: Utc::now(),
            };
            steps.push(step);
        }

        let overall_confidence = 0.85_f64.powi(steps.len() as i32);

        Ok(Some(ReasoningChain {
            chain_id: uuid::Uuid::new_v4().to_string(),
            query: query.to_string(),
            steps,
            final_conclusion: sorted_triples.last().cloned(),
            overall_confidence,
            reasoning_time_ms: 0,
            alternative_chains: Vec::new(),
        }))
    }

    /// Build analogical reasoning chain
    async fn build_analogical_chain(
        &self,
        query: &str,
        context: &AssembledContext,
    ) -> Result<Option<ReasoningChain>> {
        debug!("Building analogical reasoning chain");

        // Find analogical patterns in the data
        let analogical_candidates = context
            .semantic_results
            .iter()
            .filter(|r| self.has_analogical_potential(&r.triple))
            .map(|r| r.triple.clone())
            .collect::<Vec<_>>();

        if analogical_candidates.is_empty() {
            return Ok(None);
        }

        let mut steps = Vec::new();
        for (i, triple) in analogical_candidates.iter().enumerate() {
            let step = ReasoningStep {
                step_id: format!("analogical_step_{i}"),
                reasoning_type: ReasoningType::Analogical,
                premise_triples: vec![triple.clone()],
                conclusion_triple: None,
                confidence: 0.7, // Lower confidence for analogical reasoning
                explanation: format!("Analogical pattern identified: {}", triple.object()),
                timestamp: Utc::now(),
            };
            steps.push(step);
        }

        let overall_confidence = 0.7_f64.powi(steps.len() as i32);

        Ok(Some(ReasoningChain {
            chain_id: uuid::Uuid::new_v4().to_string(),
            query: query.to_string(),
            steps,
            final_conclusion: analogical_candidates.last().cloned(),
            overall_confidence,
            reasoning_time_ms: 0,
            alternative_chains: Vec::new(),
        }))
    }

    /// Apply modus ponens reasoning pattern
    fn apply_modus_ponens(&self, premises: &[Triple]) -> Result<Option<Triple>> {
        // Simplified modus ponens: look for implication patterns
        // In a real implementation, this would involve sophisticated logical inference

        for premise in premises {
            // Look for "implies" or similar predicates
            let predicate_str = premise.predicate().to_string();
            if predicate_str.contains("implies") || predicate_str.contains("causes") {
                // Extract conclusion from implication
                // This is a simplified version - real implementation would be more sophisticated
                return Ok(Some(premise.clone()));
            }
        }

        Ok(None)
    }

    /// Check if a triple represents a causal relation
    fn is_causal_relation(&self, triple: &Triple) -> bool {
        let predicate = triple.predicate().to_string().to_lowercase();
        predicate.contains("cause")
            || predicate.contains("result")
            || predicate.contains("lead")
            || predicate.contains("effect")
    }

    /// Check if a triple represents a temporal relation
    fn is_temporal_relation(&self, triple: &Triple) -> bool {
        let predicate = triple.predicate().to_string().to_lowercase();
        predicate.contains("before")
            || predicate.contains("after")
            || predicate.contains("during")
            || predicate.contains("when")
            || predicate.contains("time")
    }

    /// Check if a triple has analogical potential
    fn has_analogical_potential(&self, triple: &Triple) -> bool {
        let predicate = triple.predicate().to_string().to_lowercase();
        predicate.contains("similar")
            || predicate.contains("like")
            || predicate.contains("analogy")
            || predicate.contains("resemble")
    }

    /// Select the best reasoning chain from candidates
    fn select_best_chain(&self, chains: Vec<ReasoningChain>) -> Result<ReasoningChain> {
        if chains.is_empty() {
            return Err(anyhow!("No valid reasoning chains found"));
        }

        // Select chain with highest confidence above threshold
        let best_chain = chains
            .into_iter()
            .filter(|chain| chain.overall_confidence >= self.config.confidence_threshold)
            .max_by(|a, b| {
                a.overall_confidence
                    .partial_cmp(&b.overall_confidence)
                    .unwrap_or(std::cmp::Ordering::Equal)
            });

        best_chain.ok_or_else(|| anyhow!("No reasoning chain meets confidence threshold"))
    }

    /// Gather supporting and contradicting evidence
    async fn gather_evidence(
        &self,
        _chain: &ReasoningChain,
        context: &AssembledContext,
    ) -> Result<(Vec<Triple>, Vec<Triple>)> {
        let mut supporting = Vec::new();
        let mut contradicting = Vec::new();

        // Simple evidence gathering based on semantic similarity
        for result in &context.semantic_results {
            if result.score > 0.8 {
                supporting.push(result.triple.clone());
            } else if result.score < 0.3 {
                contradicting.push(result.triple.clone());
            }
        }

        Ok((supporting, contradicting))
    }

    /// Quantify uncertainty in reasoning
    async fn quantify_uncertainty(
        &self,
        chain: &ReasoningChain,
        context: &AssembledContext,
    ) -> Result<Vec<UncertaintyFactor>> {
        let mut factors = Vec::new();

        // Check for insufficient evidence
        if context.semantic_results.len() < 3 {
            factors.push(UncertaintyFactor {
                factor_type: UncertaintyType::InsufficientEvidence,
                description: "Limited evidence available for reasoning".to_string(),
                impact_score: 0.3,
                mitigation_strategy: Some("Gather more relevant information".to_string()),
            });
        }

        // Check for conflicting evidence
        let confidence_variance = chain
            .steps
            .iter()
            .map(|s| s.confidence)
            .fold((0.0, 0.0), |acc, conf| (acc.0 + conf, acc.1 + conf * conf));

        let mean_confidence = confidence_variance.0 / chain.steps.len() as f64;
        let variance =
            (confidence_variance.1 / chain.steps.len() as f64) - mean_confidence * mean_confidence;

        if variance > 0.1 {
            factors.push(UncertaintyFactor {
                factor_type: UncertaintyType::ConflictingEvidence,
                description: "High variance in step confidences".to_string(),
                impact_score: variance,
                mitigation_strategy: Some("Resolve conflicting information".to_string()),
            });
        }

        Ok(factors)
    }

    /// Assess overall reasoning quality
    async fn assess_reasoning_quality(
        &self,
        chain: &ReasoningChain,
        context: &AssembledContext,
    ) -> Result<ReasoningQuality> {
        // Logical consistency
        let logical_consistency = chain
            .steps
            .iter()
            .map(|s| s.confidence)
            .fold(0.0, |acc, conf| acc + conf)
            / chain.steps.len() as f64;

        // Evidence strength
        let evidence_strength = context
            .semantic_results
            .iter()
            .map(|r| r.score as f64)
            .fold(0.0, |acc, score| acc + score)
            / context.semantic_results.len().max(1) as f64;

        // Chain completeness
        let chain_completeness = if chain.final_conclusion.is_some() {
            1.0
        } else {
            0.5
        };

        // Temporal coherence (enhanced analysis)
        let temporal_coherence = self.analyze_temporal_coherence(chain);

        let overall_quality =
            (logical_consistency + evidence_strength + chain_completeness + temporal_coherence)
                / 4.0;

        Ok(ReasoningQuality {
            logical_consistency,
            evidence_strength,
            chain_completeness,
            temporal_coherence,
            overall_quality,
        })
    }

    /// Extract temporal score from a triple for sorting purposes
    fn extract_temporal_score(&self, triple: &Triple) -> f64 {
        let object_str = triple.object().to_string().to_lowercase();

        // Look for temporal keywords and assign scores
        if object_str.contains("before")
            || object_str.contains("first")
            || object_str.contains("initial")
        {
            0.0
        } else if object_str.contains("during")
            || object_str.contains("while")
            || object_str.contains("concurrent")
        {
            0.5
        } else if object_str.contains("after")
            || object_str.contains("then")
            || object_str.contains("following")
        {
            1.0
        } else if object_str.contains("finally")
            || object_str.contains("last")
            || object_str.contains("end")
        {
            2.0
        } else {
            // Try to extract year or date information
            if let Some(year) = self.extract_year_from_string(&object_str) {
                year as f64 / 10000.0 // Normalize to smaller range
            } else {
                0.5 // Default middle position
            }
        }
    }

    /// Extract year from string if present
    fn extract_year_from_string(&self, text: &str) -> Option<i32> {
        // Simple regex to find 4-digit years between 1000-2100
        let year_regex = Regex::new(r"\b(1[0-9]{3}|20[0-9]{2}|21[0-9]{2})\b").ok()?;
        if let Some(captures) = year_regex.find(text) {
            captures.as_str().parse().ok()
        } else {
            None
        }
    }

    /// Enhanced temporal coherence analysis
    fn analyze_temporal_coherence(&self, chain: &ReasoningChain) -> f64 {
        if chain.steps.len() < 2 {
            return 1.0; // Single step is coherent
        }

        let mut coherence_scores = Vec::new();

        for i in 1..chain.steps.len() {
            let prev_step = &chain.steps[i - 1];
            let curr_step = &chain.steps[i];

            // Check if temporal order makes sense
            let prev_temporal = self.extract_temporal_info_from_step(prev_step);
            let curr_temporal = self.extract_temporal_info_from_step(curr_step);

            let coherence = if prev_temporal <= curr_temporal {
                1.0 // Correct temporal order
            } else {
                0.3 // Potential temporal inconsistency
            };

            coherence_scores.push(coherence);
        }

        coherence_scores.iter().sum::<f64>() / coherence_scores.len() as f64
    }

    /// Extract temporal information from a reasoning step
    fn extract_temporal_info_from_step(&self, step: &ReasoningStep) -> f64 {
        if let Some(conclusion) = &step.conclusion_triple {
            self.extract_temporal_score(conclusion)
        } else if !step.premise_triples.is_empty() {
            self.extract_temporal_score(&step.premise_triples[0])
        } else {
            0.5 // Default neutral position
        }
    }
}

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

    #[tokio::test]
    async fn test_reasoning_engine_creation() {
        let config = ReasoningConfig::default();
        let engine = AdvancedReasoningEngine::new(config);

        assert_eq!(engine.config.max_inference_depth, 5);
        assert_eq!(engine.config.confidence_threshold, 0.7);
    }

    #[test]
    fn test_reasoning_patterns_initialization() {
        let patterns = AdvancedReasoningEngine::initialize_reasoning_patterns();

        assert!(patterns.contains_key("modus_ponens"));
        assert!(patterns.contains_key("causal_chain"));
        assert!(patterns.contains_key("temporal_sequence"));
    }

    #[test]
    fn test_causal_relation_detection() {
        let _engine = AdvancedReasoningEngine::new(ReasoningConfig::default());

        // This test would require actual Triple instances
        // In a real implementation, you'd create test triples with causal predicates
    }
}