Class: Stamina::Command::Adl2dot

Inherits:
Object
  • Object
show all
Includes:
Robustness
Defined in:
lib/stamina-core/stamina/command/adl2dot.rb

Overview

Prints an automaton expressed in ADL in dot (or gif) format

SYNOPSIS

#{program_name} #{command_name} automaton.adl

OPTIONS #summarized_options

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Robustness

#assert_readable_file, #assert_writable_file

Instance Attribute Details

#output_formatObject (readonly)

Returns the value of attribute output_format.



15
16
17
# File 'lib/stamina-core/stamina/command/adl2dot.rb', line 15

def output_format
  @output_format
end

Instance Method Details

#execute(args) ⇒ Object

Command execution

Raises:

  • (Quickl::Help)


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
# File 'lib/stamina-core/stamina/command/adl2dot.rb', line 44

def execute(args)
  raise Quickl::Help unless args.size <= 1

  # Loads the target automaton
  input = if args.size == 1
    File.read assert_readable_file(args.first)
  else
    $stdin.readlines.join("\n")
  end

  begin
    automaton = Stamina::ADL::parse_automaton(input)
  rescue ADL::ParseError
    sample = Stamina::ADL::parse_sample(input)
    automaton = sample.to_pta
  end

  # create a file for the dot output
  if output_format == 'dot'
    dotfile = output_file(args.first)
  else
    require 'tempfile'
    dotfile = Tempfile.new("stamina").path
  end

  # Flush automaton inside it
  File.open(dotfile, 'w') do |f|
    f << automaton.to_dot
  end

  # if gif output, use dot to convert it
  unless output_format == 'dot'
    `dot -T#{output_format} -o #{output_file(args.first)} #{dotfile}`
  end
end

#output_file(infile) ⇒ Object

options



39
40
41
# File 'lib/stamina-core/stamina/command/adl2dot.rb', line 39

def output_file(infile)
  @output_file || "#{File.basename(infile || 'stdin.adl', '.adl')}.#{output_format}"
end