oxirs-samm 0.2.4

Semantic Aspect Meta Model (SAMM) implementation for OxiRS
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
//! Query Result Caching
//!
//! This module provides caching for expensive query operations on SAMM models.
//! It wraps `ModelQuery` and caches results to avoid repeated computations.
//!
//! # Features
//!
//! - **Automatic Caching**: Transparently caches query results
//! - **LRU Eviction**: Least recently used results are evicted when cache is full
//! - **Cache Statistics**: Track hit rates and performance
//! - **Configurable Size**: Control memory usage
//!
//! # Examples
//!
//! ```rust
//! use oxirs_samm::query_cache::CachedModelQuery;
//! use oxirs_samm::metamodel::Aspect;
//!
//! # fn example(aspect: &Aspect) {
//! let mut query = CachedModelQuery::new(aspect, 100); // Cache up to 100 results
//!
//! // First call computes result
//! let metrics1 = query.complexity_metrics();
//!
//! // Second call returns cached result (much faster)
//! let metrics2 = query.complexity_metrics();
//!
//! // Check cache performance
//! let stats = query.cache_statistics();
//! println!("Hit rate: {:.2}%", stats.hit_rate * 100.0);
//! # }
//! ```

use crate::metamodel::{Aspect, ModelElement, Property};
use crate::query::{ComplexityMetrics, Dependency, ModelQuery};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};

/// Cache key for query results
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
enum CacheKey {
    ComplexityMetrics,
    OptionalProperties,
    RequiredProperties,
    CollectionProperties,
    DependencyGraph,
    CircularDependencies,
    FindByType(String),
    FindByNamespace(String),
}

/// Cached model query wrapper
pub struct CachedModelQuery<'a> {
    query: ModelQuery<'a>,
    cache: Arc<RwLock<HashMap<CacheKey, CachedValue>>>,
    hits: Arc<RwLock<usize>>,
    misses: Arc<RwLock<usize>>,
    max_cache_size: usize,
}

/// Cached value with access tracking
#[derive(Clone)]
struct CachedValue {
    value: CachedResult,
    access_count: usize,
    last_accessed: std::time::Instant,
}

/// Union type for different cached results
#[derive(Clone)]
enum CachedResult {
    ComplexityMetrics(ComplexityMetrics),
    Properties(Vec<String>), // Store URNs instead of references
    Dependencies(Vec<Dependency>),
}

impl<'a> CachedModelQuery<'a> {
    /// Create a new cached query wrapper
    ///
    /// # Arguments
    ///
    /// * `aspect` - The aspect to query
    /// * `max_cache_size` - Maximum number of cached results
    ///
    /// # Examples
    ///
    /// ```rust
    /// use oxirs_samm::query_cache::CachedModelQuery;
    /// # use oxirs_samm::metamodel::Aspect;
    /// # fn example(aspect: &Aspect) {
    /// let query = CachedModelQuery::new(aspect, 50);
    /// # }
    /// ```
    pub fn new(aspect: &'a Aspect, max_cache_size: usize) -> Self {
        Self {
            query: ModelQuery::new(aspect),
            cache: Arc::new(RwLock::new(HashMap::new())),
            hits: Arc::new(RwLock::new(0)),
            misses: Arc::new(RwLock::new(0)),
            max_cache_size: max_cache_size.max(1),
        }
    }

    /// Get complexity metrics (cached)
    pub fn complexity_metrics(&mut self) -> ComplexityMetrics {
        let key = CacheKey::ComplexityMetrics;

        if let Some(CachedResult::ComplexityMetrics(metrics)) = self.get_cached(&key) {
            return metrics;
        }

        // Compute and cache
        let metrics = self.query.complexity_metrics();
        self.cache_result(key, CachedResult::ComplexityMetrics(metrics.clone()));
        metrics
    }

    /// Find optional properties (cached)
    pub fn find_optional_properties(&mut self) -> Vec<&Property> {
        let key = CacheKey::OptionalProperties;

        if let Some(CachedResult::Properties(urns)) = self.get_cached(&key) {
            // Convert URNs back to references
            return self
                .query
                .aspect()
                .properties()
                .iter()
                .filter(|p| urns.contains(&p.urn().to_string()))
                .collect();
        }

        // Compute and cache
        let props = self.query.find_optional_properties();
        let urns: Vec<String> = props.iter().map(|p| p.urn().to_string()).collect();
        self.cache_result(key, CachedResult::Properties(urns));
        props
    }

    /// Find required properties (cached)
    pub fn find_required_properties(&mut self) -> Vec<&Property> {
        let key = CacheKey::RequiredProperties;

        if let Some(CachedResult::Properties(urns)) = self.get_cached(&key) {
            return self
                .query
                .aspect()
                .properties()
                .iter()
                .filter(|p| urns.contains(&p.urn().to_string()))
                .collect();
        }

        let props = self.query.find_required_properties();
        let urns: Vec<String> = props.iter().map(|p| p.urn().to_string()).collect();
        self.cache_result(key, CachedResult::Properties(urns));
        props
    }

