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
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
//! Package management for SAMM namespace packages
//!
//! This module provides import and export functionality for SAMM namespace packages,
//! which are ZIP files containing Aspect Models organized by namespace and version.
//!
//! ## Package Format
//!
//! A namespace package follows this structure:
//!
//! ```text
//! namespace-package.zip
//! └── org.eclipse.example.myns/
//!     └── 1.0.0/
//!         ├── AspectModel1.ttl
//!         ├── AspectModel2.ttl
//!         └── ...
//! ```
//!
//! ## Features
//!
//! - Import ZIP packages into models directory
//! - Export models to ZIP packages
//! - Dry-run mode for previewing changes
//! - Force mode for overwriting existing files
//! - URN-based and file-based exports

use crate::error::{Result, SammError};
use crate::performance::profiling;
use oxiarc_archive::{ZipCompressionLevel, ZipReader, ZipWriter};
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};
use tokio::fs as async_fs;

/// Result of a package import operation
#[derive(Debug, Clone)]
pub struct ImportResult {
    /// Models organized by namespace
    pub namespaces: HashMap<String, Vec<ModelInfo>>,
    /// Total number of models
    pub total_models: usize,
    /// Number of models skipped (already exist)
    pub skipped: usize,
}

/// Information about a model in a package
#[derive(Debug, Clone)]
pub struct ModelInfo {
    /// Model name (without .ttl extension)
    pub name: String,
    /// Namespace
    pub namespace: String,
    /// Version
    pub version: String,
    /// Full path where model will be/is located
    pub path: PathBuf,
    /// Whether the file already exists
    pub exists: bool,
}

/// Result of a package export operation
#[derive(Debug, Clone)]
pub struct ExportResult {
    /// Namespace of the exported package
    pub namespace: String,
    /// Version of the exported package
    pub version: String,
    /// List of model names included
    pub models: Vec<String>,
    /// Total size in bytes
    pub size_bytes: u64,
}

/// Import a namespace package from a ZIP file
///
/// # Arguments
///
/// * `package_path` - Path to the ZIP file
/// * `models_root` - Target directory for models
/// * `dry_run` - If true, only analyze without writing files
/// * `force` - If true, overwrite existing files
///
/// # Returns
///
/// * `Result<ImportResult>` - Import result with statistics
///
/// # Examples
///
/// ```rust,no_run
/// use oxirs_samm::package;
/// use std::path::Path;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let result = package::import_package(
///     Path::new("namespace-package.zip"),
///     Path::new("./models"),
///     false,
///     false,
/// ).await?;
///
/// println!("Imported {} models", result.total_models);
/// # Ok(())
/// # }
/// ```
pub async fn import_package(
    package_path: &Path,
    models_root: &Path,
    dry_run: bool,
    force: bool,
) -> Result<ImportResult> {
    // Profile the entire import operation
    let (result, duration) = profiling::profile_async(
        "package_import",
        import_package_impl(package_path, models_root, dry_run, force),
    )
    .await;

    tracing::info!(
        "Package import completed in {:?}: {} models ({} skipped)",
        duration,
        result.as_ref().map(|r| r.total_models).unwrap_or(0),
        result.as_ref().map(|r| r.skipped).unwrap_or(0)
    );

    result
}

