| File: | blib/lib/Language/Homespring/Visualise/GraphViz.pm |
| Coverage: | 24.5% |
| line | stmt | bran | cond | sub | time | code |
|---|---|---|---|---|---|---|
| 1 | package Language::Homespring::Visualise::GraphViz; | |||||
| 2 | ||||||
| 3 | $VERSION = 0.03; | |||||
| 4 | ||||||
| 5 | 2 2 2 | 9 2 56 | use warnings; | |||
| 6 | 2 2 2 | 11 3 11 | use strict; | |||
| 7 | 2 2 2 | 11 3 12 | use GraphViz; | |||
| 8 | ||||||
| 9 | sub new { | |||||
| 10 | 0 | my $class = shift; | ||||
| 11 | 0 | my $self = bless {}, $class; | ||||
| 12 | ||||||
| 13 | 0 | my $options = shift; | ||||
| 14 | 0 | $self->{interp} = $options->{interp}; | ||||
| 15 | 0 | $self->{spring_col} = $options->{spring_col} || '#C0C0FF'; | ||||
| 16 | 0 | $self->{node_col} = $options->{node_col} || 'white'; | ||||
| 17 | ||||||
| 18 | 0 | $self->{fontname} = $options->{fontname} || 'Times'; | ||||
| 19 | 0 | $self->{fontsize} = $options->{fontsize} || '12'; | ||||
| 20 | ||||||
| 21 | 0 | return $self; | ||||
| 22 | } | |||||
| 23 | ||||||
| 24 | sub do { | |||||
| 25 | 0 | my ($self) = @_; | ||||
| 26 | ||||||
| 27 | 0 | $self->{graph} = GraphViz->new( | ||||
| 28 | directed => 0, | |||||
| 29 | layout => 'dot', | |||||
| 30 | rankdir => 1, | |||||
| 31 | epsilon => 1, | |||||
| 32 | ); | |||||
| 33 | ||||||
| 34 | 0 | $self->add_node($self->{interp}->{root_node}, 1); | ||||
| 35 | ||||||
| 36 | 0 | return $self->{graph}; | ||||
| 37 | } | |||||
| 38 | ||||||
| 39 | sub add_node { | |||||
| 40 | 0 | my ($self, $node, $rank) = @_; | ||||
| 41 | ||||||
| 42 | 0 0 | for(@{$node->{child_nodes}}){ | ||||
| 43 | 0 | my $label = $_->{node_name_safe}; | ||||
| 44 | 0 | $label =~ s/\\/\\\\/g; | ||||
| 45 | ||||||
| 46 | 0 | my $fillcolor = $_->{spring}?$self->{spring_col}:$self->{node_col}; | ||||
| 47 | ||||||
| 48 | 0 | $self->{graph}->add_node( | ||||
| 49 | $_->{uid}, | |||||
| 50 | label => $label, | |||||
| 51 | rank => $rank, | |||||
| 52 | fillcolor => $fillcolor, | |||||
| 53 | style => 'filled', | |||||
| 54 | fontname => $self->{fontname}, | |||||
| 55 | fontsize => $self->{fontsize}, | |||||
| 56 | ); | |||||
| 57 | ||||||
| 58 | 0 | $self->{graph}->add_edge( | ||||
| 59 | $node->{uid} => $_->{uid}, | |||||
| 60 | arrowtail => 'normal', | |||||
| 61 | ) if ($node->{uid} != $self->{interp}->{root_node}->{uid}); | |||||
| 62 | ||||||
| 63 | 0 | $self->add_node($_, $rank+1); | ||||
| 64 | } | |||||
| 65 | } | |||||
| 66 | ||||||