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
//! Standalone model representation for interchange.
//!
//! This module provides a `Model` type that represents a SysML/KerML model
//! independently of the Salsa database. This enables:
//!
//! - Loading models from XMI/KPAR without text parsing
//! - Exporting models to various formats
//! - Transferring models between tools
//!
//! ## Design
//!
//! The `Model` stores elements by ID, with relationships as separate edges.
//! This matches the OMG metamodel structure and enables efficient serialization.
//!
//! ```text
//! Model
//! ├── elements: IndexMap<ElementId, Element>  (preserves insertion order)
//! ├── relationships: Vec<Relationship>
//! └── metadata: ModelMetadata
//! ```

use indexmap::IndexMap;
use std::sync::Arc;

// ============================================================================
// IDs
// ============================================================================

/// Unique identifier for a model element.
///
/// This corresponds to `xmi:id` in XMI and `@id` in JSON-LD.
/// UUIDs are preferred for global uniqueness.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct ElementId(pub Arc<str>);

impl ElementId {
    /// Create a new element ID.
    pub fn new(id: impl Into<Arc<str>>) -> Self {
        Self(id.into())
    }

    /// Generate a new UUID-based ID.
    pub fn generate() -> Self {
        // Simple UUID v4 generation (would use uuid crate in real impl)
        use std::time::{SystemTime, UNIX_EPOCH};
        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|d| d.as_nanos())
            .unwrap_or(0);
        Self(format!("{:032x}", nanos).into())
    }

    /// Get the ID as a string slice.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl std::fmt::Display for ElementId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl From<&str> for ElementId {
    fn from(s: &str) -> Self {
        Self::new(s)
    }
}

impl From<String> for ElementId {
    fn from(s: String) -> Self {
        Self::new(s)
    }
}

// ============================================================================
// ELEMENT KINDS
// ============================================================================

/// The metatype of a model element.
///
/// Maps to SysML v2 / KerML metaclasses.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ElementKind {
    // Packages
    Package,
    LibraryPackage,

    // KerML Classifiers
    Class,
    DataType,
    Structure,
    Association,
    AssociationStructure,
    Interaction,
    Behavior,
    Function,
    Predicate,

    // SysML Definitions
    PartDefinition,
    ItemDefinition,
    ActionDefinition,
    PortDefinition,
    AttributeDefinition,
    ConnectionDefinition,
    InterfaceDefinition,
    AllocationDefinition,
    RequirementDefinition,
    ConstraintDefinition,
    StateDefinition,
    CalculationDefinition,
    UseCaseDefinition,
    AnalysisCaseDefinition,
    ConcernDefinition,
    ViewDefinition,
    ViewpointDefinition,
    RenderingDefinition,
    EnumerationDefinition,
    MetadataDefinition,

    // SysML Usages
    PartUsage,
    ItemUsage,
    ActionUsage,
    PortUsage,
    AttributeUsage,
    ConnectionUsage,
    InterfaceUsage,
    AllocationUsage,
    RequirementUsage,
    ConstraintUsage,
    StateUsage,
    TransitionUsage,
    CalculationUsage,
    ReferenceUsage,
    OccurrenceUsage,
    FlowConnectionUsage,
    SuccessionFlowConnectionUsage,

    // KerML Features
    Feature,
    Step,
    Expression,
    BooleanExpression,
    Invariant,

    // Relationships (first-class)
    Membership,
    OwningMembership,
    FeatureMembership,
    Import,
    NamespaceImport,
    MembershipImport,
    Specialization,
    FeatureTyping,
    Subsetting,
    Redefinition,
    Conjugation,

    // Comments and documentation
    Comment,
    Documentation,
    TextualRepresentation,

    // Annotations
    MetadataUsage,
    AnnotatingElement,

    // Generic
    Other,
}

impl ElementKind {
    /// Returns true if this is a definition (type-like).
    pub fn is_definition(&self) -> bool {
        matches!(
            self,
            Self::Package
                | Self::LibraryPackage
                | Self::Class
                | Self::DataType
                | Self::Structure
                | Self::Association
                | Self::AssociationStructure
                | Self::Interaction
                | Self::Behavior
                | Self::Function
                | Self::Predicate
                | Self::PartDefinition
                | Self::ItemDefinition
                | Self::ActionDefinition
                | Self::PortDefinition
                | Self::AttributeDefinition
                | Self::ConnectionDefinition
                | Self::InterfaceDefinition
                | Self::AllocationDefinition
                | Self::RequirementDefinition
                | Self::ConstraintDefinition
                | Self::StateDefinition
                | Self::CalculationDefinition
                | Self::UseCaseDefinition
                | Self::AnalysisCaseDefinition
                | Self::ConcernDefinition
                | Self::ViewDefinition
                | Self::ViewpointDefinition
                | Self::RenderingDefinition
                | Self::EnumerationDefinition
                | Self::MetadataDefinition
        )
    }

