oxirs-core 0.2.3

Core RDF and SPARQL functionality for OxiRS - native Rust implementation with zero dependencies
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
//! Consciousness-Inspired Intuitive Query Planner
//!
//! This module implements artificial intuition for query optimization using
//! pattern memory, gut feeling calculations, and creative optimization techniques.

use crate::query::algebra::{AlgebraTriplePattern, TermPattern as AlgebraTermPattern};
use crate::query::pattern_optimizer::IndexStats;
use crate::OxirsError;
use scirs2_core::random::{Random, RngExt};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};

/// Pattern memory for storing learned query patterns and their effectiveness
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternMemory {
    /// Historical pattern performance data
    pub pattern_history: HashMap<String, PatternPerformance>,
    /// Intuitive weights for different pattern characteristics
    pub intuitive_weights: HashMap<PatternCharacteristic, f64>,
    /// Successful pattern combinations
    pub successful_combinations: Vec<PatternCombination>,
}

/// Performance metrics for a specific pattern
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternPerformance {
    /// Average execution time in microseconds
    pub avg_execution_time: f64,
    /// Success rate (0.0 to 1.0)
    pub success_rate: f64,
    /// Frequency of use
    pub usage_count: usize,
    /// Last used timestamp
    pub last_used: std::time::SystemTime,
    /// Intuitive effectiveness score
    pub intuitive_score: f64,
}

/// Characteristics that influence intuitive decision making
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub enum PatternCharacteristic {
    /// High selectivity patterns (few results)
    HighSelectivity,
    /// Join-heavy patterns
    JoinIntensive,
    /// Variable-heavy patterns
    VariableRich,
    /// Literal-bound patterns
    LiteralBound,
    /// Temporal patterns (time-based queries)
    Temporal,
    /// Hierarchical patterns (class/subclass)
    Hierarchical,
    /// Spatial patterns (geographic)
    Spatial,
    /// Numerical patterns (mathematical)
    Numerical,
}

/// Successful pattern combination for learning
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PatternCombination {
    /// Patterns in the combination
    pub patterns: Vec<String>,
    /// Total effectiveness score
    pub effectiveness: f64,
    /// Context where this combination was successful
    pub context: QueryContext,
}

/// Context information for query optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryContext {
    /// Dataset size category
    pub dataset_size: DatasetSize,
    /// Query complexity level
    pub complexity: ComplexityLevel,
    /// Performance requirements
    pub performance_req: PerformanceRequirement,
    /// Domain type (e.g., scientific, business, social)
    pub domain: String,
}

/// Dataset size categories
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DatasetSize {
    Small,     // < 1M triples
    Medium,    // 1M - 100M triples
    Large,     // 100M - 1B triples
    VeryLarge, // > 1B triples
}

/// Query complexity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ComplexityLevel {
    Simple,      // Single pattern queries
    Moderate,    // 2-5 patterns with basic joins
    Complex,     // 6-20 patterns with multiple joins
    VeryComplex, // 20+ patterns with nested operations
}

/// Performance requirements
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PerformanceRequirement {
    Interactive, // < 100ms response time
    Fast,        // < 1s response time
    Balanced,    // Balanced performance/quality tradeoff
    Batch,       // < 1min response time
    Background,  // No strict time limit
}

/// Neural network simulation for intuitive decision making
pub struct IntuitionNetwork {
    /// Input weights for pattern characteristics
    input_weights: HashMap<PatternCharacteristic, f64>,
    /// Hidden layer connections
    hidden_connections: Vec<Vec<f64>>,
    /// Output layer for decision scoring
    output_weights: Vec<f64>,
    /// Learning rate for weight updates
    learning_rate: f64,
}

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

