Class: Lutaml::Uml::Formatter::Graphviz

Inherits:
Base
  • Object
show all
Defined in:
lib/lutaml/uml/formatter/graphviz.rb

Defined Under Namespace

Classes: Attributes

Constant Summary collapse

ACCESS_SYMBOLS =
{
  "public"    => "+",
  "protected" => "#",
  "private"   => "-",
}.freeze
DEFAULT_CLASS_FONT =
"Helvetica".freeze
VALID_TYPES =
%i[
  dot
  xdot
  ps
  pdf
  svg
  svgz
  fig
  png
  gif
  jpg
  jpeg
  json
  imap
  cmapx
].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Instance Method Summary collapse

Methods inherited from Base

format, #format_class_relationship, inherited, name, #name

Methods included from HasAttributes

#update_attributes

Constructor Details

#initialize(attributes = {}) ⇒ Graphviz

Returns a new instance of Graphviz.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 44

def initialize(attributes = {})
  super

  @graph = Attributes.new
  # Associations lines style, `true` gives curved lines
  # https://graphviz.org/doc/info/attrs.html#d:splines
  @graph["splines"] = "ortho"
  # Padding between outside of picture and nodes
  @graph["pad"] = 0.5
  # Padding between levels
  @graph["ranksep"] = "1.2.equally"
  # Padding between nodes
  @graph["nodesep"] = "1.2.equally"
  # TODO: set rankdir
  # @graph['rankdir'] = 'BT'

  @edge = Attributes.new
  @edge["color"] = "gray50"

  @node = Attributes.new
  @node["shape"] = "box"

  @type = :dot
end

Instance Attribute Details

#edgeObject (readonly)

Returns the value of attribute edge.



70
71
72
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 70

def edge
  @edge
end

#graphObject (readonly)

Returns the value of attribute graph.



69
70
71
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 69

def graph
  @graph
end

#nodeObject (readonly)

Returns the value of attribute node.



71
72
73
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 71

def node
  @node
end

Instance Method Details

#escape_html_chars(text) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 85

def escape_html_chars(text)
  text
    .gsub(/</, "&#60;")
    .gsub(/>/, "&#62;")
    .gsub(/\[/, "&#91;")
    .gsub(/\]/, "&#93;")
end

#format(node) ⇒ Object



79
80
81
82
83
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 79

def format(node)
  dot = super.lines.map(&:rstrip).join("\n")

  generate_from_dot(dot)
end

#format_class(node, hide_members) ⇒ Object



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 223

def format_class(node, hide_members)
  name = ["<B>#{node.name}</B>"]
  name.unshift("«#{node.keyword}»") if node.keyword
  name_html = <<~HEREDOC
    <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
      #{name.map { |n| %(<TR><TD ALIGN="CENTER">#{n}</TD></TR>) }.join('\n')}
    </TABLE>
  HEREDOC

  field_table = format_member_rows(node.attributes, hide_members)
  method_table = format_member_rows(node.methods, hide_members)
  table_body = [name_html, field_table, method_table].map do |type|
    next if type.nil?

    <<~TEXT
      <TR>
        <TD>#{type}</TD>
      </TR>
    TEXT
  end

  <<~HEREDOC.chomp
    <TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0" CELLPADDING="10">
      #{table_body.compact.join("\n")}
    </TABLE>
  HEREDOC
end

#format_document(node) ⇒ Object



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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 251

def format_document(node)
  @fontname = node.fontname || DEFAULT_CLASS_FONT
  @node["fontname"] = "#{@fontname}-bold"

  if node.fidelity
    hide_members = node.fidelity["hideMembers"]
    hide_other_classes = node.fidelity["hideOtherClasses"]
  end
  classes = (node.classes +
              node.enums +
              node.data_types +
              node.primitives).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")
  associations = node.classes.map(&:associations).compact.flatten +
    node.associations
  if node.groups
    associations = sort_by_document_groupping(node.groups,
                                              associations)
  end
  classes_names = node.classes.map(&:name)
  associations = associations.map do |assoc_node|
    if hide_other_classes &&
        !classes_names.include?(assoc_node.member_end)
      next
    end

    format_relationship(assoc_node)
  end.join("\n")

  classes = classes.lines.map { |line| "  #{line}" }.join.chomp
  associations = associations
    .lines.map { |line| "  #{line}" }.join.chomp

  <<~HEREDOC
    digraph G {
      graph [#{@graph}]
      edge [#{@edge}]
      node [#{@node}]

    #{classes}

    #{associations}
    }
  HEREDOC
end

#format_field(node) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 93

def format_field(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

#format_label(name, cardinality = {}) ⇒ Object



192
193
194
195
196
197
198
199
200
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 192

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

#format_member_rows(members, hide_members) ⇒ Object



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

def format_member_rows(members, hide_members)
  unless !hide_members && members && members.length.positive?
    return <<~HEREDOC.chomp
      <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
        <TR><TD ALIGN="LEFT"></TD></TR>
      </TABLE>
    HEREDOC
  end

  field_rows = members.map do |field|
    %{<TR><TD ALIGN="LEFT">#{format_field(field)}</TD></TR>}
  end
  field_table = <<~HEREDOC.chomp
    <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
      #{field_rows.map { |row| ' ' * 10 + row }.join("\n")}
    </TABLE>
  HEREDOC
  field_table << "\n" << " " * 6
  field_table
end

#format_method(node) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 109

def format_method(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

#format_relationship(node) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 126

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



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
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 135

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
  attributes["label"] = node.action if node.action
  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
  # swap labels and arrows if `dir` eq to `back`
  if attributes["dir"] == "back"
    attributes["arrowhead"], attributes["arrowtail"] =
      [attributes["arrowtail"], attributes["arrowhead"]]
    attributes["headlabel"], attributes["taillabel"] =
      [attributes["taillabel"], attributes["headlabel"]]
  end
  attributes
end

#type=(value) ⇒ Object



73
74
75
76
77
# File 'lib/lutaml/uml/formatter/graphviz.rb', line 73

def type=(value)
  super

  @type = :dot unless VALID_TYPES.include?(@type)
end