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
593
594
595
596
/// SAMM operation registry for aspect operations.
///
/// Implements a registry for SAMM `Operation` elements.  Each operation has a
/// unique IRI id, optional description, a list of typed inputs, an optional
/// output, and error entity IRIs.
use std::collections::HashMap;

use thiserror::Error;

// ── Types ─────────────────────────────────────────────────────────────────────

/// The type of an input or output binding.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum InputOutputType {
    /// A single value of the named type.
    Single(String),
    /// A collection of the named type.
    Collection(String),
    /// An optional value of the named type.
    Optional(String),
}

impl InputOutputType {
    /// Returns the inner type name regardless of variant.
    pub fn type_name(&self) -> &str {
        match self {
            Self::Single(t) | Self::Collection(t) | Self::Optional(t) => t,
        }
    }
}

/// A single input parameter for an operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OperationInput {
    /// Name of the input parameter.
    pub name: String,
    /// Type of this input parameter.
    pub input_type: InputOutputType,
    /// If `true` the input does not need to be provided by callers.
    pub optional: bool,
}

/// The output descriptor of an operation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OperationOutput {
    /// Type of the operation output.
    pub output_type: InputOutputType,
}

/// A SAMM operation definition.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Operation {
    /// IRI identifier of this operation.
    pub id: String,
    /// Human-readable name.
    pub name: String,
    /// Optional human-readable description.
    pub description: Option<String>,
    /// Ordered list of input parameters.
    pub inputs: Vec<OperationInput>,
    /// Optional output descriptor.
    pub output: Option<OperationOutput>,
    /// Error entity IRIs this operation can raise.
    pub errors: Vec<String>,
}

// ── Error type ────────────────────────────────────────────────────────────────

/// Errors returned by `OperationRegistry`.
#[derive(Debug, Clone, PartialEq, Eq, Error)]
pub enum RegistryError {
    /// An operation with the same id was already registered.
    #[error("duplicate operation id: {0}")]
    DuplicateId(String),
    /// The operation failed validation checks.
    #[error("invalid operation: {0}")]
    InvalidOperation(String),
}

// ── Summary ───────────────────────────────────────────────────────────────────

/// Aggregate summary of the registry.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RegistrySummary {
    /// Total number of registered operations.
    pub total: usize,
    /// Number of operations that have an output descriptor.
    pub with_output: usize,
    /// Number of operations without an output descriptor.
    pub without_output: usize,
    /// Total number of input parameters across all operations.
    pub total_inputs: usize,
}

// ── OperationRegistry ─────────────────────────────────────────────────────────

/// Registry holding all operations for an aspect model.
#[derive(Debug, Default)]
pub struct OperationRegistry {
    operations: HashMap<String, Operation>,
}

