呼應一下 Ola Bini 的 blog Can your Ruby do this?
今天來介紹,用 ruby-uml 來畫 Java 的 UML
首先使用 JRuby 安裝 ruby-uml
$jruby -S gem i ruby-uml
require 'rubygems'
require 'uml/class_diagram'
require 'java'
include_class 'java.util.Random'
filename = "Random.dot"
puts filename
cd = UML::ClassDiagram.new :show_private_methods => false,
:show_protected_methods => false,
:show_public_methods => true,
:cluster_packages => true,
:include => [/^Java/]
cd.include Java::JavaUtil::Random
Random.new(123)
File.open( filename, 'w' ) { |file|
file.write cd.to_dot
}
此處也呼應一下 thegiive 的 JRuby 安裝方式
在上面程式碼當中,我們選擇使用 Class Diagram
:include => [/^Java/] # Java 開頭的我們才把他轉出
cd.include Java::JavaUtil::Random # 將 Random 列入觀察物件
# 以下動作會繪製出相關的 Class Diagram
# 此處我們只把 Random 物件生成的相關 Class 列出來
Random.new(123)
執行完之後,會產生 Random.dot 檔
dot 檔是一種描述階層或是有層次的指向圖形
又興趣的人可以用文字編輯器打開看看
接下來
請安裝 Graphviz
當中有個工具可以將 dot 檔轉換成圖形 (ex: png …)
在 程式集>>Graphviz>>dot

四個步驟就能把 dot 檔轉換成圖形
轉出結果如下

相關文章:(這不是打廣告 XD)
沒有留言:
張貼留言