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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
//! XMI (XML Model Interchange) format support.
//!
//! XMI is the OMG standard for exchanging MOF-based models in XML format.
//! SysML v2 and KerML models can be serialized to XMI for tool interoperability.
//!
//! ## XMI Structure
//!
//! ```xml
//! <?xml version="1.0" encoding="UTF-8"?>
//! <xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/20131001"
//!          xmlns:kerml="http://www.omg.org/spec/KerML/20230201"
//!          xmlns:sysml="http://www.omg.org/spec/SysML/20230201">
//!   <sysml:Package xmi:id="pkg1" name="MyPackage">
//!     <ownedMember xmi:type="sysml:PartDefinition" xmi:id="pd1" name="Vehicle"/>
//!   </sysml:Package>
//! </xmi:XMI>
//! ```

use std::sync::Arc;

use super::model::{Element, ElementId, ElementKind, Model, Relationship, RelationshipKind};
use super::{FormatCapability, InterchangeError, ModelFormat};

/// XMI namespace URIs.
pub mod namespace {
    /// XMI 2.5.1 namespace.
    pub const XMI: &str = "http://www.omg.org/spec/XMI/20131001";
    /// KerML namespace.
    pub const KERML: &str = "http://www.omg.org/spec/KerML/20230201";
    /// SysML v2 namespace.
    pub const SYSML: &str = "http://www.omg.org/spec/SysML/20230201";
}

/// XMI format handler.
#[derive(Debug, Clone, Copy, Default)]
pub struct Xmi;

impl ModelFormat for Xmi {
    fn name(&self) -> &'static str {
        "XMI"
    }

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

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

    fn capabilities(&self) -> FormatCapability {
        FormatCapability::FULL
    }

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

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

    fn validate(&self, input: &[u8]) -> Result<(), InterchangeError> {
        // Quick check for XML declaration and XMI/SysML namespace
        let content = std::str::from_utf8(input)
            .map_err(|e| InterchangeError::xml(format!("Invalid UTF-8: {e}")))?;

        // Accept either xmi:XMI root or sysml:Namespace/kerml:Namespace root
        if !content.contains("xmi:XMI")
            && !content.contains("XMI")
            && !content.contains("sysml:Namespace")
            && !content.contains("kerml:Namespace")
        {
            return Err(InterchangeError::xml("Missing XMI/SysML root element"));
        }

        Ok(())
    }
}

// ============================================================================
// XMI READER (requires interchange feature)
// ============================================================================

#[cfg(feature = "interchange")]
mod reader {
    use super::super::model::PropertyValue;
    use super::*;
    use indexmap::IndexMap;
    use quick_xml::Reader;
    use quick_xml::events::{BytesStart, Event};

    /// XMI document reader.
    pub struct XmiReader {
        /// Elements by ID for lookup (IndexMap preserves insertion order).
        elements_by_id: IndexMap<String, Element>,
        /// Parent stack for ownership tracking (element IDs only).
        parent_stack: Vec<String>,
        /// Depth tracking to match start/end tags properly.
        depth_stack: Vec<StackEntry>,
        /// Relationships collected during parsing.
        relationships: Vec<Relationship>,
        /// Counter for generating relationship IDs.
        rel_counter: u32,
        /// Tracks children per parent in parse order (parent_id -> [child_ids]).
        children_in_order: IndexMap<String, Vec<String>>,
    }

    /// Stack entry type for tracking nested elements.
    #[derive(Debug)]
    enum StackEntry {
        /// XMI root element - no push to parent stack.
        Root,
        /// Containment wrapper (ownedMember, etc.) - no push.
        Containment,
        /// Actual element - push element ID to parent stack.
        Element(String),
    }

    impl XmiReader {
        pub fn new() -> Self {
            Self {
                elements_by_id: IndexMap::new(),
                parent_stack: Vec::new(),
                depth_stack: Vec::new(),
                relationships: Vec::new(),
                rel_counter: 0,
                children_in_order: IndexMap::new(),
            }
        }

        pub fn read(&mut self, input: &[u8]) -> Result<Model, InterchangeError> {
            let mut reader = Reader::from_reader(input);
            reader.config_mut().trim_text(true);

            let mut buf = Vec::new();

            loop {
                match reader.read_event_into(&mut buf) {
                    Ok(Event::Start(ref e)) => {
                        self.handle_start_element(e)?;
                    }
                    Ok(Event::Empty(ref e)) => {
                        // Self-closing element - handle as start + end
                        self.handle_start_element(e)?;
                        self.handle_end_element();
                    }
                    Ok(Event::End(_)) => {
                        self.handle_end_element();
                    }
                    Ok(Event::Eof) => break,
                    Err(e) => {
                        return Err(InterchangeError::xml(format!(
                            "XML parse error at position {}: {e}",
                            reader.error_position()
                        )));
                    }
                    _ => {}
                }
                buf.clear();
            }

            self.build_model()
        }

