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 回答