Class: Stamina::Command::Adl2DotCommand

Inherits:
StaminaCommand show all
Defined in:
lib/stamina/command/adl2dot_command.rb

Overview

Implementation of the adl2dot command line tool

Instance Attribute Summary collapse

Attributes inherited from StaminaCommand

#description, #name, #usage

Instance Method Summary collapse

Methods inherited from StaminaCommand

#assert_readable_file, #assert_writable_file, #parse, #show_usage

Constructor Details

#initializeAdl2DotCommand

Creates a score command instance



12
13
14
15
16
# File 'lib/stamina/command/adl2dot_command.rb', line 12

def initialize
  super("adl2dot", "[options] automaton.adl",
        "Prints an automaton expressed in ADL in dot (or gif) format")
  @gif_output = false
end

Instance Attribute Details

#gif_outputObject (readonly)

Gif output instead of dot?



9
10
11
# File 'lib/stamina/command/adl2dot_command.rb', line 9

def gif_output
  @gif_output
end

Instance Method Details

#automaton_file=(file) ⇒ Object

Sets the automaton file



34
35
36
37
38
39
40
# File 'lib/stamina/command/adl2dot_command.rb', line 34

def automaton_file=(file)
  assert_readable_file(file)
  @automaton_file = file
  @automaton = Stamina::ADL.parse_automaton_file(file)
rescue Stamina::ADL::ParseError
  raise ArgumentError, "#{file} is not a valid ADL automaton file"
end

#main(argv) ⇒ Object

Executes the command



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/stamina/command/adl2dot_command.rb', line 48

def main(argv)
  parse(argv, :automaton_file)
  
  # create a file for the dot output
  if gif_output
    require 'tempfile'
    dotfile = Tempfile.new("stamina").path
  else
    dotfile = output_file
  end
  
  # Flush automaton inside it
  File.open(dotfile, 'w') do |f|
    f << Stamina::ADL.parse_automaton_file(@automaton_file).to_dot
  end
  
  # if gif output, use dot to convert it
  if gif_output
    `dot -Tgif -o #{output_file} #{dotfile}` 
  end
end

#optionsObject

Installs additional options



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stamina/command/adl2dot_command.rb', line 19

def options
  super do |opt|
    opt.on("-o", "--output=OUTPUT",
           "Flush result output file") do |value|
      assert_writable_file(value)
      @output_file = value
    end
    opt.on("-g", "--gif",
           "Generates a gif file instead of a dot one") do
      @gif_output = true
    end
  end
end

#output_fileObject

Returns the output file to use



43
44
45
# File 'lib/stamina/command/adl2dot_command.rb', line 43

def output_file
  @output_file || "#{File.basename(@automaton_file, '.adl')}.#{gif_output ? 'gif' : 'dot'}"
end