        fn handle_start_element(&mut self, e: &BytesStart<'_>) -> Result<(), InterchangeError> {
            let name_bytes = e.name();
            let tag_name = std::str::from_utf8(name_bytes.as_ref())
                .map_err(|e| InterchangeError::xml(format!("Invalid tag name: {e}")))?;

            // Skip the XMI root element or namespace root
            if tag_name == "xmi:XMI"
                || tag_name == "XMI"
                || tag_name == "sysml:Namespace"
                || tag_name == "kerml:Namespace"
            {
                self.depth_stack.push(StackEntry::Root);
                return Ok(());
            }

            // Check if this is a containment wrapper (but NOT ownedRelationship or ownedRelatedElement - we want to parse those)
            if is_containment_tag(tag_name)
                && tag_name != "ownedRelationship"
                && tag_name != "ownedRelatedElement"
            {
                self.depth_stack.push(StackEntry::Containment);
                return Ok(());
            }

            // Extract all attributes
            let mut xmi_id: Option<String> = None;
            let mut xmi_type: Option<String> = None;
            let mut name: Option<String> = None;
            let mut qualified_name: Option<String> = None;
            let mut short_name: Option<String> = None;
            let mut element_id: Option<String> = None;
            let mut is_abstract = false;
            let mut is_standard = false;
            let mut is_composite = false;
            let mut body: Option<String> = None;
            let mut href: Option<String> = None;
            let mut extra_attrs: Vec<(String, String)> = Vec::new();

            // For relationship parsing
            let mut source_ref: Option<String> = None;
            let mut target_ref: Option<String> = None;

            for attr_result in e.attributes() {
                let attr = attr_result
                    .map_err(|e| InterchangeError::xml(format!("Attribute error: {e}")))?;
                let key = std::str::from_utf8(attr.key.as_ref())
                    .map_err(|e| InterchangeError::xml(format!("Attribute key error: {e}")))?;
                let value = attr
                    .unescape_value()
                    .map_err(|e| InterchangeError::xml(format!("Attribute value error: {e}")))?
                    .to_string();

                match key {
                    "xmi:id" | "id" => xmi_id = Some(value),
                    "xmi:type" | "xsi:type" | "type" => xmi_type = Some(value),
                    "name" | "declaredName" => name = Some(value),
                    "qualifiedName" => qualified_name = Some(value),
                    "shortName" | "declaredShortName" => short_name = Some(value),
                    "elementId" => element_id = Some(value),
                    "isAbstract" => is_abstract = value == "true",
                    "isStandard" => is_standard = value == "true",
                    "isComposite" => is_composite = value == "true",
                    "body" => body = Some(value),
                    "href" => href = Some(value),
                    // Relationship source/target references
                    "source" | "relatedElement" | "subclassifier" | "typedFeature"
                    | "redefiningFeature" | "subsettingFeature" => source_ref = Some(value),
                    "target" | "superclassifier" | "redefinedFeature" | "subsettedFeature"
                    | "general" | "specific" => target_ref = Some(value),
                    _ => {
                        // Store other attributes for roundtrip
                        if !key.starts_with("xmlns") && !key.starts_with("xmi:version") {
                            extra_attrs.push((key.to_string(), value));
                        }
                    }
                }
            }

            // Use elementId as fallback for xmi:id (official SysML XMI format)
            if xmi_id.is_none() {
                xmi_id = element_id.clone();
            }

            // Determine element kind from xmi:type or tag name
            let type_str = xmi_type.as_deref().unwrap_or(tag_name);
            let kind = ElementKind::from_xmi_type(type_str);

            // Create element if we have an ID
            if let Some(id) = xmi_id {
                let mut element = Element::new(id.clone(), kind);

                if let Some(n) = name {
                    element.name = Some(Arc::from(n.as_str()));
                }
                if let Some(qn) = qualified_name {
                    element.qualified_name = Some(Arc::from(qn.as_str()));
                }
                if let Some(sn) = short_name {
                    element.short_name = Some(Arc::from(sn.as_str()));
                }

                // Set boolean flags
                element.is_abstract = is_abstract;

                // Store isStandard in properties
                if is_standard {
                    element
                        .properties
                        .insert(Arc::from("isStandard"), PropertyValue::Boolean(true));
                }
                if is_composite {
                    element
                        .properties
                        .insert(Arc::from("isComposite"), PropertyValue::Boolean(true));
                }

                // Store documentation body
                if let Some(b) = body {
                    element.documentation = Some(Arc::from(b.as_str()));
                }

                // Store href for cross-file references
                if let Some(h) = href {
                    element.properties.insert(
                        Arc::from("href"),
                        PropertyValue::String(Arc::from(h.as_str())),
                    );
                }

                // Store extra attributes
                for (key, value) in extra_attrs {
                    element.properties.insert(
                        Arc::from(key.as_str()),
                        PropertyValue::String(Arc::from(value.as_str())),
                    );
                }

                // Set owner if we have a parent, and track child order
                if let Some(parent_id) = self.parent_stack.last() {
                    element.owner = Some(ElementId::new(parent_id.clone()));
                    // Track ALL children under their parent in parse order
                    self.children_in_order
                        .entry(parent_id.clone())
                        .or_insert_with(Vec::new)
                        .push(id.clone());
                }

                // If this is a relationship kind, also create a Relationship
                if kind.is_relationship() {
                    if let (Some(src), Some(tgt)) = (
                        source_ref.or_else(|| self.parent_stack.last().cloned()),
                        target_ref,
                    ) {
                        let rel_kind = element_kind_to_relationship_kind(kind);
                        let relationship = Relationship::new(id.clone(), rel_kind, src, tgt);
                        self.relationships.push(relationship);
                    }
                }

                self.elements_by_id.insert(id.clone(), element);
                self.parent_stack.push(id.clone());
                self.depth_stack.push(StackEntry::Element(id));
            } else {
                // Element without ID - still track for depth
                self.depth_stack.push(StackEntry::Containment);
            }

            Ok(())
        }
        fn handle_end_element(&mut self) {
            // Pop from depth stack and handle accordingly
            if let Some(entry) = self.depth_stack.pop() {
                if let StackEntry::Element(_) = entry {
                    // This was an actual element, pop parent stack too
                    self.parent_stack.pop();
                }
            }
        }