impl IntuitionNetwork {
    /// Create a new intuition network with random weights
    pub fn new() -> Self {
        let characteristics = vec![
            PatternCharacteristic::HighSelectivity,
            PatternCharacteristic::JoinIntensive,
            PatternCharacteristic::VariableRich,
            PatternCharacteristic::LiteralBound,
            PatternCharacteristic::Temporal,
            PatternCharacteristic::Hierarchical,
            PatternCharacteristic::Spatial,
            PatternCharacteristic::Numerical,
        ];

        let mut input_weights = HashMap::new();
        for characteristic in characteristics {
            input_weights.insert(
                characteristic,
                {
                    let mut rng = Random::default();
                    rng.random::<f64>()
                } * 2.0
                    - 1.0,
            );
        }

        // Create hidden layer (8 inputs -> 5 hidden -> 1 output)
        let hidden_connections = vec![
            vec![
                {
                    let mut rng = Random::default();
                    rng.random::<f64>()
                } * 2.0
                    - 1.0;
                8
            ], // Hidden neuron 1
            vec![
                {
                    let mut rng = Random::default();
                    rng.random::<f64>()
                } * 2.0
                    - 1.0;
                8
            ], // Hidden neuron 2
            vec![
                {
                    let mut rng = Random::default();
                    rng.random::<f64>()
                } * 2.0
                    - 1.0;
                8
            ], // Hidden neuron 3
            vec![
                {
                    let mut rng = Random::default();
                    rng.random::<f64>()
                } * 2.0
                    - 1.0;
                8
            ], // Hidden neuron 4
            vec![
                {
                    let mut rng = Random::default();
                    rng.random::<f64>()
                } * 2.0
                    - 1.0;
                8
            ], // Hidden neuron 5
        ];

        let output_weights = vec![
            {
                let mut rng = Random::default();
                rng.random::<f64>()
            } * 2.0
                - 1.0;
            5
        ];

        Self {
            input_weights,
            hidden_connections,
            output_weights,
            learning_rate: 0.01,
        }
    }

    /// Calculate intuitive score for a pattern
    pub fn calculate_intuitive_score(&self, characteristics: &[PatternCharacteristic]) -> f64 {
        // Convert characteristics to input vector
        let mut input_vector = [0.0; 8];
        let characteristic_types = [
            PatternCharacteristic::HighSelectivity,
            PatternCharacteristic::JoinIntensive,
            PatternCharacteristic::VariableRich,
            PatternCharacteristic::LiteralBound,
            PatternCharacteristic::Temporal,
            PatternCharacteristic::Hierarchical,
            PatternCharacteristic::Spatial,
            PatternCharacteristic::Numerical,
        ];

        for (i, char_type) in characteristic_types.iter().enumerate() {
            if characteristics.contains(char_type) {
                input_vector[i] = 1.0;
            }
        }

        // Forward pass through network
        let mut hidden_activations = Vec::new();
        for connections in &self.hidden_connections {
            let mut activation = 0.0;
            for (i, &weight) in connections.iter().enumerate() {
                activation += input_vector[i] * weight;
            }
            hidden_activations.push(self.sigmoid(activation));
        }

        // Output layer
        let mut output = 0.0;
        for (i, &weight) in self.output_weights.iter().enumerate() {
            output += hidden_activations[i] * weight;
        }

        self.sigmoid(output)
    }

    /// Sigmoid activation function
    fn sigmoid(&self, x: f64) -> f64 {
        1.0 / (1.0 + (-x).exp())
    }

    /// Update weights based on feedback
    pub fn learn_from_feedback(
        &mut self,
        characteristics: &[PatternCharacteristic],
        _expected_score: f64,
        actual_performance: f64,
    ) {
        let predicted_score = self.calculate_intuitive_score(characteristics);
        let error = actual_performance - predicted_score;

        // Simple weight update (would be more sophisticated in real implementation)
        for char_type in characteristics {
            if let Some(weight) = self.input_weights.get_mut(char_type) {
                *weight += self.learning_rate * error;
            }
        }
    }
}

/// Gut feeling engine for making intuitive decisions
#[allow(dead_code)]
pub struct GutFeelingEngine {
    /// Historical success patterns
    success_patterns: HashMap<String, f64>,
    /// Confidence thresholds for different decisions
    confidence_thresholds: HashMap<String, f64>,
    /// Emotional weighting factors
    emotional_weights: HashMap<String, f64>,
}

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

