Class: Cgraph::Graph

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, info, options = {}) ⇒ Graph

Returns a new instance of Graph.



36
37
38
39
40
41
42
43
44
# File 'lib/cgraph.rb', line 36

def initialize data, info, options={}
  Entity.clear_namelist
  @connections = []
  @entities = []
  @root = options[:root] || ""
  @no_penwidth = options[:penwidth] || false
  generate_entities data, info
  generate_connections data
end

Instance Attribute Details

#connectionsObject (readonly)

Returns the value of attribute connections.



35
36
37
# File 'lib/cgraph.rb', line 35

def connections
  @connections
end

#entitiesObject (readonly)

Returns the value of attribute entities.



35
36
37
# File 'lib/cgraph.rb', line 35

def entities
  @entities
end

Instance Method Details

#add_connections_to_graph(graph) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/cgraph.rb', line 92

def add_connections_to_graph graph
  @connections.each do |conn|
    tupel = conn.first
    options = {}

    if @no_penwidth
      options = {}
    else
      options = {style: calc_penwidth(conn[1][:count])}
    end

    graph.add_edges tupel[0],tupel[1],options
  end
  graph
end

#add_entities(graph) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/cgraph.rb', line 69

def add_entities graph
  @entities.each do |e|
    if e.picture
      graph.add_nodes e.name,label: "",image: e.picture
    else
      graph.add_nodes e.name, label: e.label
    end
  end
  graph
end

#calc_penwidth(count) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/cgraph.rb', line 107

def calc_penwidth(count)
  case count
  when 4..10 then 'dashed'
  when 11..1000 then 'solid'
  else
    'dotted'
  end
end

#find_entity(name) ⇒ Object



64
65
66
67
68
# File 'lib/cgraph.rb', line 64

def find_entity name
  @entities.each do |e|
    return e if e.name == name
  end
end

#generate_connections(data) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/cgraph.rb', line 57

def generate_connections data
  @connections = data.group_by do |a,b|
    a+b; [a,b]
  end.map do |a,b|
    [a ,{count: b.count}]
  end
end

#generate_entities(data, infos = nil) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cgraph.rb', line 45

def generate_entities data, infos = nil
  data.flatten.uniq.each do |name|
    tmp = Entity.new name
    if !infos.nil? && !infos[name].nil?
      info = infos[name]
      tmp.label = info[:label] || ""
      tmp.picture = info[:picture] || ""
      tmp.group = info[:group] || ""
    end
    @entities.push tmp
  end
end

#generate_graphObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/cgraph.rb', line 79

def generate_graph
  if @root
    graph = GraphViz.new(:G, :type => :digraph, :root => @root)
  else
    graph = GraphViz.new(:G, :type => :digraph)
  end
  graph = add_connections_to_graph graph
  graph = add_entities graph
  graph
end

#to_pngObject



89
90
91
# File 'lib/cgraph.rb', line 89

def to_png
  graph.output(:png => "test.png")
end