impl OperationRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self::default()
    }

    /// Register a new operation.  Returns an error if an operation with the
    /// same `id` already exists, or if the operation is invalid.
    pub fn register(&mut self, op: Operation) -> Result<(), RegistryError> {
        // Validate before inserting
        let warnings = self.validate_operation(&op);
        if warnings.iter().any(|w| w.starts_with("ERROR")) {
            return Err(RegistryError::InvalidOperation(warnings.join("; ")));
        }
        if self.operations.contains_key(&op.id) {
            return Err(RegistryError::DuplicateId(op.id.clone()));
        }
        self.operations.insert(op.id.clone(), op);
        Ok(())
    }

    /// Retrieve an operation by id.
    pub fn get(&self, id: &str) -> Option<&Operation> {
        self.operations.get(id)
    }

    /// Remove an operation by id.  Returns `true` if it existed.
    pub fn remove(&mut self, id: &str) -> bool {
        self.operations.remove(id).is_some()
    }

    /// Find all operations with the given human-readable `name`.
    pub fn find_by_name(&self, name: &str) -> Vec<&Operation> {
        self.operations
            .values()
            .filter(|op| op.name == name)
            .collect()
    }

    /// All operations that have an output descriptor.
    pub fn operations_with_output(&self) -> Vec<&Operation> {
        self.operations
            .values()
            .filter(|op| op.output.is_some())
            .collect()
    }

    /// All operations without an output descriptor.
    pub fn operations_without_output(&self) -> Vec<&Operation> {
        self.operations
            .values()
            .filter(|op| op.output.is_none())
            .collect()
    }

    /// Validate an operation and return a list of human-readable messages.
    /// Messages beginning with `"ERROR"` indicate a blocking problem.
    pub fn validate_operation(&self, op: &Operation) -> Vec<String> {
        let mut msgs: Vec<String> = Vec::new();

        if op.id.is_empty() {
            msgs.push("ERROR: operation id must not be empty".to_string());
        }
        if op.name.is_empty() {
            msgs.push("WARNING: operation name is empty".to_string());
        }
        // Input name uniqueness
        let mut seen: HashMap<&str, usize> = HashMap::new();
        for input in &op.inputs {
            *seen.entry(input.name.as_str()).or_insert(0) += 1;
        }
        for (name, count) in &seen {
            if *count > 1 {
                msgs.push(format!("WARNING: duplicate input name '{name}'"));
            }
        }

        msgs
    }

    /// Number of registered operations.
    pub fn count(&self) -> usize {
        self.operations.len()
    }

    /// All registered operations as an unsorted vec.
    pub fn all(&self) -> Vec<&Operation> {
        self.operations.values().collect()
    }

    /// Aggregate summary of the registry.
    pub fn summary(&self) -> RegistrySummary {
        let total = self.operations.len();
        let with_output = self
            .operations
            .values()
            .filter(|o| o.output.is_some())
            .count();
        let without_output = total - with_output;
        let total_inputs = self.operations.values().map(|o| o.inputs.len()).sum();
        RegistrySummary {
            total,
            with_output,
            without_output,
            total_inputs,
        }
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    // Helper constructors

    fn single(t: &str) -> InputOutputType {
        InputOutputType::Single(t.into())
    }

    fn collection(t: &str) -> InputOutputType {
        InputOutputType::Collection(t.into())
    }

    fn optional(t: &str) -> InputOutputType {
        InputOutputType::Optional(t.into())
    }

    fn input(name: &str, ty: InputOutputType, optional: bool) -> OperationInput {
        OperationInput {
            name: name.into(),
            input_type: ty,
            optional,
        }
    }

    fn output(ty: InputOutputType) -> OperationOutput {
        OperationOutput { output_type: ty }
    }

    fn op_no_output(id: &str, name: &str) -> Operation {
        Operation {
            id: id.into(),
            name: name.into(),
            description: None,
            inputs: vec![],
            output: None,
            errors: vec![],
        }
    }

    fn op_with_output(id: &str, name: &str, ty: InputOutputType) -> Operation {
        Operation {
            id: id.into(),
            name: name.into(),
            description: None,
            inputs: vec![],
            output: Some(output(ty)),
            errors: vec![],
        }
    }

    // ── register / get ────────────────────────────────────────────────────────

    #[test]
    fn test_register_and_get() {
        let mut reg = OperationRegistry::new();
        let op = op_no_output("op:1", "myOp");
        reg.register(op.clone()).expect("should succeed");
        let retrieved = reg.get("op:1").expect("should succeed");
        assert_eq!(retrieved.name, "myOp");
    }

    #[test]
    fn test_get_nonexistent_returns_none() {
        let reg = OperationRegistry::new();
        assert!(reg.get("nonexistent").is_none());
    }

    #[test]
    fn test_register_duplicate_id_error() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "a"))
            .expect("should succeed");
        let err = reg.register(op_no_output("op:1", "b")).unwrap_err();
        assert!(matches!(err, RegistryError::DuplicateId(id) if id == "op:1"));
    }

    #[test]
    fn test_register_empty_id_error() {
        let mut reg = OperationRegistry::new();
        let err = reg.register(op_no_output("", "name")).unwrap_err();
        assert!(matches!(err, RegistryError::InvalidOperation(_)));
    }

    // ── remove ────────────────────────────────────────────────────────────────

    #[test]
    fn test_remove_existing() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "a"))
            .expect("should succeed");
        assert!(reg.remove("op:1"));
        assert!(reg.get("op:1").is_none());
    }

    #[test]
    fn test_remove_nonexistent() {
        let mut reg = OperationRegistry::new();
        assert!(!reg.remove("nope"));
    }

    #[test]
    fn test_remove_decrements_count() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "a"))
            .expect("should succeed");
        reg.register(op_no_output("op:2", "b"))
            .expect("should succeed");
        reg.remove("op:1");
        assert_eq!(reg.count(), 1);
    }

    // ── find_by_name ──────────────────────────────────────────────────────────

    #[test]
    fn test_find_by_name_single() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "getStatus"))
            .expect("should succeed");
        let found = reg.find_by_name("getStatus");
        assert_eq!(found.len(), 1);
        assert_eq!(found[0].id, "op:1");
    }

    #[test]
    fn test_find_by_name_multiple() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "reset"))
            .expect("should succeed");
        reg.register(op_no_output("op:2", "reset"))
            .expect("should succeed");
        let found = reg.find_by_name("reset");
        assert_eq!(found.len(), 2);
    }

    #[test]
    fn test_find_by_name_not_found() {
        let reg = OperationRegistry::new();
        assert!(reg.find_by_name("missing").is_empty());
    }

    // ── operations_with/without_output ────────────────────────────────────────

    #[test]
    fn test_operations_with_output() {
        let mut reg = OperationRegistry::new();
        reg.register(op_with_output("op:1", "a", single("String")))
            .expect("should succeed");
        reg.register(op_no_output("op:2", "b"))
            .expect("should succeed");
        let with_out = reg.operations_with_output();
        assert_eq!(with_out.len(), 1);
        assert_eq!(with_out[0].id, "op:1");
    }

    #[test]
    fn test_operations_without_output() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "a"))
            .expect("should succeed");
        reg.register(op_with_output("op:2", "b", single("Bool")))
            .expect("should succeed");
        let without = reg.operations_without_output();
        assert_eq!(without.len(), 1);
        assert_eq!(without[0].id, "op:1");
    }

    #[test]
    fn test_all_without_output_when_empty() {
        let reg = OperationRegistry::new();
        assert!(reg.operations_without_output().is_empty());
    }

    // ── validate_operation ────────────────────────────────────────────────────

    #[test]
    fn test_validate_operation_valid() {
        let reg = OperationRegistry::new();
        let op = op_no_output("op:1", "myOp");
        let msgs = reg.validate_operation(&op);
        assert!(msgs.iter().all(|m| !m.starts_with("ERROR")));
    }

    #[test]
    fn test_validate_operation_empty_id() {
        let reg = OperationRegistry::new();
        let op = op_no_output("", "x");
        let msgs = reg.validate_operation(&op);
        assert!(msgs.iter().any(|m| m.starts_with("ERROR")));
    }

    #[test]
    fn test_validate_operation_empty_name_warning() {
        let reg = OperationRegistry::new();
        let op = op_no_output("op:1", "");
        let msgs = reg.validate_operation(&op);
        assert!(msgs.iter().any(|m| m.starts_with("WARNING")));
    }

    #[test]
    fn test_validate_operation_duplicate_input_names() {
        let reg = OperationRegistry::new();
        let op = Operation {
            id: "op:1".into(),
            name: "op".into(),
            description: None,
            inputs: vec![
                input("dup", single("String"), false),
                input("dup", single("Int"), false),
            ],
            output: None,
            errors: vec![],
        };
        let msgs = reg.validate_operation(&op);
        assert!(msgs.iter().any(|m| m.contains("duplicate")));
    }

    // ── count / all ───────────────────────────────────────────────────────────

    #[test]
    fn test_count_zero_initially() {
        let reg = OperationRegistry::new();
        assert_eq!(reg.count(), 0);
    }

    #[test]
    fn test_count_after_register() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "a"))
            .expect("should succeed");
        reg.register(op_no_output("op:2", "b"))
            .expect("should succeed");
        assert_eq!(reg.count(), 2);
    }

    #[test]
    fn test_all_returns_all() {
        let mut reg = OperationRegistry::new();
        reg.register(op_no_output("op:1", "a"))
            .expect("should succeed");
        reg.register(op_no_output("op:2", "b"))
            .expect("should succeed");
        assert_eq!(reg.all().len(), 2);
    }

    #[test]
    fn test_all_empty_registry() {
        let reg = OperationRegistry::new();
        assert!(reg.all().is_empty());
    }

    // ── summary ───────────────────────────────────────────────────────────────

    #[test]
    fn test_summary_totals() {
        let mut reg = OperationRegistry::new();
        reg.register(op_with_output("op:1", "a", single("String")))
            .expect("should succeed");
        reg.register(op_no_output("op:2", "b"))
            .expect("should succeed");
        let s = reg.summary();
        assert_eq!(s.total, 2);
        assert_eq!(s.with_output, 1);
        assert_eq!(s.without_output, 1);
    }

    #[test]
    fn test_summary_total_inputs() {
        let mut reg = OperationRegistry::new();
        let op1 = Operation {
            id: "op:1".into(),
            name: "a".into(),
            description: None,
            inputs: vec![
                input("i1", single("String"), false),
                input("i2", single("Int"), false),
            ],
            output: None,
            errors: vec![],
        };
        let op2 = Operation {
            id: "op:2".into(),
            name: "b".into(),
            description: None,
            inputs: vec![input("i1", single("Bool"), true)],
            output: None,
            errors: vec![],
        };
        reg.register(op1).expect("should succeed");
        reg.register(op2).expect("should succeed");
        let s = reg.summary();
        assert_eq!(s.total_inputs, 3);
    }

    #[test]
    fn test_summary_empty() {
        let reg = OperationRegistry::new();
        let s = reg.summary();
        assert_eq!(s, RegistrySummary::default());
    }

    // ── InputOutputType ───────────────────────────────────────────────────────

    #[test]
    fn test_single_type_name() {
        assert_eq!(single("Foo").type_name(), "Foo");
    }

    #[test]
    fn test_collection_type_name() {
        assert_eq!(collection("Bar").type_name(), "Bar");
    }

    #[test]
    fn test_optional_type_name() {
        assert_eq!(optional("Baz").type_name(), "Baz");
    }

    #[test]
    fn test_input_optional_flag() {
        let i = input("x", optional("String"), true);
        assert!(i.optional);
    }

    #[test]
    fn test_operation_with_description() {
        let op = Operation {
            id: "op:desc".into(),
            name: "described".into(),
            description: Some("does something".into()),
            inputs: vec![],
            output: None,
            errors: vec![],
        };
        let mut reg = OperationRegistry::new();
        reg.register(op).expect("should succeed");
        let retrieved = reg.get("op:desc").expect("should succeed");
        assert_eq!(retrieved.description.as_deref(), Some("does something"));
    }

    #[test]
    fn test_operation_with_error_entities() {
        let op = Operation {
            id: "op:err".into(),
            name: "risky".into(),
            description: None,
            inputs: vec![],
            output: None,
            errors: vec!["urn:example:NotFound".into(), "urn:example:Timeout".into()],
        };
        let mut reg = OperationRegistry::new();
        reg.register(op).expect("should succeed");
        assert_eq!(reg.get("op:err").expect("should succeed").errors.len(), 2);
    }

    #[test]
    fn test_operation_with_collection_input() {
        let op = Operation {
            id: "op:col".into(),
            name: "batch".into(),
            description: None,
            inputs: vec![input("items", collection("Item"), false)],
            output: Some(output(single("Result"))),
            errors: vec![],
        };
        let mut reg = OperationRegistry::new();
        reg.register(op).expect("should succeed");
        let retrieved = reg.get("op:col").expect("should succeed");
        assert!(matches!(
            retrieved.inputs[0].input_type,
            InputOutputType::Collection(_)
        ));
    }

    #[test]
    fn test_registry_error_display() {
        let e = RegistryError::DuplicateId("op:x".into());
        assert!(e.to_string().contains("op:x"));
        let e2 = RegistryError::InvalidOperation("bad".into());
        assert!(e2.to_string().contains("bad"));
    }
}