lutaml 0.9.12 → 0.9.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b6d097a3542852b9b85447b34c3bb5c7f0a010af9aae4d63b1851688df79c3fd
4
- data.tar.gz: cf648e2d0de105baeb9306565b86b725f8016df4306610ca5f2517b0b4dcbaa8
3
+ metadata.gz: b50a1a83e93dcfdcb39affcb21e15e9a8451a74437f15ac68b9e0b34b3f342a0
4
+ data.tar.gz: 7baba352e09674337ec7dcbe2508601a7203a50a6b189571166c04fc9bc84b3b
5
5
  SHA512:
6
- metadata.gz: 9d8faa52e267adb2912c5f92140d7b529c2113fb0579c763f81a8d4395b8bac713dcf2ee204240ce94952d5507ebbbebcd9f76d14aec98b82913b542f098d35a
7
- data.tar.gz: 5c24db299e841de8cfc80033359486537c2e121bc9593f9fb42689c582d4d47d7e4c126690920f03bcc0f9086b89f2a6bf4bc94d40c1b93771caeee3b1c1e0a9
6
+ metadata.gz: c3bc110105d21b1a16cbd4a20685d7ba95580ef65ae757ac4f260ff13629540cd586ede6675f7d17f9f09a06946705e384d1e2fb691c9224b7d910eaed8901b2
7
+ data.tar.gz: ac4cc782c94b5bf4729f3440b26e014c65340c55fcbe6a724c63bb02015ec15d45b878f692b7cb6d4c074d7f055ab4f0f9d9a882f211b757983744c087e14c16
@@ -1,3 +1,3 @@
1
1
  module Lutaml
