Class: DotBuilder

Inherits:
Object
  • Object
show all
Includes:
UmlBuilder
Defined in:
lib/uml/dot_builder.rb

Overview

DotBuilder constructs a String in .dot format from a Digraph to be used with the Graphviz Dot program.

Instance Method Summary collapse

Methods included from UmlBuilder

#build_uml

Constructor Details

#initialize(config = {}, dot_config = {}) ⇒ DotBuilder

Returns a new instance of DotBuilder.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/uml/dot_builder.rb', line 17

def initialize config={}, dot_config={}
  super(config)
  @dot_config = dot_config

  @id_counter = 0
  @vertex_to_id = Hash.new

  def get_methods vertex
    methods = vertex.get_edge(Edge.new(:defines))
    return '...' if methods.empty?
    methods.to_a.map(&:name).join('\n').chomp('\n') # TODO need to figure out how to add new lines correctly
  end

  @vertex_mappings = Hash.new lambda { |*| '' }
  @vertex_mappings[:module] = lambda do |moduel|
    node_beginning(moduel) +
      get_methods(moduel) +
      node_ending
  end
  @vertex_mappings[:class] = lambda do |klass|
    node_beginning(klass) +
      "...|" + get_methods(klass) +
      node_ending
  end

  @edge_mappings = Hash.new lambda { |*| '' }
  @edge_mappings[:generalization] = lambda do |child, parent|
    return "#{parent}->#{child}[arrowtail=empty, dir=back]"
  end
  @edge_mappings[:implements] = lambda do |impl, type|
    return "#{type}->#{impl}[arrowtail=empty, dir=back, style=dashed]"
  end
  @edge_mappings[:aggregation] = lambda do |aggregator, aggregate|
    return "#{aggregator}->#{aggregate}[arrowtail=odiamond, constraint=false, dir=back]"
  end
  @edge_mappings[:dependency] = lambda do |vertex, depends_on|
    return "#{vertex}->#{depends_on}[dir=forward, style=dashed]"
  end
end

Instance Method Details

#build_entity(vertex) ⇒ Object



71
72
73
74
75
# File 'lib/uml/dot_builder.rb', line 71

def build_entity vertex
  @id_counter += 1
  @vertex_to_id[vertex] = @id_counter
  @vertex_mappings[vertex.type].call(vertex)
end


81
82
83
# File 'lib/uml/dot_builder.rb', line 81

def build_footer
  "}"
end

#build_headerObject



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/uml/dot_builder.rb', line 57

def build_header
  header = "digraph hierarchy {\n"
  header << "size=#{@dot_config["size"]}\n" if @dot_config.has_key? "size"
  if @dot_config.has_key? "node"
    header << "node["
    @dot_config["node"].each do |setting_name, setting_value|
      header << "#{setting_name}=#{setting_value}, "
    end
    header.chomp!(", ")
    header << "]\n"
  end
  header
end

#build_relation(vertex, edge, o_vertex) ⇒ Object



77
78
79
# File 'lib/uml/dot_builder.rb', line 77

def build_relation vertex, edge, o_vertex
  @edge_mappings[edge].call(@vertex_to_id[vertex], @vertex_to_id[o_vertex]) + "\n"
end

#get_methods(vertex) ⇒ Object



24
25
26
27
28
# File 'lib/uml/dot_builder.rb', line 24

def get_methods vertex
  methods = vertex.get_edge(Edge.new(:defines))
  return '...' if methods.empty?
  methods.to_a.map(&:name).join('\n').chomp('\n') # TODO need to figure out how to add new lines correctly
end

#node_beginning(vertex) ⇒ Object



7
8
9
10
11
# File 'lib/uml/dot_builder.rb', line 7

def node_beginning vertex
  id = @vertex_to_id[vertex]
  name = vertex.fully_qualified_name(@config["delimiter"]).chomp("?").chomp("!") # TODO find a better solution than removing question marks and bangs from methods
  "#{id}[label = \"{#{name}|"
end

#node_endingObject



13
14
15
# File 'lib/uml/dot_builder.rb', line 13

def node_ending
  "}\"]\n"
end