Class: Lutaml::Formatter::Graphviz
- Inherits:
-
Base
- Object
- Base
- Lutaml::Formatter::Graphviz
show all
- Defined in:
- lib/lutaml/formatter/graphviz.rb
Defined Under Namespace
Classes: Attributes
Constant Summary
collapse
- ACCESS_SYMBOLS =
{
"public" => "+",
"protected" => "#",
"private" => "-",
}.freeze
- DEFAULT_CLASS_FONT =
"Helvetica"
- VALID_TYPES =
%i[
dot
xdot
ps
pdf
svg
svgz
fig
png
gif
jpg
jpeg
json
imap
cmapx
].freeze
- EMPTY_MEMBER_TABLE =
<<~HEREDOC.chomp
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
<TR><TD ALIGN="LEFT"></TD></TR>
</TABLE>
HEREDOC
Instance Attribute Summary collapse
Attributes inherited from Base
#type
Instance Method Summary
collapse
-
#build_associations(node, hide_other_classes) ⇒ Object
-
#build_digraph(classes, associations) ⇒ Object
-
#build_member_table(field_rows) ⇒ Object
-
#build_name_table(name_parts) ⇒ Object
-
#build_table_body(name_html, field_table, method_table) ⇒ Object
-
#collect_all_associations(node) ⇒ Object
-
#escape_html_chars(text) ⇒ Object
-
#extract_fidelity_options(node) ⇒ Object
-
#format(node) ⇒ Object
-
#format_all_classes(node, hide_members) ⇒ Object
-
#format_attribute(node) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
-
#format_class(node, hide_members) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#format_document(node) ⇒ Object
-
#format_filtered_associations(associations, classes_names, hide_other_classes) ⇒ Object
-
#format_label(name, cardinality = {}) ⇒ Object
-
#format_member_rows(members, hide_members) ⇒ Object
-
#format_operation(node) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength.
-
#format_relationship(node) ⇒ Object
-
#generate_graph_relationship_attributes(node) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity.
-
#indent_lines(text) ⇒ Object
-
#initialize(attributes = {}) ⇒ Graphviz
constructor
rubocop:disable Metrics/MethodLength.
-
#type=(value) ⇒ Object
Methods inherited from Base
format, #format_class_relationship, inherited, name, #name
#update_attributes
Constructor Details
#initialize(attributes = {}) ⇒ Graphviz
rubocop:disable Metrics/MethodLength
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/lutaml/formatter/graphviz.rb', line 43
def initialize(attributes = {}) super
@graph = Attributes.new
@graph["splines"] = "ortho"
@graph["pad"] = 0.5
@graph["ranksep"] = "1.2.equally"
@graph["nodesep"] = "1.2.equally"
@graph["rankdir"] = "BT"
@edge = Attributes.new
@edge["color"] = "gray50"
@node = Attributes.new
@node["shape"] = "box"
@type = :dot
end
|
Instance Attribute Details
#edge ⇒ Object
Returns the value of attribute edge.
67
68
69
|
# File 'lib/lutaml/formatter/graphviz.rb', line 67
def edge
@edge
end
|
#graph ⇒ Object
Returns the value of attribute graph.
67
68
69
|
# File 'lib/lutaml/formatter/graphviz.rb', line 67
def graph
@graph
end
|
#node ⇒ Object
Returns the value of attribute node.
67
68
69
|
# File 'lib/lutaml/formatter/graphviz.rb', line 67
def node
@node
end
|
Instance Method Details
#build_associations(node, hide_other_classes) ⇒ Object
302
303
304
305
306
307
308
309
310
311
312
|
# File 'lib/lutaml/formatter/graphviz.rb', line 302
def build_associations(node, hide_other_classes)
associations = collect_all_associations(node)
if node.groups
associations = sort_by_document_grouping(node.groups,
associations)
end
classes_names = node.classes.map(&:name)
format_filtered_associations(associations, classes_names,
hide_other_classes)
end
|
#build_digraph(classes, associations) ⇒ Object
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
# File 'lib/lutaml/formatter/graphviz.rb', line 327
def build_digraph(classes, associations)
indented_classes = indent_lines(classes)
indented_assocs = indent_lines(associations)
<<~HEREDOC
digraph G {
graph [#{@graph}]
edge [#{@edge}]
node [#{@node}]
#{indented_classes}
#{indented_assocs}
}
HEREDOC
end
|
#build_member_table(field_rows) ⇒ Object
224
225
226
227
228
229
230
231
232
|
# File 'lib/lutaml/formatter/graphviz.rb', line 224
def build_member_table(field_rows)
<<~HEREDOC.chomp
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
#{field_rows.map { |row| (' ' * 10) + row }.join("\n")}
</TABLE>
HEREDOC
.concat("\n")
.concat(" " * 6)
end
|
#build_name_table(name_parts) ⇒ Object
252
253
254
255
256
257
258
|
# File 'lib/lutaml/formatter/graphviz.rb', line 252
def build_name_table(name_parts)
<<~HEREDOC
<TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
#{name_parts.map { |n| %(<TR><TD ALIGN="CENTER">#{n}</TD></TR>) }.join('\n')}
</TABLE>
HEREDOC
end
|
#build_table_body(name_html, field_table, method_table) ⇒ Object
260
261
262
263
264
265
266
267
268
|
# File 'lib/lutaml/formatter/graphviz.rb', line 260
def build_table_body(name_html, field_table, method_table)
[name_html, field_table, method_table].compact.filter_map do |type|
<<~TEXT
<TR>
<TD>#{type}</TD>
</TR>
TEXT
end.join("\n")
end
|
#collect_all_associations(node) ⇒ Object
314
315
316
|
# File 'lib/lutaml/formatter/graphviz.rb', line 314
def collect_all_associations(node)
node.classes.filter_map(&:associations).flatten + node.associations
end
|
#escape_html_chars(text) ⇒ Object
81
82
83
84
85
86
87
|
# File 'lib/lutaml/formatter/graphviz.rb', line 81
def escape_html_chars(text)
text
.gsub("<", "<")
.gsub(">", ">")
.gsub("[", "[")
.gsub("]", "]")
end
|
281
282
283
284
285
286
287
|
# File 'lib/lutaml/formatter/graphviz.rb', line 281
def (node)
if node.fidelity
[node.fidelity.hideMembers, node.fidelity.hideOtherClasses]
else
[nil, nil]
end
end
|
75
76
77
78
79
|
# File 'lib/lutaml/formatter/graphviz.rb', line 75
def format(node)
dot = super.lines.map(&:rstrip).join("\n")
generate_from_dot(dot)
end
|
289
290
291
292
293
294
295
296
297
298
299
300
|
# File 'lib/lutaml/formatter/graphviz.rb', line 289
def format_all_classes(node, hide_members)
all_classes = node.classes + node.enums + node.data_types + node.primitives
all_classes.map do |class_node|
graph_node_name = generate_graph_name(class_node.name)
<<~HEREDOC
#{graph_node_name} [
shape="plain"
fontname="#{@fontname || DEFAULT_CLASS_FONT}"
label=<#{format_class(class_node, hide_members)}>]
HEREDOC
end.join("\n")
end
|
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/lutaml/formatter/graphviz.rb', line 89
def format_attribute(node) symbol = ACCESS_SYMBOLS[node.visibility]
result = "#{symbol}#{node.name}"
if node.type
keyword = node.keyword ? "«#{node.keyword}»" : ""
result += " : #{keyword}#{node.type}"
end
if node.cardinality
result += "[#{node.cardinality.min}.." \
"#{node.cardinality.max}]"
end
result = escape_html_chars(result)
result = "<U>#{result}</U>" if node.static
result
end
|
rubocop:disable Metrics/MethodLength
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
# File 'lib/lutaml/formatter/graphviz.rb', line 234
def format_class(node, hide_members) name = ["<B>#{node.name}</B>"]
name.unshift("«#{node.keyword}»") if node.keyword
name_html = build_name_table(name)
field_table = format_member_rows(node.attributes, hide_members)
method_table = if node.operations&.any?
format_member_rows(node.operations, hide_members)
end
table_body = build_table_body(name_html, field_table, method_table)
<<~HEREDOC.chomp
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="10">
#{table_body}
</TABLE>
HEREDOC
end
|
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/lutaml/formatter/graphviz.rb', line 270
def format_document(node)
@fontname = node.fontname || DEFAULT_CLASS_FONT
@node["fontname"] = "#{@fontname}-bold"
hide_members, hide_other_classes = (node)
classes = format_all_classes(node, hide_members)
associations = build_associations(node, hide_other_classes)
build_digraph(classes, associations)
end
|
318
319
320
321
322
323
324
325
|
# File 'lib/lutaml/formatter/graphviz.rb', line 318
def format_filtered_associations(associations, classes_names,
hide_other_classes)
associations.filter_map do |assoc_node|
next if hide_other_classes && !classes_names.include?(assoc_node.member_end)
format_relationship(assoc_node)
end.join("\n")
end
|
199
200
201
202
203
204
205
206
207
|
# File 'lib/lutaml/formatter/graphviz.rb', line 199
def format_label(name, cardinality = {})
res = "+#{name}"
if cardinality.nil? ||
(cardinality.min.nil? || cardinality.max.nil?)
return res
end
"#{res} #{cardinality.min}..#{cardinality.max}"
end
|
215
216
217
218
219
220
221
222
|
# File 'lib/lutaml/formatter/graphviz.rb', line 215
def format_member_rows(members, hide_members)
return EMPTY_MEMBER_TABLE if hide_members || !members&.any?
field_rows = members.map do |field|
%{<TR><TD ALIGN="LEFT">#{format_attribute(field)}</TD></TR>}
end
build_member_table(field_rows)
end
|
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/lutaml/formatter/graphviz.rb', line 106
def format_operation(node) symbol = ACCESS_SYMBOLS[node.access]
result = "#{symbol} #{node.name}"
if node.arguments
arguments = node.arguments.map do |argument|
"#{argument.name}#{" : #{argument.type}" if argument.type}"
end.join(", ")
end
result << "(#{arguments})"
result << " : #{node.type}" if node.type
result = "<U>#{result}</U>" if node.static
result = "<I>#{result}</I>" if node.abstract
result
end
|
123
124
125
126
127
128
129
130
|
# File 'lib/lutaml/formatter/graphviz.rb', line 123
def format_relationship(node)
graph_parent_name = generate_graph_name(node.owner_end)
graph_node_name = generate_graph_name(node.member_end)
attributes = generate_graph_relationship_attributes(node)
graph_attributes = " [#{attributes}]" unless attributes.empty?
%{#{graph_parent_name} -> #{graph_node_name}#{graph_attributes}}
end
|
#generate_graph_relationship_attributes(node) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/lutaml/formatter/graphviz.rb', line 132
def generate_graph_relationship_attributes(node) attributes = Attributes.new
if %w[dependency realizes].include?(node.member_end_type)
attributes["style"] = "dashed"
end
attributes["dir"] = if node.owner_end_type && node.member_end_type
"both"
elsif node.owner_end_type
"back"
else
"direct"
end
if node&.action&.verb
attributes["label"] = node.action.verb
end
case node&.action&.direction
when "target"
attributes["label"] = "#{attributes['label']} \u25b6"
when "source"
attributes["label"] = "\u25c0 #{attributes['label']}"
end
if node.owner_end_attribute_name
attributes["headlabel"] = format_label(
node.owner_end_attribute_name,
node.owner_end_cardinality,
)
end
if node.member_end_attribute_name
attributes["taillabel"] = format_label(
node.member_end_attribute_name,
node.member_end_cardinality,
)
end
attributes["arrowtail"] = case node.owner_end_type
when "composition"
"diamond"
when "aggregation"
"odiamond"
when "direct"
"vee"
else
"onormal"
end
attributes["arrowhead"] = case node.member_end_type
when "composition"
"diamond"
when "aggregation"
"odiamond"
when "direct"
"vee"
else
"onormal"
end
if attributes["dir"] == "back" && attributes["arrowtail"] != "vee"
attributes["arrowhead"], attributes["arrowtail"] =
[attributes["arrowtail"], attributes["arrowhead"]]
attributes["headlabel"], attributes["taillabel"] =
[attributes["taillabel"], attributes["headlabel"]]
end
attributes
end
|
#indent_lines(text) ⇒ Object
344
345
346
|
# File 'lib/lutaml/formatter/graphviz.rb', line 344
def indent_lines(text)
text.lines.map { |line| " #{line}" }.join.chomp
end
|
#type=(value) ⇒ Object
69
70
71
72
73
|
# File 'lib/lutaml/formatter/graphviz.rb', line 69
def type=(value)
super
@type = :dot unless VALID_TYPES.include?(@type)
end
|