lutaml-uml 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/macos.yml +36 -0
  3. data/.github/workflows/ubuntu.yml +38 -0
  4. data/.github/workflows/windows.yml +41 -0
  5. data/.gitignore +1 -0
  6. data/Gemfile +2 -1
  7. data/LUTAML.adoc +314 -0
  8. data/{README.md → README.adoc} +15 -16
  9. data/Rakefile +3 -1
  10. data/bin/console +1 -0
  11. data/bin/lutaml2dotpng +23 -0
  12. data/bin/yaml2lutaml +108 -0
  13. data/exe/lutaml-uml +4 -3
  14. data/lib/lutaml/layout/engine.rb +15 -0
  15. data/lib/lutaml/layout/graph_viz_engine.rb +16 -0
  16. data/lib/lutaml/uml.rb +3 -0
  17. data/lib/lutaml/uml/abstraction.rb +7 -5
  18. data/lib/lutaml/uml/activity.rb +7 -5
  19. data/lib/lutaml/uml/actor.rb +14 -12
  20. data/lib/lutaml/uml/association.rb +40 -14
  21. data/lib/lutaml/uml/behavior.rb +7 -5
  22. data/lib/lutaml/uml/class.rb +56 -16
  23. data/lib/lutaml/uml/classifier.rb +9 -6
  24. data/lib/lutaml/uml/connector.rb +16 -12
  25. data/lib/lutaml/uml/constraint.rb +8 -7
  26. data/lib/lutaml/uml/constructor_end.rb +11 -8
  27. data/lib/lutaml/uml/data_type.rb +9 -4
  28. data/lib/lutaml/uml/dependency.rb +16 -13
  29. data/lib/lutaml/uml/document.rb +71 -0
  30. data/lib/lutaml/uml/enum.rb +33 -0
  31. data/lib/lutaml/uml/event.rb +7 -5
  32. data/lib/lutaml/uml/final_state.rb +7 -5
  33. data/lib/lutaml/uml/formatter.rb +21 -0
  34. data/lib/lutaml/uml/formatter/base.rb +67 -0
  35. data/lib/lutaml/uml/formatter/graphviz.rb +335 -0
  36. data/lib/lutaml/uml/has_attributes.rb +14 -0
  37. data/lib/lutaml/uml/has_members.rb +30 -0
  38. data/lib/lutaml/uml/instance.rb +15 -10
  39. data/lib/lutaml/uml/interface/base.rb +28 -0
  40. data/lib/lutaml/uml/interface/command_line.rb +265 -0
  41. data/lib/lutaml/uml/model.rb +11 -8
  42. data/lib/lutaml/uml/node/base.rb +21 -0
  43. data/lib/lutaml/uml/node/class_node.rb +57 -0
  44. data/lib/lutaml/uml/node/class_relationship.rb +14 -0
  45. data/lib/lutaml/uml/node/document.rb +18 -0
  46. data/lib/lutaml/uml/node/field.rb +34 -0
  47. data/lib/lutaml/uml/node/has_name.rb +15 -0
  48. data/lib/lutaml/uml/node/has_type.rb +15 -0
  49. data/lib/lutaml/uml/node/method.rb +29 -0
  50. data/lib/lutaml/uml/node/method_argument.rb +16 -0
  51. data/lib/lutaml/uml/node/relationship.rb +28 -0
  52. data/lib/lutaml/uml/opaque_behavior.rb +7 -6
  53. data/lib/lutaml/uml/package.rb +16 -13
  54. data/lib/lutaml/uml/parsers/attribute.rb +70 -0
  55. data/lib/lutaml/uml/parsers/dsl.rb +375 -0
  56. data/lib/lutaml/uml/parsers/dsl_preprocessor.rb +44 -0
  57. data/lib/lutaml/uml/parsers/dsl_transform.rb +27 -0
  58. data/lib/lutaml/uml/parsers/yaml.rb +46 -0
  59. data/lib/lutaml/uml/port.rb +6 -4
  60. data/lib/lutaml/uml/primitive_type.rb +9 -4
  61. data/lib/lutaml/uml/property.rb +25 -15
  62. data/lib/lutaml/uml/pseudostate.rb +7 -6
  63. data/lib/lutaml/uml/realization.rb +7 -5
  64. data/lib/lutaml/uml/region.rb +7 -6
  65. data/lib/lutaml/uml/serializers/association.rb +58 -0
  66. data/lib/lutaml/uml/serializers/base.rb +16 -0
  67. data/lib/lutaml/uml/serializers/class.rb +29 -0
  68. data/lib/lutaml/uml/serializers/top_element_attribute.rb +14 -0
  69. data/lib/lutaml/uml/serializers/yaml_view.rb +18 -0
  70. data/lib/lutaml/uml/state.rb +8 -6
  71. data/lib/lutaml/uml/state_machine.rb +7 -5
  72. data/lib/lutaml/uml/top_element.rb +45 -35
  73. data/lib/lutaml/uml/top_element_attribute.rb +25 -0
  74. data/lib/lutaml/uml/transition.rb +8 -6
  75. data/lib/lutaml/uml/trigger.rb +8 -6
  76. data/lib/lutaml/uml/version.rb +3 -1
  77. data/lib/lutaml/uml/vertex.rb +7 -5
  78. data/lutaml-uml.gemspec +9 -2
  79. data/spec/fixtures/datamodel/models/AddressClassProfile.yml +90 -0
  80. data/spec/fixtures/datamodel/models/AddressComponentProfile.yml +63 -0
  81. data/spec/fixtures/datamodel/models/AddressComponentSpecification.yml +15 -0
  82. data/spec/fixtures/datamodel/models/AddressProfile.yml +36 -0
  83. data/spec/fixtures/datamodel/models/AttributeProfile.yml +32 -0
  84. data/spec/fixtures/datamodel/models/InterchangeAddressClassProfile.yml +79 -0
  85. data/spec/fixtures/datamodel/models/Localization copy.yml +23 -0
  86. data/spec/fixtures/datamodel/models/Localization.yml +23 -0
  87. data/spec/fixtures/datamodel/models/ProfileCompliantAddress.yml +36 -0
  88. data/spec/fixtures/datamodel/models/ProfileCompliantAddressComponent.yml +15 -0
  89. data/spec/fixtures/datamodel/models/Signature.yml +20 -0
  90. data/spec/fixtures/datamodel/models/SignatureBlankDefinition.yml +20 -0
  91. data/spec/fixtures/datamodel/models/TextDirectionCode copy.yml +16 -0
  92. data/spec/fixtures/datamodel/models/TextDirectionCode.yml +16 -0
  93. data/spec/fixtures/datamodel/models/Validity.yml +14 -0
  94. data/spec/fixtures/datamodel/models/iso19160-1/Address.yml +22 -0
  95. data/spec/fixtures/datamodel/models/iso19160-1/AddressComponent.yml +2 -0
  96. data/spec/fixtures/datamodel/style.uml.inc +37 -0
  97. data/spec/fixtures/datamodel/views/AddressClassProfile.yml +12 -0
  98. data/spec/fixtures/datamodel/views/AddressProfile.yml +3 -0
  99. data/spec/fixtures/datamodel/views/CommonModels.yml +9 -0
  100. data/spec/fixtures/datamodel/views/TopDown.yml +62 -0
  101. data/spec/fixtures/dsl/diagram.lutaml +3 -0
  102. data/spec/fixtures/dsl/diagram_attributes.lutaml +5 -0
  103. data/spec/fixtures/dsl/diagram_class_assocation.lutaml +29 -0
  104. data/spec/fixtures/dsl/diagram_class_fields.lutaml +19 -0
  105. data/spec/fixtures/dsl/diagram_comments.lutaml +28 -0
  106. data/spec/fixtures/dsl/diagram_concept_model.lutaml +132 -0
  107. data/spec/fixtures/dsl/diagram_data_types.lutaml +24 -0
  108. data/spec/fixtures/dsl/diagram_includes.lutaml +6 -0
  109. data/spec/fixtures/dsl/diagram_multiply_classes.lutaml +7 -0
  110. data/spec/fixtures/dsl/shared.lutaml +3 -0
  111. data/spec/fixtures/dsl/shared1.lutaml +4 -0
  112. data/spec/fixtures/generated_dot/AddressClassProfile.dot +170 -0
  113. data/spec/fixtures/generated_dot/AddressProfile.dot +34 -0
  114. data/spec/lutaml/uml/formatter/graphviz_spec.rb +41 -0
  115. data/spec/lutaml/uml/parsers/dsl_spec.rb +252 -0
  116. data/spec/lutaml/uml/parsers/yaml_spec.rb +18 -0
  117. data/spec/lutaml/uml/serializers/yaml_view_spec.rb +20 -0
  118. data/spec/lutaml/uml_spec.rb +2 -4
  119. data/spec/spec_helper.rb +11 -0
  120. metadata +161 -13