    /// Find collection properties (cached)
    pub fn find_properties_with_collection_characteristic(&mut self) -> Vec<&Property> {
        let key = CacheKey::CollectionProperties;

        if let Some(CachedResult::Properties(urns)) = self.get_cached(&key) {
            return self
                .query
                .aspect()
                .properties()
                .iter()
                .filter(|p| urns.contains(&p.urn().to_string()))
                .collect();
        }

        let props = self.query.find_properties_with_collection_characteristic();
        let urns: Vec<String> = props.iter().map(|p| p.urn().to_string()).collect();
        self.cache_result(key, CachedResult::Properties(urns));
        props
    }

    /// Build dependency graph (cached)
    pub fn build_dependency_graph(&mut self) -> Vec<Dependency> {
        let key = CacheKey::DependencyGraph;

        if let Some(CachedResult::Dependencies(deps)) = self.get_cached(&key) {
            return deps;
        }

        let deps = self.query.build_dependency_graph();
        self.cache_result(key, CachedResult::Dependencies(deps.clone()));
        deps
    }

    /// Detect circular dependencies (cached)
    pub fn detect_circular_dependencies(&mut self) -> Vec<Vec<String>> {
        let key = CacheKey::CircularDependencies;

        // For now, compute without caching (complex return type)
        self.query.detect_circular_dependencies()
    }

    /// Clear the cache
    pub fn clear_cache(&mut self) {
        let mut cache = self
            .cache
            .write()
            .expect("cache mutex should not be poisoned");
        cache.clear();
    }

    /// Get cache statistics
    pub fn cache_statistics(&self) -> CacheStatistics {
        let hits = *self.hits.read().expect("hits mutex should not be poisoned");
        let misses = *self
            .misses
            .read()
            .expect("misses mutex should not be poisoned");
        let total = hits + misses;
        let hit_rate = if total == 0 {
            0.0
        } else {
            hits as f64 / total as f64
        };

        let cache = self
            .cache
            .read()
            .expect("cache mutex should not be poisoned");

        CacheStatistics {
            size: cache.len(),
            capacity: self.max_cache_size,
            hits,
            misses,
            hit_rate,
        }
    }

    /// Get cached value if it exists
    fn get_cached(&self, key: &CacheKey) -> Option<CachedResult> {
        let mut cache = self
            .cache
            .write()
            .expect("cache mutex should not be poisoned");

        if let Some(entry) = cache.get_mut(key) {
            entry.access_count += 1;
            entry.last_accessed = std::time::Instant::now();
            *self
                .hits
                .write()
                .expect("write lock should not be poisoned") += 1;
            Some(entry.value.clone())
        } else {
            *self
                .misses
                .write()
                .expect("write lock should not be poisoned") += 1;
            None
        }
    }

    /// Cache a result
    fn cache_result(&self, key: CacheKey, value: CachedResult) {
        let mut cache = self
            .cache
            .write()
            .expect("cache mutex should not be poisoned");

        // Evict LRU if at capacity
        if cache.len() >= self.max_cache_size && !cache.contains_key(&key) {
            if let Some((lru_key, _)) = cache
                .iter()
                .min_by_key(|(_, v)| v.last_accessed)
                .map(|(k, v)| (k.clone(), v.clone()))
            {
                cache.remove(&lru_key);
            }
        }

        cache.insert(
            key,
            CachedValue {
                value,
                access_count: 0,
                last_accessed: std::time::Instant::now(),
            },
        );
    }

    /// Get the underlying query (bypasses cache)
    pub fn query(&self) -> &ModelQuery<'a> {
        &self.query
    }
}

/// Cache statistics
#[derive(Debug, Clone)]
pub struct CacheStatistics {
    /// Current cache size
    pub size: usize,
    /// Maximum capacity
    pub capacity: usize,
    /// Total cache hits
    pub hits: usize,
    /// Total cache misses
    pub misses: usize,
    /// Hit rate (0.0 to 1.0)
    pub hit_rate: f64,
}

impl CacheStatistics {
    /// Get total accesses
    pub fn total_accesses(&self) -> usize {
        self.hits + self.misses
    }

