| 1 | package Map::Tube::GraphViz; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use Class::Utils qw(set_params); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use English; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Error::Pure qw(err); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use GraphViz2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use List::MoreUtils qw(none); | ||
| 11 | use Map::Tube::GraphViz::Utils qw(node_color); | ||
| 12 | use Scalar::Util qw(blessed); | ||
| 13 | |||
| 14 | our $VERSION = 0.07; | ||
| 15 | |||
| 16 | # Constructor. | ||
| 17 | sub new { | ||
| 18 | my ($class, @params) = @_; | ||
| 19 | |||
| 20 | # Create object. | ||
| 21 | my $self = bless {}, $class; | ||
| 22 | |||
| 23 | # Edge callback. | ||
| 24 | $self->{'callback_edge'} = sub { | ||
| 25 | my ($self, $from, $to) = @_; | ||
| 26 | $self->{'g'}->add_edge( | ||
| 27 | 'from' => $self->{'callback_node_id'}->($self, $from), | ||
| 28 | 'to' => $self->{'callback_node_id'}->($self, $to), | ||
| 29 | ); | ||
| 30 | return; | ||
| 31 | }; | ||
| 32 | |||
| 33 | # Node callback. | ||
| 34 | $self->{'callback_node'} = \&node_color; | ||
| 35 | |||
| 36 | # Node id callback. | ||
| 37 | $self->{'callback_node_id'} = sub { | ||
| 38 | my ($self, $node) = @_; | ||
| 39 | return $node->name; | ||
| 40 | }; | ||
| 41 | |||
| 42 | # Driver. | ||
| 43 | $self->{'driver'} = 'neato'; | ||
| 44 | |||
| 45 | # Name of map. | ||
| 46 | $self->{'name'} = undef; | ||
| 47 | |||
| 48 | # GraphViz2 object. | ||
| 49 | $self->{'g'} = undef; | ||
| 50 | |||
| 51 | # Output. | ||
| 52 | $self->{'output'} = 'png'; | ||
| 53 | |||
| 54 | # Map::Tube object. | ||
| 55 | $self->{'tube'} = undef; | ||
| 56 | |||
| 57 | # Process params. | ||
| 58 | set_params($self, @params); | ||
| 59 | |||
| 60 | # Check Map::Tube object. | ||
| 61 | if (! defined $self->{'tube'}) { | ||
| 62 | err "Parameter 'tube' is required."; | ||
| 63 | } | ||
| 64 | if (! defined &UNIVERSAL::DOES) { | ||
| 65 | eval { | ||
| 66 | require UNIVERSAL::DOES; | ||
| 67 | }; | ||
| 68 | if ($EVAL_ERROR) { | ||
| 69 | err 'Cannot load UNIVERSAL::DOES module.'; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | if (! blessed($self->{'tube'}) | ||
| 73 | || ! $self->{'tube'}->DOES('Map::Tube')) { | ||
| 74 | |||
| 75 | err "Parameter 'tube' must be 'Map::Tube' object."; | ||
| 76 | } | ||
| 77 | |||
| 78 | # GraphViz2 object. | ||
| 79 | if (defined $self->{'g'}) { | ||
| 80 | if (defined $self->{'name'}) { | ||
| 81 | err "Parameter 'name' cannot be used with ". | ||
| 82 | "'g' parameter."; | ||
| 83 | } | ||
| 84 | |||
| 85 | # Check GraphViz2 object. | ||
| 86 | if (! blessed($self->{'g'}) | ||
| 87 | || ! $self->{'g'}->isa('GraphViz2')) { | ||
| 88 | |||
| 89 | err "Parameter 'g' must be 'GraphViz2' object."; | ||
| 90 | } | ||
| 91 | } else { | ||
| 92 | my $name = $self->{'name'}; | ||
| 93 | if (! defined $name) { | ||
| 94 | $name = $self->{'tube'}->name; | ||
| 95 | } | ||
| 96 | $self->{'g'} = GraphViz2->new( | ||
| 97 | 'global' => { | ||
| 98 | 'directed' => 0, | ||
| 99 | }, | ||
| 100 | $name ? ( | ||
| 101 | 'graph' => { | ||
| 102 | 'label' => $name, | ||
| 103 | 'labelloc' => 'top', | ||
| 104 | }, | ||
| 105 | ) : (), | ||
| 106 | ); | ||
| 107 | } | ||
| 108 | |||
| 109 | # Check output format. | ||
| 110 | if (! defined $self->{'output'}) { | ||
| 111 | err "Parameter 'output' is required."; | ||
| 112 | } | ||
| 113 | if (! exists $self->{'g'}->valid_output_format->{$self->{'output'}}) { | ||
| 114 | err "Unsupported 'output' parameter '$self->{'output'}'."; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Object. | ||
| 118 | return $self; | ||
| 119 | } | ||
| 120 | |||
| 121 | # Get graph. | ||
| 122 | sub graph { | ||
| 123 | my ($self, $output_file) = @_; | ||
| 124 | my $node_cache_hr = {}; | ||
| 125 | foreach my $node (values %{$self->{'tube'}->nodes}) { | ||
| 126 | $self->{'callback_node'}->($self, $node); | ||
| 127 | $node_cache_hr->{$node->id} = $node; | ||
| 128 | } | ||
| 129 | my @processed; | ||
| 130 | foreach my $node (values %{$self->{'tube'}->nodes}) { | ||
| 131 | foreach my $link (split m/,/ms, $node->link) { | ||
| 132 | if (none { | ||
| 133 | ($_->[0] eq $node->id && $_->[1] eq $link) | ||
| 134 | || | ||
| 135 | ($_->[0] eq $link && $_->[1] eq $node->id) | ||
| 136 | } @processed) { | ||
| 137 | |||
| 138 | # Skip link to myself. | ||
| 139 | my $link_node = $node_cache_hr->{$link}; | ||
| 140 | if ($self->{'callback_node_id'}->($self, $node) | ||
| 141 | ne $self->{'callback_node_id'} | ||
| 142 | ->($self, $link_node)) { | ||
| 143 | |||
| 144 | $self->{'callback_edge'}->($self, $node, | ||
| 145 | $link_node); | ||
| 146 | } | ||
| 147 | push @processed, [$node->id, $link]; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | } | ||
| 151 | eval { | ||
| 152 | $self->{'g'}->run( | ||
| 153 | 'driver' => $self->{'driver'}, | ||
| 154 | 'format' => $self->{'output'}, | ||
| 155 | 'output_file' => $output_file, | ||
| 156 | ); | ||
| 157 | }; | ||
| 158 | if ($EVAL_ERROR) { | ||
| 159 | err 'Cannot create GraphViz output.', | ||
| 160 | 'Error', $EVAL_ERROR, | ||
| 161 | 'Dot input', $self->{'g'}->dot_input; | ||
| 162 | } | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | 1; | ||
| 167 | |||
| 168 | __END__ | ||