Ruby Graphviz - Labeled Transition System的初始状态?

时间:2011-06-07 08:07:20

标签: ruby rubygems graphviz transition-systems

我正在制作一个可以在转换系统上执行某些操作的工具,还需要将它们可视化。

虽然没有太多关于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 :-)

只有一件我不能做的事情:在开始状态下画一个“无中生有”的箭头。 建议任何人?

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

向JonasElfströ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 :-)