vowl-wasm 0.1.1

VOWL ontology visualisation engine in Rust/WASM: OWL parsing, force-directed layout (Barnes-Hut, SIMD), and rendering data for web explorers. A clean-room reimplementation of the WebVOWL visual notation.
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
//! Unified Ontology Loader
//!
//! High-performance loader for ontology markdown files with caching, filtering,
//! and parallel processing capabilities.
//!
//! # Features
//!
//! - Load single files or entire directories
//! - Domain filtering (AI, Blockchain, Robotics, etc.)
//! - Term-ID pattern matching
//! - LRU caching for performance
//! - Parallel directory loading with rayon
//! - Comprehensive statistics
//!
//! # Example
//!
//! ```ignore
//! // Illustrative only — crate is `vowl_wasm`, not `vowl`. Not compiled.
//! use vowl::ontology::loader::{OntologyLoader, LoaderConfig};
//! use std::path::Path;
//!
//! let config = LoaderConfig::default();
//! let mut loader = OntologyLoader::new(config);
//!
//! // Load single file
//! let block = loader.load_file(Path::new("ontology.md"))?;
//!
//! // Load directory with domain filter
//! let blocks = loader.load_directory(
//!     Path::new("pages/"),
//!     Some(Domain::AI)
//! )?;
//!
//! // Get statistics
//! let stats = loader.get_statistics(&blocks);
//! # Ok::<(), vowl::VowlError>(())
//! ```

use super::markdown_parser::MarkdownParser;
use super::{Domain, OntologyBlock};
use crate::{Result, VowlError};
use regex::Regex;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

#[cfg(feature = "parallel")]
use rayon::prelude::*;

/// Configuration for the ontology loader
#[derive(Debug, Clone)]
pub struct LoaderConfig {
    /// Maximum number of files to cache (LRU)
    pub cache_size: usize,

    /// Skip blocks with validation errors
    pub strict_validation: bool,

    /// Use parallel loading for directories (requires 'parallel' feature)
    pub parallel_loading: bool,

    /// Glob pattern for markdown files
    pub file_pattern: String,

    /// Recursive directory search
    pub recursive: bool,
}

impl Default for LoaderConfig {
    fn default() -> Self {
        Self {
            cache_size: 128,
            strict_validation: false,
            parallel_loading: cfg!(feature = "parallel"),
            file_pattern: "*.md".to_string(),
            recursive: true,
        }
    }
}

/// Statistics for loaded ontology blocks
#[derive(Debug, Clone, Default)]
pub struct LoaderStatistics {
    /// Total blocks loaded
    pub total_blocks: usize,

    /// Blocks by domain
    pub by_domain: HashMap<Domain, usize>,

    /// Blocks by status
    pub by_status: HashMap<String, usize>,

    /// Cache hits
    pub cache_hits: usize,

    /// Cache misses
    pub cache_misses: usize,

    /// Load time in milliseconds
    pub load_time_ms: u128,
}

impl LoaderStatistics {
    /// Calculate cache hit rate
    pub fn cache_hit_rate(&self) -> f64 {
        let total = self.cache_hits + self.cache_misses;
        if total == 0 {
            0.0
        } else {
            self.cache_hits as f64 / total as f64
        }
    }

    /// Get domain counts as a formatted string
    pub fn domain_summary(&self) -> String {
        let mut summary = String::new();
        for (domain, count) in &self.by_domain {
            summary.push_str(&format!("  {}: {}\n", domain.prefix(), count));
        }
        summary
    }
}

/// A loader with the default configuration.
impl Default for OntologyLoader {
    fn default() -> Self {
        Self::new(LoaderConfig::default())
    }
}

/// High-performance ontology loader with caching
pub struct OntologyLoader {
    /// Markdown parser
    parser: MarkdownParser,

    /// Configuration
    config: LoaderConfig,

    /// File cache: path -> OntologyBlock
    cache: HashMap<PathBuf, Option<OntologyBlock>>,

    /// Cache statistics
    cache_hits: usize,
    cache_misses: usize,
}

impl OntologyLoader {
    /// Create a new loader with the given configuration
    pub fn new(config: LoaderConfig) -> Self {
        Self {
            parser: MarkdownParser::new(),
            config,
            cache: HashMap::with_capacity(128),
            cache_hits: 0,
            cache_misses: 0,
        }
    }

