syster-base 0.2.3-alpha

Core library for SysML v2 and KerML parsing, AST, and semantic analysis
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
//! JSON-LD format support.
//!
//! JSON-LD (JSON Linked Data) is used by the OMG Systems Modeling API
//! for REST API responses. This module provides serialization compatible
//! with the API specification.
//!
//! ## JSON-LD Structure
//!
//! ```json
//! {
//!   "@context": "https://www.omg.org/spec/SysML/20230201/context",
//!   "@type": "PartDefinition",
//!   "@id": "550e8400-e29b-41d4-a716-446655440000",
//!   "name": "Vehicle",
//!   "ownedMember": [
//!     { "@id": "550e8400-e29b-41d4-a716-446655440001" }
//!   ]
//! }
//! ```

use super::model::Model;
use super::{FormatCapability, InterchangeError, ModelFormat};

/// JSON-LD context URIs.
pub mod context {
    /// SysML v2 JSON-LD context.
    pub const SYSML: &str = "https://www.omg.org/spec/SysML/20230201/context";
    /// KerML JSON-LD context.
    pub const KERML: &str = "https://www.omg.org/spec/KerML/20230201/context";
}

/// JSON-LD format handler.
#[derive(Debug, Clone, Copy, Default)]
pub struct JsonLd;

impl ModelFormat for JsonLd {
    fn name(&self) -> &'static str {
        "JSON-LD"
    }

    fn extensions(&self) -> &'static [&'static str] {
        &["jsonld", "json"]
    }

    fn mime_type(&self) -> &'static str {
        "application/ld+json"
    }

    fn capabilities(&self) -> FormatCapability {
        // JSON-LD is primarily for API output, read support is secondary
        FormatCapability {
            read: true,
            write: true,
            streaming: true, // Can stream JSON arrays
            lossless: true,
        }
    }

    fn read(&self, input: &[u8]) -> Result<Model, InterchangeError> {
        #[cfg(feature = "interchange")]
        {
            JsonLdReader::new().read(input)
        }
        #[cfg(not(feature = "interchange"))]
        {
            let _ = input;
            Err(InterchangeError::Unsupported(
                "JSON-LD reading requires the 'interchange' feature".to_string(),
            ))
        }
    }

    fn write(&self, model: &Model) -> Result<Vec<u8>, InterchangeError> {
        #[cfg(feature = "interchange")]
        {
            JsonLdWriter::new().write(model)
        }
        #[cfg(not(feature = "interchange"))]
        {
            let _ = model;
            Err(InterchangeError::Unsupported(
                "JSON-LD writing requires the 'interchange' feature".to_string(),
            ))
        }
    }

    fn validate(&self, input: &[u8]) -> Result<(), InterchangeError> {
        let content = std::str::from_utf8(input)
            .map_err(|e| InterchangeError::json(format!("Invalid UTF-8: {e}")))?;

        // Quick check for JSON structure
        let trimmed = content.trim();
        if !trimmed.starts_with('{') && !trimmed.starts_with('[') {
            return Err(InterchangeError::json("Not valid JSON"));
        }

        Ok(())
    }
}

// ============================================================================
// JSON-LD READER (requires interchange feature)
// ============================================================================

#[cfg(feature = "interchange")]
mod reader {
    use super::*;
    use crate::interchange::model::{Element, ElementId, ElementKind, PropertyValue};
    use serde_json::Value;
    use std::sync::Arc;

    /// JSON-LD reader.
    pub struct JsonLdReader;

    impl JsonLdReader {
        pub fn new() -> Self {
            Self
        }

        pub fn read(&self, input: &[u8]) -> Result<Model, InterchangeError> {
            let value: Value = serde_json::from_slice(input)
                .map_err(|e| InterchangeError::json(format!("Parse error: {e}")))?;

            let mut model = Model::new();

            match value {
                Value::Object(obj) => {
                    // Single element
                    if let Some(element) = parse_element(&obj)? {
                        model.add_element(element);
                    }
                }
                Value::Array(arr) => {
                    // Array of elements
                    for item in arr {
                        if let Value::Object(obj) = item {
                            if let Some(element) = parse_element(&obj)? {
                                model.add_element(element);
                            }
                        }
                    }
                }
                _ => {
                    return Err(InterchangeError::json("Expected object or array"));
                }
            }

            // Build ownership relationships
            build_ownership(&mut model);

            Ok(model)
        }
    }

