Class: GraphGen

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

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tableinfo, builtin_tables, cycle, name, budtime, collapse = false, cardinalities = {}, pathsto = {}, begins = {}) ⇒ GraphGen



7
8
9
10
11
12
13
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
# File 'lib/bud/graphs.rb', line 7

def initialize(tableinfo, builtin_tables, cycle, name, budtime, collapse=false, cardinalities={}, pathsto={}, begins={})
  @graph = GraphViz.new(:G, :type => :digraph, :label => "")
  #@graph.dim =  2
  @graph.node[:fontname] = "Times-Roman"
  @graph.node[:fontsize] = 18
  @graph.edge[:fontname] = "Times-Roman"
  @graph.edge[:fontsize] = 18
  @cards = cardinalities
  @name = name
  @collapse = collapse
  @budtime = budtime
  @builtin_tables = builtin_tables
  @pathsto = pathsto
  @begins = begins

  # map: table name -> type
  @tabinf = {}
  tableinfo.each do |ti|
    @tabinf[ti[0].to_s] = ti[1]
  end

  @redcycle = {}
  cycle.each do |c|
    # assumption: !(c[2] and !c[3]), or stratification would have bombed out
    if c[2] and c[3]
      @redcycle[c[0]] ||= []
      @redcycle[c[0]] << c[1]
    end
  end

  @nodes = {}
  @edges = {}
  @labels = {}
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes



5
6
7
# File 'lib/bud/graphs.rb', line 5

def nodes
  @nodes
end

Instance Method Details

#addedge(body, head, op, nm, negcluster, rule_id = nil) ⇒ 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
189
190
191
192
# File 'lib/bud/graphs.rb', line 158

def addedge(body, head, op, nm, negcluster, rule_id=nil)
  return if body.nil? or head.nil?
  body = body.to_s
  head = head.to_s
  return if negcluster and body == head

  ekey = body + head
  if !@edges[ekey]
    @edges[ekey] = @graph.add_edges(@nodes[body], @nodes[head], :penwidth => 5)
    @edges[ekey].arrowsize = 2

    @edges[ekey].color = (@nodes[body]["color"].source || "")
    @edges[ekey].URL = "#{rule_id}-#{head}.html" unless rule_id.nil?
    if head =~ /_msg\z/
      @edges[ekey].minlen = 2
    else
      @edges[ekey].minlen = 1.5
    end
    @labels[ekey] = {}
  end

  if op == '<+'
    @labels[ekey][' +/-'] = true
  elsif op == "<~"
    @edges[ekey].style = 'dashed'
  elsif op == "<-"
    #@labels[ekey] = @labels[ekey] + 'NEG(del)'
    @labels[ekey][' +/-'] = true
    @edges[ekey].arrowhead = 'veeodot'
  end
  if nm and head != "T"
    # hm, nonmono
    @edges[ekey].arrowhead = 'veeodot'
  end
end

#addonce(node, negcluster, inhead = false) ⇒ Object



107
108
109
110
111
112
113
114
115
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/bud/graphs.rb', line 107

def addonce(node, negcluster, inhead=false)
  if !@nodes[node]
    @nodes[node] = @graph.add_nodes(node)
    node_p = @nodes[node]
    node_p.label = node
    if @begins[:finish] and @begins[:finish][node]
      # point of divergence.  
      node_p.penwidth = 4
    end

    if @cards and @cards[node]
      node_p.label = "#{node}\n (#{@cards[node].to_s})"
      node_p.color = "green"
    else
      p = @pathsto[node].nil? ? "" : "\n(#{@pathsto[node][0][:val]})"
      node_p.label = node + p
      node_p.color = color_node(@pathsto[node])
    end
  else
    node_p = @nodes[node]
  end

  if @budtime == -1
    node_p.URL = "#{node}.html" if inhead
  else
    node_p.URL = "javascript:openWin(\"#{node}\", #{@budtime})"
  end

  if negcluster
    # cleaning
    res = node
    # pretty-printing issues
    node.split(", ").each_with_index do |p, i|
      if i == 0
        res = p
      elsif i % 4 == 0
        res = res + ",\n" + p
      else
        res = res + ", " + p
      end
    end
    node_p.label = res
    node_p.color = "red"
    node_p.shape = "octagon"
    node_p.penwidth = 3
    node_p.URL = "#{File.basename(@name).gsub(".staging", "").gsub("collapsed", "expanded")}.svg"
  elsif @tabinf[node] and (@tabinf[node] == "Bud::BudTable")
    node_p.shape = "rect"
  end