/// Internal implementation of import_package with profiling support
async fn import_package_impl(
    package_path: &Path,
    models_root: &Path,
    dry_run: bool,
    force: bool,
) -> Result<ImportResult> {
    // Open the ZIP file
    let file = std::fs::File::open(package_path)
        .map_err(|e| SammError::ParseError(format!("Failed to open package file: {}", e)))?;

    let mut archive = ZipReader::new(file)
        .map_err(|e| SammError::ParseError(format!("Invalid ZIP package: {}", e)))?;

    let mut namespaces: HashMap<String, Vec<ModelInfo>> = HashMap::new();
    let mut total_models = 0;
    let mut skipped = 0;

    // First, collect entry information (to avoid borrowing issues)
    let entries_to_process: Vec<_> = archive
        .entries()
        .iter()
        .filter(|entry| !entry.is_dir() && entry.name.ends_with(".ttl"))
        .cloned()
        .collect();

    // Process each entry
    for entry in entries_to_process {
        let file_path = entry.name.clone();

        // Parse the path: namespace/version/filename.ttl
        let parts: Vec<&str> = file_path.split('/').collect();
        if parts.len() < 3 {
            continue; // Invalid structure, skip
        }

        let namespace = parts[parts.len() - 3].to_string();
        let version = parts[parts.len() - 2].to_string();
        let filename = parts[parts.len() - 1];
        let model_name = filename.trim_end_matches(".ttl").to_string();

        // Construct target path
        let target_path = models_root.join(&namespace).join(&version).join(filename);

        let exists = target_path.exists();

        // Determine if we should skip this file
        if exists && !force {
            skipped += 1;
        }

        // Create model info
        let model_info = ModelInfo {
            name: model_name,
            namespace: namespace.clone(),
            version: version.clone(),
            path: target_path.clone(),
            exists,
        };

        // Add to namespace map
        namespaces
            .entry(namespace.clone())
            .or_default()
            .push(model_info);

        total_models += 1;

        // Write file if not in dry-run mode and (force or doesn't exist)
        if !dry_run && (force || !exists) {
            // Create parent directory
            if let Some(parent) = target_path.parent() {
                async_fs::create_dir_all(parent).await?;
            }

            // Read content from ZIP
            let content = archive.extract(&entry).map_err(|e| {
                SammError::ParseError(format!("Failed to extract file from ZIP: {}", e))
            })?;

            // Write to target path
            async_fs::write(&target_path, content).await?;
        }
    }

    Ok(ImportResult {
        namespaces,
        total_models,
        skipped,
    })
}

/// Export a single Aspect Model file to a namespace package
///
/// # Arguments
///
/// * `model_file` - Path to the Aspect Model TTL file
/// * `output_path` - Output ZIP file path
///
/// # Returns
///
/// * `Result<ExportResult>` - Export result with statistics
///
/// # Examples
///
/// ```rust,no_run
/// use oxirs_samm::package;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let result = package::export_from_file(
///     "AspectModel.ttl",
///     std::path::Path::new("package.zip"),
/// ).await?;
///
/// println!("Exported {} models", result.models.len());
/// # Ok(())
/// # }
/// ```
pub async fn export_from_file(model_file: &str, output_path: &Path) -> Result<ExportResult> {
    // Profile the entire export operation
    let (result, duration) = profiling::profile_async(
        "package_export_file",
        export_from_file_impl(model_file, output_path),
    )
    .await;

    tracing::info!(
        "Package export from file completed in {:?}: {}",
        duration,
        model_file
    );

    result
}

/// Internal implementation of export_from_file with profiling support
async fn export_from_file_impl(model_file: &str, output_path: &Path) -> Result<ExportResult> {
    let model_path = PathBuf::from(model_file);

    // Read the model file
    let content = async_fs::read_to_string(&model_path).await?;

    // Parse the file to extract namespace and version from URN
    let (namespace, version) = extract_namespace_from_content(&content)?;

    // Get model name from filename
    let model_name = model_path
        .file_stem()
        .and_then(|s| s.to_str())
        .ok_or_else(|| SammError::ParseError("Invalid filename".to_string()))?
        .to_string();

    // Create ZIP file
    let zip_file = std::fs::File::create(output_path).map_err(SammError::Io)?;

    let mut zip = ZipWriter::new(zip_file);

    // Set compression level
    zip.set_compression(ZipCompressionLevel::Normal);

    // Add file to ZIP with proper structure: namespace/version/filename.ttl
    let zip_path = format!("{}/{}/{}.ttl", namespace, version, model_name);
    zip.add_file(&zip_path, content.as_bytes())
        .map_err(|e| SammError::ParseError(format!("Failed to add ZIP entry: {}", e)))?;

    let file = zip
        .into_inner()
        .map_err(|e| SammError::ParseError(format!("Failed to finalize ZIP: {}", e)))?;

    let size_bytes = file.metadata().map(|m| m.len()).unwrap_or(0);

    Ok(ExportResult {
        namespace,
        version,
        models: vec![model_name],
        size_bytes,
    })
}