        fn build_model(&mut self) -> Result<Model, InterchangeError> {
            let mut model = Model::new();

            // Add all elements (drain with full range to preserve order)
            for (_, element) in self.elements_by_id.drain(..) {
                model.add_element(element);
            }

            // Add relationships
            for rel in self.relationships.drain(..) {
                model.add_relationship(rel);
            }

            // Update owned_elements using the recorded parse order (children_in_order)
            for (parent_id, child_ids) in self.children_in_order.drain(..) {
                if let Some(owner) = model.elements.get_mut(&ElementId::new(parent_id)) {
                    for child_id in child_ids {
                        owner.owned_elements.push(ElementId::new(child_id));
                    }
                }
            }

            Ok(model)
        }

        /// Generate a unique relationship ID.
        #[allow(dead_code)]
        fn next_rel_id(&mut self) -> ElementId {
            self.rel_counter += 1;
            ElementId::new(format!("_rel_{}", self.rel_counter))
        }
    }

    /// Check if a tag name is a containment wrapper (not an element itself).
    fn is_containment_tag(tag: &str) -> bool {
        matches!(
            tag,
            "ownedMember"
                | "ownedFeature"
                | "ownedElement"
                | "ownedImport"
                | "member"
                | "feature"
                | "ownedSpecialization"
                | "ownedSubsetting"
                | "ownedRedefinition"
                | "ownedTyping"
                | "importedMembership"
                | "superclassifier"
                | "redefinedFeature"
                | "subsettedFeature"
        )
        // Note: ownedRelationship and ownedRelatedElement are NOT containment -
        // they have xsi:type and should be parsed as elements
    }