impl GutFeelingEngine {
    /// Create a new gut feeling engine
    pub fn new() -> Self {
        let mut confidence_thresholds = HashMap::new();
        confidence_thresholds.insert("high_confidence".to_string(), 0.8);
        confidence_thresholds.insert("medium_confidence".to_string(), 0.6);
        confidence_thresholds.insert("low_confidence".to_string(), 0.4);

        let mut emotional_weights = HashMap::new();
        emotional_weights.insert("excitement".to_string(), 1.2); // Boost for novel patterns
        emotional_weights.insert("caution".to_string(), 0.8); // Reduce for risky patterns
        emotional_weights.insert("confidence".to_string(), 1.1); // Boost for familiar patterns

        Self {
            success_patterns: HashMap::new(),
            confidence_thresholds,
            emotional_weights,
        }
    }

    /// Calculate gut feeling score for a decision
    pub fn calculate_gut_feeling(&self, pattern_signature: &str, context: &QueryContext) -> f64 {
        let base_confidence = self
            .success_patterns
            .get(pattern_signature)
            .copied()
            .unwrap_or(0.5);

        // Apply contextual adjustments
        let context_multiplier = match (&context.complexity, &context.performance_req) {
            (ComplexityLevel::Simple, PerformanceRequirement::Interactive) => 1.2,
            (ComplexityLevel::VeryComplex, PerformanceRequirement::Interactive) => 0.7,
            (ComplexityLevel::Complex, PerformanceRequirement::Background) => 1.1,
            _ => 1.0,
        };

        // Apply emotional weighting
        let emotional_factor = if base_confidence > 0.8 {
            self.emotional_weights
                .get("confidence")
                .copied()
                .unwrap_or(1.0)
        } else if pattern_signature.contains("novel") {
            self.emotional_weights
                .get("excitement")
                .copied()
                .unwrap_or(1.0)
        } else {
            self.emotional_weights
                .get("caution")
                .copied()
                .unwrap_or(1.0)
        };

        (base_confidence * context_multiplier * emotional_factor).min(1.0)
    }

    /// Update gut feeling based on actual results
    pub fn update_gut_feeling(&mut self, pattern_signature: String, success_rate: f64) {
        let current = self
            .success_patterns
            .get(&pattern_signature)
            .copied()
            .unwrap_or(0.5);
        // Exponential moving average
        let updated = current * 0.8 + success_rate * 0.2;
        self.success_patterns.insert(pattern_signature, updated);
    }
}

/// Creativity engine for generating novel optimization strategies
#[allow(dead_code)]
pub struct CreativityEngine {
    /// Repository of creative optimization techniques
    techniques: Vec<CreativeTechnique>,
    /// Randomness factor for exploration
    exploration_factor: f64,
    /// Combination history for avoiding repetition
    combination_history: HashSet<String>,
}

/// Creative optimization techniques
#[derive(Debug, Clone)]
pub enum CreativeTechnique {
    /// Reverse the usual optimization order
    ReverseOptimization,
    /// Try parallel execution paths
    ParallelPaths,
    /// Use predictive prefetching
    PredictivePrefetch,
    /// Apply genetic algorithm mutations
    GeneticMutation,
    /// Use chaos theory for exploration
    ChaoticExploration,
    /// Apply artistic principles (golden ratio, symmetry)
    ArtisticPrinciples,
    /// Use biomimetic approaches
    BiomimeticOptimization,
}

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

impl CreativityEngine {
    /// Create a new creativity engine
    pub fn new() -> Self {
        let techniques = vec![
            CreativeTechnique::ReverseOptimization,
            CreativeTechnique::ParallelPaths,
            CreativeTechnique::PredictivePrefetch,
            CreativeTechnique::GeneticMutation,
            CreativeTechnique::ChaoticExploration,
            CreativeTechnique::ArtisticPrinciples,
            CreativeTechnique::BiomimeticOptimization,
        ];

        Self {
            techniques,
            exploration_factor: 0.1,
            combination_history: HashSet::new(),
        }
    }

