Module: Lutaml::Converter::XmiToUml

Included in:
Xmi::Parsers::Xml
Defined in:
lib/lutaml/converter/xmi_to_uml.rb

Instance Method Summary collapse

Instance Method Details

#create_uml_assoc_generalizations(klass) ⇒ Object

rubocop:disable Metrics/AbcSize



343
344
345
346
347
348
349
350
351
352
353
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 343

def create_uml_assoc_generalizations(klass) # rubocop:disable Metrics/AbcSize
  return [] if klass.generalization.nil? || klass.generalization.empty?

  klass.generalization.map do |gen|
    ::Lutaml::Uml::AssociationGeneralization.new.tap do |assoc_gen|
      assoc_gen.id = gen.id
      assoc_gen.type = gen.type
      assoc_gen.general = gen.general
    end
  end
end

#create_uml_associations(xmi_id) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity



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
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 355

def create_uml_associations(xmi_id) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
  matched_element = xmi_index&.find_element(xmi_id)

  return if !matched_element || !matched_element.links

  links = []
  matched_element.links.each do |link|
    links << link.association if link.association.any?
  end

  links.flatten.compact.filter_map do |assoc| # rubocop:disable Metrics/BlockLength
    link_member = assoc.start == xmi_id ? "end" : "start"
    link_owner = link_member == "start" ? "end" : "start"

    member_end, member_end_type, member_end_cardinality,
      member_end_attribute_name, member_end_xmi_id =
      serialize_member_type(xmi_id, assoc, link_member)

    owner_end = serialize_owned_type(xmi_id, assoc, link_owner)
    doc_node = link_member == "start" ? "source" : "target"
    definition = fetch_definition_node_value(assoc.id, doc_node)

    # Get owner_end_attribute_name from the ownedAttribute that
    # references this association
    owner_end_attribute_name = find_owner_attribute_name(xmi_id, assoc.id)

    if member_end &&
        (
          (member_end_type != "aggregation") ||
          (member_end_type == "aggregation" && member_end_attribute_name)
        )

      ::Lutaml::Uml::Association.new.tap do |association|
        association.xmi_id = assoc.id
        association.member_end = member_end
        association.member_end_type = member_end_type
        association.member_end_cardinality = create_uml_cardinality(
          member_end_cardinality,
        )
        association.member_end_attribute_name = member_end_attribute_name
        association.member_end_xmi_id = member_end_xmi_id
        association.owner_end = owner_end
        association.owner_end_xmi_id = xmi_id
        association.owner_end_attribute_name = owner_end_attribute_name
        association.definition = definition
      end
    end
  end
end

#create_uml_attribute(owned_attr) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 169

def create_uml_attribute(owned_attr) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  uml_type = owned_attr.uml_type
  uml_type_idref = uml_type.idref if uml_type

  ::Lutaml::Uml::TopElementAttribute.new.tap do |attr|
    attr.id = owned_attr.id
    attr.name = owned_attr.name
    attr.type = lookup_entity_name(uml_type_idref) || uml_type_idref
    attr.xmi_id = uml_type_idref
    attr.is_derived = !!owned_attr.is_derived
    attr.cardinality = ::Lutaml::Uml::Cardinality.new.tap do |car|
      car.min = owned_attr.lower_value&.value
      car.max = owned_attr.upper_value&.value
    end
    attr.definition = lookup_attribute_documentation(owned_attr.id)

    if owned_attr.association
      attr.association = owned_attr.association
      attr.definition = loopup_assoc_def(owned_attr.association)
      attr.type_ns = get_ns_by_xmi_id(attr.xmi_id)
    end
  end
end

#create_uml_attributes(uml_general_obj) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 202

def create_uml_attributes(uml_general_obj) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  upper_klass = uml_general_obj.general_upper_klass
  gen_attrs = uml_general_obj.general_attributes
  gen_name = uml_general_obj.general_name

  gen_attrs&.each do |i|
    name_ns = case i.type_ns
              when "core", "gml"
                upper_klass
              else
                i.type_ns
              end
    name_ns = upper_klass if name_ns.nil?

    i.name_ns = name_ns
    i.gen_name = gen_name
    i.name = "" if i.name.nil?
  end

  gen_attrs