    /// Convert ElementKind to RelationshipKind for relationship elements.
    fn element_kind_to_relationship_kind(kind: ElementKind) -> RelationshipKind {
        match kind {
            ElementKind::Specialization => RelationshipKind::Specialization,
            ElementKind::FeatureTyping => RelationshipKind::FeatureTyping,
            ElementKind::Subsetting => RelationshipKind::Subsetting,
            ElementKind::Redefinition => RelationshipKind::Redefinition,
            ElementKind::Import | ElementKind::NamespaceImport => RelationshipKind::NamespaceImport,
            ElementKind::MembershipImport => RelationshipKind::MembershipImport,
            ElementKind::Membership => RelationshipKind::Membership,
            ElementKind::OwningMembership => RelationshipKind::OwningMembership,
            ElementKind::FeatureMembership => RelationshipKind::FeatureMembership,
            ElementKind::Conjugation => RelationshipKind::Conjugation,
            _ => RelationshipKind::Dependency, // Default fallback
        }
    }
}

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

// ============================================================================
// XMI WRITER (requires interchange feature)
// ============================================================================

#[cfg(feature = "interchange")]
mod writer {
    use super::*;
    use quick_xml::Writer;
    use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, Event};
    use std::io::Cursor;

    /// XMI document writer.
    pub struct XmiWriter;

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

        pub fn write(&self, model: &Model) -> Result<Vec<u8>, InterchangeError> {
            let mut buffer = Cursor::new(Vec::new());
            let mut writer = Writer::new_with_indent(&mut buffer, b' ', 2);

            // Write XML declaration
            writer
                .write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))
                .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

            // Write XMI root element
            let mut xmi_start = BytesStart::new("xmi:XMI");
            xmi_start.push_attribute(("xmlns:xmi", namespace::XMI));
            xmi_start.push_attribute(("xmlns:kerml", namespace::KERML));
            xmi_start.push_attribute(("xmlns:sysml", namespace::SYSML));

            writer
                .write_event(Event::Start(xmi_start))
                .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

            // Write root elements
            for root in model.iter_roots() {
                self.write_element(&mut writer, model, root)?;
            }

            // Close XMI root
            writer
                .write_event(Event::End(BytesEnd::new("xmi:XMI")))
                .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

            Ok(buffer.into_inner())
        }

        fn write_element<W: std::io::Write>(
            &self,
            writer: &mut Writer<W>,
            model: &Model,
            element: &Element,
        ) -> Result<(), InterchangeError> {
            let type_name = element.kind.xmi_type();

            let mut elem_start = BytesStart::new(type_name);
            elem_start.push_attribute(("xmi:id", element.id.as_str()));

            if let Some(ref name) = element.name {
                elem_start.push_attribute(("name", name.as_ref()));
            }
            if let Some(ref qualified_name) = element.qualified_name {
                elem_start.push_attribute(("qualifiedName", qualified_name.as_ref()));
            }
            if let Some(ref short_name) = element.short_name {
                elem_start.push_attribute(("shortName", short_name.as_ref()));
            }

            // Write boolean flags
            if element.is_abstract {
                elem_start.push_attribute(("isAbstract", "true"));
            }
            if let Some(super::super::model::PropertyValue::Boolean(true)) =
                element.properties.get("isStandard")
            {
                elem_start.push_attribute(("isStandard", "true"));
            }
            if let Some(super::super::model::PropertyValue::Boolean(true)) =
                element.properties.get("isComposite")
            {
                elem_start.push_attribute(("isComposite", "true"));
            }

            // Write documentation body if present
            if let Some(ref doc) = element.documentation {
                elem_start.push_attribute(("body", doc.as_ref()));
            }

            // Write href if present (for cross-file references)
            if let Some(super::super::model::PropertyValue::String(href)) =
                element.properties.get("href")
            {
                elem_start.push_attribute(("href", href.as_ref()));
            }

            // Write other stored attributes
            for (key, value) in &element.properties {
                // Skip ones we've already handled
                if key.as_ref() == "isStandard"
                    || key.as_ref() == "isComposite"
                    || key.as_ref() == "href"
                {
                    continue;
                }
                if let super::super::model::PropertyValue::String(s) = value {
                    elem_start.push_attribute((key.as_ref(), s.as_ref()));
                }
            }

            // Check if we have children
            let has_children = !element.owned_elements.is_empty();

            if has_children {
                writer
                    .write_event(Event::Start(elem_start))
                    .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

                // Write all owned children in order, choosing wrapper based on kind
                for child_id in &element.owned_elements {
                    if let Some(child) = model.get(child_id) {
                        // Use ownedRelationship for relationship kinds, ownedMember for others
                        let wrapper = if child.kind.is_relationship() {
                            "ownedRelationship"
                        } else {
                            "ownedMember"
                        };

                        writer
                            .write_event(Event::Start(BytesStart::new(wrapper)))
                            .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

                        self.write_element(writer, model, child)?;

                        writer
                            .write_event(Event::End(BytesEnd::new(wrapper)))
                            .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;
                    }
                }

                writer
                    .write_event(Event::End(BytesEnd::new(type_name)))
                    .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;
            } else {
                // Self-closing element
                writer
                    .write_event(Event::Empty(elem_start))
                    .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;
            }

            Ok(())
        }

        #[allow(dead_code)]
        fn write_relationship<W: std::io::Write>(
            &self,
            writer: &mut Writer<W>,
            _model: &Model,
            rel: &Relationship,
        ) -> Result<(), InterchangeError> {
            // Start ownedRelationship wrapper
            writer
                .write_event(Event::Start(BytesStart::new("ownedRelationship")))
                .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

            // Write the relationship element
            let rel_type = relationship_kind_to_xmi_type(rel.kind);
            let mut rel_start = BytesStart::new(rel_type);
            rel_start.push_attribute(("xmi:id", rel.id.as_str()));

            // Add source and target based on relationship kind
            match rel.kind {
                RelationshipKind::Specialization => {
                    rel_start.push_attribute(("subclassifier", rel.source.as_str()));
                    rel_start.push_attribute(("superclassifier", rel.target.as_str()));
                }
                RelationshipKind::FeatureTyping => {
                    rel_start.push_attribute(("typedFeature", rel.source.as_str()));
                    rel_start.push_attribute(("type", rel.target.as_str()));
                }
                RelationshipKind::Redefinition => {
                    rel_start.push_attribute(("redefiningFeature", rel.source.as_str()));
                    rel_start.push_attribute(("redefinedFeature", rel.target.as_str()));
                }
                RelationshipKind::Subsetting => {
                    rel_start.push_attribute(("subsettingFeature", rel.source.as_str()));
                    rel_start.push_attribute(("subsettedFeature", rel.target.as_str()));
                }
                _ => {
                    rel_start.push_attribute(("source", rel.source.as_str()));
                    rel_start.push_attribute(("target", rel.target.as_str()));
                }
            }

            writer
                .write_event(Event::Empty(rel_start))
                .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

            // End ownedRelationship wrapper
            writer
                .write_event(Event::End(BytesEnd::new("ownedRelationship")))
                .map_err(|e| InterchangeError::xml(format!("Write error: {e}")))?;

            Ok(())
        }
    }

    /// Convert RelationshipKind to XMI type name.
    fn relationship_kind_to_xmi_type(kind: RelationshipKind) -> &'static str {
        match kind {
            RelationshipKind::Specialization => "kerml:Specialization",
            RelationshipKind::FeatureTyping => "kerml:FeatureTyping",
            RelationshipKind::Subsetting => "kerml:Subsetting",
            RelationshipKind::Redefinition => "kerml:Redefinition",
            RelationshipKind::NamespaceImport => "kerml:NamespaceImport",
            RelationshipKind::MembershipImport => "kerml:MembershipImport",
            RelationshipKind::Membership => "kerml:Membership",
            RelationshipKind::OwningMembership => "kerml:OwningMembership",
            RelationshipKind::FeatureMembership => "kerml:FeatureMembership",
            RelationshipKind::Conjugation => "kerml:Conjugation",
            RelationshipKind::Dependency => "kerml:Dependency",
            _ => "kerml:Relationship", // Fallback for other kinds
        }
    }
}

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

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

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

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

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

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

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