    /// Returns true if this is a usage (instance-like).
    pub fn is_usage(&self) -> bool {
        matches!(
            self,
            Self::PartUsage
                | Self::ItemUsage
                | Self::ActionUsage
                | Self::PortUsage
                | Self::AttributeUsage
                | Self::ConnectionUsage
                | Self::InterfaceUsage
                | Self::AllocationUsage
                | Self::RequirementUsage
                | Self::ConstraintUsage
                | Self::StateUsage
                | Self::TransitionUsage
                | Self::CalculationUsage
                | Self::ReferenceUsage
                | Self::OccurrenceUsage
                | Self::FlowConnectionUsage
                | Self::SuccessionFlowConnectionUsage
                | Self::Feature
                | Self::Step
                | Self::Expression
                | Self::BooleanExpression
                | Self::Invariant
        )
    }

    /// Returns true if this is a relationship.
    pub fn is_relationship(&self) -> bool {
        matches!(
            self,
            Self::Membership
                | Self::OwningMembership
                | Self::FeatureMembership
                | Self::Import
                | Self::NamespaceImport
                | Self::MembershipImport
                | Self::Specialization
                | Self::FeatureTyping
                | Self::Subsetting
                | Self::Redefinition
                | Self::Conjugation
        )
    }

    /// Get the XMI type name for this kind.
    pub fn xmi_type(&self) -> &'static str {
        match self {
            Self::Package => "sysml:Package",
            Self::LibraryPackage => "sysml:LibraryPackage",
            Self::Class => "kerml:Class",
            Self::DataType => "kerml:DataType",
            Self::Structure => "kerml:Structure",
            Self::Association => "kerml:Association",
            Self::AssociationStructure => "kerml:AssociationStructure",
            Self::Interaction => "kerml:Interaction",
            Self::Behavior => "kerml:Behavior",
            Self::Function => "kerml:Function",
            Self::Predicate => "kerml:Predicate",
            Self::PartDefinition => "sysml:PartDefinition",
            Self::ItemDefinition => "sysml:ItemDefinition",
            Self::ActionDefinition => "sysml:ActionDefinition",
            Self::PortDefinition => "sysml:PortDefinition",
            Self::AttributeDefinition => "sysml:AttributeDefinition",
            Self::ConnectionDefinition => "sysml:ConnectionDefinition",
            Self::InterfaceDefinition => "sysml:InterfaceDefinition",
            Self::AllocationDefinition => "sysml:AllocationDefinition",
            Self::RequirementDefinition => "sysml:RequirementDefinition",
            Self::ConstraintDefinition => "sysml:ConstraintDefinition",
            Self::StateDefinition => "sysml:StateDefinition",
            Self::CalculationDefinition => "sysml:CalculationDefinition",
            Self::UseCaseDefinition => "sysml:UseCaseDefinition",
            Self::AnalysisCaseDefinition => "sysml:AnalysisCaseDefinition",
            Self::ConcernDefinition => "sysml:ConcernDefinition",
            Self::ViewDefinition => "sysml:ViewDefinition",
            Self::ViewpointDefinition => "sysml:ViewpointDefinition",
            Self::RenderingDefinition => "sysml:RenderingDefinition",
            Self::EnumerationDefinition => "sysml:EnumerationDefinition",
            Self::MetadataDefinition => "sysml:MetadataDefinition",
            Self::PartUsage => "sysml:PartUsage",
            Self::ItemUsage => "sysml:ItemUsage",
            Self::ActionUsage => "sysml:ActionUsage",
            Self::PortUsage => "sysml:PortUsage",
            Self::AttributeUsage => "sysml:AttributeUsage",
            Self::ConnectionUsage => "sysml:ConnectionUsage",
            Self::InterfaceUsage => "sysml:InterfaceUsage",
            Self::AllocationUsage => "sysml:AllocationUsage",
            Self::RequirementUsage => "sysml:RequirementUsage",
            Self::ConstraintUsage => "sysml:ConstraintUsage",
            Self::StateUsage => "sysml:StateUsage",
            Self::TransitionUsage => "sysml:TransitionUsage",
            Self::CalculationUsage => "sysml:CalculationUsage",
            Self::ReferenceUsage => "sysml:ReferenceUsage",
            Self::OccurrenceUsage => "sysml:OccurrenceUsage",
            Self::FlowConnectionUsage => "sysml:FlowConnectionUsage",
            Self::SuccessionFlowConnectionUsage => "sysml:SuccessionFlowConnectionUsage",
            Self::Feature => "kerml:Feature",
            Self::Step => "kerml:Step",
            Self::Expression => "kerml:Expression",
            Self::BooleanExpression => "kerml:BooleanExpression",
            Self::Invariant => "kerml:Invariant",
            Self::Membership => "kerml:Membership",
            Self::OwningMembership => "kerml:OwningMembership",
            Self::FeatureMembership => "kerml:FeatureMembership",
            Self::Import => "kerml:Import",
            Self::NamespaceImport => "kerml:NamespaceImport",
            Self::MembershipImport => "kerml:MembershipImport",
            Self::Specialization => "kerml:Specialization",
            Self::FeatureTyping => "kerml:FeatureTyping",
            Self::Subsetting => "kerml:Subsetting",
            Self::Redefinition => "kerml:Redefinition",
            Self::Conjugation => "kerml:Conjugation",
            Self::Comment => "kerml:Comment",
            Self::Documentation => "kerml:Documentation",
            Self::TextualRepresentation => "kerml:TextualRepresentation",
            Self::MetadataUsage => "sysml:MetadataUsage",
            Self::AnnotatingElement => "kerml:AnnotatingElement",
            Self::Other => "kerml:Element",
        }
    }

    /// Parse from XMI type name.
    pub fn from_xmi_type(xmi_type: &str) -> Self {
        // Strip namespace prefix if present
        let type_name = xmi_type.split(':').last().unwrap_or(xmi_type);

        match type_name {
            "Package" => Self::Package,
            "LibraryPackage" => Self::LibraryPackage,
            "Class" => Self::Class,
            "DataType" => Self::DataType,
            "Structure" => Self::Structure,
            "Association" => Self::Association,
            "AssociationStructure" => Self::AssociationStructure,
            "Interaction" => Self::Interaction,
            "Behavior" => Self::Behavior,
            "Function" => Self::Function,
            "Predicate" => Self::Predicate,
            "PartDefinition" => Self::PartDefinition,
            "ItemDefinition" => Self::ItemDefinition,
            "ActionDefinition" => Self::ActionDefinition,
            "PortDefinition" => Self::PortDefinition,
            "AttributeDefinition" => Self::AttributeDefinition,
            "ConnectionDefinition" => Self::ConnectionDefinition,
            "InterfaceDefinition" => Self::InterfaceDefinition,
            "AllocationDefinition" => Self::AllocationDefinition,
            "RequirementDefinition" => Self::RequirementDefinition,
            "ConstraintDefinition" => Self::ConstraintDefinition,
            "StateDefinition" => Self::StateDefinition,
            "CalculationDefinition" => Self::CalculationDefinition,
            "UseCaseDefinition" => Self::UseCaseDefinition,
            "AnalysisCaseDefinition" => Self::AnalysisCaseDefinition,
            "ConcernDefinition" => Self::ConcernDefinition,
            "ViewDefinition" => Self::ViewDefinition,
            "ViewpointDefinition" => Self::ViewpointDefinition,
            "RenderingDefinition" => Self::RenderingDefinition,
            "EnumerationDefinition" => Self::EnumerationDefinition,
            "MetadataDefinition" => Self::MetadataDefinition,
            "PartUsage" => Self::PartUsage,
            "ItemUsage" => Self::ItemUsage,
            "ActionUsage" => Self::ActionUsage,
            "PortUsage" => Self::PortUsage,
            "AttributeUsage" => Self::AttributeUsage,
            "ConnectionUsage" => Self::ConnectionUsage,
            "InterfaceUsage" => Self::InterfaceUsage,
            "AllocationUsage" => Self::AllocationUsage,
            "RequirementUsage" => Self::RequirementUsage,
            "ConstraintUsage" => Self::ConstraintUsage,
            "StateUsage" => Self::StateUsage,
            "TransitionUsage" => Self::TransitionUsage,
            "CalculationUsage" => Self::CalculationUsage,
            "ReferenceUsage" => Self::ReferenceUsage,
            "OccurrenceUsage" => Self::OccurrenceUsage,
            "FlowConnectionUsage" => Self::FlowConnectionUsage,
            "SuccessionFlowConnectionUsage" => Self::SuccessionFlowConnectionUsage,
            "Feature" => Self::Feature,
            "Step" => Self::Step,
            "Expression" => Self::Expression,
            "BooleanExpression" => Self::BooleanExpression,
            "Invariant" => Self::Invariant,
            "Membership" => Self::Membership,
            "OwningMembership" => Self::OwningMembership,
            "FeatureMembership" => Self::FeatureMembership,
            "Import" => Self::Import,
            "NamespaceImport" => Self::NamespaceImport,
            "MembershipImport" => Self::MembershipImport,
            "Specialization" => Self::Specialization,
            "FeatureTyping" => Self::FeatureTyping,
            "Subsetting" => Self::Subsetting,
            "Redefinition" => Self::Redefinition,
            "Conjugation" => Self::Conjugation,
            "Comment" => Self::Comment,
            "Documentation" => Self::Documentation,
            "TextualRepresentation" => Self::TextualRepresentation,
            "MetadataUsage" => Self::MetadataUsage,
            "AnnotatingElement" => Self::AnnotatingElement,
            _ => Self::Other,
        }
    }

    /// Get the JSON-LD @type value.
    pub fn jsonld_type(&self) -> &'static str {
        // JSON-LD uses the same type names without namespace prefix
        self.xmi_type().split(':').last().unwrap_or("Element")
    }
}