    /// Parse a JSON object into an Element.
    fn parse_element(
        obj: &serde_json::Map<String, Value>,
    ) -> Result<Option<Element>, InterchangeError> {
        // Get @id (required)
        let id = match obj.get("@id") {
            Some(Value::String(s)) => s.clone(),
            _ => return Ok(None), // Skip elements without @id
        };

        // Get @type
        let type_str = match obj.get("@type") {
            Some(Value::String(s)) => s.as_str(),
            _ => "Element",
        };
        let kind = ElementKind::from_xmi_type(type_str);

        let mut element = Element::new(id, kind);

        // Get name (also check declaredName for compatibility)
        if let Some(Value::String(name)) = obj.get("name").or_else(|| obj.get("declaredName")) {
            element.name = Some(Arc::from(name.as_str()));
        }

        // Get shortName (also check declaredShortName)
        if let Some(Value::String(short_name)) = obj
            .get("shortName")
            .or_else(|| obj.get("declaredShortName"))
        {
            element.short_name = Some(Arc::from(short_name.as_str()));
        }

        // Get isAbstract
        if let Some(Value::Bool(is_abstract)) = obj.get("isAbstract") {
            element.is_abstract = *is_abstract;
        }

        // Get documentation (body text)
        if let Some(Value::String(doc)) = obj.get("documentation").or_else(|| obj.get("body")) {
            element.documentation = Some(Arc::from(doc.as_str()));
        }

        // Get owner (as @id reference)
        if let Some(Value::Object(owner_obj)) = obj.get("owner") {
            if let Some(Value::String(owner_id)) = owner_obj.get("@id") {
                element.owner = Some(ElementId::new(owner_id.clone()));
            }
        }

        // Get ownedMember (array of @id references)
        if let Some(Value::Array(members)) = obj.get("ownedMember") {
            for member in members {
                if let Value::Object(member_obj) = member {
                    if let Some(Value::String(member_id)) = member_obj.get("@id") {
                        element
                            .owned_elements
                            .push(ElementId::new(member_id.clone()));
                    }
                }
            }
        }

        // Get additional properties (isStandard, isComposite, etc.)
        for (key, value) in obj {
            // Skip already-handled properties
            if matches!(
                key.as_str(),
                "@id"
                    | "@type"
                    | "@context"
                    | "name"
                    | "declaredName"
                    | "shortName"
                    | "declaredShortName"
                    | "isAbstract"
                    | "documentation"
                    | "body"
                    | "owner"
                    | "ownedMember"
                    | "ownedRelationship"
                    | "ownedRelatedElement"
            ) {
                continue;
            }
            // Store string/bool properties using PropertyValue
            let prop_key: Arc<str> = Arc::from(key.as_str());
            match value {
                Value::String(s) => {
                    element
                        .properties
                        .insert(prop_key, PropertyValue::from(s.as_str()));
                }
                Value::Bool(b) => {
                    element.properties.insert(prop_key, PropertyValue::from(*b));
                }
                Value::Number(n) => {
                    if let Some(i) = n.as_i64() {
                        element.properties.insert(prop_key, PropertyValue::from(i));
                    } else if let Some(f) = n.as_f64() {
                        element.properties.insert(prop_key, PropertyValue::from(f));
                    }
                }
                _ => {}
            }
        }

        Ok(Some(element))
    }

    /// Build ownership relationships from ownedMember arrays.
    fn build_ownership(model: &mut Model) {
        // Collect owner updates
        let mut updates: Vec<(ElementId, ElementId)> = Vec::new();

        for element in model.elements.values() {
            for owned_id in &element.owned_elements {
                updates.push((element.id.clone(), owned_id.clone()));
            }
        }

        // Apply owner to owned elements
        for (owner_id, owned_id) in updates {
            if let Some(owned) = model.elements.get_mut(&owned_id) {
                if owned.owner.is_none() {
                    owned.owner = Some(owner_id);
                }
            }
        }
    }
}

#[cfg(feature = "interchange")]
use reader::JsonLdReader;

// ============================================================================
// JSON-LD WRITER (requires interchange feature)
// ============================================================================

