Class: Ea::Model::Document

Inherits:
Base
  • Object
show all
Defined in:
lib/ea/model/document.rb

Overview

Root container for a harmonized model. Source adapters build one of these; consumer adapters read from it.

Element identity is global within the document: every package, classifier, relationship, diagram, and diagram element has a unique id. References between elements are by id, so the document can be sharded cheaply (no need to walk a tree to resolve a reference).

Instance Method Summary collapse

Instance Method Details

#alias_eaid_for_package(idx, package) ⇒ Object



101
102
103
104
105
# File 'lib/ea/model/document.rb', line 101

def alias_eaid_for_package(idx, package)
  return unless package.id&.start_with?("EAPK_")

  idx["EAID_#{package.id[5..]}"] = package
end

#classifiers_in_package(package_id) ⇒ Object



111
112
113
# File 'lib/ea/model/document.rb', line 111

def classifiers_in_package(package_id)
  classifiers.select { |c| c.package_id == package_id }
end

#index_by_idObject

Flat lookup indexes, built lazily by consumers. Not serialized — they're derived from the element collections.

EA XMI stores packages with EAPK_<guid> ids inside the uml:Model hierarchy, but diagram element subject= refs use EAID_<guid>. We alias packages under both prefixes so model_element_ref lookups from diagram elements resolve.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ea/model/document.rb', line 59

def index_by_id
  @index_by_id ||= begin
    idx = {}
    packages.each do |p|
      idx[p.id] = p
      alias_eaid_for_package(idx, p)
    end
    classifiers.each { |c| idx[c.id] = c }
    relationships.each { |r| idx[r.id] = r }
    stereotypes.each { |s| idx[s.id] = s }
    instance_specifications.each { |i| idx[i.id] = i }
    diagrams.each { |d| idx[d.id] = d }
    notes.each { |n| idx[n.id] = n }
    diagrams.each do |d|
      d.elements.each { |e| idx[e.id] = e }
      d.connectors.each { |c| idx[c.id] = c }
    end
    idx
  end
end

#property_by_id(id) ⇒ Object

O(1) Property lookup by id. Returns nil when not found.



95
96
97
98
99
# File 'lib/ea/model/document.rb', line 95

def property_by_id(id)
  return nil unless id

  property_index[id]
end

#property_indexObject

Flat lookup of Properties by id across all classifiers. Built lazily; not serialized. Used by connector label rendering to resolve association-end properties in O(1) rather than walking every classifier's property list per lookup.



84
85
86
87
88
89
90
91
92
# File 'lib/ea/model/document.rb', line 84

def property_index
  @property_index ||= begin
    idx = {}
    classifiers.each do |cls|
      (cls.properties || []).each { |p| idx[p.id] = p }
    end
    idx
  end
end

#relationships_for(classifier_id) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ea/model/document.rb', line 115

def relationships_for(classifier_id)
  relationships.select do |r|
    case r
    when Association
      r.source_id == classifier_id || r.target_id == classifier_id
    when Generalization
      r.specific_id == classifier_id || r.general_id == classifier_id
    when Realization
      r.realizing_id == classifier_id || r.contract_id == classifier_id
    when Dependency
      r.client_id == classifier_id || r.supplier_id == classifier_id
    else
      false
    end
  end
end

#reset_indexesObject



132
133
134
135
# File 'lib/ea/model/document.rb', line 132

def reset_indexes
  @index_by_id = nil
  @property_index = nil
end

#root_packagesObject



107
108
109
# File 'lib/ea/model/document.rb', line 107

def root_packages
  packages.select { |p| p.parent_id.nil? }
end