| 1 | # | ||||||||||||||||||||||||||||
| 2 | # Graph::Writer::Dot - write a directed graph out in Dot format | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | package Graph::Writer::Dot; | ||||||||||||||||||||||||||||
| 5 | 2 | $Graph::Writer::Dot::VERSION = '2.10'; | |||||||||||||||||||||||||||
| 6 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use parent 'Graph::Writer'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 13 | # List of valid dot attributes for the entire graph, per node, | ||||||||||||||||||||||||||||
| 14 | # and per edge. You can set other attributes, but they won't get | ||||||||||||||||||||||||||||
| 15 | # written out. | ||||||||||||||||||||||||||||
| 16 | #----------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 17 | 2 | my %valid_attributes = | |||||||||||||||||||||||||||
| 18 | ( | ||||||||||||||||||||||||||||
| 19 | graph => [qw(bb bgcolor center clusterrank color comment compound | ||||||||||||||||||||||||||||
| 20 | concentrate Damping defaultlist dim dpi epsilon fontcolor | ||||||||||||||||||||||||||||
| 21 | fontname fontpath fontsize label labeljust labelloc layers | ||||||||||||||||||||||||||||
| 22 | layersep lp margin maxiter mclimit mindist mode model nodesep | ||||||||||||||||||||||||||||
| 23 | nojustify normalize nslimit nslimit1 ordering ordering | ||||||||||||||||||||||||||||
| 24 | orientation outputorder overlap pack packmode page pagedir | ||||||||||||||||||||||||||||
| 25 | quantum rank rankdir ranksep ratio remincross resolution | ||||||||||||||||||||||||||||
| 26 | root rotate samplepoints searchsize sep showboxes size | ||||||||||||||||||||||||||||
| 27 | splines start stylesheet target truecolor viewport voro_margin)], | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | node => [qw(bottomlabel color comment distortion fillcolor fixedsize | ||||||||||||||||||||||||||||
| 30 | fontcolor fontname fontsize group height width label layer | ||||||||||||||||||||||||||||
| 31 | margin nojustify orientation peripheries pin pos rects regular | ||||||||||||||||||||||||||||
| 32 | root shape shapefile showboxes sides skew style target | ||||||||||||||||||||||||||||
| 33 | tooltip toplabel URL vertices width z)], | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | edge => [qw(arrowhead arrowsize arrowtail color comment constraint decorate | ||||||||||||||||||||||||||||
| 36 | dir fontcolor fontname fontsize headURL headclip headhref | ||||||||||||||||||||||||||||
| 37 | headlabel headport headtarget headtooltip href id label | ||||||||||||||||||||||||||||
| 38 | labelangle labeldistance labelfloat labelfontcolor labelfontname | ||||||||||||||||||||||||||||
| 39 | labelfontsize layer len lhead lp ltail minlen nojustify | ||||||||||||||||||||||||||||
| 40 | pos samehead sametail showboxes style tailURL tailclip | ||||||||||||||||||||||||||||
| 41 | tailhref taillabel tailport tailtarget tailtooltip target | ||||||||||||||||||||||||||||
| 42 | tooltip weight)], | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | #======================================================================= | ||||||||||||||||||||||||||||
| 46 | # | ||||||||||||||||||||||||||||
| 47 | # _init() | ||||||||||||||||||||||||||||
| 48 | # | ||||||||||||||||||||||||||||
| 49 | # class-specific initialisation. There isn't any here, but it's | ||||||||||||||||||||||||||||
| 50 | # kept as a place-holder. | ||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||
| 52 | #======================================================================= | ||||||||||||||||||||||||||||
| 53 | sub _init | ||||||||||||||||||||||||||||
| 54 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 55 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | 6 | my %param = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 6 | $self->SUPER::_init(); | |||||||||||||||||||||||||||
| 59 | 6 | $self->{_cluster} = $param{cluster}; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | #======================================================================= | ||||||||||||||||||||||||||||
| 63 | # | ||||||||||||||||||||||||||||
| 64 | # _write_graph() | ||||||||||||||||||||||||||||
| 65 | # | ||||||||||||||||||||||||||||
| 66 | # The private method which actually does the writing out in | ||||||||||||||||||||||||||||
| 67 | # dot format. | ||||||||||||||||||||||||||||
| 68 | # | ||||||||||||||||||||||||||||
| 69 | # This is called from the public method, write_graph(), which is | ||||||||||||||||||||||||||||
| 70 | # found in Graph::Writer. | ||||||||||||||||||||||||||||
| 71 | # | ||||||||||||||||||||||||||||
| 72 | #======================================================================= | ||||||||||||||||||||||||||||
| 73 | sub _write_graph | ||||||||||||||||||||||||||||
| 74 | 6 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_write_graph: called CC=18 SCAR=29.1
| |||||||||||||||||||||||||||||
| 75 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 76 | 6 | my $graph = shift; | |||||||||||||||||||||||||||
| 77 | 6 | my $FILE = shift; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 6 | my $v; | |||||||||||||||||||||||||||
| 80 | my $from; | ||||||||||||||||||||||||||||
| 81 | 6 | my $to; | |||||||||||||||||||||||||||
| 82 | 6 | my $gn; | |||||||||||||||||||||||||||
| 83 | 6 | my $attrref; | |||||||||||||||||||||||||||
| 84 | 6 | my @keys; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 88 | # If the graph has a 'name' attribute, then we use that for the | ||||||||||||||||||||||||||||
| 89 | # name of the digraph instance. Else it's just 'g'. | ||||||||||||||||||||||||||||
| 90 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 91 | 6 | ▶ | $gn = $graph->has_graph_attribute('name') ? $graph->get_graph_attribute('name') : 'g'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 6 | ▶ | my $graphtype = $graph->is_directed ? "digraph" : "graph"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 6 | print $FILE "$graphtype $gn\n{\n"; | |||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 96 | # Dump out any overall attributes of the graph | ||||||||||||||||||||||||||||
| 97 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 98 | 6 | $attrref = $graph->get_graph_attributes(); | |||||||||||||||||||||||||||
| 99 | 6 | @keys = grep(exists $attrref->{$_}, @{$valid_attributes{'graph'}}); | |||||||||||||||||||||||||||
| 100 | 6 | ▶ | if (@keys > 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | { | ||||||||||||||||||||||||||||
| 102 | 0 | print $FILE " /* graph attributes */\n"; | |||||||||||||||||||||||||||
| 103 | 0 | foreach my $a (@keys) | |||||||||||||||||||||||||||
| 104 | { | ||||||||||||||||||||||||||||
| 105 | 0 | print $FILE " $a = \"", $attrref->{$a}, "\";\n"; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 110 | # Generate a list of nodes, with attributes for those that have any. | ||||||||||||||||||||||||||||
| 111 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 112 | 6 | print $FILE "\n /* list of nodes */\n"; | |||||||||||||||||||||||||||
| 113 | 6 | foreach $v (sort $graph->vertices) | |||||||||||||||||||||||||||
| 114 | { | ||||||||||||||||||||||||||||
| 115 | 20 | print $FILE " \"$v\""; | |||||||||||||||||||||||||||
| 116 | 20 | $attrref = $graph->get_vertex_attributes($v); | |||||||||||||||||||||||||||
| 117 | 20 | @keys = grep(exists $attrref->{$_}, @{$valid_attributes{'node'}}); | |||||||||||||||||||||||||||
| 118 | 20 | ▶ | if (@keys > 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | { | ||||||||||||||||||||||||||||
| 120 | print $FILE " [", join(',', | ||||||||||||||||||||||||||||
| 121 | 8 | map { "$_=\"".$attrref->{$_}."\"" } @keys), "]"; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 20 | print $FILE ";\n"; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 6 | ▶ | if ($self->{_cluster} && grep { $_ eq $self->{_cluster} } @{$valid_attributes{'node'}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"_cluster"} and grep {$_ eq $self->{"_cluster"};} @{$valid_attributes{"node"};}
MC/DC100%2/2
$self->{"_cluster"} and grep {$_ eq $self->{"_cluster"};} @{$valid_attributes{"node"};}
$self->{"_cluster"}
grep {$_ eq $self->{"_cluster"};} @{$valid_attributes{"node"};}
Truth table100%3/3
A and B
A= $self->{"_cluster"}B= grep {$_ eq $self->{"_cluster"};} @{$valid_attributes{"node"};}
| |||||||||||||||||||||||||||||
| 127 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 128 | # Generate a list of subgraphs based on the attribute value. | ||||||||||||||||||||||||||||
| 129 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 130 | 3 | print $FILE "\n /* list of subgraphs */\n"; | |||||||||||||||||||||||||||
| 131 | 3 | my %cluster = (); | |||||||||||||||||||||||||||
| 132 | 3 | foreach my $v (sort $graph->vertices) { | |||||||||||||||||||||||||||
| 133 | 9 | my $attrs = $graph->get_vertex_attributes($v); | |||||||||||||||||||||||||||
| 134 | 9 | ▶ | next unless $attrs && $attrs->{$self->{_cluster}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$attrs and $$attrs{$self->{"_cluster"}}
MC/DC50%1/2
$attrs and $$attrs{$self->{"_cluster"}}
$attrs
$$attrs{$self->{"_cluster"}}
Truth table66%2/3
A and B
A= $attrsB= $$attrs{$self->{"_cluster"}}
| |||||||||||||||||||||||||||||
| 135 | 8 | my $attr_value = $attrs->{$self->{_cluster}}; | |||||||||||||||||||||||||||
| 136 | 8 | ▶ | $cluster{$attr_value} = [] unless exists $cluster{$attr_value}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 8 | push @{ $cluster{$attr_value} }, $v; | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 3 | foreach my $attr_value (sort keys %cluster) { | |||||||||||||||||||||||||||
| 140 | 5 | print $FILE " subgraph \"cluster_$attr_value\" {\n"; | |||||||||||||||||||||||||||
| 141 | 5 | print $FILE " label = \"$attr_value\";\n"; | |||||||||||||||||||||||||||
| 142 | 5 | foreach my $node (@{ $cluster{$attr_value} }) { | |||||||||||||||||||||||||||
| 143 | 8 | print $FILE " node [label=\"$node\"] \"$node\";\n"; | |||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | 5 | print $FILE " }\n"; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 150 | # Generate a list of edges, along with any attributes | ||||||||||||||||||||||||||||
| 151 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 152 | 6 | print $FILE "\n /* list of edges */\n"; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | #-------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 155 | # Handle directed and undirected graphs properly: | ||||||||||||||||||||||||||||
| 156 | # - create a graph (not a digraph) instance for undirected graphs | ||||||||||||||||||||||||||||
| 157 | # - the edge operator is '--' for undirected graphs, as opposed to '->' | ||||||||||||||||||||||||||||
| 158 | #-------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 159 | 6 | ▶ | my $edgeop = $graph->is_directed() ? '->' : '--'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 6 | foreach my $edge (sort _by_vertex $graph->edges) | |||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 17 | ($from, $to) = @$edge; | |||||||||||||||||||||||||||
| 164 | 17 | print $FILE " \"$from\" $edgeop \"$to\""; | |||||||||||||||||||||||||||
| 165 | 17 | $attrref = $graph->get_edge_attributes($from, $to); | |||||||||||||||||||||||||||
| 166 | 17 | @keys = grep(exists $attrref->{$_}, @{$valid_attributes{'edge'}}); | |||||||||||||||||||||||||||
| 167 | 17 | ▶ | if (@keys > 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | { | ||||||||||||||||||||||||||||
| 169 | print $FILE " [", join(',', | ||||||||||||||||||||||||||||
| 170 | 0 | map { "$_ = \"".$attrref->{$_}."\"" } @keys), "]"; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 17 | print $FILE ";\n"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 176 | # close off the digraph instance | ||||||||||||||||||||||||||||
| 177 | #------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 178 | 6 | print $FILE "}\n"; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 6 | return 1; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | sub _by_vertex | ||||||||||||||||||||||||||||
| 185 | 18 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_by_vertex: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 186 | 18 | return $a->[0].$a->[1] cmp $b->[0].$b->[1]; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 2 | 1; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | __END__ | ||||||||||||||||||||||||||||