2
- VERSION = "0.9.12".freeze
2
+ VERSION = "0.9.14".freeze
3
3
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module XMI
5
+ class GeneralizationAttributeDrop < Liquid::Drop
6
+ LOWER_VALUE_MAPPINGS = {
7
+ "C" => "0",
8
+ "M" => "1",
9
+ }.freeze
10
+
11
+ def initialize(attr, upper_klass, gen_name) # rubocop:disable Lint/MissingSuper
12
+ @attr = attr
13
+ @upper_klass = upper_klass
14
+ @gen_name = gen_name
15
+ end
16
+
17
+ def id
18
+ @attr[:id]
19
+ end
20
+
21
+ def name
22
+ @attr[:name]
23
+ end
24
+
25
+ def type
26
+ @attr[:type]
27
+ end
28
+
29
+ def xmi_id
30
+ @attr[:xmi_id]
31
+ end
32
+
33
+ def is_derived
34
+ @attr[:is_derived]
35
+ end
36
+
37
+ def cardinality
38
+ min = @attr[:cardinality]["min"]
39
+ min = min.nil? ? nil : LOWER_VALUE_MAPPINGS[min]
40
+
41
+ "#{min}..#{@attr[:cardinality]['max']}"
42
+ end
43
+
44
+ def min_cardinality
45
+ @attr[:cardinality]["min"]
46
+ end
47
+
48
+ def max_cardinality
49
+ @attr[:cardinality]["max"]
50
+ end
51
+
52
+ def definition
53
+ @attr[:definition]
54
+ end
55
+
56
+ def association
57
+ @attr[:association]
58
+ end
59
+
60
+ def has_association?
61
+ !!@attr[:association]
62
+ end
63
+
64
+ def type_ns
65
+ @attr[:type_ns]
66
+ end
67
+
68
+ def upper_klass
69
+ @upper_klass
70
+ end
71
+
72
+ def gen_name
73
+ @gen_name
74
+ end
75
+
76
+ def name_ns
77
+ name_ns = case @attr[:type_ns]
78
+ when "core", "gml"
79
+ upper_klass
80
+ else
81
+ @attr[:type_ns]
82
+ end
83
+
84
+ name_ns = upper_klass if name_ns.nil?
85
+ name_ns
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module XMI
5
+ class GeneralizationDrop < Liquid::Drop
6
+ def initialize(gen) # rubocop:disable Lint/MissingSuper
7
+ @gen = gen
8
+ @looped_general_item = false
9
+ @inherited_props = []
10
+ @inherited_assoc_props = []
11
+ end
12
+
13
+ def id
14
+ @gen[:general_id]
15
+ end
16
+
17
+ def name
18
+ @gen[:general_name]
19
+ end
20
+
21
+ def upper_klass
22
+ @gen[:general_upper_klass]
23
+ end
24
+
25
+ def general
26
+ GeneralizationDrop.new(@gen[:general]) if @gen[:general]
27
+ end
28
+
29
+ def has_general?
30
+ !!@gen[:general]
31
+ end
32
+
33
+ def attributes
34
+ @gen[:general_attributes]
35
+ # @gen[:general_attributes].map do |attr|
36
+ # GeneralizationAttributeDrop.new(attr, upper_klass, name)
37
+ # end
38
+ end
39
+
40
+ def type
41
+ @gen[:type]
42
+ end
43
+
44
+ def definition
45
+ @gen[:definition]
46
+ end
47
+
48
+ def stereotype
49
+ @gen[:stereotype]
50
+ end
51
+
52
+ # get attributes without association
53
+ def owned_props
54
+ attributes.select do |attr|
55
+ attr[:association].nil?
56
+ end.map do |attr|
57
+ GeneralizationAttributeDrop.new(attr, upper_klass, name)
58
+ end
59
+ end
60
+
61
+ # get attributes with association
62
+ def assoc_props
63
+ attributes.select do |attr|
64
+ attr[:association]
65
+ end.map do |attr|
66
+ GeneralizationAttributeDrop.new(attr, upper_klass, name)
67
+ end
68
+ end
69
+
70
+ # get items without association by looping through the generation
71
+ def inherited_props
72
+ loop_general_item unless @looped_general_item
73
+
74
+ @inherited_props.reverse
75
+ end
76
+
77
+ # get items with association by looping through the generation
78
+ def inherited_assoc_props
79
+ loop_general_item unless @looped_general_item
80
+
81
+ @inherited_assoc_props.reverse
82
+ end
83
+
84
+ def loop_general_item # rubocop:disable Metrics/MethodLength
85
+ general_item = general
86
+ while general_item.has_general?
87
+ gen_upper_klass = general_item.upper_klass
88
+ gen_name = general_item.name
89
+ # reverse the order to show super class first
90
+ general_item.attributes.reverse_each do |attr|
91
+ attr_drop = GeneralizationAttributeDrop.new(attr, gen_upper_klass,
92
+ gen_name)
93
+ if attr[:association]
94
+ @inherited_assoc_props << attr_drop
95
+ else
96
+ @inherited_props << attr_drop
97
+ end
98
+ end
99
+
100
+ general_item = general_item.general
101
+ end
102
+
103
+ @looped_general_item = true
104
+ end
105
+ end
106
+ end
107
+ end
@@ -47,6 +47,12 @@ module Lutaml
47
47
  end
48
48
  end
49
49
 
50
+ def generalization
51
+ return {} if @model[:generalization].nil?
52
+
53
+ ::Lutaml::XMI::GeneralizationDrop.new(@model[:generalization])
54
+ end
55
+
50
56
  def is_abstract
51
57
  @model[:is_abstract]
52
58
  end
@@ -26,10 +26,11 @@ module Lutaml
26
26
  end
27
27
 
28
28
  # @param xml [String] path to xml
29
+ # @param with_gen [Boolean]
29
30
  # @return [Hash]
30
- def serialize_xmi(xml)
31
+ def serialize_xmi(xml, with_gen: false)
31
32
  xmi_model = get_xmi_model(xml)
32
- new.serialize_xmi(xmi_model)
33
+ new.serialize_xmi(xmi_model, with_gen: with_gen)
33
34
  end
34
35
 
35
36
  # @param xml [String] path to xml
@@ -57,10 +58,15 @@ module Lutaml
57
58
  end
58
59
 
59
60
  # @param xmi_model [Shale::Mapper]
60
- # @return [Lutaml::Uml::Document]
61
- def parse(xmi_model)
61
+ def set_xmi_model(xmi_model)
62
62
  @xmi_cache = {}
63
63
  @xmi_root_model = xmi_model
64
+ end
65
+
66
+ # @param xmi_model [Shale::Mapper]
67
+ # @return [Lutaml::Uml::Document]
68
+ def parse(xmi_model)
69
+ set_xmi_model(xmi_model)
64
70
  serialized_hash = serialize_xmi(xmi_model)
