Permalink
Browse files

added missing files

  • Loading branch information...
1 parent d14b57d commit 3934a0902a278bc20c590c223d9d2e8b1ec5382a @slagyr committed Sep 29, 2010
View
2 .gitignore
@@ -4,6 +4,8 @@ doc/website/webgen.cache
*.iml
generate_tests/java/turnstile/
generate_tests/java/turnstile2/
+generate_tests/dot_graph/turnstile/
+generate_tests/dot_graph/turnstile2/
out/
test_dir/
pkg/
View
127 lib/statemachine/generate/dot_graph/dot_graph_statemachine.rb
@@ -0,0 +1,127 @@
+require 'statemachine/generate/util'
+require 'statemachine/generate/src_builder'
+
+module Statemachine
+ class Statemachine
+
+ attr_reader :states
+
+ def to_dot(options = {})
+ generator = Generate::DotGraph::DotGraphStatemachine.new(self, options)
+ generator.generate!
+ end
+
+ end
+
+ module Generate
+ module DotGraph
+
+ class DotGraphStatemachine
+
+ include Generate::Util
+
+ def initialize(sm, options)
+ @sm = sm
+ @output_dir = options[:output]
+ raise "Please specify an output directory. (:output => 'where/you/want/your/code')" if @output_dir.nil?
+ raise "Output dir '#{@output_dir}' doesn't exist." if !File.exist?(@output_dir)
+ end
+
+ def generate!
+ explore_sm
+ save_output(src_file("main"), build_full_graph)
+ @sm.states.values.each do |state|
+ save_output(src_file("#{state.id}"), build_state_graph(state))
+ end
+ end
+
+ private
+
+ def explore_sm
+ @nodes = []
+ @transitions = []
+ @sm.states.values.each { |state|
+ state.transitions.values.each { |transition|
+ @nodes << transition.origin_id
+ @nodes << transition.destination_id
+ @transitions << transition
+ }
+ }
+ @nodes = @nodes.uniq
+ end
+
+ def build_full_graph
+ builder = Generate::SrcBuilder.new
+
+ add_graph_header(builder, "main")
+
+ @nodes.each { |node| add_node(builder, node) }
+ builder << endl
+
+ @transitions.each do |transition|
+ add_transition(builder, transition)
+ end
+
+ add_graph_footer(builder)
+
+ return builder.to_s
+ end
+
+ def build_state_graph(state)
+ builder = Generate::SrcBuilder.new
+
+ add_graph_header(builder, state.id)
+
+ state.transitions.values.each do |transition|
+ add_transition(builder, transition)
+ end
+
+ add_graph_footer(builder)
+
+ return builder.to_s
+ end
+
+ def add_graph_header(builder, graph_name)
+ builder << "digraph #{graph_name} {" << endl
+ builder.indent!
+ end
+
+ def add_graph_footer(builder)
+ builder.undent!
+ builder << "}" << endl
+ end
+
+ def add_node(builder, node)
+ builder << node
+ builder << " [ href = \"#{node}.svg\"]"
+ builder << endl
+ end
+
+ def add_transition(builder, transition)
+ builder << transition.origin_id
+ builder << " -> "
+ builder << transition.destination_id
+ builder << " [ "
+ builder << "label = #{transition.event} "
+ builder << "]"
+ builder << endl
+ end
+
+ def src_file(name)
+ return name if @output_dir.nil?
+ path = @output_dir
+ answer = File.join(path, "#{name}.dot")
+ return answer
+ end
+
+ def save_output(filename, content)
+ if @output_dir.nil?
+ say "Writing to file: #{filename}"
+ else
+ create_file(filename, content)
+ end
+ end
+ end
+ end
+ end
+end
View
27 spec/generate/dot_graph/dot_graph_stagemachine_spec.rb
@@ -0,0 +1,27 @@
+require File.dirname(__FILE__) + '/../../spec_helper'
+require 'statemachine/generate/dot_graph/dot_graph_statemachine'
+
+describe Statemachine::Statemachine, "(Turn Stile)" do
+ include TurnstileStatemachine
+
+ before(:each) do
+ remove_test_dir("dot")
+ @output = test_dir("dot")
+ create_turnstile
+ end
+
+ it "should output to console when no output dir provided" do
+ # Note - this test doesn't verify output to the console, but it does
+ # ensure that the to_dot call does not fail if not output is provided.
+ @sm.to_dot
+ end
+
+ it "should generate a basic graph declaration" do
+ @sm.to_dot(:output => @output)
+
+ dot = load_lines(@output, "main.dot")
+
+ dot.should_not equal(nil)
+ dot[0].include?("digraph").should == true
+ end
+end

0 comments on commit 3934a09

Please sign in to comment.