Method: Databaseformalizer::EntitiesHelper.setModelGraph

Defined in:
app/helpers/databaseformalizer/entities_helper.rb

.setModelGraph(uri) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
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
157
158
# File 'app/helpers/databaseformalizer/entities_helper.rb', line 94

def EntitiesHelper.setModelGraph(uri )
  require 'graphviz'
  # Create a new graph
  g = GraphViz.new( :G, :type => :digraph )
  
  # Graph configuration
  g.node[:color]    = "#ddaa66"
  g.node[:style]    = "filled"
  g.node[:shape]    = "box"
  g.node[:penwidth] = "1"
  g.node[:fontname] = "Trebuchet MS"
  g.node[:fontsize] = "8"
  g.node[:fillcolor]= "#ffeecc"
  g.node[:fontcolor]= "#000000"
  g.node[:margin]   = "0.05"
  
  # set global edge options
  g.edge[:color]    = "#999999"
  g.edge[:weight]   = "1"
  g.edge[:fontsize] = "6"
  g.edge[:fontcolor]= "#444444"
  g.edge[:fontname] = "Verdana"
  
  # Create two nodes
  nodesMap = {}
  @links = Array.new
  Entity.all.each do |entity|
    if entity.entity_def == nil
      next
    end
    attrs = '';
    entity.attr_vals.each do |attrVal|
      if attrVal.attrDef.dataType == "entityDef"
        #we need to add a link
        if attrVal.attrDef.childEntityDef != nil
          @links.push([attrVal.value,entity.id.to_s])
          begin
            attrs += attrVal.attrDef.label+':'+attrVal.attrDef.dataType+'="'+CGI.escapeHTML(attrVal.value)+'"\l'
          rescue
          end
        else
          attrs += '+ '+attrVal.attrDef.label+' : null\l'
        end
      else
        begin
          attrs += attrVal.attrDef.label+':'+attrVal.attrDef.dataType+'="'+CGI.escapeHTML(attrVal.value)+'"\l'
        rescue
        end
      end
    end
    begin
      nodesMap[entity.id.to_s] = g.add_nodes( entity.id.to_s, "shape" => "record", "label" => '{'+entity.label+':'+entity.entity_def.id.to_s+'|'+attrs+'}' )
      g.add_nodes( entity.id.to_s,"shape" => "record", "label" => '{'+entity.label+':'+entity.entity_def.id.to_s+'|'+attrs+'}' )
    rescue
    end
  end
  @links.each do |link|
    begin
      g.add_edges( nodesMap[link[0]], nodesMap[link[1]])
    rescue
    end
  end
  # Generate output image
  g.output( :png => uri )
end