0

移行システムでいくつかの操作を実行できるツールを作成しており、それらを視覚化する必要もあります。

ruby-gem に関するドキュメントはあまりありませんが (これは私が入手できる最高のものでした: http://www.omninerd.com/articles/Automating_Data_Visualization_with_Ruby_and_Graphviz )、移行システムからグラフを作成することができました。(自由に使用してください。サンプルコードはあまりありません。コメント/質問も大歓迎です)

# note: model is something of my own datatype, 
   # having states, labels, transitions, start_state and a name
   # I hope the code is self-explaining

@graph = GraphViz::new(model.name, "type" => "graph" )

#settings
@graph.edge[:dir]      = "forward"
@graph.edge[:arrowsize]= "0.5"

#make the graph
model.states.each do |cur_state|
  @graph.add_node(cur_state.name).label = cur_state.name

  cur_state.out_transitions.each do |cur_transition|
      @graph.add_edge(cur_transition.from.name, cur_transition.to.name).label = cur_transition.label.to_s
  end
end

#make a .pdf output (can also be changed to .eps, .png or whatever)
@graph.output("pdf" => File.join(".")+"/" + @graph.name + ".pdf")
#it's really not that hard :-)

私ができない唯一のことは、「何もないところから」開始状態に矢印を描くことです。提案は誰ですか?

4

2 に答える 2

1

形状のノードを追加するnonepoint、そこから矢印を描画しようとします。

@graph.add_node("Start", 
  "shape" => "point", 
  "label" => "" )

そして、あなたのループでこのようなもの

if cur_transition.from.name.nil?
  @graph.add_edge("Start", cur_transition.to.name)
else
  @graph.add_edge(cur_transition.from.name, cur_transition.to.name).label = cur_transition.label.to_s
end
于 2011-06-07T09:17:09.833 に答える
0

Jonas Elfström の功績によるものです。これが私のソリューションです

# note: model is something of my own datatype, 
   # having states, labels, transitions, start_state and a name
   # I hope the code is self-explaining    
@graph = GraphViz::new(model.name, "type" => "graph" )

#settings
@graph.edge[:dir]      = "forward"
@graph.edge[:arrowsize]= "0.5"

#make the graph
model.states.each do |cur_state|
  @graph.add_node(cur_state.name).label = cur_state.name

  cur_state.out_transitions.each do |cur_transition|
      @graph.add_edge(cur_transition.from.name, cur_transition.to.name).label = cur_transition.label.to_s
  end
end
#draw the arrow to the initial state (THE ADDED CODE)
@graph.add_node("Start",
  "shape" => "none",
  "label" => "" )
@graph.add_edge("Start", model.start_state.name)

#make a .pdf output (can also be changed to .eps, .png or whatever)
@graph.output("pdf" => File.join(".")+"/" + @graph.name + ".pdf")
#it's really not that hard :-)
于 2011-06-07T09:51:18.510 に答える