// ============================================================================
// ELEMENT
// ============================================================================

/// A model element with its properties.
#[derive(Clone, Debug)]
pub struct Element {
    /// Unique identifier.
    pub id: ElementId,
    /// The metatype.
    pub kind: ElementKind,
    /// The declared name (may be None for anonymous elements).
    pub name: Option<Arc<str>>,
    /// Short name alias.
    pub short_name: Option<Arc<str>>,
    /// Qualified name (computed from ownership hierarchy).
    pub qualified_name: Option<Arc<str>>,
    /// The owning element's ID (None for root elements).
    pub owner: Option<ElementId>,
    /// IDs of directly owned elements.
    pub owned_elements: Vec<ElementId>,
    /// Documentation text.
    pub documentation: Option<Arc<str>>,
    /// Whether this element is abstract.
    pub is_abstract: bool,
    /// Visibility (public, private, protected).
    pub visibility: Visibility,
    /// Additional properties as key-value pairs (IndexMap preserves order).
    pub properties: IndexMap<Arc<str>, PropertyValue>,
}

impl Element {
    /// Create a new element with the given ID and kind.
    pub fn new(id: impl Into<ElementId>, kind: ElementKind) -> Self {
        Self {
            id: id.into(),
            kind,
            name: None,
            short_name: None,
            qualified_name: None,
            owner: None,
            owned_elements: Vec::new(),
            documentation: None,
            is_abstract: false,
            visibility: Visibility::Public,
            properties: IndexMap::new(),
        }
    }