    /// Load a single ontology file
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the markdown file
    ///
    /// # Returns
    ///
    /// `Ok(Some(block))` if the file contains a valid ontology block,
    /// `Ok(None)` if no block found, or `Err` on I/O errors.
    pub fn load_file(&mut self, path: &Path) -> Result<Option<OntologyBlock>> {
        let path_buf = path.to_path_buf();

        // Check cache
        if let Some(cached) = self.cache.get(&path_buf) {
            self.cache_hits += 1;
            return Ok(cached.clone());
        }

        self.cache_misses += 1;

        // Read file
        let content = fs::read_to_string(path)
            .map_err(|e| VowlError::ParseError(format!("Failed to read file {:?}: {}", path, e)))?;

        // Parse ontology block
        let block = match self.parser.parse(&content) {
            Ok(block) => Some(block),
            Err(VowlError::ParseError(_)) => {
                // OntologyBlock not found or parsing failed
                None
            }
            Err(e) => return Err(e),
        };

        // Cache result (with LRU eviction)
        if self.cache.len() >= self.config.cache_size {
            // Simple eviction: remove first entry (not true LRU, but simple)
            if let Some(first_key) = self.cache.keys().next().cloned() {
                self.cache.remove(&first_key);
            }
        }

        self.cache.insert(path_buf, block.clone());

        Ok(block)
    }

    /// Load all ontology files from a directory
    ///
    /// # Arguments
    ///
    /// * `path` - Directory path
    /// * `domain` - Optional domain filter
    ///
    /// # Returns
    ///
    /// Vector of successfully loaded ontology blocks
    pub fn load_directory(
        &mut self,
        path: &Path,
        domain: Option<Domain>,
    ) -> Result<Vec<OntologyBlock>> {
        let _start = std::time::Instant::now();

        // Collect all markdown files
        let files = self.collect_files(path)?;

        // Load files (parallel or sequential)
        let blocks = if self.config.parallel_loading && cfg!(feature = "parallel") {
            self.load_files_parallel(&files)?
        } else {
            self.load_files_sequential(&files)?
        };

        // Apply domain filter
        let filtered = if let Some(d) = domain {
            blocks.into_iter().filter(|b| b.domain == d).collect()
        } else {
            blocks
        };

        Ok(filtered)
    }

    /// Load multiple specific files
    pub fn load_files(&mut self, paths: &[PathBuf]) -> Result<Vec<OntologyBlock>> {
        self.load_files_sequential(paths)
    }

    /// Filter blocks by domain
    pub fn filter_by_domain(
        &self,
        blocks: Vec<OntologyBlock>,
        domain: Domain,
    ) -> Vec<OntologyBlock> {
        blocks.into_iter().filter(|b| b.domain == domain).collect()
    }

    /// Filter blocks by term-id pattern
    pub fn filter_by_pattern(
        &self,
        blocks: Vec<OntologyBlock>,
        pattern: &str,
    ) -> Result<Vec<OntologyBlock>> {
        let regex = Regex::new(pattern)
            .map_err(|e| VowlError::ParseError(format!("Invalid regex: {}", e)))?;

        Ok(blocks
            .into_iter()
            .filter(|b| regex.is_match(&b.term_id))
            .collect())
    }

    /// Generate statistics for loaded blocks
    pub fn get_statistics(&self, blocks: &[OntologyBlock]) -> LoaderStatistics {
        let mut stats = LoaderStatistics {
            total_blocks: blocks.len(),
            cache_hits: self.cache_hits,
            cache_misses: self.cache_misses,
            ..Default::default()
        };

        for block in blocks {
            // Count by domain
            *stats.by_domain.entry(block.domain).or_insert(0) += 1;

            // Count by status (if present in metadata)
            if let Some(ref status) = block.metadata.status {
                *stats.by_status.entry(status.clone()).or_insert(0) += 1;
            }
        }

        stats
    }

    /// Create a term-id -> block lookup index
    pub fn create_term_index(&self, blocks: Vec<OntologyBlock>) -> HashMap<String, OntologyBlock> {
        blocks.into_iter().map(|b| (b.term_id.clone(), b)).collect()
    }

    /// Group blocks by domain
    pub fn group_by_domain(
        &self,
        blocks: Vec<OntologyBlock>,
    ) -> HashMap<Domain, Vec<OntologyBlock>> {
        let mut groups: HashMap<Domain, Vec<OntologyBlock>> = HashMap::new();

        for block in blocks {
            groups.entry(block.domain).or_default().push(block);
        }

        groups
    }

    /// Clear the file cache
    pub fn clear_cache(&mut self) {
        self.cache.clear();
        self.cache_hits = 0;
        self.cache_misses = 0;
    }

    /// Get cache statistics
    pub fn get_cache_stats(&self) -> (usize, usize, usize, f64) {
        let total = self.cache_hits + self.cache_misses;
        let hit_rate = if total == 0 {
            0.0
        } else {
            self.cache_hits as f64 / total as f64
        };

        (
            self.cache.len(),
            self.cache_hits,
            self.cache_misses,
            hit_rate,
        )
    }

    // Private helper methods

    /// Collect all markdown files from a directory
    fn collect_files(&self, path: &Path) -> Result<Vec<PathBuf>> {
        if !path.exists() {
            return Err(VowlError::ParseError(format!(
                "Directory not found: {:?}",
                path
            )));
        }

        let mut files = Vec::new();

        if self.config.recursive {
            self.collect_files_recursive(path, &mut files)?;
        } else {
            let entries = fs::read_dir(path).map_err(|e| {
                VowlError::ParseError(format!("Failed to read directory {:?}: {}", path, e))
            })?;

            for entry in entries {
                let entry = entry.map_err(|e| {
                    VowlError::ParseError(format!("Failed to read directory entry: {}", e))
                })?;
                let path = entry.path();
                if path.is_file() && self.matches_pattern(&path) {
                    files.push(path);
                }
            }
        }

        files.sort();
        Ok(files)
    }

