| 1 | # | ||||||||||
| 2 | # Graph::Writer::daVinci - write a directed graph out in daVinci format | ||||||||||
| 3 | # | ||||||||||
| 4 | package Graph::Writer::daVinci; | ||||||||||
| 5 | 1 | $Graph::Writer::daVinci::VERSION = '2.10'; | |||||||||
| 6 | 1 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 1 | ▶ | use parent 'Graph::Writer'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | #----------------------------------------------------------------------- | ||||||||||
| 13 | # List of valid daVinci 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 | 1 | my %valid_attributes = | |||||||||
| 18 | ( | ||||||||||
| 19 | node => [qw(OBJECT FONTFAMILY FONTSTYLE COLOR CCOLOR _GO _CGO | ||||||||||
| 20 | ICONFILE CICONFILE HIDDEN BORDER)], | ||||||||||
| 21 | |||||||||||
| 22 | edge => [qw(EDGECOLOR EDGEPATTERN _DIR HEAD)], | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | #======================================================================= | ||||||||||
| 26 | # | ||||||||||
| 27 | # _write_graph() | ||||||||||
| 28 | # | ||||||||||
| 29 | # The private method which actually does the writing out in | ||||||||||
| 30 | # daVinci format. | ||||||||||
| 31 | # | ||||||||||
| 32 | # This is called from the public method, write_graph(), which is | ||||||||||
| 33 | # found in Graph::Writer. | ||||||||||
| 34 | # | ||||||||||
| 35 | #======================================================================= | ||||||||||
| 36 | sub _write_graph | ||||||||||
| 37 | 1 | ▶ | { | ||||||||
|
Subroutine
_write_graph: called CC=7 SCAR=25.4
| |||||||||||
| 38 | 1 | my $self = shift; | |||||||||
| 39 | 1 | my $graph = shift; | |||||||||
| 40 | 1 | my $FILE = shift; | |||||||||
| 41 | |||||||||||
| 42 | 1 | my $v; | |||||||||
| 43 | my $from; | ||||||||||
| 44 | 1 | my $to; | |||||||||
| 45 | 1 | my $gn; | |||||||||
| 46 | 1 | my $aref; | |||||||||
| 47 | 1 | my @keys; | |||||||||
| 48 | 1 | my (@nodes, @edges); | |||||||||
| 49 | 1 | my %done = (); | |||||||||
| 50 | 1 | my $node; | |||||||||
| 51 | |||||||||||
| 52 | |||||||||||
| 53 | 1 | @nodes = sort $graph->source_vertices; | |||||||||
| 54 | 1 | ▶ | if (@nodes == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | { | ||||||||||
| 56 | 0 | die "expecting source vertices!\n"; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 1 | print $FILE "[\n"; | |||||||||
| 60 | 1 | ▶ | while (@nodes > 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | { | ||||||||||
| 62 | 1 | $node = shift @nodes; | |||||||||
| 63 | 1 | $self->_dump_node($graph, $FILE, $node, \%done, 1); | |||||||||
| 64 | 1 | ▶ | print $FILE ",\n" if @nodes > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | } | ||||||||||
| 66 | 1 | print $FILE "\n]\n"; | |||||||||
| 67 | |||||||||||
| 68 | 1 | return 1; | |||||||||
| 69 | |||||||||||
| 70 | #------------------------------------------------------------------- | ||||||||||
| 71 | # Generate a list of edges, along with any attributes | ||||||||||
| 72 | #------------------------------------------------------------------- | ||||||||||
| 73 | 0 | print $FILE "\n /* list of edges */\n"; | |||||||||
| 74 | 0 | @edges = sort _by_vertex $graph->edges; | |||||||||
| 75 | 0 | ▶ | for (my $i = 0; $i < @edges; $i++) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | { | ||||||||||
| 77 | 0 | ($from, $to) = @{ $edges[$i] }; | |||||||||
| 78 | 0 | print $FILE " $from -> $to"; | |||||||||
| 79 | 0 | $aref = $graph->get_graph_attributes($from, $to); | |||||||||
| 80 | 0 | @keys = grep(exists $aref->{$_}, @{$valid_attributes{'edge'}}); | |||||||||
| 81 | 0 | ▶ | if (@keys > 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | { | ||||||||||
| 83 | print $FILE " [", join(',', | ||||||||||
| 84 | 0 | map { "$_ = \"".$aref->{$_}."\"" } @keys), "]"; | |||||||||
| 85 | } | ||||||||||
| 86 | 0 | ▶ | print $FILE ", " if $i < @edges - 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 0 | return 1; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | |||||||||||
| 93 | sub _by_vertex | ||||||||||
| 94 | 0 | ▶ | { | ||||||||
|
Subroutine
_by_vertex: not called CC=1 SCAR=6.9
| |||||||||||
| 95 | 0 | return $a->[0].$a->[1] cmp $b->[0].$b->[1]; | |||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | |||||||||||
| 99 | #======================================================================= | ||||||||||
| 100 | # | ||||||||||
| 101 | # _dump_node | ||||||||||
| 102 | # | ||||||||||
| 103 | # Write out a node, using a reference if we've already written it. | ||||||||||
| 104 | # If there are any outgoing edges, we dump them out, recursively | ||||||||||
| 105 | # calling ourself to dump the nodes at the other end of each edge. | ||||||||||
| 106 | # | ||||||||||
| 107 | #======================================================================= | ||||||||||
| 108 | sub _dump_node | ||||||||||
| 109 | 8 | ▶ | { | ||||||||
|
Subroutine
_dump_node: called CC=7 SCAR=19.6
| |||||||||||
| 110 | 8 | my ($self, $graph, $FILE, $node, $doneref, $depth) = @_; | |||||||||
| 111 | 8 | my $aref; | |||||||||
| 112 | my @keys; | ||||||||||
| 113 | 8 | my @children; | |||||||||
| 114 | 8 | my $child; | |||||||||
| 115 | 8 | local $_; | |||||||||
| 116 | |||||||||||
| 117 | |||||||||||
| 118 | 8 | ▶ | if (exists $doneref->{$node}) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | { | ||||||||||
| 120 | 3 | print $FILE ' ' x (2 * $depth), "r(\"Node $node\")"; | |||||||||
| 121 | } | ||||||||||
| 122 | else | ||||||||||
| 123 | { | ||||||||||
| 124 | 5 | print $FILE ' ' x (2 * $depth), "l(\"Node $node\", n(\"\""; | |||||||||
| 125 | 5 | $aref = $graph->get_vertex_attributes($node); | |||||||||
| 126 | 5 | @keys = grep(exists $aref->{$_}, @{$valid_attributes{'node'}}); | |||||||||
| 127 | 5 | ▶ | if (@keys > 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | { | ||||||||||
| 129 | print $FILE ", [", join(', ', | ||||||||||
| 130 | 0 | map { "a(\"$_\", \"".$aref->{$_}."\")" } @keys), "]"; | |||||||||
| 131 | } | ||||||||||
| 132 | else | ||||||||||
| 133 | { | ||||||||||
| 134 | 5 | print $FILE ", []"; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 5 | $doneref->{$node} = 1; | |||||||||
| 138 | |||||||||||
| 139 | 5 | @children = sort $graph->successors($node); | |||||||||
| 140 | 5 | ▶ | if (@children == 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 141 | { | ||||||||||
| 142 | 1 | print $FILE ", []"; | |||||||||
| 143 | } | ||||||||||
| 144 | else | ||||||||||
| 145 | { | ||||||||||
| 146 | 4 | print $FILE ",\n", ' ' x (2 * $depth + 1), "[\n"; | |||||||||
| 147 | 4 | ▶ | while (@children > 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 148 | { | ||||||||||
| 149 | 7 | $child = shift @children; | |||||||||
| 150 | 7 | print $FILE ' ' x (2 * $depth + 2), | |||||||||
| 151 | "l(\"Edge ${node}->$child\", e(\"\", ["; | ||||||||||
| 152 | |||||||||||
| 153 | # write out any attributes of the edge | ||||||||||
| 154 | 7 | $aref = $graph->get_edge_attributes($node, $child); | |||||||||
| 155 | 7 | @keys = grep(exists $aref->{$_}, @{$valid_attributes{'edge'}}); | |||||||||
| 156 | 7 | ▶ | if (@keys > 0) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | { | ||||||||||
| 158 | print $FILE join(', ', | ||||||||||
| 159 | 0 | map { "a(\"$_\", \"".$aref->{$_}."\")" } @keys); | |||||||||
| 160 | } | ||||||||||
| 161 | |||||||||||
| 162 | 7 | print $FILE "],\n"; | |||||||||
| 163 | 7 | $self->_dump_node($graph, $FILE, $child, $doneref, $depth+2); | |||||||||
| 164 | 7 | print $FILE "))"; | |||||||||
| 165 | 7 | ▶ | print $FILE ",\n" if @children > 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 166 | } | ||||||||||
| 167 | 4 | print $FILE ' ' x (2 * $depth + 1), "]"; | |||||||||
| 168 | } | ||||||||||
| 169 | 5 | print $FILE "))"; | |||||||||
| 170 | } | ||||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | 1 | 1; | |||||||||
| 174 | |||||||||||
| 175 | __END__ | ||||||||||