end

#create_uml_cardinality(hash) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 193

def create_uml_cardinality(hash)
  return nil unless hash

  ::Lutaml::Uml::Cardinality.new.tap do |cardinality|
    cardinality.min = hash[:min]
    cardinality.max = hash[:max]
  end
end

#create_uml_class(klass) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 55

def create_uml_class(klass) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  ::Lutaml::Uml::Class.new.tap do |k| # rubocop:disable Metrics/BlockLength
    k.xmi_id = klass.id
    k.name = klass.name
    k.type = klass.type.split(":").last
    k.is_abstract = doc_node_attribute_value(klass.id, "isAbstract")
    k.definition = doc_node_attribute_value(klass.id, "documentation")
    k_st = doc_node_attribute_value(klass.id, "stereotype")
    k.stereotype = [k_st] if k_st

    k.attributes = create_uml_class_attributes(klass)
    k.associations = create_uml_associations(klass.id)
    k.operations = create_uml_operations(klass)
    k.constraints = create_uml_constraints(klass.id)
    k.association_generalization = create_uml_assoc_generalizations(klass)

    if klass.type?("uml:Class")
      k.generalization = create_uml_generalization(klass)
    end
  end
end

#create_uml_class_attributes(klass) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 157

def create_uml_class_attributes(klass) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
  return [] if klass.owned_attribute.nil?

  all_props = klass.owned_attribute.select do |attr|
    attr.type?("uml:Property")
  end

  all_props.filter_map do |oa|
    create_uml_attribute(oa)
  end
end

#create_uml_classes(package) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 42

def create_uml_classes(package)
  return [] if package.packaged_element.nil?

  klasses = package.packaged_element.select do |e|
    e.type?("uml:Class") || e.type?("uml:AssociationClass") ||
      e.type?("uml:Interface")
  end

  klasses.map do |klass|
    create_uml_class(klass)
  end
end

#create_uml_constraints(klass_id) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 435

def create_uml_constraints(klass_id) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  connector_node = fetch_connector(klass_id)
  return [] if connector_node.nil?

  # In ea-xmi-2.5.1, constraints are moved to source/target under
  # connectors
  constraints = %i[source target].map do |st|
    connector_node.send(st).constraints.constraint
  end.flatten

  constraints.map do |constraint|
    ::Lutaml::Uml::Constraint.new.tap do |con|
      con.name = HTMLEntities.new.decode(constraint.name)
      con.type = constraint.type
      con.weight = constraint.weight
      con.status = constraint.status
    end
  end
end

#create_uml_data_types(package) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



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
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 96

def create_uml_data_types(package) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  return [] if package.packaged_element.nil?

  data_types = package.packaged_element.select do |e|
    e.type?("uml:DataType")
  end

  data_types.map do |dt|
    ::Lutaml::Uml::DataType.new.tap do |data_type|
      data_type.xmi_id = dt.id
      data_type.name = dt.name
      data_type.is_abstract = doc_node_attribute_value(
        dt.id, "isAbstract"
      )
      data_type.definition = doc_node_attribute_value(
        dt.id, "documentation"
      )
      dt_st = doc_node_attribute_value(
        dt.id, "stereotype"
      )
      data_type.stereotype = [dt_st] if dt_st

      data_type.attributes = create_uml_class_attributes(dt)
      data_type.operations = create_uml_operations(dt)
      data_type.associations = create_uml_associations(dt.id)
      data_type.constraints = create_uml_constraints(dt.id)
    end
  end
end

#create_uml_diagrams(node_id) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 126

def create_uml_diagrams(node_id) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  return [] if @xmi_root_model.extension&.diagrams&.diagram.nil?

  diagrams = diagram_lookup[node_id]

  diagrams.map do |diagram|
    ::Lutaml::Uml::Diagram.new.tap do |dia|
      dia.xmi_id = diagram.id
      dia.name = diagram&.properties&.name
      dia.definition = diagram&.properties&.documentation

      package_id = diagram&.model&.package
      if package_id
        dia.package_id = package_id
        dia.package_name = find_packaged_element_by_id(package_id)&.name
      end
    end
  end