end

#color_node(paths) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/bud/graphs.rb', line 94

def color_node(paths)
  return "" if paths.nil?

  case paths[0][:val]
  when :A, :N
    "yellow"
  when :D, :G
    "red"
  else
    "black"
  end
end

#finish(depanalysis = nil, output = nil) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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
# File 'lib/bud/graphs.rb', line 194

def finish(depanalysis=nil, output=nil)
  @labels.each_key do |k|
    @edges[k].label = @labels[k].keys.join(" ")
  end

  addonce("S", false)
  addonce("T", false)

  @nodes["T"].URL = "javascript:advanceTo(#{@budtime+1})"
  @nodes["S"].URL = "javascript:advanceTo(#{@budtime-1})"

  @nodes["S"].color = "blue"
  @nodes["T"].color = "blue"
  @nodes["S"].shape = "diamond"
  @nodes["T"].shape = "diamond"

  @nodes["S"].penwidth = 3
  @nodes["T"].penwidth = 3

  @tabinf.each_pair do |k, v|
    unless @nodes[name_of(k)] or @builtin_tables[k.to_sym]
      addonce(k, false)
    end
    if v == "Bud::BudPeriodic"
      addedge("S", k, false, false, false)
    end
  end

  unless depanalysis.nil?
    depanalysis.source.to_a.each {|s| addedge("S", s.pred, false, false, false)}
    depanalysis.sink.to_a.each {|s| addedge(s.pred, "T", false, false, false)}

    unless depanalysis.underspecified.empty?
      addonce("??", false)
      @nodes["??"].color = "red"
      @nodes["??"].shape = "diamond"
      @nodes["??"].penwidth = 2
    end

    depanalysis.underspecified.to_a.each do |u|
      if u.input
        addedge(u.pred, "??", false, false, false)
      else
        addedge("??", u.pred, false, false, false)
      end
    end
  end

  if output.nil?
    @graph.output(:svg => @name)
  else
    @graph.output(output.to_sym => @name)
  end
end

#name_bag(predicate, bag) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bud/graphs.rb', line 42

def name_bag(predicate, bag)
  if bag[predicate]
    return bag
  else
    bag[predicate] = true
    res = bag
    if @redcycle[predicate].nil?
      return res
    end
    @redcycle[predicate].each do |rp|
      res = name_bag(rp, res)
    end
  end

  return res
end

#name_of(predicate) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/bud/graphs.rb', line 59

def name_of(predicate)
  # consider doing this in bud
  if @redcycle[predicate] and @collapse
    via = @redcycle[predicate]
    bag = name_bag(predicate, {})
    str = bag.keys.sort.join(", ")
    return str
  else
    return predicate
  end
end

#process(depends) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bud/graphs.rb', line 71

def process(depends)
  # collapsing NEG/+ cycles.
  # we want to create a function from any predicate to (cycle_name or bottom)
  # bottom if the predicate is not in a NEG/+ cycle.  otherwise,
  # its name is "CYC" + concat(sort(predicate names))
  depends.each do |d|
    # b/c bud_obj was pruned before serialization...
    bud_obj, rule_id, lhs, op, body, nm, in_body = d.to_a
    head = lhs
    body = body

    if @builtin_tables.has_key?(head.to_sym) or @builtin_tables.has_key?(body.to_sym)
      next
    end

    head = name_of(head)
    body = name_of(body)
    addonce(head, (head != lhs), true)
    addonce(body, (body != body))
    addedge(body, head, op, nm, (head != lhs), rule_id)
  end
end