blib/lib/Graph/GML.pm

filter: Statementstmt 17.6% 25 / 142 Branchbran 1.5% 1 / 64 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 38.4% 5 / 13 Podpod 0.0% 0 / 1 totaltotal 12.9% 31 / 240 CC 38 SCAR 44.1
CC
38
Cov
12%
_handle_edge
156.0
_handle_file
110.0
_handle_node
90.0
CRAP 81.9
↑ Summary
1 package Graph::GML;
2  
3 1 ▶use 5.012003;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Graph;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 require Exporter;
10  
11 1 our @ISA = qw(Exporter);
12  
13 # Items to export into callers namespace by default. Note: do not export
14 # names by default without a very good reason. Use EXPORT_OK instead.
15 # Do not simply export all your public functions/methods/constants.
16  
17 # This allows declaration use Graph::GML ':all';
18 # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
19 # will save memory.
20 1 our %EXPORT_TAGS = ( 'all' => [ qw(
21
22 ) ] );
23  
24 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
25  
26 1 our @EXPORT = qw(
27
28 );
29  
30 1 our $VERSION = '0.01';
31  
32  
33 # Preloaded methods go here.
34  
35 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
36 1 my $class = shift;
37 1 my %args = @_;
38 1 my $self = {};
39  
40 1 bless $self, $class;
41  
42 1 ▶ if (defined $args{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"file"}) 0 1 1
43 0 return _handle_file($args{'file'});
44 }
45
46 1 return undef;
47 }
48  
49 0 ▶sub _handle_file {
Subroutine _handle_file: not called CC=10 SCAR=47.0
50  
51 0 my $file = shift;
52  
53 0 my $string = _read_file($file);
54  
55 0 ▶ if ($string =~ /^Creator/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /^Creator/u) 0 0 0
56 0 my $creator;
57 0 ($creator,$string) = _get_string($string,"Creator");
58 0 print $creator,"\n";
59 }
60
61 0 my $wd;
62 0 ($wd,$string) = _get_next($string);
63 0 $wd =~ s/\s+//g;
64 0 my $graph;
65 my $nodes;
66 0 my $populated;
67 0 ▶ while (length($wd)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $wd) 0 0 0
68 # print $wd,"\n";
69 0 ▶ if ($string =~ /directed/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /directed/u) 0 0 0
70 #according to the spec, directed isn't really a tag
71 #but's used, so we deal
72 0 my $val;
73 0 ($val,$string) = _get_word($string);
74 0 ($val,$string) = _get_word($string);
75 # print "Directed: ",$val,"\n";
76 0 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) 0 0 0
77 0 print "Made directed graph\n";
78 0 $graph = Graph->new(directed=>1);
79 }
80 0 ($wd,$string) = _get_next($string);
81 0 next;
82 }
83 0 ▶ if ($wd eq "graph") {
Branch: 3 branches
BranchTrueFalseTotal
if ($wd eq 'graph') { } 0 0 0
elsif ($wd eq 'node') { } 0 0 0
elsif ($wd eq 'edge') { } 0 0 0
84 0 ($wd,$string) = _get_next($string);
85 0 next;
86 }
87 elsif ($wd eq "node") {
88 0 ▶ if (!defined $graph) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $graph) 0 0 0
89 #if not already created, assume undirected
90 0 $graph = Graph::Undirected->new;
91 }
92 0 ($nodes,$string) = _handle_node($nodes,$string);
93 }
94 elsif ($wd eq "edge") {
95 0 ▶ if (!$populated) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($populated) 0 0 0
96 0 $graph = _populate_graph($graph,$nodes);
97 0 $populated = 1;
98 }
99 0 ($graph,$string) = _handle_edge($graph,$nodes,$string);
100 }
101 0 ($wd,$string) = _get_next($string);
102 0 $wd =~ s/\s+//g;
103 }
104  
105 0 return $graph;
106 }
107  
108 0 ▶sub _populate_graph {
Subroutine _populate_graph: not called CC=6 SCAR=37.4
109  
110 0 my $graph = shift;
111 0 my $nodes = shift;
112  
113 0 my $vert;
114 0 foreach my $el (keys %{$nodes}) {
115  
116 0 ▶ if (ref($nodes->{$el}) eq "HASH" && defined $nodes->{$el}->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $nodes->{$el} eq 'HASH' and defined $nodes->{$el}{'label'}) { } 0 0 0
Condition0%0/3
ref $nodes->{$el} eq "HASH" and defined $nodes->{$el}{"label"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nodes->{$el} eq "HASH" and defined $nodes->{$el}{"label"}
ref $nodes->{$el} eq "HASH" defined $nodes->{$el}{"label"}
Truth table0%0/3
A and B
A= ref $nodes->{$el} eq "HASH"B= defined $nodes->{$el}{"label"}
ABresult
0X0
100
111
117 0 $vert = $nodes->{$el}->{label};
118 }
119 else {
120 0 $vert = $el;
121 }
122 0 $graph->add_vertex($vert);
123 0 ▶ if (ref($nodes->{$el}) eq "HASH" && defined $nodes->{$el}->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $nodes->{$el} eq "HASH" and defined $nodes->{$el}{"value"}) 0 0 0
Condition0%0/3
ref $nodes->{$el} eq "HASH" and defined $nodes->{$el}{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nodes->{$el} eq "HASH" and defined $nodes->{$el}{"value"}
ref $nodes->{$el} eq "HASH" defined $nodes->{$el}{"value"}
Truth table0%0/3
A and B
A= ref $nodes->{$el} eq "HASH"B= defined $nodes->{$el}{"value"}
ABresult
0X0
100
111
124 0 $graph->set_vertex_weight($vert,$nodes->{$el}->{value});
125 }
126 }
127  
128 0 return $graph;
129 }
130  
131 0 ▶sub _handle_node {
Subroutine _handle_node: not called CC=9 SCAR=45.0
132 0 my $nodehash = shift;
133 0 my $string = shift;
134
135 0 my $node;
136 0 ($node,$string) = _get_next($string);
137 0 $node =~ s/^\s+//;
138 0 my $tmphash;
139 0 my ($key,$id,$val);
140 0 ▶ while (length($node)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $node) 0 0 0
141 0 ($key,$node) = _get_word($node);
142 0 ▶ if ($key eq "id") {
Branch: 5 branches
BranchTrueFalseTotal
if ($key eq 'id') { } 0 0 0
elsif ($key eq 'name') { } 0 0 0
elsif ($key eq 'label') { } 0 0 0
elsif ($key eq 'comment') { } 0 0 0
elsif ($key eq 'value') { } 0 0 0
143 0 ($id,$node) = _get_word($node);
144 # $tmphash->{id} = $id;
145 }
146 elsif ($key eq "name") {
147 0 ($tmphash->{name},$node) = _get_word($node);
148 }
149 elsif ($key eq "label") {
150 0 ($tmphash->{label},$node) = _get_string($node);
151 }
152 elsif ($key eq "comment") {
153 0 ($tmphash->{comment},$node) = _get_string($node);
154 }
155 elsif ($key eq "value") {
156 0 ($tmphash->{value},$node) = _get_string($node);
157 }
158 }
159
160 # print $id,"\n";
161 0 ▶ if (!defined $id) { return ($nodehash,$string); }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 0 0 0
162 0 ▶ if (!defined $tmphash) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tmphash) { } 0 0 0
163 0 $nodehash->{$id} = 1;
164
165 }
166 else {
167 0 $nodehash->{$id} = $tmphash;
168 }
169 0 return ($nodehash,$string);
170 }
171  
172 0 ▶sub _handle_edge {
Subroutine _handle_edge: not called CC=12 SCAR=50.5
173 0 my $graph = shift;
174 0 my $nodes = shift;
175 0 my $string = shift;
176
177 0 my $edge;
178 0 ($edge,$string) = _get_next($string);
179 0 $edge =~ s/^\s+//;
180 0 my ($key,$source,$target,$label,$comment,$value);
181 0 ▶ while (length($edge)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $edge) 0 0 0
182 0 ($key,$edge) = _get_word($edge);
183 0 ▶ if ($key eq "source") {
Branch: 5 branches
BranchTrueFalseTotal
if ($key eq 'source') { } 0 0 0
elsif ($key eq 'target') { } 0 0 0
elsif ($key eq 'label') { } 0 0 0
elsif ($key eq 'comment') { } 0 0 0
elsif ($key eq 'value') { } 0 0 0
184 0 ($source,$edge) = _get_word($edge);
185 }
186 elsif ($key eq "target") {
187 0 ($target,$edge) = _get_word($edge);
188 }
189 elsif ($key eq "label") {
190 0 ($label,$edge) = _get_string($edge);
191 }
192 elsif ($key eq "comment") {
193 0 ($comment,$edge) = _get_string($edge);
194 }
195 elsif ($key eq "value") {
196 0 ($value,$edge) = _get_string($edge);
197 }
198 }
199  
200 0 ▶ if (ref($nodes->{$source}) eq "HASH" && defined $nodes->{$source}->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $nodes->{$source} eq "HASH" and defined $nodes->{$source}{"label"}) 0 0 0
Condition0%0/3
ref $nodes->{$source} eq "HASH" and defined $nodes->{$source}{"label"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nodes->{$source} eq "HASH" and defined $nodes->{$source}{"label"}
ref $nodes->{$source} eq "HASH" defined $nodes->{$source}{"label"}
Truth table0%0/3
A and B
A= ref $nodes->{$source} eq "HASH"B= defined $nodes->{$source}{"label"}
ABresult
0X0
100
111
201 0 $source = $nodes->{$source}->{label};
202 }
203 0 ▶ if (ref($nodes->{$target}) eq "HASH" && defined $nodes->{$target}->{label}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $nodes->{$target} eq "HASH" and defined $nodes->{$target}{"label"}) 0 0 0
Condition0%0/3
ref $nodes->{$target} eq "HASH" and defined $nodes->{$target}{"label"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $nodes->{$target} eq "HASH" and defined $nodes->{$target}{"label"}
ref $nodes->{$target} eq "HASH" defined $nodes->{$target}{"label"}
Truth table0%0/3
A and B
A= ref $nodes->{$target} eq "HASH"B= defined $nodes->{$target}{"label"}
ABresult
0X0
100
111
204 0 $target = $nodes->{$target}->{label};
205 }
206 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
207 0 $graph->add_weighted_edge($source,$target,$value);
208 }
209 else {
210 0 $graph->add_edge($source,$target);
211 }
212 # print "Edge -- Source: ",$source," Target: ",$target," Label",$label,"\n";
213 0 return ($graph,$string);
214 }
215  
216 0 ▶sub _get_next {
Subroutine _get_next: not called CC=1 SCAR=6.9
217 0 my $string = shift;
218 0 my $split= '[';
219 0 my $split2 = ']';
220 0 my $wd;
221 0 ($wd,$string) = split(/[\Q$split$split2\E]/,$string,2);
222 0 return ($wd,$string);
223 }
224  
225 0 ▶sub _read_file {
Subroutine _read_file: not called CC=3 SCAR=24.8
226 0 my $file = shift;
227
228 0 my $data;
229 0 ▶ open(IF,$file) || die $file," ",$!;
Branch: 1 branch
BranchTrueFalseTotal
open IF, $file or die $file, " ", $! 0 0 0
230 0 ▶ while (<IF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IF)) 0 0 0
231 0 chop;
232 0 $data .= $_;
233 }
234 0 close(IF);
235 0 $data =~ s/^\s+//; # remove any strings at the beginning
236 0 return $data;
237 }
238  
239 0 ▶sub _get_string {
Subroutine _get_string: not called CC=2 SCAR=17.9
240 0 my $string = shift;
241 0 my $id = shift;
242  
243 0 ▶ if (defined $id) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 0 0
244 0 $string =~ s/$id\s+//; #remove that pesky id string
245 }
246 0 my $split= '"';
247 0 my ($bar,$creator,$str) = split(/\"/,$string,3);
248 0 return ($creator,$str);
249 }
250  
251 0 ▶sub _get_word {
Subroutine _get_word: not called CC=1 SCAR=6.9
252 0 my $string = shift;
253  
254 0 $string =~ s/^\s+//;
255 0 my ($wd,$str) = split(/\s+/,$string,2);
256  
257 0 return ($wd,$str);
258 }
259  
260 1 1;
261 __END__
↑ Summary