@@ -1,8 +1,11 @@
1
- module Lutaml::Uml
2
-
3
- class Classifier < TopElement
4
- attr_accessor :generalization
5
- end
1
+ # frozen_string_literal: true
6
2
 
3
+ require "lutaml/uml/top_element"
7
4
 
8
- end
5
+ module Lutaml
6
+ module Uml
7
+ class Classifier < TopElement
8
+ attr_accessor :generalization
9
+ end
10
+ end
11
+ end
@@ -1,17 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  ## Behaviour metamodel
3
5
  ##
4
- module Lutaml::Uml
5
-
6
- class Connector < TopElement
7
- attr_accessor :kind, :connector_end
8
- def initialize
9
- @name = nil
10
- @xmi_id = nil
11
- @xmi_uuid = nil
12
- @connector_end = []
13
- @namespace = nil
14
- @kind = nil
15
- end
6
+ module Lutaml
7
+ module Uml
8
+ class Connector < TopElement
9
+ attr_accessor :kind, :connector_end
16
10
 
11
+ def initialize
12
+ @name = nil
13
+ @xmi_id = nil
14
+ @xmi_uuid = nil
15
+ @connector_end = []
16
+ @namespace = nil
17
+ @kind = nil
18
+ end
19
+ end
20
+ end
17
21
  end
