2011-06-07 23 views
0

我正在製作一個工具,可以在轉換系統上執行一些操作,並且還需要對它們進行可視化。Ruby Graphviz - 標籤過渡系統的初始狀態?

雖然沒有太多關於紅寶石 (這是最好的,我可以得到: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 :-) 

只有一兩件事,我不能做:畫一個箭頭「無中生有」的開始狀態。 建議任何人?

回答

0

學分喬納斯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 :-) 
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 
+0

太好了!這正是我所期待的,不需要添加任何東西到我的循環中。我將發佈我的新代碼作爲答案 – SirLenz0rlot 2011-06-07 09:48:04