| 1 | 1 | ▶ | use 5.10.0; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | package Map::Metro::Cmd::Graphviz; | ||||||||||||||
| 6 | |||||||||||||||
| 7 | # ABSTRACT: Make a visualization using GraphViz2 | ||||||||||||||
| 8 | 1 | our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY | |||||||||||||
| 9 | 1 | our $VERSION = '0.2405'; | |||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | ▶ | use Map::Metro::Elk; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 1 | ▶ | use MooseX::App::Command; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | 1 | extends 'Map::Metro::Cmd'; | |||||||||||||
| 14 | 1 | ▶ | use Path::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 1 | ▶ | use Types::Standard qw/Str HashRef/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 1 | ▶ | use Module::Load qw/load/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | ▶ | use Try::Tiny; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | |||||||||||||||
| 19 | 1 | parameter cityname => ( | |||||||||||||
| 20 | is => 'rw', | ||||||||||||||
| 21 | isa => Str, | ||||||||||||||
| 22 | documentation => 'The name of the map', | ||||||||||||||
| 23 | required => 1, | ||||||||||||||
| 24 | ); | ||||||||||||||
| 25 | 1 | parameter customlens => ( | |||||||||||||
| 26 | is => 'rw', | ||||||||||||||
| 27 | isa => Str, | ||||||||||||||
| 28 | documentation => 'Custom distances between stations (origin_station_id->destination_station_id:len)', | ||||||||||||||
| 29 | ); | ||||||||||||||
| 30 | 1 | option into => ( | |||||||||||||
| 31 | is => 'rw', | ||||||||||||||
| 32 | isa => Str, | ||||||||||||||
| 33 | ); | ||||||||||||||
| 34 | 1 | has lens => ( | |||||||||||||
| 35 | is => 'rw', | ||||||||||||||
| 36 | isa => HashRef, | ||||||||||||||
| 37 | traits => ['Hash'], | ||||||||||||||
| 38 | handles => { | ||||||||||||||
| 39 | set_len => 'set', | ||||||||||||||
| 40 | get_len => 'get', | ||||||||||||||
| 41 | }, | ||||||||||||||
| 42 | ); | ||||||||||||||
| 43 | |||||||||||||||
| 44 | 1 | command_short_description 'Make a visualization using GraphViz2'; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | ▶ | sub run { | ||||||||||||
|
Subroutine
run: not called CC=14 SCAR=53.5
| |||||||||||||||
| 47 | 0 | my $self = shift; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | ▶ | try { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 50 | 0 | load "GraphViz2"; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | 0 | ▶ | catch { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 53 | 0 | die $_; | |||||||||||||
| 54 | 0 | }; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 0 | my %hooks = (hooks => ['PrettyPrinter']); | |||||||||||||
| 57 | 0 | ▶ | my $graph = $self->cityname !~ m{\.} ? Map::Metro->new($self->cityname, %hooks)->parse : Map::Metro::Shim->new($self->cityname, %hooks)->parse; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | my $customconnections = { }; | |||||||||||||
| 60 | 0 | ▶ | if($self->customlens) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 61 | 0 | ▶ | my $customlens = path($self->customlens)->exists ? do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | 0 | my $settings = path($self->customlens)->slurp; | |||||||||||||
| 63 | 0 | $settings =~ s{^#.*$}{}g; | |||||||||||||
| 64 | 0 | $settings =~ s{\n}{ }g; | |||||||||||||
| 65 | 0 | $settings; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | : $self->customlens | ||||||||||||||
| 68 | ; | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | foreach my $custom (split m/ +/ => $customlens) { | |||||||||||||
| 71 | 0 | ▶ | if($custom =~ m{^(\d+)->(\d+):([\d\.]+)$}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 72 | 0 | my $origin_station_id = $1; | |||||||||||||
| 73 | 0 | my $destination_station_id = $2; | |||||||||||||
| 74 | 0 | my $len = $3; | |||||||||||||
| 75 | |||||||||||||||
| 76 | 0 | $self->set_len(sprintf ('%s-%s', $origin_station_id, $destination_station_id), $len); | |||||||||||||
| 77 | 0 | $self->set_len(sprintf ('%s-%s', $destination_station_id, $origin_station_id), $len); | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | elsif($custom =~ m{^!(\d+)->(\d+):([\d\.]+)$}) { | ||||||||||||||
| 80 | 0 | my $origin_station_id = $1; | |||||||||||||
| 81 | 0 | my $destination_station_id = $2; | |||||||||||||
| 82 | 0 | my $len = $3; | |||||||||||||
| 83 | |||||||||||||||
| 84 | 0 | $customconnections->{ $origin_station_id }{ $destination_station_id } = $len; | |||||||||||||
| 85 | } | ||||||||||||||
| 86 | } | ||||||||||||||
| 87 | } | ||||||||||||||
| 88 | |||||||||||||||
| 89 | 0 | my $viz = GraphViz2->new( | |||||||||||||
| 90 | global => { directed => 0 }, | ||||||||||||||
| 91 | graph => { epsilon => 0.00001 }, | ||||||||||||||
| 92 | node => { shape => 'circle', fixedsize => 'true', width => 0.8, height => 0.8, penwidth => 3, fontname => 'sans-serif', fontsize => 20 }, | ||||||||||||||
| 93 | edge => { penwidth => 5, len => 1.2 }, | ||||||||||||||
| 94 | ); | ||||||||||||||
| 95 | 0 | foreach my $station ($graph->all_stations) { | |||||||||||||
| 96 | 0 | $viz->add_node(name => $station->id, label => $station->id); | |||||||||||||
| 97 | } | ||||||||||||||
| 98 | |||||||||||||||
| 99 | 0 | foreach my $transfer ($graph->all_transfers) { | |||||||||||||
| 100 | 0 | my %len = $self->get_len_for($transfer->origin_station->id, $transfer->destination_station->id); | |||||||||||||
| 101 | 0 | $viz->add_edge(from => $transfer->origin_station->id, to => $transfer->destination_station->id, color => '#888888', style => 'dashed', %len); | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | 0 | foreach my $segment ($graph->all_segments) { | |||||||||||||
| 104 | 0 | foreach my $line_id ($segment->all_line_ids) { | |||||||||||||
| 105 | 0 | my $color = $graph->get_line_by_id($line_id)->color; | |||||||||||||
| 106 | 0 | my $width = $graph->get_line_by_id($line_id)->width; | |||||||||||||
| 107 | 0 | my %len = $self->get_len_for($segment->origin_station->id, $segment->destination_station->id); | |||||||||||||
| 108 | |||||||||||||||
| 109 | 0 | $viz->add_edge(from => $segment->origin_station->id, | |||||||||||||
| 110 | to => $segment->destination_station->id, | ||||||||||||||
| 111 | color => $color, | ||||||||||||||
| 112 | penwidth => $width, | ||||||||||||||
| 113 | %len, | ||||||||||||||
| 114 | ); | ||||||||||||||
| 115 | } | ||||||||||||||
| 116 | } | ||||||||||||||
| 117 | #* Custom connections (for better visuals) | ||||||||||||||
| 118 | 0 | foreach my $origin_station_id (keys %{ $customconnections }) { | |||||||||||||
| 119 | 0 | foreach my $destination_station_id (keys %{ $customconnections->{ $origin_station_id }}) { | |||||||||||||
| 120 | 0 | my $len = $customconnections->{ $origin_station_id }{ $destination_station_id }; | |||||||||||||
| 121 | 0 | $viz->add_edge(from => $origin_station_id, | |||||||||||||
| 122 | to => $destination_station_id, | ||||||||||||||
| 123 | color => '#ffffff', | ||||||||||||||
| 124 | penwidth => 0, | ||||||||||||||
| 125 | len => $len, | ||||||||||||||
| 126 | ); | ||||||||||||||
| 127 | } | ||||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 0 | ▶ | my $output = $self->into // sprintf 'viz-%s-%s.png', $self->cityname, time; | ||||||||||||
|
Condition0%0/2
$self->into // sprintf("viz-%s-%s.png", $self->cityname, time)
MC/DC0%0/1
$self->into // sprintf("viz-%s-%s.png", $self->cityname, time)
$self->into
Truth table0%0/2
A
A= $self->into
| |||||||||||||||
| 131 | 0 | $viz->run(format => 'png', output_file => $output, driver => 'neato'); | |||||||||||||
| 132 | |||||||||||||||
| 133 | 0 | say sprintf 'Saved in %s.', $output; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | ▶ | sub get_len_for { | ||||||||||||
|
Subroutine
get_len_for: not called CC=4 SCAR=30.0
| |||||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||||
| 138 | 0 | my $origin_station_id = shift; | |||||||||||||
| 139 | 0 | my $destination_station_id = shift; | |||||||||||||
| 140 | |||||||||||||||
| 141 | 0 | ▶ | return (len => $self->get_len("$origin_station_id-$destination_station_id")) if $self->get_len("$origin_station_id-$destination_station_id"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | 0 | ▶ | return (len => $self->get_len("$origin_station_id-0")) if $self->get_len("$origin_station_id-0"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 143 | 0 | ▶ | return (len => $self->get_len("0-$destination_station_id")) if $self->get_len("0-$destination_station_id"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 0 | return (); | |||||||||||||
| 145 | } | ||||||||||||||
| 146 | |||||||||||||||
| 147 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 148 | |||||||||||||||
| 149 | 1 | 1; | |||||||||||||
| 150 | |||||||||||||||
| 151 | __END__ | ||||||||||||||