lutaml 0.9.11 → 0.9.13

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: 9de4708a54b2e1e0cd6994dc7931e56711d2e257d1077f96003c4f8de6260c7e
4
- data.tar.gz: 461a1f380240be81347038fac000ab836067599ac3485ed7d9e5ba61594f379f
3
+ metadata.gz: 05c32d8cfd15503c91aa70205b2cf130785f6fb5c2c14e6622b0131f530a74d9
4
+ data.tar.gz: 1aefef310bed4c0438bb37dfdbfac9cf2a9748744f779ae4ecb1eccc18941bb5
5
5
  SHA512:
6
- metadata.gz: 40e3e939f49e84407257d9d80cebe83e42ef74ba87d5763f5f0c245d445b857273bce8576f24948a4e0e836fa6df2a6f15980c7add173176910a3180da5b4477
7
- data.tar.gz: 0c0c19132496f42ee08b6f9773d1807be9df6f4a3ada9ac49a3d846ee612742db086ca7e15494afbfee601f6ca6571ea532a28a2758f61c7576ba255602718ef
6
+ metadata.gz: 517a71b871804994c345e2676ec6f462efa698c4247bb77b7d3cbd62ce82025d53421a0f67ebf0645e0c47ebfbf9f4ae77629cceab3d4bf1af19548981e01f51
7
+ data.tar.gz: 4bdcc32531aae771bd148c57d4a41a391afe4eb35d74ff96c1276832ed4cb03c38c42c6407d543e8842240137819a04effd821473e2fd9d7b298ef04a926981e
@@ -1,3 +1,3 @@
1
1
  module Lutaml
2
- VERSION = "0.9.11".freeze
2
+ VERSION = "0.9.13".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,11 @@ 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)
93
96
  klass = find_klass_packaged_element_by_name(name)
94
- serialize_generalization(klass)
97
+ serialized_hash = serialize_generalization(klass)
98
+
99
+ ::Lutaml::XMI::GeneralizationDrop.new(serialized_hash)
95
100
  end
96
101
 
97
102
  private
@@ -99,29 +104,30 @@ module Lutaml
99
104
  # @param xmi_model [Shale::Mapper]
100
105
  # @return [Hash]
101
106
  # @note xpath: //uml:Model[@xmi:type="uml:Model"]
102
- def serialize_to_hash(xmi_model)
107
+ def serialize_to_hash(xmi_model, with_gen: false)
103
108
  model = xmi_model.model
104
109
  {
105
110
  name: model.name,
106
- packages: serialize_model_packages(model),
111
+ packages: serialize_model_packages(model, with_gen: with_gen),
107
112
  }
108
113
  end
109
114
 
110
115
  # @param model [Shale::Mapper]
111
116
  # @return [Array<Hash>]
112
117
  # @note xpath ./packagedElement[@xmi:type="uml:Package"]
113
- def serialize_model_packages(model)
118
+ def serialize_model_packages(model, with_gen: false)
114
119
  model.packaged_element.select do |e|
115
120
  e.type?("uml:Package")
116
121
  end.map do |package|
117
122
  {
118
123
  xmi_id: package.id,
119
124
  name: get_package_name(package),
120
- classes: serialize_model_classes(package, model),
125
+ classes: serialize_model_classes(package, model,
126
+ with_gen: with_gen),
121
127
  enums: serialize_model_enums(package),
122
128
  data_types: serialize_model_data_types(package),
123
129
  diagrams: serialize_model_diagrams(package.id),
124
- packages: serialize_model_packages(package),
130
+ packages: serialize_model_packages(package, with_gen: with_gen),
125
131
  definition: doc_node_attribute_value(package.id, "documentation"),
126
132
  stereotype: doc_node_attribute_value(package.id, "stereotype"),
127
133
  }
@@ -145,25 +151,38 @@ module Lutaml
145
151
  # @return [Array<Hash>]
146
152
  # @note xpath ./packagedElement[@xmi:type="uml:Class" or
147
153
  # @xmi:type="uml:AssociationClass"]
148
- def serialize_model_classes(package, model)
154
+ def serialize_model_classes(package, model, with_gen: false)
149
155
  package.packaged_element.select do |e|
150
156
  e.type?("uml:Class") || e.type?("uml:AssociationClass") ||
151
157
  e.type?("uml:Interface")
152
158
  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
- }
159
+ build_klass_hash(klass, model, with_gen: with_gen)
160
+ end
161
+ end
162
+
163
+ # @param klass [Shale::Mapper]
164
+ # @param model [Shale::Mapper]
165
+ # @return [Hash]
166
+ def build_klass_hash(klass, model, with_gen: false) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
167
+ klass_hash = {
168
+ xmi_id: klass.id,
169
+ name: klass.name,
170
+ package: model,
171
+ type: klass.type.split(":").last,
172
+ attributes: serialize_class_attributes(klass),
173
+ associations: serialize_model_associations(klass.id),
174
+ operations: serialize_class_operations(klass),
175
+ constraints: serialize_class_constraints(klass.id),
176
+ is_abstract: doc_node_attribute_value(klass.id, "isAbstract"),
177
+ definition: doc_node_attribute_value(klass.id, "documentation"),
178
+ stereotype: doc_node_attribute_value(klass.id, "stereotype"),
179
+ }
180
+
181
+ if with_gen && klass.type?("uml:Class")
182
+ klass_hash[:generalization] = serialize_generalization(klass)
166
183
  end
184
+
185
+ klass_hash
167
186
  end
168
187
 
169
188
  # @param klass [Shale::Mapper]
@@ -237,8 +256,9 @@ module Lutaml
237
256
  # @param general_node [Shale::Mapper]
238
257
  # # @return [Hash]
239
258
  def get_general_attributes(general_node)
240
- attrs = serialize_class_attributes(general_node, with_assoc: true)
241
- attrs.sort_by { |i| i[:name] }
259
+ serialize_class_attributes(general_node, with_assoc: true)
260
+ # turn on sorting if necessary
261
+ # attrs.sort_by { |i| i[:name] }
242
262
  end
243
263
 
244
264
  # @param general_node [Shale::Mapper]
@@ -251,6 +271,8 @@ module Lutaml
251
271
  # @return [Array<Hash>]
252
272
  def get_general_hash(general_id)
253
273
  general_node = get_general_node(general_id)
274
+ return [] unless general_node
275
+
254
276
  general_node_attrs = get_general_attributes(general_node)
255
277
  general_upper_klass = find_upper_level_packaged_element(general_id)
256
278
  next_general_node_id = get_next_general_node_id(general_node)
@@ -714,6 +736,8 @@ module Lutaml
714
736
  return unless type
715
737
 
716
738
  p = find_klass_packaged_element_by_name(type)
739
+ return unless p
740
+
717
741
  find_upper_level_packaged_element(p.id)
718
742
  end
719
743
 
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.11
4
+ version: 0.9.13
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-19 00:00:00.000000000 Z
11
+ date: 2024-10-03 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