#[cfg(feature = "interchange")]
mod writer {
    use super::*;
    use crate::interchange::model::{Element, PropertyValue};
    use serde_json::{Map, Value, json};

    /// JSON-LD writer.
    pub struct JsonLdWriter;

    impl JsonLdWriter {
        pub fn new() -> Self {
            Self
        }

        pub fn write(&self, model: &Model) -> Result<Vec<u8>, InterchangeError> {
            let elements: Vec<Value> = model.iter_elements().map(element_to_json).collect();

            let output = if elements.len() == 1 {
                // Single element - return object directly
                elements.into_iter().next().unwrap()
            } else {
                // Multiple elements - return array
                Value::Array(elements)
            };

            serde_json::to_vec_pretty(&output)
                .map_err(|e| InterchangeError::json(format!("Serialization error: {e}")))
        }
    }

    /// Convert an Element to JSON-LD Value.
    fn element_to_json(element: &Element) -> Value {
        let mut obj = Map::new();

        // @context (only for root elements)
        if element.owner.is_none() {
            obj.insert("@context".to_string(), json!(context::SYSML));
        }

        // @type
        obj.insert("@type".to_string(), json!(element.kind.jsonld_type()));

        // @id
        obj.insert("@id".to_string(), json!(element.id.as_str()));

        // name
        if let Some(ref name) = element.name {
            obj.insert("name".to_string(), json!(name.as_ref()));
        }

        // shortName
        if let Some(ref short_name) = element.short_name {
            obj.insert("shortName".to_string(), json!(short_name.as_ref()));
        }

        // isAbstract (only if true)
        if element.is_abstract {
            obj.insert("isAbstract".to_string(), json!(true));
        }

        // documentation
        if let Some(ref doc) = element.documentation {
            obj.insert("documentation".to_string(), json!(doc.as_ref()));
        }

        // Additional properties (isStandard, isComposite, etc.)
        for (key, value) in &element.properties {
            let json_value = property_value_to_json(value);
            obj.insert(key.to_string(), json_value);
        }

        // owner (as @id reference)
        if let Some(ref owner_id) = element.owner {
            obj.insert("owner".to_string(), json!({"@id": owner_id.as_str()}));
        }

        // ownedMember (as array of @id references)
        if !element.owned_elements.is_empty() {
            let members: Vec<Value> = element
                .owned_elements
                .iter()
                .map(|id| json!({"@id": id.as_str()}))
                .collect();
            obj.insert("ownedMember".to_string(), Value::Array(members));
        }

        Value::Object(obj)
    }

    /// Convert a PropertyValue to JSON Value.
    fn property_value_to_json(value: &PropertyValue) -> Value {
        use crate::interchange::model::PropertyValue;
        match value {
            PropertyValue::String(s) => json!(s.as_ref()),
            PropertyValue::Integer(i) => json!(*i),
            PropertyValue::Real(f) => json!(*f),
            PropertyValue::Boolean(b) => json!(*b),
            PropertyValue::Reference(id) => json!({"@id": id.as_str()}),
            PropertyValue::List(items) => {
                Value::Array(items.iter().map(property_value_to_json).collect())
            }
        }
    }
}

#[cfg(feature = "interchange")]
use writer::JsonLdWriter;

// Stub implementations when feature is disabled
#[cfg(not(feature = "interchange"))]
struct JsonLdReader;

#[cfg(not(feature = "interchange"))]
impl JsonLdReader {
    fn new() -> Self {
        Self
    }

    fn read(&self, _input: &[u8]) -> Result<Model, InterchangeError> {
        Err(InterchangeError::Unsupported(
            "JSON-LD reading requires the 'interchange' feature".to_string(),
        ))
    }
}

#[cfg(not(feature = "interchange"))]
struct JsonLdWriter;

#[cfg(not(feature = "interchange"))]
impl JsonLdWriter {
    fn new() -> Self {
        Self
    }