    /// Generate creative optimization suggestions
    pub fn generate_creative_optimizations(
        &mut self,
        _patterns: &[AlgebraTriplePattern],
    ) -> Vec<CreativeOptimization> {
        let mut optimizations = Vec::new();

        // Apply each technique with some probability
        for technique in &self.techniques {
            if {
                let mut rng = Random::default();
                rng.random::<f64>()
            } < self.exploration_factor
            {
                match technique {
                    CreativeTechnique::ReverseOptimization => {
                        optimizations.push(CreativeOptimization {
                            technique: technique.clone(),
                            description: "Try executing patterns in reverse selectivity order"
                                .to_string(),
                            confidence: 0.7,
                            novelty: 0.8,
                        });
                    }
                    CreativeTechnique::ParallelPaths => {
                        optimizations.push(CreativeOptimization {
                            technique: technique.clone(),
                            description: "Execute independent patterns in parallel".to_string(),
                            confidence: 0.9,
                            novelty: 0.6,
                        });
                    }
                    CreativeTechnique::ArtisticPrinciples => {
                        optimizations.push(CreativeOptimization {
                            technique: technique.clone(),
                            description: "Apply golden ratio to join ordering".to_string(),
                            confidence: 0.5,
                            novelty: 0.95,
                        });
                    }
                    _ => {
                        // Other techniques would be implemented similarly
                    }
                }
            }
        }

        optimizations
    }
}

/// A creative optimization suggestion
#[derive(Debug, Clone)]
pub struct CreativeOptimization {
    /// The technique used
    pub technique: CreativeTechnique,
    /// Human-readable description
    pub description: String,
    /// Confidence in this optimization (0.0 to 1.0)
    pub confidence: f64,
    /// Novelty factor (0.0 to 1.0)
    pub novelty: f64,
}

/// Intuitive query planner that combines all consciousness-inspired components
#[allow(dead_code)]
pub struct IntuitiveQueryPlanner {
    /// Pattern memory for learning
    pattern_memory: Arc<RwLock<PatternMemory>>,
    /// Neural network for intuitive scoring
    intuition_network: Arc<RwLock<IntuitionNetwork>>,
    /// Gut feeling engine
    gut_feeling: Arc<RwLock<GutFeelingEngine>>,
    /// Creativity engine
    creativity: Arc<RwLock<CreativityEngine>>,
    /// Traditional optimizer for baseline comparison
    traditional_stats: Arc<IndexStats>,
}

impl IntuitiveQueryPlanner {
    /// Create a new intuitive query planner
    pub fn new(traditional_stats: Arc<IndexStats>) -> Self {
        Self {
            pattern_memory: Arc::new(RwLock::new(PatternMemory {
                pattern_history: HashMap::new(),
                intuitive_weights: HashMap::new(),
                successful_combinations: Vec::new(),
            })),
            intuition_network: Arc::new(RwLock::new(IntuitionNetwork::new())),
            gut_feeling: Arc::new(RwLock::new(GutFeelingEngine::new())),
            creativity: Arc::new(RwLock::new(CreativityEngine::new())),
            traditional_stats,
        }
    }

    /// Plan query execution using intuitive optimization
    pub fn plan_intuitive_execution(
        &self,
        patterns: &[AlgebraTriplePattern],
        context: &QueryContext,
    ) -> Result<IntuitiveExecutionPlan, OxirsError> {
        // Extract pattern characteristics
        let characteristics = self.extract_pattern_characteristics(patterns);

        // Calculate intuitive scores
        let intuitive_scores = match self.intuition_network.read() {
            Ok(network) => patterns
                .iter()
                .enumerate()
                .map(|(i, _)| network.calculate_intuitive_score(&characteristics[i]))
                .collect::<Vec<_>>(),
            _ => {
                vec![0.5; patterns.len()]
            }
        };

        // Get gut feeling assessment
        let gut_feelings = match self.gut_feeling.read() {
            Ok(gut) => patterns
                .iter()
                .map(|pattern| {
                    let signature = format!("{pattern:?}");
                    gut.calculate_gut_feeling(&signature, context)
                })
                .collect::<Vec<_>>(),
            _ => {
                vec![0.5; patterns.len()]
            }
        };

        // Generate creative optimizations
        let creative_opts = match self.creativity.write() {
            Ok(mut creativity) => creativity.generate_creative_optimizations(patterns),
            _ => Vec::new(),
        };

        // Combine all factors to create plan
        let mut pattern_rankings = Vec::new();
        for (i, pattern) in patterns.iter().enumerate() {
            let combined_score = (intuitive_scores[i] + gut_feelings[i]) * 0.5;
            pattern_rankings.push((i, pattern.clone(), combined_score));
        }

        // Sort by combined intuitive score
        pattern_rankings.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));

        Ok(IntuitiveExecutionPlan {
            ordered_patterns: pattern_rankings,
            creative_optimizations: creative_opts,
            confidence_level: intuitive_scores.iter().sum::<f64>() / intuitive_scores.len() as f64,
            gut_feeling_average: gut_feelings.iter().sum::<f64>() / gut_feelings.len() as f64,
            context: context.clone(),
        })
    }

    /// Extract pattern characteristics for intuitive analysis
    fn extract_pattern_characteristics(
        &self,
        patterns: &[AlgebraTriplePattern],
    ) -> Vec<Vec<PatternCharacteristic>> {
        patterns
            .iter()
            .map(|pattern| {
                let mut characteristics = Vec::new();

                // Check for high selectivity (bound terms)
                let bound_terms = [&pattern.subject, &pattern.predicate, &pattern.object]
                    .iter()
                    .filter(|term| !matches!(term, AlgebraTermPattern::Variable(_)))
                    .count();

                if bound_terms >= 2 {
                    characteristics.push(PatternCharacteristic::HighSelectivity);
                }

                // Check for literal bounds
                if matches!(pattern.object, AlgebraTermPattern::Literal(_)) {
                    characteristics.push(PatternCharacteristic::LiteralBound);
                }

                // Count variables
                let variable_count = [&pattern.subject, &pattern.predicate, &pattern.object]
                    .iter()
                    .filter(|term| matches!(term, AlgebraTermPattern::Variable(_)))
                    .count();

                if variable_count >= 2 {
                    characteristics.push(PatternCharacteristic::VariableRich);
                }

                // Check for temporal patterns (basic heuristic)
                if let AlgebraTermPattern::NamedNode(pred) = &pattern.predicate {
                    if pred.as_str().contains("time") || pred.as_str().contains("date") {
                        characteristics.push(PatternCharacteristic::Temporal);
                    }
                }

                // Check for hierarchical patterns
                if let AlgebraTermPattern::NamedNode(pred) = &pattern.predicate {
                    if pred.as_str().contains("type") || pred.as_str().contains("subClass") {
                        characteristics.push(PatternCharacteristic::Hierarchical);
                    }
                }

                characteristics
            })
            .collect()
    }

    /// Learn from execution results to improve future planning
    pub fn learn_from_execution(
        &self,
        plan: &IntuitiveExecutionPlan,
        actual_performance: &ExecutionResults,
    ) {
        // Update pattern memory
        if let Ok(mut memory) = self.pattern_memory.write() {
            for (i, (_, pattern, predicted_score)) in plan.ordered_patterns.iter().enumerate() {
                let signature = format!("{pattern:?}");
                let actual_score = actual_performance
                    .pattern_scores
                    .get(&i)
                    .copied()
                    .unwrap_or(0.5);

                let performance = PatternPerformance {
                    avg_execution_time: actual_performance
                        .execution_times
                        .get(&i)
                        .copied()
                        .unwrap_or(1000.0),
                    success_rate: actual_score,
                    usage_count: memory
                        .pattern_history
                        .get(&signature)
                        .map(|p| p.usage_count + 1)
                        .unwrap_or(1),
                    last_used: std::time::SystemTime::now(),
                    intuitive_score: *predicted_score,
                };

                memory.pattern_history.insert(signature, performance);
            }
        }

        // Update intuition network
        if let Ok(mut network) = self.intuition_network.write() {
            for (i, (_, pattern, predicted_score)) in plan.ordered_patterns.iter().enumerate() {
                let characteristics =
                    self.extract_pattern_characteristics(std::slice::from_ref(pattern))[0].clone();
                let actual_score = actual_performance
                    .pattern_scores
                    .get(&i)
                    .copied()
                    .unwrap_or(0.5);

                network.learn_from_feedback(&characteristics, *predicted_score, actual_score);
            }
        }

        // Update gut feeling
        if let Ok(mut gut) = self.gut_feeling.write() {
            for (i, (_, pattern, _)) in plan.ordered_patterns.iter().enumerate() {
                let signature = format!("{pattern:?}");
                let success_rate = actual_performance
                    .pattern_scores
                    .get(&i)
                    .copied()
                    .unwrap_or(0.5);

                gut.update_gut_feeling(signature, success_rate);
            }
        }
    }
}