    /// Set the name.
    pub fn with_name(mut self, name: impl Into<Arc<str>>) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Set the qualified name.
    pub fn with_qualified_name(mut self, qualified_name: impl Into<Arc<str>>) -> Self {
        self.qualified_name = Some(qualified_name.into());
        self
    }

    /// Set the short name.
    pub fn with_short_name(mut self, short_name: impl Into<Arc<str>>) -> Self {
        self.short_name = Some(short_name.into());
        self
    }

    /// Set the owner.
    pub fn with_owner(mut self, owner: impl Into<ElementId>) -> Self {
        self.owner = Some(owner.into());
        self
    }

    /// Add an owned element ID.
    pub fn with_owned(mut self, owned: impl Into<ElementId>) -> Self {
        self.owned_elements.push(owned.into());
        self
    }

    /// Set a property value.
    pub fn with_property(mut self, key: impl Into<Arc<str>>, value: PropertyValue) -> Self {
        self.properties.insert(key.into(), value);
        self
    }
}

/// Visibility of an element.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Visibility {
    #[default]
    Public,
    Private,
    Protected,
}

/// A property value that can be stored on an element.
#[derive(Clone, Debug, PartialEq)]
pub enum PropertyValue {
    /// String value.
    String(Arc<str>),
    /// Integer value.
    Integer(i64),
    /// Floating-point value.
    Real(f64),
    /// Boolean value.
    Boolean(bool),
    /// Reference to another element by ID.
    Reference(ElementId),
    /// List of values.
    List(Vec<PropertyValue>),
}

impl From<&str> for PropertyValue {
    fn from(s: &str) -> Self {
        Self::String(s.into())
    }
}

