class Wongi::Engine::Graph

Public Class Methods

new(rete) click to toggle source
# File lib/wongi-engine/graph.rb, line 5
def initialize rete
  @rete = rete
end

Public Instance Methods

dot(io, opts = { }) click to toggle source
# File lib/wongi-engine/graph.rb, line 9
def dot io, opts = { }

  @seen_betas = []

  if String === io
    File.open io, "w" do |actual_io|
      dot actual_io
    end
    return
  end

  @io = io

  @io.puts "digraph {"

  dump_alphas(opts) unless opts[:alpha] == false
  dump_betas(opts)

  @io.puts "}"

ensure
  @io = nil
end

Private Instance Methods

dump_alphas(opts) click to toggle source
# File lib/wongi-engine/graph.rb, line 39
def dump_alphas opts
  @io.puts "subgraph cluster_alphas {"
  @rete.alphas.select { |alpha| not alpha.betas.empty? }.each do |alpha|
    @io.puts "node#{print_hash alpha.object_id} [shape=box label=\"#{alpha.template.to_s.gsub /"/, "\\\""}\"];"
  end
  @io.puts "};"
end
dump_beta(beta, opts) click to toggle source
# File lib/wongi-engine/graph.rb, line 51
def dump_beta beta, opts
  return if @seen_betas.include? beta
  @seen_betas << beta
  @io.puts "node#{print_hash beta.object_id} [label=\"#{beta.class.name.split('::').last}\"];"
  if beta.is_a? NccNode
    @io.puts "node#{print_hash beta.partner.object_id} -> node#{print_hash beta.object_id};"
    @io.puts "{ rank=same; node#{print_hash beta.partner.object_id} node#{print_hash beta.object_id} }"
  end
  if beta.respond_to? :alpha and opts[:alpha] != false
    alpha = beta.alpha
    if alpha
      @io.puts "node#{print_hash alpha.object_id} -> node#{print_hash beta.object_id};"
    end
  end
  beta.children.each do |child|
    @io.puts "node#{print_hash beta.object_id} -> node#{print_hash child.object_id};"
    dump_beta child, opts
  end
end
dump_betas(opts) click to toggle source
# File lib/wongi-engine/graph.rb, line 47
def dump_betas opts
  dump_beta @rete.beta_top, opts
end
print_hash(h) click to toggle source