/// Execution plan generated by intuitive planner
#[derive(Debug, Clone)]
pub struct IntuitiveExecutionPlan {
    /// Patterns ordered by intuitive ranking
    pub ordered_patterns: Vec<(usize, AlgebraTriplePattern, f64)>,
    /// Creative optimization suggestions
    pub creative_optimizations: Vec<CreativeOptimization>,
    /// Overall confidence in the plan
    pub confidence_level: f64,
    /// Average gut feeling score
    pub gut_feeling_average: f64,
    /// Context used for planning
    pub context: QueryContext,
}

/// Results from executing an intuitive plan
#[derive(Debug)]
pub struct ExecutionResults {
    /// Performance scores for each pattern (0.0 to 1.0)
    pub pattern_scores: HashMap<usize, f64>,
    /// Execution times in microseconds
    pub execution_times: HashMap<usize, f64>,
    /// Overall success rate
    pub overall_success: f64,
    /// Creative optimizations that were applied
    pub applied_optimizations: Vec<CreativeTechnique>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Literal, NamedNode, Variable};

    #[test]
    fn test_intuition_network_creation() {
        let network = IntuitionNetwork::new();
        assert_eq!(network.input_weights.len(), 8);
        assert_eq!(network.hidden_connections.len(), 5);
        assert_eq!(network.output_weights.len(), 5);
    }

    #[test]
    fn test_gut_feeling_calculation() {
        let engine = GutFeelingEngine::new();
        let context = QueryContext {
            dataset_size: DatasetSize::Medium,
            complexity: ComplexityLevel::Simple,
            performance_req: PerformanceRequirement::Interactive,
            domain: "test".to_string(),
        };

        let score = engine.calculate_gut_feeling("test_pattern", &context);
        assert!((0.0..=1.0).contains(&score));
    }

    #[test]
    fn test_creativity_engine() {
        let mut engine = CreativityEngine::new();
        let patterns = vec![AlgebraTriplePattern::new(
            AlgebraTermPattern::Variable(Variable::new("s").expect("valid variable name")),
            AlgebraTermPattern::NamedNode(
                NamedNode::new("http://example.org/pred").expect("valid IRI"),
            ),
            AlgebraTermPattern::Variable(Variable::new("o").expect("valid variable name")),
        )];

        let optimizations = engine.generate_creative_optimizations(&patterns);
        // Should generate some optimizations based on exploration factor
        assert!(optimizations.len() <= engine.techniques.len());
    }

    #[test]
    fn test_pattern_characteristics_extraction() {
        let stats = Arc::new(IndexStats::new());
        let planner = IntuitiveQueryPlanner::new(stats);

        let patterns = vec![AlgebraTriplePattern::new(
            AlgebraTermPattern::NamedNode(
                NamedNode::new("http://example.org/subj").expect("valid IRI"),
            ),
            AlgebraTermPattern::NamedNode(
                NamedNode::new("http://example.org/type").expect("valid IRI"),
            ),
            AlgebraTermPattern::Literal(Literal::new("test")),
        )];

        let characteristics = planner.extract_pattern_characteristics(&patterns);
        assert_eq!(characteristics.len(), 1);
        assert!(characteristics[0].contains(&PatternCharacteristic::HighSelectivity));
        assert!(characteristics[0].contains(&PatternCharacteristic::LiteralBound));
    }
}