65
71
 
66
72
  ::Lutaml::Uml::Document.new(serialized_hash)
@@ -68,18 +74,16 @@ module Lutaml
68
74
 
69
75
  # @param xmi_model [Shale::Mapper]
70
76
  # return [Hash]
71
- def serialize_xmi(xmi_model)
72
- @xmi_cache = {}
73
- @xmi_root_model = xmi_model
74
- serialize_to_hash(xmi_model)
77
+ def serialize_xmi(xmi_model, with_gen: false)
78
+ set_xmi_model(xmi_model)
79
+ serialize_to_hash(xmi_model, with_gen: with_gen)
75
80
  end
76
81
 
77
82
  # @param xmi_model [Shale::Mapper]
78
83
  # return [Liquid::Drop]
79
84
  def serialize_xmi_to_liquid(xmi_model)
80
- @xmi_cache = {}
81
- @xmi_root_model = xmi_model
82
- serialized_hash = serialize_xmi(xmi_model)
85
+ set_xmi_model(xmi_model)
86
+ serialized_hash = serialize_xmi(xmi_model, with_gen: true)
83
87
 
84
88
  ::Lutaml::XMI::RootDrop.new(serialized_hash)
85
89
  end
@@ -88,10 +92,12 @@ module Lutaml
88
92
  # @param name [String]
89
93
  # @return [Hash]
90
94
  def serialize_generalization_by_name(xmi_model, name)
91
- @xmi_cache = {}
92
- @xmi_root_model = xmi_model
95
+ set_xmi_model(xmi_model)
96
+ model = xmi_model.model
93
97
  klass = find_klass_packaged_element_by_name(name)
94
- serialize_generalization(klass)
98
+ serialized_hash = build_klass_hash(klass, model, with_gen: true)
99
+
100
+ ::Lutaml::XMI::KlassDrop.new(serialized_hash)
95
101
  end
96
102
 
97
103
  private
@@ -99,29 +105,30 @@ module Lutaml
99
105
  # @param xmi_model [Shale::Mapper]
100
106
  # @return [Hash]
101
107
  # @note xpath: //uml:Model[@xmi:type="uml:Model"]
102
- def serialize_to_hash(xmi_model)
108
+ def serialize_to_hash(xmi_model, with_gen: false)
103
109
  model = xmi_model.model
104
110
  {
105
111
  name: model.name,
106
- packages: serialize_model_packages(model),
112
+ packages: serialize_model_packages(model, with_gen: with_gen),
107
113
  }
108
114
  end
109
115
 
110
116
  # @param model [Shale::Mapper]
111
117
  # @return [Array<Hash>]
112
118
  # @note xpath ./packagedElement[@xmi:type="uml:Package"]
113
- def serialize_model_packages(model)
119
+ def serialize_model_packages(model, with_gen: false)
114
120
  model.packaged_element.select do |e|
115
121
  e.type?("uml:Package")
116
122
  end.map do |package|
117
123
  {
118
124
  xmi_id: package.id,
119
125
  name: get_package_name(package),
120
- classes: serialize_model_classes(package, model),
126
+ classes: serialize_model_classes(package, model,
127
+ with_gen: with_gen),
121
128
  enums: serialize_model_enums(package),
122
129
  data_types: serialize_model_data_types(package),
123
130
  diagrams: serialize_model_diagrams(package.id),
124
- packages: serialize_model_packages(package),
131
+ packages: serialize_model_packages(package, with_gen: with_gen),
125
132
  definition: doc_node_attribute_value(package.id, "documentation"),
126
133
  stereotype: doc_node_attribute_value(package.id, "stereotype"),
127
134
  }
@@ -145,27 +152,40 @@ module Lutaml
145
152
  # @return [Array<Hash>]
146
153
  # @note xpath ./packagedElement[@xmi:type="uml:Class" or
147
154
  # @xmi:type="uml:AssociationClass"]
148
- def serialize_model_classes(package, model)
155
+ def serialize_model_classes(package, model, with_gen: false)
149
156
  package.packaged_element.select do |e|
150
157
  e.type?("uml:Class") || e.type?("uml:AssociationClass") ||
