Class: Mkgraph

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

Overview

Example : Mkgraph#new <ruby.rb> - initialise

Mkgraph#run           - process file
Mkgraph#make_image    - create the image file
Mkgraph#show_image    - display the image file

Arguments: The file to be visualised when creating a new instance

Note : A valid mgopts.yml must be in the working directory.

You could copy a template file from the gem installation directory.

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ Mkgraph

Initialise

Params: The name of the file to be processed



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/mkgraph.rb', line 34

def initialize pattern

  begin
    $yml = YAML.load_file 'mgopts.yml'
  rescue Exception => e
    print e.message + "\n"
    print "You could use the default \'mgopts.yml\' file in the installation\n"
    print "directory as a template.\n"
    print "Example:\n"

    tip = <<-EOS
      Log Options:
        format: brief   # brief or full
        output: stderr  # stderr, stdout or file name
        level: WARN     # INFO, WARN or FATAL
      Labels:
        root: Entry Point
        root comment: The Root Comment
      Output:
        image name: view.png      
    EOS

    puts "#{tip}"
    exit 1
  end

  # puts $yml['sectionA']['user'] + "**************\n"
  # puts $yml['sectionB']['file'] + "**************\n"
  log_fmt = $yml['Log Options']['format']

  log_opt = $yml['Log Options']['output']
  case log_opt
  when 'stderr'
    log_op = $stderr
  when 'stdout'
    log_op = $stdout
  else
    log_op = log_opt
  end

  loglev = $yml['Log Options']['level']
  case loglev
  when 'INFO'
    ll = Logger::INFO
  when 'WARN'
    ll = Logger::WARN
  else
    ll = Logger::FATAL
  end

  @file_pattern = pattern

  $LOG = Logger.new(log_op).tap do |log|
    log.progname = 'mkgraph'
    log.level = ll # Logger::INFO # $yml['Log Options']['level']
    if log_fmt == 'brief'
      log.formatter = proc do |severity, datetime, progname, msg|
        "#{progname}: #{msg}\n"
      end
    end
  end

  $LOG.info "Pattern: " + @file_pattern
  # Rough test for OS TODO
  if RUBY_PLATFORM.include?("linux") == false
    $windows = true
  else
    $windows = false
  end
end

Instance Method Details

#make_imageObject

Use Graphviz to create the image file



278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/mkgraph.rb', line 278

def make_image 
  g = GraphViz.new( :G, :rankdir => "TB", :type => :digraph, :bgcolor => "lightgrey", :nodesep => 0.85, :normalize => true, :concentrate => true)
  root = $root_node
  g = add_element g, root
  if $windows != true
    res = system("rm " + $iname + " > /dev/null")
  end
  begin
    g.output( :png => $iname)
  rescue Exception => e
    $LOG.warn e.message + "\n"
  end
end

#runObject

Does the work. Params: None



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
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
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/mkgraph.rb', line 109

def run
  end_count           = 0
  end_expected        = 0            # how many ends before class end
  class_end_expected  = 0            # how many class ends we expect (internal definitions)
  cname               = ""
  in_class            = false
  parent_class_name   = nil
  print_parent        = true

  $iname = $yml['Output']['image name']

  $root_node = Tree::TreeNode.new($yml['Labels']['root'], $yml['Labels']['root comment'])
  tnode = $root_node

  # Process all file matching @file_pattern
  Dir.glob(@file_pattern) do |file|

    $LOG.info "Looking at file - " + file
    ignore = false

    next if file == '.' or file == '..'

    # Process each line in the file
    File.open(file) do |f|
      f.each_line do |line|

        # Ignore =begin/=end blocks
        if line.match(/^=end/) != nil
          ignore = false
        elsif line.match(/^=begin/) != nil
          ignore = true
        else
          nil
        end

        next if ignore == true

        # Process the line read from file
        case line
        when /^\s*#/
          nil
        when /\s*when.*new/     # only to avoid this situation HERE
          nil
        when /\'\.new/          # only to avoid the situation in line.split('.new')
          nil
        when /\.new/
          name = line.split('.new')
          cname = name[0].split.last
          if in_class == true
            $LOG.debug "NEW INSTANCE OF CLASS = [" + cname + "] created in class " + parent_class_name
            tnode = proc_node tnode, parent_class_name, cname, false, false
          else
            if true == print_parent
              $LOG.debug "NEW INSTANCE OF CLASS = [" + cname + "] created outside any parent class"
              tnode = proc_node tnode, parent_class_name, cname, true, true
            end
          end
        when /^\s*elsif/
          nil
        when /^\s*class[\s]+/
          parent_class_name = line.split(' ')[1]
          class_end_expected += 1
          in_class = true
          if print_parent == true
            $LOG.info "Start of class " + parent_class_name
          end
          tnode = proc_node tnode, $root_node.name, parent_class_name, true, false
        when /^\s*begin/
          end_expected += 1
        when /^.*if /
          end_expected += 1
        when /^\s*def /
          end_expected += 1
        when /^.* do/
          end_expected += 1
        when /^\s*case/
          end_expected += 1
        when /^\s*end[\s.]/
          if end_expected == 0
            if class_end_expected > 0
              class_end_expected -= 1
              in_class = false
              if print_parent == true && parent_class_name != nil
                $LOG.info "End of class " + parent_class_name
              end
              parent_class_name = nil
            end
          else
            end_expected -= 1
          end
        end      # case
      end
    end
  end    
end

#show_imageObject

Display the image file



293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/mkgraph.rb', line 293

def show_image 
  begin
    if $windows != true
      res = system("pkill eog; " + $yml['Osdep']['viewer'] + " " + $iname + " > /dev/null 2>&1")
    else
      res = system("call " + $yml['Osdep']['viewer'] + " " + $iname)
    end
    if res == false
      raise "Bad Command"
    end
  rescue Exception => e
    $LOG.info e.message
  end
end