Advertisement
ragrawal

DAG Dot Generator

Mar 21st, 2012
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.57 KB | None | 0 0
  1. # Class: Node
  2. # Description: Contains relevant information related to node
  3. #              Generates conditional probability distribution table
  4. #              Generates dot schema
  5. class Node
  6.   attr_reader :parents
  7.     attr_accessor :label, :values, :id
  8.    
  9.     def initialize
  10.       @label = nil
  11.       @parents = []
  12.       @values = []
  13.     end
  14.    
  15.     def add_parent(parent)
  16.       @parents << parent
  17.     end
  18.    
  19.     #generate conditional probability distribution table
  20.     def cpd
  21.       cpd = []
  22.       labels = @values.map{|i| i.strip }
  23.       cpd << [0.0] * @values.length
  24.  
  25.       @parents.reverse.each do |parent|
  26.         tmp = []
  27.         labels = [parent.label] + labels
  28.       parent.values.each do |val|
  29.         tmp = tmp + Array.new(cpd).map{|i| [val.strip] + i}
  30.       end          
  31.       cpd = tmp
  32.       end
  33.       cpd = [labels] + cpd
  34.       return cpd
  35.     end
  36.    
  37.     def get_dot
  38.       self.id.to_s + " [shape=none, margin=0, label=<" + get_dot_table + '>]'
  39.     end
  40.    
  41.     private
  42.       def get_dot_table
  43.         table = cpd
  44.         output= '<TABLE>' + "\n"
  45.         output = output + '<TR><TD COLSPAN="' + table.first.length.to_s + '">' + label + '</TD></TR>' + "\n"
  46.         table.each do |row|
  47.             output = output + '<TR>' + row.map{|i| "<TD>#{i}</TD>"}.join() + '</TR>' + "\n"
  48.         end
  49.         output = output + '</TABLE>'
  50.       end
  51.    
  52. end
  53.  
  54. #Helper method to convert string to label
  55. def get_label(text)
  56.   text.strip
  57. end
  58.  
  59.  
  60. ids=['A','B','C','D','E','F','G','H']
  61. nodes = {}
  62. read_edge = false
  63.  
  64. #open file
  65. File.open(ARGV[0]).each do |line|
  66.   line = line.to_s.chomp.strip
  67.   next unless line.length > 0
  68.  
  69.   #check if we are reading edges or nodes
  70.   read_edge = true if !read_edge and line.downcase == "#edges"
  71.  
  72.   #read edge
  73.   if read_edge
  74.       tokens = line.split('->').reverse
  75.       (0..tokens.length-2).each do |index|
  76.         child = nodes[get_label tokens[index]]
  77.         parent = nodes[get_label tokens[index+1]]
  78.         if child and parent
  79.            child.add_parent(parent)
  80.         elsif !child
  81.           puts "ERROR: MISSING #{tokens[index]}"
  82.           exit
  83.         elsif !parent
  84.           puts "ERROR: MISSING #{tokens[index]}"
  85.           exit
  86.         end
  87.       end
  88.  
  89.   # read nodes
  90.   else
  91.       node = Node.new
  92.       tokens = line.split(',')
  93.       node.label = get_label(tokens.shift)
  94.       node.values = tokens
  95.       node.id = ids[nodes.length]
  96.       nodes[node.label] = node
  97.   end
  98. end
  99.  
  100. #Generate dot notation
  101. puts "digraph g{\n"
  102.  
  103. nodes.each do |key, value|
  104.   value.parents.each do |parent|
  105.     puts parent.id + " -> " + value.id
  106.   end
  107. end
  108.  
  109. nodes.each do |key, value|
  110.   puts value.get_dot
  111. end
  112. puts "}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement