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.



14
15
16
17
18
19
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 14

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



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

def compute_inherited_associations(klass, visited = Set.new)
  unless klass.respond_to?(:generalization) && klass.generalization
    return []
  end
  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 << {
        associationId: item[:id],
        inheritedFrom: @id_generator.class_id(parent_class),
        inheritedFromName: parent_class.name,
        parentOrder: parent_order,
        localRole: item[:role],
      }
    end

    parent_order += 1
    current_gen = current_gen.general if current_gen.respond_to?(:general)
  end

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

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



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

def compute_inherited_attributes(klass, visited = Set.new)
  unless klass.respond_to?(:generalization) && klass.generalization
    return []
  end
  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 { |a| a.name || "" }
      sorted_attrs.each do |attr|
        attr_id = @id_generator.attribute_id(attr, parent_class)
        inherited << {
          attributeId: attr_id,
          attribute: serialize_attribute(attr, parent_class, attr_id),
          inheritedFrom: @id_generator.class_id(parent_class),
          inheritedFromName: parent_class.name,
          parentOrder: parent_order,
        }
      end
    end

    parent_order += 1
    current_gen = current_gen.general if current_gen.respond_to?(:general)
  end

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

#find_generalizations(klass) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 21

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



41
42
43
44
45
46
47
# File 'lib/lutaml/uml_repository/static_site/serializers/inheritance_resolver.rb', line 41

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



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

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.respond_to?(:upper_klass) ? attr.upper_klass : nil,
    nameNs: attr.respond_to?(:name_ns) ? attr.name_ns : nil,
    typeNs: attr.respond_to?(:type_ns) ? attr.type_ns : nil,
  }
end

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



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

def serialize_generalization(klass, visited = Set.new)
  unless klass.respond_to?(:generalization) && klass.generalization
    return nil
  end
  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.respond_to?(:general_upper_klass) ? gen.general_upper_klass : nil,
    hasGeneral: gen.respond_to?(:has_general) ? gen.has_general : false,
    name: gen.name,
    type: gen.type,
    definition: format_definition(gen.definition),
    stereotype: gen.respond_to?(:stereotype) ? gen.stereotype : nil,
    ownedProps: serialize_general_attrs(gen, :owned_props),
    assocProps: serialize_general_attrs(gen, :assoc_props),
    inheritedProps: serialize_general_attrs(gen, :inherited_props),
    inheritedAssocProps: serialize_general_attrs(gen, :inherited_assoc_props),
  }
rescue StandardError => e
  warn "Error serializing generalization: #{e.message}"
  nil
end