@@ -1,11 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  ## Behaviour metamodel
3
5
  ##
4
- module Lutaml::Uml
5
-
6
- class Constraint < TopElement
7
- attr_accessor :body
8
- end
9
-
10
-
6
+ module Lutaml
7
+ module Uml
8
+ class Constraint < TopElement
9
+ attr_accessor :body
10
+ end
11
+ end
11
12
  end
@@ -1,13 +1,16 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  ## Behaviour metamodel
3
5
  ##
4
- module Lutaml::Uml
6
+ module Lutaml
7
+ module Uml
8
+ class ConnectorEnd < TopElement
9
+ attr_accessor :role, :part_with_port, :connector
5
10
 
6
- class ConnectorEnd < TopElement
7
- attr_accessor :role, :part_with_port, :connector
8
- def initialize
9
- @role = nil
10
- end
11
+ def initialize
12
+ @role = nil
13
+ end
14
+ end
15
+ end
11
16
  end
12
-
13
- end
@@ -1,6 +1,11 @@
1
- module Lutaml::Uml
1
+ # frozen_string_literal: true
2
2
 
3
- class DataType < Class
3
+ module Lutaml
4
+ module Uml
5
+ class DataType < Class
6
+ def keyword
7
+ "dataType"
8
+ end
9
+ end
10
+ end
4
11
  end
5
-
6
- end
@@ -1,18 +1,21 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  ## Behaviour metamodel
3
5
  ##
4
- module Lutaml::Uml
5
-
6
- class Dependency < TopElement
7
- attr_accessor :client, :supplier
8
- def initialize
9
- @name = nil
10
- @xmi_id = nil
11
- @xmi_uuid = nil
12
- @client = []
13
- @supplier = []
14
- @namespace = nil
15
- end
16
- end
6
+ module Lutaml
7
+ module Uml
8
+ class Dependency < TopElement
9
+ attr_accessor :client, :supplier
17
10
 
11
+ def initialize
12
+ @name = nil
13
+ @xmi_id = nil
14
+ @xmi_uuid = nil
15
+ @client = []
16
+ @supplier = []
17
+ @namespace = nil
18
+ end
19
+ end
20
+ end
18
21
  end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/uml/class"
