| 1 | package Map::Tube::Plugin::Graph::Utils; | ||
| 2 | |||
| 3 | 1 | ▶ | use version; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = qv('v1.2.1'); | ||
| 6 | our $AUTHORITY = 'cpan:MANWAR'; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Map::Tube::Plugin::Graph::Utils - Helper package for Map::Tube::Plugin::Graph. | ||
| 11 | |||
| 12 | =head1 VERSION | ||
| 13 | |||
| 14 | Version v1.2.1 | ||
| 15 | |||
| 16 | =cut | ||
| 17 | |||
| 18 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 22 | # use Data::Dumper; | ||
| 23 | 1 | ▶ | use File::Which; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 24 | 1 | ▶ | use GraphViz2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 25 | use IPC::Run3; | ||
| 26 | use Map::Tube::Utils qw(is_valid_color); | ||
| 27 | use Map::Tube::Exception::MissingLineName; | ||
| 28 | use Map::Tube::Exception::InvalidLineName; | ||
| 29 | use Map::Tube::Exception::InvalidColorName; | ||
| 30 | use Map::Tube::Exception::InvalidColorHexCode; | ||
| 31 | use parent 'Exporter'; | ||
| 32 | |||
| 33 | our @EXPORT_OK = qw(graph_line_image graph_map_image get_graphviz_supported); | ||
| 34 | |||
| 35 | our $STYLE = 'dashed'; | ||
| 36 | our $NODE_COLOR = 'black'; | ||
| 37 | our $EDGE_COLOR = 'brown'; | ||
| 38 | our $SHAPE = 'oval'; | ||
| 39 | our $DIRECTED = 1; | ||
| 40 | our $ARROWSIZE = 1; | ||
| 41 | our $LABELLOC = 'top'; | ||
| 42 | our $BGCOLOR = 'grey'; | ||
| 43 | |||
| 44 | =head1 DESCRIPTION | ||
| 45 | |||
| 46 | B<FOR INTERNAL USE ONLY> | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub graph_line_image { | ||
| 51 | my ($map, $line_name, %args) = @_; | ||
| 52 | $args{format} ||= 'png'; | ||
| 53 | $args{format} = lc($args{format}); | ||
| 54 | if ( exists $args{output_file} && !defined $args{output_file} ) { | ||
| 55 | $args{output_file} = $map->name() . '_' . $line_name . '.' . $args{format}; | ||
| 56 | $args{output_file} =~ s/[^\w.]+/_/g; | ||
| 57 | } | ||
| 58 | |||
| 59 | my @caller = caller(0); | ||
| 60 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 61 | |||
| 62 | Map::Tube::Exception::MissingLineName->throw({ | ||
| 63 | method => __PACKAGE__."::graph_line_image", | ||
| 64 | message => "ERROR: Missing Line name.", | ||
| 65 | filename => $caller[1], | ||
| 66 | line_number => $caller[2] }) | ||
| 67 | unless defined $line_name; | ||
| 68 | |||
| 69 | my $line = $map->_get_line_object_by_name($line_name); | ||
| 70 | Map::Tube::Exception::InvalidLineName->throw({ | ||
| 71 | method => __PACKAGE__."::_validate_param", | ||
| 72 | message => "ERROR: Invalid Line name [$line_name].", | ||
| 73 | filename => $caller[1], | ||
| 74 | line_number => $caller[2] }) | ||
| 75 | unless defined $line; | ||
| 76 | |||
| 77 | my $color = $EDGE_COLOR; | ||
| 78 | $color = $line->color if defined $line->color; | ||
| 79 | $line_name = $line->name; | ||
| 80 | my $bgcolor = $map->bgcolor; | ||
| 81 | $bgcolor = _graph_bgcolor($color) unless defined $bgcolor; | ||
| 82 | my $overlap = exists $args{overlap} ? !!$args{overlap} : 0; | ||
| 83 | |||
| 84 | my $g = $map->as_graph; | ||
| 85 | $g->set_graph_attribute(graphviz => { | ||
| 86 | edge => { color => $color, | ||
| 87 | arrowsize => $ARROWSIZE }, | ||
| 88 | node => { shape => $SHAPE }, | ||
| 89 | graph => { label => _graph_line_label($line_name, $map->name), | ||
| 90 | labelloc => $LABELLOC, | ||
| 91 | bgcolor => $bgcolor, | ||
| 92 | overlap => $overlap, | ||
| 93 | } | ||
| 94 | }); | ||
| 95 | my $skip = $map->{skip}; | ||
| 96 | $g->filter_edges(sub { !exists $skip->{$_[3]}{$_[1]}{$_[2]} }) if defined $skip; | ||
| 97 | my @line_v = $g->copy->filter_edges(sub { $_[3] eq $line_name}) | ||
| 98 | ->filter_vertices(sub { !$_[0]->is_isolated_vertex($_[1]) })->vertices; | ||
| 99 | my %line = map +($_=>undef), @line_v; | ||
| 100 | my %neighbour; | ||
| 101 | @neighbour{ map { $g->successors($_) } @line_v } = (); | ||
| 102 | delete @neighbour{ @line_v }; | ||
| 103 | $g->filter_vertices(sub { exists $line{$_[1]} || exists $neighbour{$_[1]} }); | ||
| 104 | for my $v(@line_v) { | ||
| 105 | my $attrs = $g->get_vertex_attributes($v) // { }; | ||
| 106 | $attrs->{graphviz}{color} = $attrs->{graphviz}{fontcolor} = $color; | ||
| 107 | $g->set_vertex_attribute($v, graphviz=>$attrs->{graphviz}); | ||
| 108 | } | ||
| 109 | my %seen; | ||
| 110 | $g->filter_edges(sub { | ||
| 111 | return 0 if exists $neighbour{$_[1]}; # zap if from is neighbour | ||
| 112 | return 0 if exists $line{$_[1]} and exists $line{$_[2]} and $_[3] ne $line_name; | ||
| 113 | return 0 if $_[3] ne $line_name and $seen{$_[1]}{$_[2]}++; | ||
| 114 | $g->set_edge_attribute_by_id(@_[1..3], graphviz=>{style => $STYLE}) | ||
| 115 | if $_[3] ne $line_name; | ||
| 116 | 1; # keep | ||
| 117 | }); | ||
| 118 | my $gvgraph = GraphViz2->from_graph($g); | ||
| 119 | my $output; | ||
| 120 | if ( lc($args{format}) eq 'gv' ) { | ||
| 121 | if ( exists($args{output_file}) ) { | ||
| 122 | open( my $outf, '>', $args{output_file} ) or croak "Cannot write output file '$args{output_file}': $!"; | ||
| 123 | print $outf $gvgraph->dot_input(); | ||
| 124 | close($outf); | ||
| 125 | } | ||
| 126 | $output = $gvgraph->dot_input(); | ||
| 127 | } else { | ||
| 128 | $output = $gvgraph->run(%args)->dot_output(); | ||
| 129 | } | ||
| 130 | return ( $output, $args{output_file} ); | ||
| 131 | } | ||
| 132 | |||
| 133 | sub graph_map_image { | ||
| 134 | my ($map, %args) = @_; | ||
| 135 | $args{format} ||= 'png'; | ||
| 136 | $args{format} = lc($args{format}); | ||
| 137 | if ( exists $args{output_file} && !defined $args{output_file} ) { | ||
| 138 | $args{output_file} = $map->name() . '.' . $args{format}; | ||
| 139 | $args{output_file} =~ s/[^\w.]+/_/g; | ||
| 140 | } | ||
| 141 | my $bgcolor = $map->bgcolor; | ||
| 142 | $bgcolor = $BGCOLOR unless defined $bgcolor; | ||
| 143 | my $overlap = exists $args{overlap} ? !!$args{overlap} : 0; | ||
| 144 | |||
| 145 | my $g = $map->as_graph; | ||
| 146 | $g->set_graph_attribute(graphviz => { | ||
| 147 | node => { shape => $SHAPE, color => $NODE_COLOR, fontcolor => $NODE_COLOR }, | ||
| 148 | edge => { arrowsize => $ARROWSIZE }, | ||
| 149 | graph => { label => _graph_map_label($map->name), | ||
| 150 | labelloc => $LABELLOC, | ||
| 151 | bgcolor => $bgcolor, | ||
| 152 | overlap => $overlap, | ||
| 153 | }, | ||
| 154 | global => { name => 'Map_Tube', }, | ||
| 155 | }); | ||
| 156 | my $l2c = $g->get_graph_attribute('line2colour'); | ||
| 157 | for my $v ($g->vertices) { | ||
| 158 | my %lines; @lines{map $g->get_multiedge_ids(@$_), $g->edges_at($v)} = (); | ||
| 159 | next if keys %lines != 1; | ||
| 160 | my $l = (keys %lines)[0]; | ||
| 161 | next unless defined (my $color = $l2c->{$l}); | ||
| 162 | my $attrs = $g->get_vertex_attributes($v); | ||
| 163 | $attrs->{graphviz}{color} = $attrs->{graphviz}{fontcolor} = $color; | ||
| 164 | $g->set_vertex_attribute($v, graphviz=>$attrs->{graphviz}); | ||
| 165 | } | ||
| 166 | my %seen; | ||
| 167 | $g->filter_edges(sub { !$seen{$_[1]}{$_[2]}++ }); | ||
| 168 | my $gvgraph = GraphViz2->from_graph($g); | ||
| 169 | my $output; | ||
| 170 | if ( lc($args{format}) eq 'gv' ) { | ||
| 171 | if ( exists($args{output_file}) ) { | ||
| 172 | open( my $outf, '>', $args{output_file} ) or croak "Cannot write output file '$args{output_file}': $!"; | ||
| 173 | print $outf $gvgraph->dot_input(); | ||
| 174 | close($outf); | ||
| 175 | } | ||
| 176 | $output = $gvgraph->dot_input(); | ||
| 177 | } else { | ||
| 178 | $output = $gvgraph->run(%args)->dot_output(); | ||
| 179 | } | ||
| 180 | return ( $output, $args{output_file} ); | ||
| 181 | } | ||
| 182 | |||
| 183 | sub get_graphviz_supported { | ||
| 184 | my ($what) = @_; | ||
| 185 | my $dot_bin = which('dot'); | ||
| 186 | return unless $dot_bin; | ||
| 187 | my $dot_out; | ||
| 188 | run3( [ $dot_bin, $what ], \undef, \undef, \$dot_out ); | ||
| 189 | return unless $dot_out =~ /Use one of:/s; | ||
| 190 | $dot_out =~ s/.*Use one of:\s*//s; | ||
| 191 | return split( /\s+/, $dot_out ); | ||
| 192 | } | ||
| 193 | |||
| 194 | # | ||
| 195 | # | ||
| 196 | # PRIVATE METHODS | ||
| 197 | |||
| 198 | sub _graph_line_label { | ||
| 199 | my ($line_name, $map_name) = @_; | ||
| 200 | |||
| 201 | $map_name = '' unless defined $map_name; | ||
| 202 | return sprintf("%s Map: %s Line (Generated by Map::Tube::Plugin::Graph %s at %s)", | ||
| 203 | $map_name, $line_name, $Map::Tube::Plugin::Graph::VERSION, _graph_timestamp()); | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _graph_map_label { | ||
| 207 | my ($map_name) = @_; | ||
| 208 | |||
| 209 | $map_name = '' unless defined $map_name; | ||
| 210 | return sprintf("%s Map (Generated by Map::Tube::Plugin::Graph %s at %s)", | ||
| 211 | $map_name, $Map::Tube::Plugin::Graph::VERSION, _graph_timestamp()); | ||
| 212 | } | ||
| 213 | |||
| 214 | sub _graph_timestamp { | ||
| 215 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime(time); | ||
| 216 | return sprintf("%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec); | ||
| 217 | } | ||
| 218 | |||
| 219 | # TODO: Unfinished work, still not getting the right combination. | ||
| 220 | sub _graph_bgcolor { | ||
| 221 | my ($color) = @_; | ||
| 222 | |||
| 223 | unless ($color =~ /^#(..)(..)(..)$/) { | ||
| 224 | my $hexcode = Map::Tube::Utils::is_valid_color($color); | ||
| 225 | unless ($hexcode) { | ||
| 226 | my @caller = caller(0); | ||
| 227 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 228 | |||
| 229 | Map::Tube::Exception::InvalidColorName->throw({ | ||
| 230 | method => __PACKAGE__."::_graph_bgcolor", | ||
| 231 | message => "ERROR: Invalid Color Name [$color].", | ||
| 232 | filename => $caller[1], | ||
| 233 | line_number => $caller[2] }); | ||
| 234 | } | ||
| 235 | |||
| 236 | $color = $hexcode; | ||
| 237 | } | ||
| 238 | |||
| 239 | return _graph_contrast_color($color); | ||
| 240 | } | ||
| 241 | |||
| 242 | # Code borrowed from http://www.perlmonks.org/?node_id=261561 provided by msemtd. | ||
| 243 | sub _graph_contrast_color { | ||
| 244 | my ($color) = @_; | ||
| 245 | |||
| 246 | my @caller = caller(0); | ||
| 247 | @caller = caller(2) if $caller[3] eq '(eval)'; | ||
| 248 | |||
| 249 | Map::Tube::Exception::InvalidColorHexCode->throw({ | ||
| 250 | method => __PACKAGE__."::graph_line_image", | ||
| 251 | message => "ERROR: Invalid color hex code.", | ||
| 252 | filename => $caller[1], | ||
| 253 | line_number => $caller[2] }) | ||
| 254 | unless ($color =~ /^#(..)(..)(..)$/); | ||
| 255 | |||
| 256 | my ($r, $g, $b) = (hex($1), hex($2), hex($3)); | ||
| 257 | my %oppcolors = ( | ||
| 258 | "00" => "FF", | ||
| 259 | "33" => "FF", | ||
| 260 | "66" => "FF", | ||
| 261 | "99" => "FF", | ||
| 262 | "CC" => "00", | ||
| 263 | "FF" => "00", | ||
| 264 | ); | ||
| 265 | |||
| 266 | $r = int($r / 51) * 51; | ||
| 267 | $g = int($g / 51) * 51; | ||
| 268 | $b = int($b / 51) * 51; | ||
| 269 | |||
| 270 | $r = $oppcolors{sprintf("%02X", $r)}; | ||
| 271 | $g = $oppcolors{sprintf("%02X", $g)}; | ||
| 272 | $b = $oppcolors{sprintf("%02X", $b)}; | ||
| 273 | |||
| 274 | return "#$r$g$b"; | ||
| 275 | } | ||
| 276 | |||
| 277 | =head1 AUTHOR | ||
| 278 | |||
| 279 | Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >> | ||
| 280 | |||
| 281 | =head1 REPOSITORY | ||
| 282 | |||
| 283 | L<https://github.com/manwar/Map-Tube-Plugin-Graph> | ||
| 284 | |||
| 285 | =head1 BUGS | ||
| 286 | |||
| 287 | Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/Map-Tube-Plugin-Graph/issues>. | ||
| 288 | I will be notified and then you'll automatically be notified of progress on your | ||
| 289 | bug as I make changes. | ||
| 290 | |||
| 291 | =head1 SUPPORT | ||
| 292 | |||
| 293 | You can find documentation for this module with the perldoc command. | ||
| 294 | |||
| 295 | perldoc Map::Tube::Plugin::Graph::Utils | ||
| 296 | |||
| 297 | You can also look for information at: | ||
| 298 | |||
| 299 | =over 4 | ||
| 300 | |||
| 301 | =item * BUG Report | ||
| 302 | |||
| 303 | L<https://github.com/manwar/Map-Tube-Plugin-Graph/issues> | ||
| 304 | |||
| 305 | =item * Search MetaCPAN | ||
| 306 | |||
| 307 | L<https://metacpan.org/dist/Map-Tube-Plugin-Graph> | ||
| 308 | |||
| 309 | =back | ||
| 310 | |||
| 311 | =head1 LICENSE AND COPYRIGHT | ||
| 312 | |||
| 313 | Copyright (C) 2015 - 2026 Mohammad S Anwar. | ||
| 314 | |||
| 315 | This program is free software; you can redistribute it and/or modify it under | ||
| 316 | the terms of the the Artistic License (2.0). You may obtain a copy of the full | ||
| 317 | license at: | ||
| 318 | |||
| 319 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 320 | |||
| 321 | Any use, modification, and distribution of the Standard or Modified Versions is | ||
| 322 | governed by this Artistic License.By using, modifying or distributing the Package, | ||
| 323 | you accept this license. Do not use, modify, or distribute the Package, if you do | ||
| 324 | not accept this license. | ||
| 325 | |||
| 326 | If your Modified Version has been derived from a Modified Version made by someone | ||
| 327 | other than you,you are nevertheless required to ensure that your Modified Version | ||
| 328 | complies with the requirements of this license. | ||
| 329 | |||
| 330 | This license does not grant you the right to use any trademark, service mark, | ||
| 331 | tradename, or logo of the Copyright Holder. | ||
| 332 | |||
| 333 | This license includes the non-exclusive, worldwide, free-of-charge patent license | ||
| 334 | to make, have made, use, offer to sell, sell, import and otherwise transfer the | ||
| 335 | Package with respect to any patent claims licensable by the Copyright Holder that | ||
| 336 | are necessarily infringed by the Package. If you institute patent litigation | ||
| 337 | (including a cross-claim or counterclaim) against any party alleging that the | ||
| 338 | Package constitutes direct or contributory patent infringement,then this Artistic | ||
| 339 | License to you shall terminate on the date that such litigation is filed. | ||
| 340 | |||
| 341 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND | ||
| 342 | CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED | ||
| 343 | WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR | ||
| 344 | NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS | ||
| 345 | REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, | ||
| 346 | INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE | ||
| 347 | OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | 1; # End of Map::Tube::Plugin::Graph::Utils | ||