end

#create_uml_document(xmi_model) ⇒ Object



6
7
8
9
10
11
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 6

def create_uml_document(xmi_model)
  ::Lutaml::Uml::Document.new.tap do |doc|
    doc.name = xmi_model.model.name
    doc.packages = create_uml_packages(xmi_model.model)
  end
end

#create_uml_enums(package) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 77

def create_uml_enums(package) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  return [] if package.packaged_element.nil?

  enums = package.packaged_element.select do |e|
    e.type?("uml:Enumeration")
  end

  enums.map do |enum|
    ::Lutaml::Uml::Enum.new.tap do |en|
      en.xmi_id = enum.id
      en.name = enum.name
      en.values = create_uml_values(enum)
      en.definition = doc_node_attribute_value(enum.id, "documentation")
      en_st = doc_node_attribute_value(enum.id, "stereotype")
      en.stereotype = [en_st] if en_st
    end
  end
end

#create_uml_generalization(klass) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 224

def create_uml_generalization(klass) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  uml_general_obj, next_general_node_id = get_uml_general(klass.id)
  return uml_general_obj unless next_general_node_id

  if uml_general_obj.general
    inherited_props = []
    inherited_assoc_props = []
    level = 0

    loop_general_item(
      uml_general_obj.general,
      level,
      inherited_props,
      inherited_assoc_props,
    )
    uml_general_obj.inherited_props = inherited_props.reverse
    uml_general_obj.inherited_assoc_props = inherited_assoc_props.reverse
  end

  uml_general_obj
end

#create_uml_operations(klass) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 418

def create_uml_operations(klass) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  return [] if klass.owned_operation.nil?

  klass.owned_operation.filter_map do |operation|
    uml_type = operation.uml_type.first
    uml_type_idref = uml_type.idref if uml_type

    if !operation.respond_to?(:association) || operation.association.nil?
      ::Lutaml::Uml::Operation.new.tap do |op|
        op.xmi_id = uml_type_idref
        op.name = operation.name
        op.definition = lookup_attribute_documentation(operation.id)
      end
    end
  end
end

#create_uml_package(package) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 25

def create_uml_package(package) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  pkg = ::Lutaml::Uml::Package.new
  pkg.xmi_id = package.id
  pkg.name = get_package_name(package)
  pkg.definition = doc_node_attribute_value(package.id, "documentation")
  st = doc_node_attribute_value(package.id, "stereotype")
  pkg.stereotype = [st] if st

  pkg.packages = create_uml_packages(package)
  pkg.classes = create_uml_classes(package)
  pkg.enums = create_uml_enums(package)
  pkg.data_types = create_uml_data_types(package)
  pkg.diagrams = create_uml_diagrams(package.id)

  pkg
end

#create_uml_packages(model) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 13

def create_uml_packages(model)
  return [] if model.packaged_element.nil?

  packages = model.packaged_element.select do |e|
    e.type?("uml:Package")
  end

  packages.map do |package|
    create_uml_package(package)
  end
end

#create_uml_values(enum) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 455

def create_uml_values(enum) # rubocop:disable Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/AbcSize
  return [] if enum.owned_literal.nil?

  owned_literals = enum.owned_literal.select do |owned_literal|
    owned_literal.type?("uml:EnumerationLiteral")
  end

  owned_literals.map do |owned_literal|
    uml_type_id = owned_literal&.uml_type&.idref

    ::Lutaml::Uml::Value.new.tap do |value|
      value.name = owned_literal.name
      value.type = lookup_entity_name(uml_type_id) || uml_type_id
      value.definition = lookup_attribute_documentation(owned_literal.id)
    end
  end
end

#diagram_lookupHash

Lazy-built hash index for O(1) diagram lookups by package

Returns:

  • (Hash)

    Mapping of package_id => [diagrams]



148
149
150
151
152
153
154
155
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 148

