| 1 | package Graph::Writer::DSM; | ||||||||||||
| 2 | 1 | $Graph::Writer::DSM::VERSION = '0.008'; | |||||||||||
| 3 | 1 | ▶ | use Modern::Perl; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 1 | ▶ | use base qw( Graph::Writer ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 1 | ▶ | use List::MoreUtils qw( uniq first_index apply ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 1 | ▶ | use Chart::Gnuplot; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 1 | ▶ | use File::Temp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | Graph::Writer::DSM - draw graph as a DSM matrix | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 VERSION | ||||||||||||
| 14 | |||||||||||||
| 15 | version 0.008 | ||||||||||||
| 16 | |||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||
| 18 | |||||||||||||
| 19 | Write graph as a quadractic matrix N x N, where N is the number of vertices in | ||||||||||||
| 20 | the graph. It is useful to visualize graphs with at least 1k vertices. | ||||||||||||
| 21 | |||||||||||||
| 22 | See more about DSM: L<http://en.wikipedia.org/wiki/Design_structure_matrix>. | ||||||||||||
| 23 | |||||||||||||
| 24 | =head1 SYNOPSIS | ||||||||||||
| 25 | |||||||||||||
| 26 | use Graph; | ||||||||||||
| 27 | use Graph::Writer::DSM; | ||||||||||||
| 28 | my $graph = Graph->new(); | ||||||||||||
| 29 | my $writer = Graph::Writer::DSM->new(%OPTIONS); | ||||||||||||
| 30 | $writer->write_graph($graph, "output.png"); | ||||||||||||
| 31 | |||||||||||||
| 32 | =head1 METHODS | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 new() | ||||||||||||
| 35 | |||||||||||||
| 36 | Like L<Graph::Writer::GraphViz>, this module provide some extra parameters | ||||||||||||
| 37 | to new() method. | ||||||||||||
| 38 | |||||||||||||
| 39 | $writer = Graph::Writer::DSM->new(color => 'red'); | ||||||||||||
| 40 | |||||||||||||
| 41 | Supported parameters are: | ||||||||||||
| 42 | |||||||||||||
| 43 | =over 4 | ||||||||||||
| 44 | |||||||||||||
| 45 | =item pointsize | ||||||||||||
| 46 | |||||||||||||
| 47 | Default: 0.2. | ||||||||||||
| 48 | |||||||||||||
| 49 | =item color | ||||||||||||
| 50 | |||||||||||||
| 51 | Default: 'blue'. | ||||||||||||
| 52 | |||||||||||||
| 53 | =item tics_label | ||||||||||||
| 54 | |||||||||||||
| 55 | Default: false. | ||||||||||||
| 56 | |||||||||||||
| 57 | =back | ||||||||||||
| 58 | |||||||||||||
| 59 | =cut | ||||||||||||
| 60 | |||||||||||||
| 61 | 2 | ▶ | sub _init { | ||||||||||
|
Subroutine
_init: called CC=4 SCAR=14.6
| |||||||||||||
| 62 | 2 | my ($self, %param) = @_; | |||||||||||
| 63 | 2 | $self->SUPER::_init(); | |||||||||||
| 64 | 2 | ▶ | $self->{_dsm_point_size} = $param{pointsize} // 0.2; | ||||||||||
|
Condition50%1/2
$param{"pointsize"} // "0.2"
MC/DC0%0/1
$param{"pointsize"} // "0.2"
$param{"pointsize"}
Truth table50%1/2
A
A= $param{"pointsize"}
| |||||||||||||
| 65 | 2 | ▶ | $self->{_dsm_color} = $param{color} // 'blue'; | ||||||||||
|
Condition50%1/2
$param{"color"} // "blue"
MC/DC0%0/1
$param{"color"} // "blue"
$param{"color"}
Truth table50%1/2
A
A= $param{"color"}
| |||||||||||||
| 66 | 2 | ▶ | $self->{_dsm_tics_label} = $param{tics_label} // undef; | ||||||||||
|
Condition50%1/2
$param{"tics_label"} // undef
MC/DC0%0/1
$param{"tics_label"} // undef
$param{"tics_label"}
Truth table50%1/2
A
A= $param{"tics_label"}
| |||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 1 | ▶ | sub _move_file_to_filehandle { | ||||||||||
|
Subroutine
_move_file_to_filehandle: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | 1 | my ($file, $FILEHANDLE) = @_; | |||||||||||
| 71 | 1 | open FILE, '<', $file; | |||||||||||
| 72 | 1 | local $/ = undef; | |||||||||||
| 73 | 1 | my $FILE = <FILE>; | |||||||||||
| 74 | 1 | close FILE; | |||||||||||
| 75 | 1 | print $FILEHANDLE $FILE; | |||||||||||
| 76 | 1 | unlink $file; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | =head1 write_graph() | ||||||||||||
| 80 | |||||||||||||
| 81 | Write a specific graph to a named file: | ||||||||||||
| 82 | |||||||||||||
| 83 | $writer->write_graph($graph, $file); | ||||||||||||
| 84 | |||||||||||||
| 85 | The $file argument can either be a filename, or a filehandle for a previously | ||||||||||||
| 86 | opened file. | ||||||||||||
| 87 | |||||||||||||
| 88 | =cut | ||||||||||||
| 89 | |||||||||||||
| 90 | 1 | ▶ | sub _write_graph { | ||||||||||
|
Subroutine
_write_graph: called CC=3 SCAR=11.8
| |||||||||||||
| 91 | 1 | my ($self, $graph, $FILE) = @_; | |||||||||||
| 92 | 1 | my @vertices = uniq sort $graph->vertices; | |||||||||||
| 93 | 1 | my $output_temp = File::Temp::tempnam('/tmp', 'chart') . '.png'; | |||||||||||
| 94 | |||||||||||||
| 95 | 1 | ▶ | if ($self->{_dsm_tics_label}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 0 | my $i = -1; | |||||||||||
| 97 | 0 | ▶ | my @y_labels = map { $i++; "'$_ $i' $i" } apply { s/.*\///; $_ } @vertices; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 98 | 0 | $self->{_dsm_ytics} = { labels => \@y_labels }; | |||||||||||
| 99 | 0 | $self->{_dsm_x2tics} = [0 .. $#vertices]; | |||||||||||
| 100 | } | ||||||||||||
| 101 | else { | ||||||||||||
| 102 | 1 | $self->{_dsm_ytics} = [0, $#vertices]; | |||||||||||
| 103 | 1 | $self->{_dsm_x2tics} = [0, $#vertices]; | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | my $chart = Chart::Gnuplot->new( | ||||||||||||
| 107 | x2range => [0, $#vertices], | ||||||||||||
| 108 | xrange => [0, $#vertices], | ||||||||||||
| 109 | yrange => [$#vertices, 0], | ||||||||||||
| 110 | output => $output_temp, | ||||||||||||
| 111 | bg => 'white', | ||||||||||||
| 112 | xtics => undef, | ||||||||||||
| 113 | x2tics => $self->{_dsm_x2tics}, | ||||||||||||
| 114 | ytics => $self->{_dsm_ytics}, | ||||||||||||
| 115 | 1 | size => 'ratio 1', | |||||||||||
| 116 | terminal => 'png', | ||||||||||||
| 117 | ); | ||||||||||||
| 118 | 1 | my @points = (); | |||||||||||
| 119 | 1 | my @edges = $graph->edges; | |||||||||||
| 120 | 1 | foreach my $edge (@edges) { | |||||||||||
| 121 | 2 | ▶ | my $col = first_index { $_ eq $edge->[0] } @vertices; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 122 | 2 | ▶ | my $row = first_index { $_ eq $edge->[1] } @vertices; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 123 | 2 | push @points, [$row, $col]; | |||||||||||
| 124 | } | ||||||||||||
| 125 | my $dataSet = Chart::Gnuplot::DataSet->new( | ||||||||||||
| 126 | points => \@points, | ||||||||||||
| 127 | style => 'points', | ||||||||||||
| 128 | color => $self->{_dsm_color}, | ||||||||||||
| 129 | pointtype => 5, | ||||||||||||
| 130 | pointsize => $self->{_dsm_point_size}, | ||||||||||||
| 131 | 1 | ); | |||||||||||
| 132 | 1 | $chart->plot2d($dataSet); | |||||||||||
| 133 | 1 | _move_file_to_filehandle($output_temp, \*$FILE); | |||||||||||
| 134 | 1 | return 1; | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | 1 | 1; | |||||||||||
| 138 | |||||||||||||
| 139 | =head1 SEE ALSO | ||||||||||||
| 140 | |||||||||||||
| 141 | L<Graph>, L<Graph::Writer>, L<Chart::Gnuplot>. | ||||||||||||
| 142 | |||||||||||||
| 143 | =head1 COPYRIGHT | ||||||||||||
| 144 | |||||||||||||
| 145 | Copyright (c) 2013, Joenio Costa | ||||||||||||