LoginSignup
13
10

More than 5 years have passed since last update.

railroadyを利用してAASMの状態遷移図をGraphvizで出力する

Last updated at Posted at 2014-04-02

AASMでコーディングされた以下のコードをGraphvizでPNG出力してみます

cpu_state.rb
require 'aasm'
class CPUState
  include AASM
  aasm do
    state :new, :initial => true
    state :ready
    state :waiting
    state :running
    state :terminated

    event :wake_up do
      transitions :from => :new,     :to => :ready
      transitions :from => :waiting, :to => :ready
    end
    event :schedule do
      transitions :from => :ready, :to => :running
    end
    event :preempt do
      transitions :from => :running, :to => :ready
    end
    event :yield do
      transitions :from => :running, :to => :waiting
    end
    event :terminate do
      transitions :from => :running, :to => :terminated
    end
  end
end

準備

Gemfile
gem "railroady"
Rakefile
namespace "doc" do
  desc "print dot eg) | dot -Tpng -o aasm.png"
  task "dot" do
    require 'railroady/options_struct'
    require 'railroady/aasm_diagram'
    a = AasmDiagram.new
    require "./cpu_state"
    a.send(:process_aasm_class, CPUState)
    puts a.instance_variable_get(:@graph).to_dot
  end
end

実行

$ bundle exec rake doc:dot | dot -Tpng -o cpu_state.png

※AASM 4.0以降の場合deprecatedが多発しますが、railroadyが悪いのです。:-p)

cpu_state.png

あとがき

railroadyはRailsプロジェクトを対象にしており、単純にAASMで組まれたコードを出力するのが難しいです。

この方法で一応出力はできます。private methodですので、こういう呼び出し方はいかがなものかと思いますが。というか、もっと汎用的に使えるようにrailroadyの作者に提案はしてみたいと思います。

13
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
10