/// Export models from a namespace URN to a package
///
/// # Arguments
///
/// * `urn` - Namespace URN (e.g., "urn:samm:org.eclipse.example.myns:1.0.0")
/// * `output_path` - Output ZIP file path
/// * `version_filter` - Optional version filter
///
/// # Returns
///
/// * `Result<ExportResult>` - Export result with statistics
///
/// # Examples
///
/// ```rust,no_run
/// use oxirs_samm::package;
/// use std::path::Path;
///
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let result = package::export_from_urn(
///     "urn:samm:org.eclipse.example.myns:1.0.0",
///     Path::new("package.zip"),
///     None,
/// ).await?;
///
/// println!("Exported {} models", result.models.len());
/// # Ok(())
/// # }
/// ```
pub async fn export_from_urn(
    urn: &str,
    output_path: &Path,
    version_filter: Option<&str>,
) -> Result<ExportResult> {
    // Profile the entire export operation
    let (result, duration) = profiling::profile_async(
        "package_export_urn",
        export_from_urn_impl(urn, output_path, version_filter),
    )
    .await;

    tracing::info!(
        "Package export from URN completed in {:?}: {} ({} models)",
        duration,
        urn,
        result.as_ref().map(|r| r.models.len()).unwrap_or(0)
    );

    result
}

/// Internal implementation of export_from_urn with profiling support
async fn export_from_urn_impl(
    urn: &str,
    output_path: &Path,
    version_filter: Option<&str>,
) -> Result<ExportResult> {
    use tokio::fs as async_fs;

    // Parse URN to extract namespace and version
    let (namespace, version) = parse_urn(urn)?;

    // Use version filter if provided
    let final_version = version_filter.unwrap_or(&version);

    // Determine models root directory
    // Try these in order:
    // 1. Environment variable SAMM_MODELS_ROOT
    // 2. ./models if it exists
    // 3. Current directory
    let models_root = if let Ok(env_path) = std::env::var("SAMM_MODELS_ROOT") {
        PathBuf::from(env_path)
    } else if PathBuf::from("./models").exists() {
        PathBuf::from("./models")
    } else {
        std::env::current_dir().map_err(SammError::Io)?
    };

    // Construct path to namespace/version directory
    let namespace_dir = models_root.join(&namespace).join(final_version);

    // Check if directory exists
    if !namespace_dir.exists() {
        return Err(SammError::ParseError(format!(
            "Namespace directory not found: {} (models root: {})",
            namespace_dir.display(),
            models_root.display()
        )));
    }

    // Scan directory for .ttl files
    let mut entries = async_fs::read_dir(&namespace_dir)
        .await
        .map_err(SammError::Io)?;

    let mut model_files: Vec<PathBuf> = Vec::new();
    while let Some(entry) = entries.next_entry().await.map_err(SammError::Io)? {
        let path = entry.path();
        if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("ttl") {
            model_files.push(path);
        }
    }

    if model_files.is_empty() {
        return Err(SammError::ParseError(format!(
            "No .ttl files found in namespace directory: {}",
            namespace_dir.display()
        )));
    }

    // Create ZIP file
    let zip_file = std::fs::File::create(output_path).map_err(SammError::Io)?;

    let mut zip = ZipWriter::new(zip_file);

    // Set compression level
    zip.set_compression(ZipCompressionLevel::Normal);

    let mut model_names = Vec::new();

    // Add all model files to ZIP
    for model_file in &model_files {
        let content = async_fs::read_to_string(model_file)
            .await
            .map_err(SammError::Io)?;

        // Get model name from filename
        let model_name = model_file
            .file_stem()
            .and_then(|s| s.to_str())
            .ok_or_else(|| SammError::ParseError("Invalid filename".to_string()))?;

        model_names.push(model_name.to_string());

        // Add file to ZIP with proper structure: namespace/version/filename.ttl
        let zip_path = format!("{}/{}/{}.ttl", namespace, final_version, model_name);
        zip.add_file(&zip_path, content.as_bytes())
            .map_err(|e| SammError::ParseError(format!("Failed to add ZIP entry: {}", e)))?;
    }

    let file = zip
        .into_inner()
        .map_err(|e| SammError::ParseError(format!("Failed to finalize ZIP: {}", e)))?;

    let size_bytes = file.metadata().map(|m| m.len()).unwrap_or(0);

    Ok(ExportResult {
        namespace,
        version: final_version.to_string(),
        models: model_names,
        size_bytes,
    })
}

/// Extract namespace and version from Turtle content
fn extract_namespace_from_content(content: &str) -> Result<(String, String)> {
    // Look for Aspect URN pattern first: <urn:samm:namespace:version#Element> a samm:Aspect
    // This avoids matching prefix declarations like @prefix samm: <urn:...>
    for line in content.lines() {
        // Skip prefix declarations
        if line.trim().starts_with("@prefix") {
            continue;
        }

        // Look for Aspect definitions: <urn:...> a samm:Aspect
        if line.contains("urn:samm:")
            && (line.contains("a samm:Aspect")
                || line.contains("a samm:Property")
                || line.contains("a samm:Operation"))
        {
            // Find URN pattern between < and >
            if let Some(start) = line.find("<urn:samm:") {
                let urn_part = &line[start + 1..]; // Skip the '<'
                if let Some(end) = urn_part.find('>') {
                    let urn = &urn_part[..end];
                    // Also need to strip the #Element part for parsing
                    let urn_without_element = if let Some(hash_pos) = urn.find('#') {
                        &urn[..hash_pos]
                    } else {
                        urn
                    };

                    if let Ok((namespace, version)) = parse_urn(urn_without_element) {
                        // Skip meta-model and characteristic namespaces
                        if !namespace.contains("esmf.samm") {
                            return Ok((namespace, version));
                        }
                    }
                }
            }
        }
    }

    // Fallback: look for any URN
    for line in content.lines() {
        if line.contains("urn:samm:") && !line.trim().starts_with("@prefix") {
            if let Some(start) = line.find("urn:samm:") {
                let urn_part = &line[start..];
                if let Some(end) = urn_part.find(['>', '#', ' ']) {
                    let urn = &urn_part[..end];
                    if let Ok((namespace, version)) = parse_urn(urn) {
                        if !namespace.contains("esmf.samm") {
                            return Ok((namespace, version));
                        }
                    }
                }
            }
        }
    }

    // Default fallback
    Ok(("org.example.default".to_string(), "1.0.0".to_string()))
}

/// Parse a SAMM URN into namespace and version components
///
/// Format: urn:samm:namespace:version#Element
fn parse_urn(urn: &str) -> Result<(String, String)> {
    // Remove "urn:samm:" prefix
    let without_prefix = urn
        .strip_prefix("urn:samm:")
        .ok_or_else(|| SammError::ParseError(format!("Invalid URN format: {}", urn)))?;

    // Split by '#' to remove element name if present
    let without_element = without_prefix.split('#').next().unwrap_or(without_prefix);

    // Split by ':' to get namespace and version
    let parts: Vec<&str> = without_element.rsplitn(2, ':').collect();

    if parts.len() == 2 {
        let version = parts[0].to_string();
        let namespace = parts[1].to_string();
        Ok((namespace, version))
    } else {
        // No version specified, use default
        Ok((without_element.to_string(), "1.0.0".to_string()))
    }
}

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

    #[test]
    fn test_parse_urn_with_version() {
        let (namespace, version) = parse_urn("urn:samm:org.eclipse.example:1.0.0#Movement")
            .expect("parsing should succeed");
        assert_eq!(namespace, "org.eclipse.example");
        assert_eq!(version, "1.0.0");
    }

    #[test]
    fn test_parse_urn_without_element() {
        let (namespace, version) =
            parse_urn("urn:samm:org.eclipse.example:2.1.0").expect("parsing should succeed");
        assert_eq!(namespace, "org.eclipse.example");
        assert_eq!(version, "2.1.0");
    }

    #[test]
    fn test_parse_urn_without_version() {
        let (namespace, version) =
            parse_urn("urn:samm:org.eclipse.example").expect("parsing should succeed");
        assert_eq!(namespace, "org.eclipse.example");
        assert_eq!(version, "1.0.0"); // Default version
    }

    #[test]
    fn test_parse_urn_invalid() {
        assert!(parse_urn("invalid:urn").is_err());
        assert!(parse_urn("urn:other:namespace").is_err());
    }

    #[test]
    fn test_extract_namespace_from_content() {
        let content = r#"
@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:2.1.0#> .

<urn:samm:org.eclipse.example:1.0.0#Movement> a samm:Aspect .
"#;
        let (namespace, version) =
            extract_namespace_from_content(content).expect("operation should succeed");
        assert_eq!(namespace, "org.eclipse.example");
        assert_eq!(version, "1.0.0");
    }
}