    fn write(&self, _model: &Model) -> Result<Vec<u8>, InterchangeError> {
        Err(InterchangeError::Unsupported(
            "JSON-LD writing requires the 'interchange' feature".to_string(),
        ))
    }
}

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

    #[test]
    fn test_jsonld_format_metadata() {
        let jsonld = JsonLd;
        assert_eq!(jsonld.name(), "JSON-LD");
        assert_eq!(jsonld.extensions(), &["jsonld", "json"]);
        assert_eq!(jsonld.mime_type(), "application/ld+json");
        assert!(jsonld.capabilities().read);
        assert!(jsonld.capabilities().write);
        assert!(jsonld.capabilities().streaming);
    }

    #[test]
    fn test_jsonld_validate_object() {
        let jsonld = JsonLd;
        let input = br#"{"@context": "...", "@type": "Package"}"#;
        assert!(jsonld.validate(input).is_ok());
    }

    #[test]
    fn test_jsonld_validate_array() {
        let jsonld = JsonLd;
        let input = br#"[{"@id": "1"}, {"@id": "2"}]"#;
        assert!(jsonld.validate(input).is_ok());
    }

    #[test]
    fn test_jsonld_validate_invalid() {
        let jsonld = JsonLd;
        let input = b"not json";
        assert!(jsonld.validate(input).is_err());
    }

    #[cfg(feature = "interchange")]
    mod interchange_tests {
        use super::*;
        use crate::interchange::model::{Element, ElementId, ElementKind, PropertyValue};
        use std::sync::Arc;

        #[test]
        fn test_jsonld_write_single_element() {
            let mut model = Model::new();
            model.add_element(Element::new("pkg1", ElementKind::Package).with_name("TestPackage"));

            let json_bytes = JsonLd.write(&model).expect("Write failed");
            let json_str = String::from_utf8(json_bytes).expect("Invalid UTF-8");

            assert!(json_str.contains("@context"));
            assert!(json_str.contains("@type"));
            assert!(json_str.contains("Package"));
            assert!(json_str.contains("pkg1"));
            assert!(json_str.contains("TestPackage"));
        }

        #[test]
        fn test_jsonld_write_multiple_elements() {
            let mut model = Model::new();
            model.add_element(Element::new("pkg1", ElementKind::Package).with_name("Package1"));
            model.add_element(Element::new("pkg2", ElementKind::Package).with_name("Package2"));

            let json_bytes = JsonLd.write(&model).expect("Write failed");
            let json_str = String::from_utf8(json_bytes).expect("Invalid UTF-8");

            // Multiple elements should be an array
            assert!(json_str.trim().starts_with('['));
            assert!(json_str.contains("Package1"));
            assert!(json_str.contains("Package2"));
        }

        #[test]
        fn test_jsonld_read_single_element() {
            let json = br#"{
                "@type": "Package",
                "@id": "pkg1",
                "name": "TestPackage"
            }"#;

            let model = JsonLd.read(json).expect("Read failed");
            assert_eq!(model.element_count(), 1);

            let pkg = model
                .get(&ElementId::new("pkg1"))
                .expect("Package not found");
            assert_eq!(pkg.name.as_deref(), Some("TestPackage"));
            assert_eq!(pkg.kind, ElementKind::Package);
        }

        #[test]
        fn test_jsonld_read_array() {
            let json = br#"[
                {"@type": "Package", "@id": "pkg1", "name": "First"},
                {"@type": "Package", "@id": "pkg2", "name": "Second"}
            ]"#;

            let model = JsonLd.read(json).expect("Read failed");
            assert_eq!(model.element_count(), 2);
        }

        #[test]
        fn test_jsonld_read_with_attributes() {
            let json = br#"{
                "@type": "Class",
                "@id": "cls1",
                "name": "AbstractClass",
                "shortName": "AC",
                "isAbstract": true,
                "documentation": "This is a doc comment",
                "isStandard": true,
                "customNumber": 42,
                "customString": "hello"
            }"#;

            let model = JsonLd.read(json).expect("Read failed");
            let cls = model.get(&ElementId::new("cls1")).expect("Class not found");

            assert_eq!(cls.name.as_deref(), Some("AbstractClass"));
            assert_eq!(cls.short_name.as_deref(), Some("AC"));
            assert!(cls.is_abstract);
            assert_eq!(cls.documentation.as_deref(), Some("This is a doc comment"));
            assert_eq!(
                cls.properties.get(&Arc::from("isStandard")),
                Some(&PropertyValue::Boolean(true))
            );
            assert_eq!(
                cls.properties.get(&Arc::from("customNumber")),
                Some(&PropertyValue::Integer(42))
            );
            assert_eq!(
                cls.properties.get(&Arc::from("customString")),
                Some(&PropertyValue::String(Arc::from("hello")))
            );
        }

        #[test]
        fn test_jsonld_write_with_attributes() {
            let mut model = Model::new();
            let mut cls = Element::new("cls1", ElementKind::Class);
            cls.name = Some(Arc::from("AbstractClass"));
            cls.short_name = Some(Arc::from("AC"));
            cls.is_abstract = true;
            cls.documentation = Some(Arc::from("This is documented"));
            cls.properties
                .insert(Arc::from("isStandard"), PropertyValue::Boolean(true));
            cls.properties
                .insert(Arc::from("count"), PropertyValue::Integer(99));
            model.add_element(cls);

            let json_bytes = JsonLd.write(&model).expect("Write failed");
            let json_str = String::from_utf8(json_bytes).expect("Invalid UTF-8");

            assert!(json_str.contains("\"isAbstract\": true"));
            assert!(json_str.contains("\"documentation\": \"This is documented\""));
            assert!(json_str.contains("\"isStandard\": true"));
            assert!(json_str.contains("\"count\": 99"));
            assert!(json_str.contains("\"shortName\": \"AC\""));
        }

        #[test]
        fn test_jsonld_roundtrip() {
            let mut model = Model::new();
            let pkg = Element::new("pkg1", ElementKind::Package).with_name("RoundtripTest");
            model.add_element(pkg);

            let part = Element::new("part1", ElementKind::PartDefinition)
                .with_name("Vehicle")
                .with_owner("pkg1");
            model.add_element(part);

            // Update ownership
            if let Some(pkg) = model.elements.get_mut(&ElementId::new("pkg1")) {
                pkg.owned_elements.push(ElementId::new("part1"));
            }

            // Write to JSON-LD
            let json_bytes = JsonLd.write(&model).expect("Write failed");

            // Read back
            let model2 = JsonLd.read(&json_bytes).expect("Read failed");

            // Verify
            assert_eq!(model2.element_count(), 2);
            let pkg2 = model2.get(&ElementId::new("pkg1")).unwrap();
            assert_eq!(pkg2.name.as_deref(), Some("RoundtripTest"));
        }

        #[test]
        fn test_jsonld_roundtrip_with_all_attributes() {
            let mut model = Model::new();

            // Create element with all attributes
            let mut cls = Element::new("cls1", ElementKind::Class);
            cls.name = Some(Arc::from("TestClass"));
            cls.short_name = Some(Arc::from("TC"));
            cls.is_abstract = true;
            cls.documentation = Some(Arc::from("A documented class"));
            cls.properties
                .insert(Arc::from("isStandard"), PropertyValue::Boolean(true));
            cls.properties
                .insert(Arc::from("priority"), PropertyValue::Integer(5));
            cls.properties
                .insert(Arc::from("ratio"), PropertyValue::Real(3.14));
            cls.properties
                .insert(Arc::from("label"), PropertyValue::String(Arc::from("test")));
            model.add_element(cls);

            // Roundtrip
            let json_bytes = JsonLd.write(&model).expect("Write failed");
            let model2 = JsonLd.read(&json_bytes).expect("Read failed");

            // Verify all attributes preserved
            let cls2 = model2
                .get(&ElementId::new("cls1"))
                .expect("Class not found");
            assert_eq!(cls2.name.as_deref(), Some("TestClass"));
            assert_eq!(cls2.short_name.as_deref(), Some("TC"));
            assert!(cls2.is_abstract, "isAbstract not preserved");
            assert_eq!(cls2.documentation.as_deref(), Some("A documented class"));
            assert_eq!(
                cls2.properties.get(&Arc::from("isStandard")),
                Some(&PropertyValue::Boolean(true)),
                "isStandard property not preserved"
            );
            assert_eq!(
                cls2.properties.get(&Arc::from("priority")),
                Some(&PropertyValue::Integer(5)),
                "priority property not preserved"
            );
            assert_eq!(
                cls2.properties.get(&Arc::from("ratio")),
                Some(&PropertyValue::Real(3.14)),
                "ratio property not preserved"
            );
            assert_eq!(
                cls2.properties.get(&Arc::from("label")),
                Some(&PropertyValue::String(Arc::from("test"))),
                "label property not preserved"
            );
        }
    }
}