4
+ require "lutaml/uml/enum"
5
+ require "lutaml/uml/data_type"
6
+ require "lutaml/uml/primitive_type"
7
+
8
+ module Lutaml
9
+ module Uml
10
+ class Document
11
+ include HasAttributes
12
+ include HasMembers
13
+
14
+ attr_accessor :name,
15
+ :title,
16
+ :caption,
17
+ :groups,
18
+ :fidelity,
19
+ :fontname,
20
+ :comments
21
+
22
+ # rubocop:disable Rails/ActiveRecordAliases
23
+ def initialize(attributes = {})
24
+ update_attributes(attributes)
25
+ end
26
+ # rubocop:enable Rails/ActiveRecordAliases
27
+
28
+ def classes=(value)
29
+ @classes = value.to_a.map { |attributes| Class.new(attributes) }
30
+ end
31
+
32
+ def enums=(value)
33
+ @enums = value.to_a.map { |attributes| Enum.new(attributes) }
34
+ end
35
+
36
+ def data_types=(value)
37
+ @data_types = value.to_a.map { |attributes| DataType.new(attributes) }
38
+ end
39
+
40
+ def primitives=(value)
41
+ @primitives = value.to_a.map { |attributes| PrimitiveType.new(attributes) }
42
+ end
43
+
44
+ def associations=(value)
45
+ @associations = value.to_a.map do |attributes|
46
+ Association.new(attributes)
47
+ end
48
+ end
49
+
50
+ def classes
51
+ @classes || []
52
+ end
53
+
54
+ def enums
55
+ @enums || []
56
+ end
57
+
58
+ def data_types
59
+ @data_types || []
60
+ end
61
+
62
+ def primitives
63
+ @primitives || []
64
+ end
65
+
66
+ def associations
67
+ @associations || []
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/uml/has_members"
4
+ require "lutaml/uml/classifier"
5
+ require "lutaml/uml/association"
6
+ require "lutaml/uml/top_element_attribute"
7
+
8
+ module Lutaml
9
+ module Uml
10
+ class Enum < Classifier
11
+ include HasMembers
12
+
13
+ attr_reader :attributes,
14
+ :members,
15
+ :modifier
16
+
17
+ def attributes=(value)
18
+ @attributes = value.to_a.map do |attr|
19
+ TopElementAttribute.new(attr)
20
+ end
21
+ end
22
+
23
+ # TODO: reserved name, change
24
+ def methods
25
+ []
26
+ end
27
+
28
+ def keyword
29
+ "enumeration"
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,10 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  ## Behaviour metamodel
3
5
  ##
4
6
 
5
- module Lutaml::Uml
6
-
7
- class Event < TopElement
7
+ module Lutaml
8
+ module Uml
9
+ class Event < TopElement
10
+ end
11
+ end
8
12
  end
9
-
10
- end
@@ -1,9 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  ##
2
4
  ## Behaviour metamodel
3
5
  ##
