Class: Divine::GraphGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/divine/graph_generator/graph_generator.rb

Overview

Responsible for drawing ERD diagram corresponding to structs

Instance Method Summary collapse

Instance Method Details

Build links from relation map



59
60
61
62
63
64
65
# File 'lib/divine/graph_generator/graph_generator.rb', line 59

def addLinks()
  @relations.each_pair do |src, trgs|
    trgs.each { |trg|
      @graph.add_edges( @nodes_map[trg], @nodes_map[src] )
    }
  end
end

#addNode(struct_name, struct_content) ⇒ Object

Add node to the graph

  • Args :

-struct_name- -> struct name
-struct_content- -> struct contents "Fields"


51
52
53
# File 'lib/divine/graph_generator/graph_generator.rb', line 51

def addNode(struct_name, struct_content)
  @graph.add_nodes( "{ #{struct_name} | #{struct_content} }", "shape" => "record", "style" => "rounded")
end

#addRelation(src, trg) ⇒ Object

Add relation between nodes

  • Args :

-src- -> the source struct
-trg- -> the target struct


72
73
74
75
76
77
78
# File 'lib/divine/graph_generator/graph_generator.rb', line 72

def addRelation(src, trg)
  if @relations.keys.include?(src)
    @relations[src].push(trg)
  else
    @relations[src] = [trg]
  end
end

#draw(path = "", file_name = "graph", format = "jpg") ⇒ Object

Draw Graph for current structs on $all_structs

  • Args :

-path- -> target path


14
15
16
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
# File 'lib/divine/graph_generator/graph_generator.rb', line 14

def draw(path = "", file_name = "graph", format = "jpg")
  @relations = {}
  @nodes_map = {}
  @graph = GraphViz.new( :G, :type => :digraph )
  $all_structs.keys.each do |k|
      name = "Struct: #{k}"
      content = "Temp Contents"
      simple_fields = ""
      complex_fields = ""
      $all_structs[k][0].simple_fields.each  { |f|
        simple_fields  << "#{f.name}: #{f.type}\n"
      }
      $all_structs[k][0].complex_fields.each { |f|
        type = f.referenced_types.to_s.gsub(':','').gsub("[list,", "list[").gsub("[map,", "map[").gsub(/[\s]*/, "").gsub("[", "&lt;").gsub("]", "&gt;")
        $all_structs.keys.each do |l|
          if type.include?(l.to_s)
            addRelation(l, k)
          end
        end
        complex_fields << "#{f.name}: #{type}\n"
      }
      content = simple_fields + complex_fields
      @nodes_map[k] = addNode(name, content)
  end
  
  addLinks()

  # Generate output image
  @graph.output( format.to_sym => File.join(path, "#{file_name}.#{format}") )
end