websequencediagrams をPerlでごにょごにょ

http://www.websequencediagrams.com というサイトを知ってますか?ZIGOROuさんがブログで使っていたので知ったんですが、実に便利っぽいです。専用のダイアグラム構文を書いてサイトにHTTP POSTするとPNGやPDFでシーケンス図を生成して返してくれる代物です。

構文自体とてもシンプルです。

Alice->Bob: Authentication Request
Bob->Alice: Authentication Response

こんな感じでとても簡単&スマートなものなんですが、さらにこいつをプログラマブルに書けたら便利かなー、なんて思ってしまい、Perlでモジュール化してみました。

WebService::WebSequenceDiagrams

github: http://github.com/miki/WebService-WebSequenceDiagrams

使い方

こんな感じです。

use strict;
use warnings;
use WebService::WebSequenceDiagrams;

my $wsd = WebService::WebSequenceDiagrams->new;
$wsd->participant( name => 'Bob' );
$wsd->participant( name => 'Alice' );
$wsd->participant(
    name => 'I have a really\nlong name',
    as   => 'L'
);
$wsd->signal(
    from => 'Alice',
    to   => 'Bob',
    text => 'Authentication Request',
);
$wsd->signal(
    from => 'Bob',
    to   => 'Alice',
    text => 'Authentication Response',
);
$wsd->signal(
    from => 'Bob',
    to   => 'L',
    text => 'Log transaction',
);
$wsd->draw(
    style   => "omegapple",
    outfile => "out.png"
);


他にも色々なスタイルで絵が作れます。

use strict;
use warnings;
use WebService::WebSequenceDiagrams;

my $wsd = WebService::WebSequenceDiagrams->new;
$wsd->participant( name => "Alice" );
$wsd->participant( name => "Bob" );
$wsd->note(
    position => "left_of",
    name     => "Alice",
    text     => "This is displayed\nleft of Alice"
);
$wsd->note(
    position => "right_of",
    name     => "Alice",
    text     => "This is displayed right of Alice."
);
$wsd->note(
    position => "over",
    name     => "Alice",
    text     => "This is displayed over Alice."
);
$wsd->note(
    position => "over",
    name     => [ "Alice", "Bob" ],
    text     => "This is displayed over Bob and Alice."
);
$wsd->draw(
    style   => 'roundgreen',
    outfile => "out.png"
);

こんなのも出来ます。

use strict;
use warnings;
use WebService::WebSequenceDiagrams;

my $wsd = WebService::WebSequenceDiagrams->new;
$wsd->signal(
    from => "User",
    to   => "A",
    text => "DoWork"
);
$wsd->activate("A");
$wsd->signal(
    from => "A",
    to   => "B",
    text => "<<createRequest>>"
);
$wsd->activate("B");
$wsd->signal(
    from => "B",
    to   => "C",
    text => "DoWork"
);
$wsd->activate("C");
$wsd->signal(
    from => "C",
    to   => "B",
    text => "WorkDone"
);
$wsd->destroy("C");
$wsd->signal(
    from => "B",
    to   => "A",
    text => "RequestCreated"
);
$wsd->deactivate("B");
$wsd->signal(
    from => "A",
    to   => "User",
    text => "Done"
);
$wsd->draw(
    style   => 'modern-blue',
    outfile => "out/ex6.png"
);

構文を直接入出力

オリジナルの構文をそのまま入出力することも可能です。

use strict;
use warnings;
use WebService::WebSequenceDiagrams;

my $wsd = WebService::WebSequenceDiagrams->new;

my $message =<<"END";
Alice->Bob: Authentication Request
alt successful case
	Bob->Alice: Authentication Accepted
else some kind of failure
	Bob->Alice: Authentication Failure
	opt 
		loop 1000 times
			Alice->Bob: DNS Attack
		end
	end
else Another type of failure
	Bob->Alice: Please repeat
end
END

$wsd->message($message);

$wsd->draw(
    style   => 'modern-blue',
    outfile => "out.png"
);

いちいちプログラマブルに書くのは面倒だよ、という場合はこの方法が楽です。
ちなみに$wsd->message()で出力もできます。



簡単&奇麗なので、ぜひ使ってみてください。