151
158
  e.type?("uml:Interface")
152
159
  end.map do |klass|
153
- {
154
- xmi_id: klass.id,
155
- name: klass.name,
156
- package: model,
157
- type: klass.type.split(":").last,
158
- attributes: serialize_class_attributes(klass),
159
- associations: serialize_model_associations(klass.id),
160
- operations: serialize_class_operations(klass),
161
- constraints: serialize_class_constraints(klass.id),
162
- is_abstract: doc_node_attribute_value(klass.id, "isAbstract"),
163
- definition: doc_node_attribute_value(klass.id, "documentation"),
164
- stereotype: doc_node_attribute_value(klass.id, "stereotype"),
165
- }
160
+ build_klass_hash(klass, model, with_gen: with_gen)
166
161
  end
167
162
  end
168
163
 
164
+ # @param klass [Shale::Mapper]
165
+ # @param model [Shale::Mapper]
166
+ # @return [Hash]
167
+ def build_klass_hash(klass, model, with_gen: false) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
168
+ klass_hash = {
169
+ xmi_id: klass.id,
170
+ name: klass.name,
171
+ package: model,
172
+ type: klass.type.split(":").last,
173
+ attributes: serialize_class_attributes(klass),
174
+ associations: serialize_model_associations(klass.id),
175
+ operations: serialize_class_operations(klass),
176
+ constraints: serialize_class_constraints(klass.id),
177
+ is_abstract: doc_node_attribute_value(klass.id, "isAbstract"),
178
+ definition: doc_node_attribute_value(klass.id, "documentation"),
179
+ stereotype: doc_node_attribute_value(klass.id, "stereotype"),
180
+ }
181
+
182
+ if with_gen && klass.type?("uml:Class")
183
+ klass_hash[:generalization] = serialize_generalization(klass)
184
+ end
185
+
186
+ klass_hash
187
+ end
188
+
169
189
  # @param klass [Shale::Mapper]
170
190
  # # @return [Hash]
171
191
  def serialize_generalization(klass)
@@ -237,10 +257,9 @@ module Lutaml
237
257
  # @param general_node [Shale::Mapper]
238
258
  # # @return [Hash]
239
259
  def get_general_attributes(general_node)
240
- attrs = serialize_class_attributes(general_node, with_assoc: true)
260
+ serialize_class_attributes(general_node, with_assoc: true)
241
261
  # turn on sorting if necessary
242
262
  # attrs.sort_by { |i| i[:name] }
243
- attrs
244
263
  end
245
264
 
246
265
  # @param general_node [Shale::Mapper]
@@ -253,6 +272,8 @@ module Lutaml
253
272
  # @return [Array<Hash>]
254
273
  def get_general_hash(general_id)
255
274
  general_node = get_general_node(general_id)
275
+ return [] unless general_node
276
+
256
277
  general_node_attrs = get_general_attributes(general_node)
257
278
  general_upper_klass = find_upper_level_packaged_element(general_id)
258
279
  next_general_node_id = get_next_general_node_id(general_node)
@@ -716,6 +737,8 @@ module Lutaml
716
737
  return unless type
717
738
 
718
739
  p = find_klass_packaged_element_by_name(type)
740
+ return unless p
741
+
719
742
  find_upper_level_packaged_element(p.id)
720
743
  end
721
744
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lutaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.12
4
+ version: 0.9.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-24 00:00:00.000000000 Z
11
+ date: 2024-10-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: liquid
@@ -376,6 +376,8 @@ files:
376
376
  - lib/lutaml/xmi/liquid_drops/diagram_drop.rb
377
377
  - lib/lutaml/xmi/liquid_drops/enum_drop.rb
378
378
  - lib/lutaml/xmi/liquid_drops/enum_owned_literal_drop.rb
379
+ - lib/lutaml/xmi/liquid_drops/generalization_attribute_drop.rb
380
+ - lib/lutaml/xmi/liquid_drops/generalization_drop.rb
379
381
  - lib/lutaml/xmi/liquid_drops/klass_drop.rb
380
382
  - lib/lutaml/xmi/liquid_drops/operation_drop.rb
381
383
  - lib/lutaml/xmi/liquid_drops/package_drop.rb