impl From<String> for PropertyValue {
    fn from(s: String) -> Self {
        Self::String(s.into())
    }
}

impl From<i64> for PropertyValue {
    fn from(v: i64) -> Self {
        Self::Integer(v)
    }
}

impl From<f64> for PropertyValue {
    fn from(v: f64) -> Self {
        Self::Real(v)
    }
}

impl From<bool> for PropertyValue {
    fn from(v: bool) -> Self {
        Self::Boolean(v)
    }
}

impl From<ElementId> for PropertyValue {
    fn from(id: ElementId) -> Self {
        Self::Reference(id)
    }
}

// ============================================================================
// RELATIONSHIP
// ============================================================================

/// A relationship between two elements.
///
/// In the KerML/SysML metamodel, relationships are first-class elements.
/// This struct captures the essential information for interchange.
#[derive(Clone, Debug)]
pub struct Relationship {
    /// Unique identifier for the relationship itself.
    pub id: ElementId,
    /// The kind of relationship.
    pub kind: RelationshipKind,
    /// The source element ID.
    pub source: ElementId,
    /// The target element ID.
    pub target: ElementId,
    /// The owning element (usually the source).
    pub owner: Option<ElementId>,
}

impl Relationship {
    /// Create a new relationship.
    pub fn new(
        id: impl Into<ElementId>,
        kind: RelationshipKind,
        source: impl Into<ElementId>,
        target: impl Into<ElementId>,
    ) -> Self {
        Self {
            id: id.into(),
            kind,
            source: source.into(),
            target: target.into(),
            owner: None,
        }
    }
}

/// The kind of relationship.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RelationshipKind {
    /// Specialization (general → specific).
    Specialization,
    /// Feature typing (feature → type).
    FeatureTyping,
    /// Subsetting (feature → subsetted feature).
    Subsetting,
    /// Redefinition (feature → redefined feature).
    Redefinition,
    /// Conjugation (conjugated port → original).
    Conjugation,
    /// Membership (namespace → member).
    Membership,
    /// Owning membership (owner → owned).
    OwningMembership,
    /// Feature membership (type → feature).
    FeatureMembership,
    /// Namespace import.
    NamespaceImport,
    /// Membership import.
    MembershipImport,
    /// Dependency.
    Dependency,
    /// Requirement satisfaction.
    Satisfaction,
    /// Requirement verification.
    Verification,
    /// Allocation.
    Allocation,
    /// Connection.
    Connection,
    /// Flow connection.
    FlowConnection,
    /// Succession.
    Succession,
}

impl RelationshipKind {
    /// Get the XMI type name.
    pub fn xmi_type(&self) -> &'static str {
        match self {
            Self::Specialization => "kerml:Specialization",
            Self::FeatureTyping => "kerml:FeatureTyping",
            Self::Subsetting => "kerml:Subsetting",
            Self::Redefinition => "kerml:Redefinition",
            Self::Conjugation => "kerml:Conjugation",
            Self::Membership => "kerml:Membership",
            Self::OwningMembership => "kerml:OwningMembership",
            Self::FeatureMembership => "kerml:FeatureMembership",
            Self::NamespaceImport => "kerml:NamespaceImport",
            Self::MembershipImport => "kerml:MembershipImport",
            Self::Dependency => "kerml:Dependency",
            Self::Satisfaction => "sysml:SatisfyRequirementUsage",
            Self::Verification => "sysml:RequirementVerificationMembership",
            Self::Allocation => "sysml:AllocationUsage",
            Self::Connection => "sysml:ConnectionUsage",
            Self::FlowConnection => "sysml:FlowConnectionUsage",
            Self::Succession => "sysml:SuccessionAsUsage",
        }
    }
}

// ============================================================================
// MODEL
// ============================================================================

/// A complete SysML/KerML model.
///
/// This is a standalone representation that can be:
/// - Loaded from XMI, KPAR, or JSON-LD
/// - Exported to various formats
/// - Integrated into a `RootDatabase` for IDE features
#[derive(Clone, Debug, Default)]
pub struct Model {
    /// All elements by ID (IndexMap preserves insertion order for deterministic serialization).
    pub elements: IndexMap<ElementId, Element>,
    /// All relationships.
    pub relationships: Vec<Relationship>,
    /// Root element IDs (top-level packages).
    pub roots: Vec<ElementId>,
    /// Metadata about the model.
    pub metadata: ModelMetadata,
}