def diagram_lookup
  @diagram_lookup ||= begin
    idx = Hash.new { |h, k| h[k] = [] }
    diagrams = @xmi_root_model.extension&.diagrams&.diagram || []
    diagrams.each { |d| idx[d.model.package] << d if d.model&.package }
    idx
  end
end

#find_owner_attribute_name(owner_xmi_id, assoc_id) ⇒ Object

Find the ownedAttribute name that references this association This gives us the role name from the owner’s perspective



407
408
409
410
411
412
413
414
415
416
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 407

def find_owner_attribute_name(owner_xmi_id, assoc_id)
  owner_node = find_packaged_element_by_id(owner_xmi_id)
  return nil unless owner_node&.owned_attribute

  owned_attr = owner_node.owned_attribute.find do |oa|
    oa.association == assoc_id
  end

  owned_attr&.name
end

#get_uml_general(general_id) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



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
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 246

def get_uml_general(general_id) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  general_node = find_packaged_element_by_id(general_id)
  return [] unless general_node

  general_node_attrs = get_uml_general_attributes(general_node)
  general_upper_klass = find_upper_level_packaged_element(general_id)
  next_general_node_id = get_next_general_node_id(general_node)

  uml_general = ::Lutaml::Uml::Generalization.new.tap do |gen|
    gen.general_id = general_id
    gen.general_name = general_node.name
    gen.general_attributes = general_node_attrs
    gen.general_upper_klass = general_upper_klass&.name
    gen.name = general_node.name
    gen.type = general_node.type
    gen.definition = lookup_element_prop_documentation(general_id)
    gen_st = doc_node_attribute_value(general_id, "stereotype")
    gen.stereotype = [gen_st] if gen_st

    if next_general_node_id
      gen.general = set_uml_generalization(
        next_general_node_id,
      )
      gen.has_general = true
      gen.general_id = general_node.id
      gen.general_name = general_node.name
    end
  end

  uml_general.attributes = create_uml_attributes(uml_general)
  uml_general.owned_props = uml_general.attributes.select do |attr|
    attr.association.nil?
  end
  uml_general.assoc_props = uml_general.attributes.select(&:association)

  [uml_general, next_general_node_id]
end

#get_uml_general_attributes(general_node) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 284

def get_uml_general_attributes(general_node) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  attrs = create_uml_class_attributes(general_node)

  attrs.sort_by { |a| [a.name.to_s, a.id] }.map do |attr|
    ::Lutaml::Uml::GeneralAttribute.new.tap do |gen_attr|
      gen_attr.id = attr.id
      gen_attr.name = attr.name
      gen_attr.type = attr.type
      gen_attr.xmi_id = attr.xmi_id
      gen_attr.is_derived = !!attr.is_derived
      gen_attr.cardinality = attr.cardinality
      gen_attr.definition = attr.definition&.strip
      gen_attr.association = attr.association
      gen_attr.has_association = !!attr.association
      gen_attr.type_ns = attr.type_ns
    end
  end
end

#loop_general_item(general_item, level, inherited_props, inherited_assoc_props) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity



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
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 316

def loop_general_item( # rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/PerceivedComplexity,Metrics/CyclomaticComplexity
  general_item, level, inherited_props, inherited_assoc_props
)
  gen_upper_klass = general_item.general_upper_klass
  gen_name = general_item.general_name

  # reverse the order to show super class first
  general_item.attributes.reverse_each do |attr|
    attr.upper_klass = gen_upper_klass
    attr.gen_name = gen_name
    attr.level = level

    if attr.association
      inherited_assoc_props << attr
    else
      inherited_props << attr
    end
  end

  if general_item&.has_general && general_item.general
    level += 1
    loop_general_item(
      general_item.general, level, inherited_props, inherited_assoc_props
    )
  end
end

#set_uml_generalization(general_id) ⇒ Object



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/lutaml/converter/xmi_to_uml.rb', line 303

def set_uml_generalization(general_id)
  uml_general_obj, next_general_node_id = get_uml_general(general_id)

  if next_general_node_id
    uml_general_obj.general = set_uml_generalization(
      next_general_node_id,
    )
    uml_general_obj.has_general = true
  end

  uml_general_obj
end