    /// Get fill percentage
    pub fn fill_percentage(&self) -> f64 {
        if self.capacity == 0 {
            0.0
        } else {
            (self.size as f64 / self.capacity as f64) * 100.0
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::metamodel::{Characteristic, CharacteristicKind};

    fn create_test_aspect() -> Aspect {
        let mut aspect = Aspect::new("urn:samm:test:1.0.0#TestAspect".to_string());

        let mut prop1 = Property::new("urn:samm:test:1.0.0#prop1".to_string());
        prop1.optional = false;

        let mut prop2 = Property::new("urn:samm:test:1.0.0#prop2".to_string());
        prop2.optional = true;

        let mut prop3 = Property::new("urn:samm:test:1.0.0#prop3".to_string());
        prop3.is_collection = true;
        // Add a Collection characteristic so it can be found by find_properties_with_collection_characteristic
        let collection_char = Characteristic::new(
            "urn:samm:test:1.0.0#CollectionChar".to_string(),
            CharacteristicKind::Collection {
                element_characteristic: None,
            },
        );
        prop3.characteristic = Some(collection_char);

        aspect.add_property(prop1);
        aspect.add_property(prop2);
        aspect.add_property(prop3);

        aspect
    }

    #[test]
    fn test_cached_complexity_metrics() {
        let aspect = create_test_aspect();
        let mut query = CachedModelQuery::new(&aspect, 10);

        // First call - cache miss
        let metrics1 = query.complexity_metrics();
        assert_eq!(metrics1.total_properties, 3);

        let stats1 = query.cache_statistics();
        assert_eq!(stats1.misses, 1);
        assert_eq!(stats1.hits, 0);

        // Second call - cache hit
        let metrics2 = query.complexity_metrics();
        assert_eq!(metrics2.total_properties, 3);

        let stats2 = query.cache_statistics();
        assert_eq!(stats2.misses, 1);
        assert_eq!(stats2.hits, 1);
        assert!((stats2.hit_rate - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_cached_optional_properties() {
        let aspect = create_test_aspect();
        let mut query = CachedModelQuery::new(&aspect, 10);

        let props1 = query.find_optional_properties();
        assert_eq!(props1.len(), 1);

        let stats1 = query.cache_statistics();
        assert_eq!(stats1.misses, 1);

        let props2 = query.find_optional_properties();
        assert_eq!(props2.len(), 1);

        let stats2 = query.cache_statistics();
        assert_eq!(stats2.hits, 1);
    }

    #[test]
    fn test_cached_required_properties() {
        let aspect = create_test_aspect();
        let mut query = CachedModelQuery::new(&aspect, 10);

        let props1 = query.find_required_properties();
        assert_eq!(props1.len(), 2);

        let props2 = query.find_required_properties();
        assert_eq!(props2.len(), 2);

        let stats = query.cache_statistics();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
    }

    #[test]
    fn test_cache_clear() {
        let aspect = create_test_aspect();
        let mut query = CachedModelQuery::new(&aspect, 10);

        query.complexity_metrics();
        query.complexity_metrics(); // Hit

        let stats_before = query.cache_statistics();
        assert_eq!(stats_before.size, 1);

        query.clear_cache();

        let stats_after = query.cache_statistics();
        assert_eq!(stats_after.size, 0);
        assert_eq!(stats_after.hits, 1); // Stats not cleared
    }

    #[test]
    fn test_cache_lru_eviction() {
        let aspect = create_test_aspect();
        let mut query = CachedModelQuery::new(&aspect, 2); // Small cache

        // Fill cache
        query.complexity_metrics();
        query.find_optional_properties();

        let stats1 = query.cache_statistics();
        assert_eq!(stats1.size, 2);

        // This should evict LRU
        query.find_required_properties();

        let stats2 = query.cache_statistics();
        assert_eq!(stats2.size, 2); // Still at capacity
    }

    #[test]
    fn test_cache_statistics() {
        let aspect = create_test_aspect();
        let mut query = CachedModelQuery::new(&aspect, 10);

        query.complexity_metrics();
        query.complexity_metrics();
        query.find_optional_properties();

        let stats = query.cache_statistics();
        assert_eq!(stats.total_accesses(), 3);
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 2);
        assert!((stats.hit_rate - 0.333).abs() < 0.01);
        assert_eq!(stats.size, 2);
    }

    #[test]
    fn test_collection_properties_caching() {
        let aspect = create_test_aspect();
        let mut query = CachedModelQuery::new(&aspect, 10);

        let props1 = query.find_properties_with_collection_characteristic();
        assert_eq!(props1.len(), 1);

        let props2 = query.find_properties_with_collection_characteristic();
        assert_eq!(props2.len(), 1);

        let stats = query.cache_statistics();
        assert_eq!(stats.hits, 1);
    }

    #[test]
    fn test_dependency_graph_caching() {
        let mut aspect = Aspect::new("urn:samm:test:1.0.0#TestAspect".to_string());

        let mut prop = Property::new("urn:samm:test:1.0.0#prop1".to_string());
        let char = Characteristic::new(
            "urn:samm:test:1.0.0#Char1".to_string(),
            CharacteristicKind::Trait,
        );
        prop.characteristic = Some(char);
        aspect.add_property(prop);

        let mut query = CachedModelQuery::new(&aspect, 10);

        let deps1 = query.build_dependency_graph();
        assert!(!deps1.is_empty());

        let deps2 = query.build_dependency_graph();
        assert!(!deps2.is_empty());

        let stats = query.cache_statistics();
        assert_eq!(stats.hits, 1);
    }
}