4
- module Lutaml::Uml
5
-
6
- class FinalState < State
7
- end
8
-
6
+ module Lutaml
7
+ module Uml
8
+ class FinalState < State
9
+ end
10
+ end
9
11
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lutaml
4
+ module Uml
5
+ module Formatter
6
+ class << self
7
+ def all
8
+ @all ||= []
9
+ end
10
+
11
+ def find_by_name(name)
12
+ name = name.to_sym
13
+
14
+ all.detect { |formatter_class| formatter_class.name == name }
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ require "lutaml/uml/formatter/graphviz"
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/uml/formatter"
4
+ require "lutaml/uml/has_attributes"
5
+
6
+ module Lutaml
7
+ module Uml
8
+ module Formatter
9
+ class Base
10
+ class << self
11
+ def inherited(subclass)
12
+ Formatter.all << subclass
13
+ end
14
+
15
+ def format(node, attributes = {})
16
+ new(attributes).format(node)
17
+ end
18
+
19
+ def name
20
+ to_s.split("::").last.downcase.to_sym
21
+ end
22
+ end
23
+
24
+ include HasAttributes
25
+
26
+ # rubocop:disable Rails/ActiveRecordAliases
27
+ def initialize(attributes = {})
28
+ update_attributes(attributes)
29
+ end
30
+ # rubocop:enable Rails/ActiveRecordAliases
31
+
32
+ def name
33
+ self.class.name
34
+ end
35
+
36
+ attr_reader :type
37
+
38
+ def type=(value)
39
+ @type = value.to_s.strip.downcase.to_sym
40
+ end
41
+
42
+ def format(node)
43
+ case node
44
+ when Node::Field then format_field(node)
45
+ when Node::Method then format_method(node)
46
+ when Node::Relationship then format_relationship(node)
47
+ when Node::ClassRelationship then format_class_relationship(node)
48
+ when Node::ClassNode then format_class(node)
49
+ when Lutaml::Uml::Document then format_document(node)
50
+ end
51
+ end
52
+
53
+ def format_field(_node); raise NotImplementedError; end
54
+
55
+ def format_method(_node); raise NotImplementedError; end
56
+
57
+ def format_relationship(_node); raise NotImplementedError; end
58
+
59
+ def format_class_relationship(_node); raise NotImplementedError; end
60
+
61
+ def format_class(_node); raise NotImplementedError; end
62
+
63
+ def format_document(_node); raise NotImplementedError; end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,335 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+ require "lutaml/uml/formatter/base"
5
+ require "lutaml/layout/graph_viz_engine"
6
+
7
+ module Lutaml
8
+ module Uml
9
+ module Formatter
10
+ class Graphviz < Base
11
+ class Attributes < Hash
12
+ def to_s
13
+ to_a
14
+ .reject { |(_k, val)| val.nil? }
15
+ .map { |(a, b)| "#{a}=#{b.inspect}" }
16
+ .join(" ")
17
+ end
18
+ end
19
+
20
+ ACCESS_SYMBOLS = {
21
+ "public" => "+",
22
+ "protected" => "#",
23
+ "private" => "-",
24
+ }.freeze
25
+ DEFAULT_CLASS_FONT = "Helvetica".freeze
26
+
27
+ VALID_TYPES = %i[
28
+ dot
29
+ xdot
30
+ ps
31
+ pdf
32
+ svg
33
+ svgz
34
+ fig
35
+ png
36
+ gif
37
+ jpg
38
+ jpeg
39
+ json
40
+ imap
41
+ cmapx
42
+ ].freeze
43
+
44
+ def initialize(attributes = {})
45
+ super
46
+
47
+ @graph = Attributes.new
48
+ # Associations lines style, `true` gives curved lines
49
+ # https://graphviz.org/doc/info/attrs.html#d:splines
50
+ @graph["splines"] = "ortho"
51
+ # Padding between outside of picture and nodes
52
+ @graph["pad"] = 0.5
53
+ # Padding between levels
54
+ @graph["ranksep"] = "1.2.equally"
55
+ # Padding between nodes
56
+ @graph["nodesep"] = "1.2.equally"
57
+ # TODO: set rankdir
58
+ # @graph['rankdir'] = 'BT'
59
+
60
+ @edge = Attributes.new
61
+ @edge["color"] = "gray50"
62
+
63
+ @node = Attributes.new
64
+ @node["shape"] = "box"
65
+
66
+ @type = :dot
67
+ end
68
+
69
+ attr_reader :graph
70
+ attr_reader :edge
71
+ attr_reader :node
72
+
73
+ def type=(value)
74
+ super
75
+
76
+ @type = :dot unless VALID_TYPES.include?(@type)
77
+ end
78
+
79
+ def format(node)
80
+ dot = super.lines.map(&:rstrip).join("\n")
81
+
82
+ generate_from_dot(dot)
83
+ end
84
+
85
+ def escape_html_chars(text)
86
+ text
87
+ .gsub(/</, "&#60;")
88
+ .gsub(/>/, "&#62;")
89
+ .gsub(/\[/, "&#91;")
90
+ .gsub(/\]/, "&#93;")
91
+ end
92
+
93
+ def format_field(node)
94
+ symbol = ACCESS_SYMBOLS[node.visibility]
95
+ result = "#{symbol}#{node.name}"
96
+ if node.type
97
+ keyword = node.keyword ? "«#{node.keyword}»" : ""
98
+ result += " : #{keyword}#{node.type}"
99
+ end
100
+ if node.cardinality
101
+ result += "[#{node.cardinality[:min]}..#{node.cardinality[:max]}]"
102
+ end
103
+ result = escape_html_chars(result)
104
+ result = "<U>#{result}</U>" if node.static
105
+
106
+ result
107
+ end
108
+
109
+ def format_method(node)
110
+ symbol = ACCESS_SYMBOLS[node.access]
111
+ result = "#{symbol} #{node.name}"
112
+ if node.arguments
113
+ arguments = node.arguments.map do |argument|
114
+ "#{argument.name}#{" : #{argument.type}" if argument.type}"
115
+ end.join(", ")
116
+ end
117
+
118
+ result << "(#{arguments})"
119
+ result << " : #{node.type}" if node.type
120
+ result = "<U>#{result}</U>" if node.static
121
+ result = "<I>#{result}</I>" if node.abstract
122
+
123
+ result
124
+ end
125
+
126
+ def format_relationship(node)
127
+ graph_parent_name = generate_graph_name(node.owner_end)
128
+ graph_node_name = generate_graph_name(node.member_end)
129
+ attributes = generate_graph_relationship_attributes(node)
130
+ graph_attributes = " [#{attributes}]" unless attributes.empty?
131
+
132
+ %{#{graph_parent_name} -> #{graph_node_name}#{graph_attributes}}
133
+ end
134
+
135
+ def generate_graph_relationship_attributes(node)
136
+ attributes = Attributes.new
137
+ if %w[dependency realizes].include?(node.member_end_type)
138
+ attributes["style"] = "dashed"
139
+ end
140
+ attributes["dir"] = if node.owner_end_type && node.member_end_type
141
+ "both"
142
+ elsif node.owner_end_type
143
+ "back"
144
+ else
145
+ "direct"
146
+ end
147
+ attributes["label"] = node.action if node.action
148
+ if node.owner_end_attribute_name
149
+ attributes["headlabel"] = format_label(
150
+ node.owner_end_attribute_name,
151
+ node.owner_end_cardinality
152
+ )
153
+ end
154
+ if node.member_end_attribute_name
155
+ attributes["taillabel"] = format_label(
156
+ node.member_end_attribute_name,
157
+ node.member_end_cardinality
158
+ )
159
+ end
160
+
161
+ attributes["arrowtail"] = case node.owner_end_type
162
+ when "composition"
163
+ "diamond"
164
+ when "aggregation"
165
+ "odiamond"
166
+ when "direct"
167
+ "vee"
168
+ else
169
+ "onormal"
170
+ end
171
+
172
+ attributes["arrowhead"] = case node.member_end_type
173
+ when "composition"
174
+ "diamond"
175
+ when "aggregation"
176
+ "odiamond"
177
+ when "direct"
178
+ "vee"
179
+ else
180
+ "onormal"
181
+ end
182
+ # swap labels and arrows if `dir` eq to `back`
183
+ if attributes["dir"] == "back"
184
+ attributes["arrowhead"], attributes["arrowtail"] = [attributes["arrowtail"], attributes["arrowhead"]]
185
+ attributes["headlabel"], attributes["taillabel"] = [attributes["taillabel"], attributes["headlabel"]]
186
+ end
187
+ attributes
188
+ end
189
+
190
+ def format_label(name, cardinality = {})
191
+ res = "+#{name}"
192
+ if cardinality.nil? ||
193
+ (cardinality["min"].nil? || cardinality["max"].nil?)
194
+ return res
195
+ end
196
+
197
+ "#{res} #{cardinality['min']}..#{cardinality['max']}"
198
+ end
199
+
200
+ def format_member_rows(members, hide_members)
201
+ unless !hide_members && members && members.length.positive?
202
+ return <<~HEREDOC.chomp
203
+ <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
204
+ <TR><TD ALIGN="LEFT"></TD></TR>
205
+ </TABLE>
206
+ HEREDOC
207
+ end
208
+
209
+ field_rows = members.map do |field|
210
+ %{<TR><TD ALIGN="LEFT">#{format_field(field)}</TD></TR>}
211
+ end
212
+ field_table = <<~HEREDOC.chomp
213
+ <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
214
+ #{field_rows.map { |row| ' ' * 10 + row }.join("\n")}
215
+ </TABLE>
216
+ HEREDOC
217
+ field_table << "\n" << " " * 6
218
+ field_table
219
+ end
220
+
221
+ def format_class(node, hide_members)
222
+ name = ["<B>#{node.name}</B>"]
223
+ name.unshift("«#{node.keyword}»") if node.keyword
224
+ name_html = <<~HEREDOC
225
+ <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
226
+ #{name.map { |n| %(<TR><TD ALIGN="CENTER">#{n}</TD></TR>) }.join('\n')}
227
+ </TABLE>
228
+ HEREDOC
229
+ # name = "«abstract»<BR/><I>#{name}</I>" if node.modifier == "abstract"
230
+ # name = "«interface»<BR/>#{name}" if node.modifier == "interface"
231
+
232
+ field_table = format_member_rows(node.attributes, hide_members)
233
+ method_table = format_member_rows(node.methods, hide_members)
234
+ table_body = [name_html, field_table, method_table].map do |type|
235
+ next if type.nil?
236
+
237
+ <<~TEXT
238
+ <TR>
239
+ <TD>#{type}</TD>
240
+ </TR>
241
+ TEXT
242
+ end
243
+
244
+ <<~HEREDOC.chomp
245
+ <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="10">
246
+ #{table_body.compact.join("\n")}
247
+ </TABLE>
248
+ HEREDOC
249
+ end
250
+
251
+ def format_document(node)
252
+ @fontname = node.fontname || DEFAULT_CLASS_FONT
253
+ @node["fontname"] = "#{@fontname}-bold"
254
+
255
+ if node.fidelity
256
+ hide_members = node.fidelity["hideMembers"]
257
+ hide_other_classes = node.fidelity["hideOtherClasses"]
258
+ end
259
+ classes = (node.classes + node.enums).map do |class_node|
260
+ graph_node_name = generate_graph_name(class_node.name)
261
+
262
+ <<~HEREDOC
263
+ #{graph_node_name} [
264
+ shape="plain"
265
+ fontname="#{@fontname || DEFAULT_CLASS_FONT}"
266
+ label=<#{format_class(class_node, hide_members)}>]
267
+ HEREDOC
268
+ end.join("\n")
269
+ associations = node.classes.map(&:associations).compact.flatten +
270
+ node.associations
271
+ if node.groups
272
+ associations = sort_by_document_groupping(node.groups, associations)
273
+ end
274
+ classes_names = node.classes.map(&:name)
275
+ associations = associations.map do |assoc_node|
276
+ if hide_other_classes &&
277
+ !classes_names.include?(assoc_node.member_end)
278
+ next
279
+ end
280
+
281
+ format_relationship(assoc_node)
282
+ end.join("\n")
283
+
284
+ classes = classes.lines.map { |line| " #{line}" }.join.chomp
285
+ associations = associations
286
+ .lines.map { |line| " #{line}" }.join.chomp
287
+
288
+ <<~HEREDOC
289
+ digraph G {
290
+ graph [#{@graph}]
291
+ edge [#{@edge}]
292
+ node [#{@node}]
293
+
294
+ #{classes}
295
+
296
+ #{associations}
297
+ }
298
+ HEREDOC
299
+ end
300
+
301
+ protected
302
+
303
+ def sort_by_document_groupping(groups, associations)
304
+ result = []
305
+ groups.each do |batch|
306
+ batch.each do |group_name|
307
+ associations
308
+ .select { |assc| assc.owner_end == group_name }
309
+ .each do |association|
310
+ result.push(association) unless result.include?(association)
311
+ end
312
+ end
313
+ end
314
+ associations.each do |association|
315
+ result.push(association) unless result.include?(association)
316
+ end
317
+ result
318
+ end
319
+
320
+ def generate_from_dot(input)
321
+ # https://github.com/glejeune/Ruby-Graphviz/issues/78
322
+ # Ruby-Graphviz has an old bug when html labels was not displayed
323
+ # property because of `<` and `>` characters escape, add additional
324
+ # `<` and `>` symbols to workaround it
325
+ escaped_dot = input.gsub("<<", "<<<").gsub(">>", ">>>")
326
+ Lutaml::Layout::GraphVizEngine.new(input: escaped_dot).render(@type)
327
+ end
328
+
329
+ def generate_graph_name(name)
330
+ name.gsub(/[^0-9a-zA-Z]/i, "")
331
+ end
332
+ end
333
+ end
334
+ end
335
+ end