Class: UCD::Formatter::Graphviz

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

Defined Under Namespace

Classes: Attributes

Constant Summary collapse

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

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Instance Method Summary collapse

Methods inherited from Base

format, inherited, name, #name

Methods included from HasAttributes

#update_attributes

Constructor Details

#initialize(attributes = {}) ⇒ Graphviz

Returns a new instance of Graphviz.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ucd/formatter/graphviz.rb', line 22

def initialize(attributes={})
  super

  @graph = Attributes.new
  @graph["splines"] = "ortho"
  @graph["rankdir"] = "BT"

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

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

  @type = :dot
end

Instance Attribute Details

#edgeObject (readonly)

Returns the value of attribute edge.



39
40
41
# File 'lib/ucd/formatter/graphviz.rb', line 39

def edge
  @edge
end

#graphObject (readonly)

Returns the value of attribute graph.



38
39
40
# File 'lib/ucd/formatter/graphviz.rb', line 38

def graph
  @graph
end

#nodeObject (readonly)

Returns the value of attribute node.



40
41
42
# File 'lib/ucd/formatter/graphviz.rb', line 40

def node
  @node
end

Instance Method Details

#format(node) ⇒ Object



48
49
50
51
52
# File 'lib/ucd/formatter/graphviz.rb', line 48

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

  generate_from_dot(dot)
end

#format_class(node) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
# File 'lib/ucd/formatter/graphviz.rb', line 116

def format_class(node)
  name = "<B>#{node.name}</B>"
  name = "«abstract»<BR/><I>#{name}</I>" if node.modifier == "abstract"
  name = "«interface»<BR/>#{name}" if node.modifier == "interface"

  unless node.fields.empty?
    field_rows  = node.fields.map { |field| %Q{<TR><TD ALIGN="LEFT">#{format_field(field)}</TD></TR>}}
    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
  end

  unless node.methods.empty?
    method_rows  = node.methods.map { |method| %Q{<TR><TD ALIGN="LEFT">#{format_method(method)}</TD></TR>}}
    method_table = <<HEREDOC.chomp

  <TABLE BORDER="0" CELLPADDING="0" CELLSPACING="0">
#{method_rows.map { |row| " " * 10 + row }.join("\n")}
  </TABLE>
HEREDOC
    method_table << "\n" << " " * 6
  end

  <<-HEREDOC.chomp
<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0">
    <TR>
<TD>#{name}</TD>
    </TR>
    <TR>
<TD>#{field_table}</TD>
    </TR>
    <TR>
<TD>#{method_table}</TD>
    </TR>
  </TABLE>
HEREDOC
end

#format_class_relationship(node) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/ucd/formatter/graphviz.rb', line 104

def format_class_relationship(node)
  attributes = Attributes.new

  attributes["arrowhead"] = "onormal"
  attributes["style"] = "dashed" if node.type == "realizes"

  graph_parent_name = generate_graph_name(node.parent.name)
  graph_node_name = generate_graph_name(node.name)

  %Q{Class#{graph_parent_name} -> Class#{graph_node_name} [#{attributes}]}
end

#format_document(node) ⇒ Object



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
# File 'lib/ucd/formatter/graphviz.rb', line 158

def format_document(node)
  classes = node.classes.map do |node|
    graph_node_name = generate_graph_name(node.name)

    <<-HEREDOC
Class#{graph_node_name} [label=<
  #{format_class(node)}
>]
    HEREDOC
  end.join("\n")
  class_relationships = node.classes.map(&:class_relationships).flatten.map { |node| format_class_relationship(node) }.join("\n")
  relationships = node.classes.map(&:relationships).flatten.map { |node| format_relationship(node) }.join("\n")

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

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

#{classes}

#{class_relationships}

#{relationships}
}
HEREDOC
end

#format_field(node) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/ucd/formatter/graphviz.rb', line 54

def format_field(node)
  symbol = ACCESS_SYMBOLS[node.access]
  result = "#{symbol} #{node.name}"
  result << " : #{node.type}" if node.type
  result = "<U>#{result}</U>" if node.static

  result
end

#format_method(node) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ucd/formatter/graphviz.rb', line 63

def format_method(node)
  symbol = ACCESS_SYMBOLS[node.access]
  result = "#{symbol} #{node.name}"
  arguments = node.arguments.map do |argument|
    "#{argument.name}#{" : #{argument.type}" if argument.type}"
  end.join(", ") if node.arguments

  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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ucd/formatter/graphviz.rb', line 78

def format_relationship(node)
  dir       = "back" if %w[aggregation composition].include?(node.type)
  arrow_key = dir == "back" ? "arrowtail" : "arrowhead"
  from_key  = dir == "back" ? "taillabel" : "headlabel"
  to_key    = dir == "back" ? "headlabel" : "taillabel"

  attributes = Attributes.new

  attributes["style"]   = "dashed"  if node.type == "dependency"
  attributes["dir"]     = dir       if dir
  attributes[from_key]  = node.from if node.from
  attributes[to_key]    = node.to   if node.to

  if %w[aggregation composition].include?(node.type)
    arrow     = "diamond"  if node.type == "composition"
    arrow     = "odiamond" if node.type == "aggregation"
    attributes[arrow_key] = arrow
  end

  graph_parent_name = generate_graph_name(node.parent.name)
  graph_node_name   = generate_graph_name(node.name)
  graph_attributes  = " [#{attributes}]" unless attributes.empty?

  %Q{Class#{graph_parent_name} -> Class#{graph_node_name}#{graph_attributes}}
end

#type=(value) ⇒ Object



42
43
44
45
46
# File 'lib/ucd/formatter/graphviz.rb', line 42

def type=(value)
  super

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