木曜日, 12月 14, 2006

[Perl]UMLクラス図の自動生成(UML::Class::Simple)

UML続きでこんどはこれを試してみました。

http://search.cpan.org/~agent/UML-Class-Simple/


まずGraphvizをインストールしておく必要があります。

私はソースからインストールしました。(Graphviz version 2.12)

./configure
make
make install

で問題なくインストールできました。
つづいてcpanコマンドでUML::Class::Simple(0.07)をインストール。
大量のモジュールをインストールした後最後にこれをインストールしようとするがテストでこけまくる。
とりあえずforceでインストールしてみてだめだったら考えようということでインストールしてしまいました。

とりあえず以下のような簡単なクラス構成で実行してみます。
package Base;
sub new { bless {}, shift }
sub base_method {
    print "I am base\n";
    shift->protected_method();
}
1;

package Foo;
use strict;
use base 'Base';

sub new { bless {} };
sub method_foo {
    print "method_foo called\n";
    shift->base_method;
};

sub protected_method {
    print "overrided\n";
}

1;

package Hoge;
use Foo;
sub foo {
    my $self = shift;
    return $self->{foo} ||= Foo->new();
}

1;


# umlclass.pl -M Hoge -o hoge.png -p "^(Hoge|Foo|Base)$"
Error: syntax error in line 8
... ...
in label of node class_1
at /usr/bin/umlclass.pl line 102

となりエラー。。。追記(2007/04/11)作者の方が上記内容をパッチとしてアップデートしてくださいました(コメント参照)。


どうもfontタグの中身が空だからエラーを出しているんじゃないかと思い、
仕方ないのでデバッガでソースを追っていると、Graphvizのdotコマンドに渡すファイルをTTで作っていることがわかったので、その結果を一時ファイルに保存するようにコードを変えて見てみると、属性にあたるところだということが分かりました。そこで属性が無いときはfontタグを出さないように変更してやると上手く動きました。(その後操作の部分も同様に修正)

# diff Simple.pm Simple.org 
327,329d326
< open (my $fh, '>', 'dotresult.txt');
< print $fh $dot;
< close $fh;
359c356
< <td>[% IF class.properties.size > 0 %]<font color="red">
---
> <td><font color="red">
363c360
< [%- END %]</font>[% END %]</td>
---
> [%- END %]</font></td>
377c374
< <td>[% IF class.methods.size > 0 %]<font color="red">
---
> <td><font color="red">
381c378
< [%- END %]</font>[% END %]</td>
---
> [%- END %]</font></td>


結果


わりといい感じに見えますがメソッドと継承関係しか出してくれないことと、pngとかで出されても修正できないのであまりうれしくないかもしれません。
UML::Sequenceとあわせてこの辺がXMIで出力してくれたら便利なんですけどね。
誰か作ってくれないですかね。



4 件のコメント:

agentzh さんのコメント...

Hello, I'm the author of UML::Class::Simple. Thank you for trying out this module and write a review for it.

I've tried the example in your post using UML::Class::Simple 0.08 and Graphviz 2.8 and didn't find any problem. Here is the output in my ubuntu box:

====================================
$ umlclass.pl -M Hoge -o hoge.png -p "^(Hoge|Foo|Base)$"
Base
Foo
Hoge

hoge.png generated.
====================================

From the errors you described, I guess you were using a different version of Graphviz which doesn't allow <font
> tags with empty contents. No?

I'm glad to see you've also offered a patch in your journal. I'll include it in the next CPAN release (0.09).

It's possible to generate the .dot file using an unpatched umlclass.pl. Here is how:

$ umlclass.pl -M Hoge -o hoge.dot -p "^(Hoge|Foo|Base)$"

(That is, simply replacing hoge.png with hoge.dot will do.)

If you have further issues, please contact me directly. Thank you for your patience :)

agentzh さんのコメント...

Your patch has already been applied to 0.09 and it will appear on the CPAN mirror near you. Please try it again to ensure the bug is indeed fixed on your side if you have the tuits. (I can't reproduce your error here.) Thank you in advance :)

usuihiro さんのコメント...

Thank you very much for your reading my blog, and applying the patch!
I confirmed that the latest version 0.09 works for my environment ( Graphviz version 2.12 ).
I also added above things to my blog.

agentzh さんのコメント...

That's cool. Thanks :)