    /// Recursively collect markdown files
    fn collect_files_recursive(&self, path: &Path, files: &mut Vec<PathBuf>) -> Result<()> {
        let entries = fs::read_dir(path).map_err(|e| {
            VowlError::ParseError(format!("Failed to read directory {:?}: {}", path, e))
        })?;

        for entry in entries {
            let entry = entry.map_err(|e| {
                VowlError::ParseError(format!("Failed to read directory entry: {}", e))
            })?;
            let path = entry.path();

            if path.is_dir() {
                self.collect_files_recursive(&path, files)?;
            } else if path.is_file() && self.matches_pattern(&path) {
                files.push(path);
            }
        }

        Ok(())
    }

    /// Check if a file matches the configured pattern
    fn matches_pattern(&self, path: &Path) -> bool {
        if let Some(ext) = path.extension() {
            if ext == "md" {
                return true;
            }
        }
        false
    }

    /// Load files sequentially
    fn load_files_sequential(&mut self, files: &[PathBuf]) -> Result<Vec<OntologyBlock>> {
        let mut blocks = Vec::new();

        for file in files {
            if let Some(block) = self.load_file(file)? {
                blocks.push(block);
            }
        }

        Ok(blocks)
    }

    /// Load files in parallel (requires 'parallel' feature)
    #[cfg(feature = "parallel")]
    fn load_files_parallel(&mut self, files: &[PathBuf]) -> Result<Vec<OntologyBlock>> {
        // Note: We can't use self.cache in parallel without Mutex
        // For now, just use parallel parsing without caching
        let parser = MarkdownParser::new();

        let blocks: Vec<OntologyBlock> = files
            .par_iter()
            .filter_map(|file| {
                let content = fs::read_to_string(file).ok()?;
                parser.parse(&content).ok()
            })
            .collect();

        Ok(blocks)
    }

    #[cfg(not(feature = "parallel"))]
    fn load_files_parallel(&mut self, files: &[PathBuf]) -> Result<Vec<OntologyBlock>> {
        // Fallback to sequential if parallel feature not enabled
        self.load_files_sequential(files)
    }
}

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

    #[allow(dead_code)] // retained test helper; call sites vary by feature set
    fn create_test_markdown(term_id: &str, domain: &str) -> String {
        format!(
            r#"
- ### OntologyBlock
  - **Identification**
    - term-id:: {}
    - preferred-term:: Test Term
    - public-access:: true
    - ontology:: true

  - **Semantic Classification**
    - owl:class:: {}:TestClass
    - owl:physicality:: VirtualEntity
    - owl:role:: Object
"#,
            term_id, domain
        )
    }

    #[test]
    fn test_loader_creation() {
        let config = LoaderConfig::default();
        let loader = OntologyLoader::new(config);

        assert_eq!(loader.cache.len(), 0);
        assert_eq!(loader.cache_hits, 0);
    }

    #[test]
    fn test_filter_by_domain() {
        let loader = OntologyLoader::default();

        // Create test blocks
        let blocks = Vec::new();

        // Would need actual OntologyBlock instances here
        // This is a simplified example

        let filtered = loader.filter_by_domain(blocks, Domain::AI);
        assert_eq!(filtered.len(), 0);
    }

    #[test]
    fn test_statistics() {
        let loader = OntologyLoader::default();
        let blocks = Vec::new();

        let stats = loader.get_statistics(&blocks);
        assert_eq!(stats.total_blocks, 0);
        assert_eq!(stats.cache_hit_rate(), 0.0);
    }

    #[test]
    fn test_cache_stats() {
        let loader = OntologyLoader::default();
        let (size, hits, misses, rate) = loader.get_cache_stats();

        assert_eq!(size, 0);
        assert_eq!(hits, 0);
        assert_eq!(misses, 0);
        assert_eq!(rate, 0.0);
    }

    #[test]
    fn test_group_by_domain() {
        let loader = OntologyLoader::default();
        let blocks = Vec::new();

        let groups = loader.group_by_domain(blocks);
        assert_eq!(groups.len(), 0);
    }

    #[test]
    fn test_filter_by_pattern() {
        let loader = OntologyLoader::default();
        let blocks = Vec::new();

        let result = loader.filter_by_pattern(blocks, r"AI-\d+");
        assert!(result.is_ok());
    }

    #[test]
    fn test_invalid_pattern() {
        let loader = OntologyLoader::default();
        let blocks = Vec::new();

        let result = loader.filter_by_pattern(blocks, "[invalid");
        assert!(result.is_err());
    }
}