Class: Lutaml::UmlRepository::StaticSite::Serializers::InheritanceResolver

Inherits:
Object
  • Object
show all
Includes:
Lutaml::Uml::ModelHelpers
Defined in:
lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb

Instance Method Summary collapse

Methods included from Lutaml::Uml::ModelHelpers

#class_type_for, #format_cardinality, #normalize_stereotypes, #package_path_for, #parse_cardinality, #qualified_name_for

Constructor Details

#initialize(repository, id_generator, options, generalization_map) ⇒ InheritanceResolver

Returns a new instance of InheritanceResolver.



17
18
19
20
21
22
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 17

def initialize(repository, id_generator, options, generalization_map)
  @repository = repository
  @id_generator = id_generator
  @options = options
  @generalization_map = generalization_map
end

Instance Method Details

#compute_inherited_associations(klass, visited = Set.new) ⇒ Object



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
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 96

def compute_inherited_associations(klass, visited = Set.new)
  return [] unless klass.generalization
  return [] if visited.include?(klass.xmi_id)

  visited.add(klass.xmi_id)
  inherited = []
  current_gen = klass.generalization
  parent_order = 0

  while current_gen
    parent_class = class_lookup.by_xmi_id(current_gen.general_id)
    break unless parent_class
    break if visited.include?(parent_class.xmi_id)

    visited.add(parent_class.xmi_id)
    parent_associations = find_class_associations(parent_class)

    assoc_with_roles = parent_associations.filter_map do |assoc_id|
      assoc = @repository.associations_index.find do |a|
        @id_generator.association_id(a) == assoc_id
      end
      next unless assoc

      local_role = if assoc.owner_end_xmi_id == parent_class.xmi_id
                     assoc.owner_end_attribute_name || assoc.owner_end || ""
                   elsif assoc.member_end_xmi_id == parent_class.xmi_id
                     assoc.member_end_attribute_name || assoc.member_end || ""
                   else
                     ""
                   end
      { id: assoc_id, role: local_role }
    end

    assoc_with_roles.sort_by { |a| a[:role] }.each do |item|
      inherited << Models::SpaInheritedAssociation.new(
        association_id: item[:id],
        inherited_from: @id_generator.class_id(parent_class),
        inherited_from_name: parent_class.name,
        parent_order: parent_order,
        local_role: item[:role],
      )
    end

    parent_order += 1
    current_gen = current_gen.general
  end

  inherited
rescue StandardError => e
  warn "Error computing inherited associations: #{e.message}"
  []
end

#compute_inherited_attributes(klass, visited = Set.new) ⇒ Object



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
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 54

def compute_inherited_attributes(klass, visited = Set.new)
  return [] unless klass.generalization
  return [] if visited.include?(klass.xmi_id)

  visited.add(klass.xmi_id)
  inherited = []
  current_gen = klass.generalization
  parent_order = 0

  while current_gen
    parent_class = class_lookup.by_xmi_id(current_gen.general_id)
    break unless parent_class
    break if visited.include?(parent_class.xmi_id)

    visited.add(parent_class.xmi_id)

    if parent_class.attributes
      sorted_attrs = parent_class.attributes.sort_by do |a|
        a.name || ""
      end
      sorted_attrs.each do |attr|
        attr_id = @id_generator.attribute_id(attr, parent_class)
        inherited << Models::SpaInheritedAttribute.new(
          attribute_id: attr_id,
          attribute: serialize_attribute(attr, parent_class, attr_id),
          inherited_from: @id_generator.class_id(parent_class),
          inherited_from_name: parent_class.name,
          parent_order: parent_order,
        )
      end
    end

    parent_order += 1
    current_gen = current_gen.general
  end

  inherited
rescue StandardError => e
  warn "Error computing inherited attributes: #{e.message}"
  []
end

#find_generalizations(klass) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 24

def find_generalizations(klass)
  parent_xmi_ids = @generalization_map[klass.xmi_id]

  if parent_xmi_ids && !parent_xmi_ids.empty?
    parents = parent_xmi_ids.filter_map do |parent_xmi_id|
      next if parent_xmi_id == klass.xmi_id

      parent = class_lookup.by_xmi_id(parent_xmi_id)
      parent ? @id_generator.class_id(parent) : nil
    end
    return parents unless parents.empty?
  end

  parent = @repository.supertype_of(klass)
  return [] if parent && parent.xmi_id == klass.xmi_id

  parent ? [@id_generator.class_id(parent)] : []
rescue StandardError => e
  warn "Error finding generalizations for #{klass.name}: #{e.message}"
  []
end

#find_specializations(klass) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 46

def find_specializations(klass)
  children = @repository.subtypes_of(klass)
  children.reject { |child| child.xmi_id == klass.xmi_id }
    .map { |child| @id_generator.class_id(child) }
rescue StandardError
  []
end

#serialize_general_attribute(attr) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 175

def serialize_general_attribute(attr)
  return nil unless attr

  {
    name: attr.name,
    type: attr.type,
    cardinality: serialize_cardinality(attr.cardinality),
    definition: format_definition(attr.definition),
    upperKlass: attr.upper_klass,
    nameNs: attr.name_ns,
    typeNs: attr.type_ns,
  }
end

#serialize_generalization(klass, visited = Set.new) ⇒ Object



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
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 149

def serialize_generalization(klass, visited = Set.new)
  return nil unless klass.generalization
  return nil if visited.include?(klass.xmi_id)

  visited.add(klass.xmi_id)
  gen = klass.generalization

  {
    generalId: gen.general_id,
    generalName: gen.general_name,
    generalUpperKlass: gen.general_upper_klass,
    hasGeneral: gen.has_general,
    name: gen.name,
    type: gen.type,
    definition: format_definition(gen.definition),
    stereotype: gen.stereotype,
    ownedProps: serialize_general_collection(gen.owned_props),
    assocProps: serialize_general_collection(gen.assoc_props),
    inheritedProps: serialize_general_collection(gen.inherited_props),
    inheritedAssocProps: serialize_general_collection(gen.inherited_assoc_props),
  }
rescue StandardError => e
  warn "Error serializing generalization: #{e.message}"
  nil
end