// ============================================================================
// CONVERSION HELPERS
// ============================================================================

/// Convert an XMI type string to ElementKind.
#[allow(dead_code)]
pub fn element_kind_from_xmi(xmi_type: &str) -> ElementKind {
    ElementKind::from_xmi_type(xmi_type)
}

/// Convert an ElementKind to XMI type string.
#[allow(dead_code)]
pub fn element_kind_to_xmi(kind: ElementKind) -> &'static str {
    kind.xmi_type()
}

/// Convert a relationship XMI type to RelationshipKind.
#[allow(dead_code)]
pub fn relationship_kind_from_xmi(xmi_type: &str) -> Option<RelationshipKind> {
    let type_name = xmi_type.split(':').last().unwrap_or(xmi_type);
    match type_name {
        "Specialization" => Some(RelationshipKind::Specialization),
        "FeatureTyping" => Some(RelationshipKind::FeatureTyping),
        "Subsetting" => Some(RelationshipKind::Subsetting),
        "Redefinition" => Some(RelationshipKind::Redefinition),
        "Conjugation" => Some(RelationshipKind::Conjugation),
        "Membership" => Some(RelationshipKind::Membership),
        "OwningMembership" => Some(RelationshipKind::OwningMembership),
        "FeatureMembership" => Some(RelationshipKind::FeatureMembership),
        "NamespaceImport" => Some(RelationshipKind::NamespaceImport),
        "MembershipImport" => Some(RelationshipKind::MembershipImport),
        "Dependency" => Some(RelationshipKind::Dependency),
        _ => None,
    }
}

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

    #[test]
    fn test_xmi_format_metadata() {
        let xmi = Xmi;
        assert_eq!(xmi.name(), "XMI");
        assert_eq!(xmi.extensions(), &["xmi"]);
        assert_eq!(xmi.mime_type(), "application/xmi+xml");
        assert!(xmi.capabilities().read);
        assert!(xmi.capabilities().write);
    }

    #[test]
    fn test_xmi_validate_valid() {
        let xmi = Xmi;
        let input =
            br#"<?xml version="1.0"?><xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/20131001"/>"#;
        assert!(xmi.validate(input).is_ok());
    }

    #[test]
    fn test_xmi_validate_invalid() {
        let xmi = Xmi;
        let input = b"<root>not xmi</root>";
        assert!(xmi.validate(input).is_err());
    }

    #[test]
    fn test_element_kind_from_xmi() {
        assert_eq!(element_kind_from_xmi("sysml:Package"), ElementKind::Package);
        assert_eq!(
            element_kind_from_xmi("sysml:PartDefinition"),
            ElementKind::PartDefinition
        );
        assert_eq!(element_kind_from_xmi("kerml:Feature"), ElementKind::Feature);
    }

    #[test]
    fn test_relationship_kind_from_xmi() {
        assert_eq!(
            relationship_kind_from_xmi("kerml:Specialization"),
            Some(RelationshipKind::Specialization)
        );
        assert_eq!(
            relationship_kind_from_xmi("kerml:FeatureTyping"),
            Some(RelationshipKind::FeatureTyping)
        );
    }

    #[cfg(feature = "interchange")]
    mod interchange_tests {
        use super::*;

        #[test]
        fn test_xmi_read_simple_package() {
            let xmi_content = br#"<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/20131001"
         xmlns:sysml="http://www.omg.org/spec/SysML/20230201">
  <sysml:Package xmi:id="pkg1" name="MyPackage"/>
</xmi:XMI>"#;

            let model = Xmi.read(xmi_content).expect("Failed to read XMI");
            assert_eq!(model.element_count(), 1);

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

        #[test]
        fn test_xmi_read_nested_elements() {
            let xmi_content = br#"<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmlns:xmi="http://www.omg.org/spec/XMI/20131001"
         xmlns:sysml="http://www.omg.org/spec/SysML/20230201">
  <sysml:Package xmi:id="pkg1" name="Vehicles">
    <ownedMember>
      <sysml:PartDefinition xmi:id="pd1" name="Car"/>
    </ownedMember>
    <ownedMember>
      <sysml:PartDefinition xmi:id="pd2" name="Truck"/>
    </ownedMember>
  </sysml:Package>
</xmi:XMI>"#;

            let model = Xmi.read(xmi_content).expect("Failed to read XMI");
            assert_eq!(model.element_count(), 3);

            let pkg = model
                .get(&ElementId::new("pkg1"))
                .expect("Package not found");
            assert_eq!(pkg.owned_elements.len(), 2);

            let car = model.get(&ElementId::new("pd1")).expect("Car not found");
            assert_eq!(car.name.as_deref(), Some("Car"));
            assert_eq!(car.kind, ElementKind::PartDefinition);
            assert_eq!(car.owner.as_ref().map(|id| id.as_str()), Some("pkg1"));
        }

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

            let output = Xmi.write(&model).expect("Failed to write XMI");
            let output_str = String::from_utf8(output).expect("Invalid UTF-8");

            assert!(output_str.contains("xmi:XMI"));
            assert!(output_str.contains("sysml:Package"));
            assert!(output_str.contains(r#"xmi:id="pkg1""#));
            assert!(output_str.contains(r#"name="TestPackage""#));
        }

        #[test]
        fn test_xmi_roundtrip() {
            // Create a model
            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 XMI
            let xmi_bytes = Xmi.write(&model).expect("Write failed");

            // Read back
            let model2 = Xmi.read(&xmi_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"));
            assert_eq!(pkg2.owned_elements.len(), 1);
        }
    }
}