impl Model {
    /// Create a new empty model.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add an element to the model.
    pub fn add_element(&mut self, element: Element) -> &ElementId {
        let id = element.id.clone();
        if element.owner.is_none() {
            self.roots.push(id.clone());
        }
        self.elements.insert(id.clone(), element);
        // Return reference to the ID in the map
        &self.elements.get(&id).unwrap().id
    }

    /// Add a relationship to the model.
    pub fn add_relationship(&mut self, relationship: Relationship) {
        self.relationships.push(relationship);
    }

    /// Get an element by ID.
    pub fn get(&self, id: &ElementId) -> Option<&Element> {
        self.elements.get(id)
    }

    /// Get a mutable element by ID.
    pub fn get_mut(&mut self, id: &ElementId) -> Option<&mut Element> {
        self.elements.get_mut(id)
    }

    /// Iterate over all elements.
    pub fn iter_elements(&self) -> impl Iterator<Item = &Element> {
        self.elements.values()
    }

    /// Iterate over root elements.
    pub fn iter_roots(&self) -> impl Iterator<Item = &Element> {
        self.roots.iter().filter_map(|id| self.elements.get(id))
    }

    /// Get relationships where the given element is the source.
    pub fn relationships_from<'a>(
        &'a self,
        source: &'a ElementId,
    ) -> impl Iterator<Item = &'a Relationship> {
        self.relationships
            .iter()
            .filter(move |r| &r.source == source)
    }

    /// Get relationships where the given element is the target.
    pub fn relationships_to<'a>(
        &'a self,
        target: &'a ElementId,
    ) -> impl Iterator<Item = &'a Relationship> {
        self.relationships
            .iter()
            .filter(move |r| &r.target == target)
    }

    /// Get the number of elements.
    pub fn element_count(&self) -> usize {
        self.elements.len()
    }

    /// Get the number of relationships.
    pub fn relationship_count(&self) -> usize {
        self.relationships.len()
    }
}

/// Metadata about a model.
#[derive(Clone, Debug, Default)]
pub struct ModelMetadata {
    /// Name of the model/project.
    pub name: Option<String>,
    /// Version string.
    pub version: Option<String>,
    /// Description.
    pub description: Option<String>,
    /// URI of the model.
    pub uri: Option<String>,
    /// SysML/KerML version this model conforms to.
    pub sysml_version: Option<String>,
    /// Tool that created this model.
    pub tool: Option<String>,
    /// Creation timestamp.
    pub created: Option<String>,
    /// Last modified timestamp.
    pub modified: Option<String>,
}

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

    #[test]
    fn test_element_id_generation() {
        let id1 = ElementId::generate();
        let id2 = ElementId::generate();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_element_builder() {
        let element = Element::new("pkg1", ElementKind::Package)
            .with_name("MyPackage")
            .with_short_name("mp");

        assert_eq!(element.id.as_str(), "pkg1");
        assert_eq!(element.name.as_deref(), Some("MyPackage"));
        assert_eq!(element.short_name.as_deref(), Some("mp"));
        assert_eq!(element.kind, ElementKind::Package);
    }

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

        let pkg = Element::new("pkg1", ElementKind::Package).with_name("Root");
        model.add_element(pkg);

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

        assert_eq!(model.element_count(), 2);
        assert_eq!(model.roots.len(), 1);
        assert_eq!(
            model.get(&ElementId::new("pkg1")).unwrap().name.as_deref(),
            Some("Root")
        );
    }

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

        model.add_element(Element::new("def1", ElementKind::PartDefinition).with_name("Base"));
        model.add_element(Element::new("def2", ElementKind::PartDefinition).with_name("Derived"));

        model.add_relationship(Relationship::new(
            "rel1",
            RelationshipKind::Specialization,
            "def2",
            "def1",
        ));

        assert_eq!(model.relationship_count(), 1);
        let source_id = ElementId::new("def2");
        let rels: Vec<_> = model.relationships_from(&source_id).collect();
        assert_eq!(rels.len(), 1);
        assert_eq!(rels[0].target.as_str(), "def1");
    }

    #[test]
    fn test_element_kind_xmi_roundtrip() {
        let kinds = [
            ElementKind::Package,
            ElementKind::PartDefinition,
            ElementKind::ActionUsage,
            ElementKind::Specialization,
        ];

        for kind in kinds {
            let xmi_type = kind.xmi_type();
            let parsed = ElementKind::from_xmi_type(xmi_type);
            assert_eq!(kind, parsed, "Failed roundtrip for {xmi_type}");
        }
    }
}