RDF/Trine/Iterator/Graph.pm

filter: Statementstmt 78.4% 138 / 176 Branchbran 50.0% 19 / 38 Conditioncond 52.9% 9 / 17 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 96.3% 26 / 27 Podpod 100% 11 / 11 totaltotal 73.8% 206 / 279 CC 29 SCAR 28.9
CC
29
Cov
72%
as_string
90.0
print_xml
10.6
as_bindings
5.0
CRAP 18.0
« RDF/Trine/Parser/NTriples.pm ↑ Summary RDF/Trine/Serializer/TriG.pm »
1 # RDF::Trine::Iterator::Graph
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Iterator::Graph - Iterator class for graph query results
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Iterator::Graph version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Iterator::Graph;
15
16 my $iterator = RDF::Trine::Iterator::Graph->new( \&data );
17 while (my $st = $iterator->next) {
18 # $st is a RDF::Trine::Statement object
19 print $st->as_string;
20 }
21  
22 =head1 METHODS
23  
24 Beyond the methods documented below, this class inherits methods from the
25 L<RDF::Trine::Iterator> class.
26  
27 =over 4
28  
29 =cut
30  
31 package RDF::Trine::Iterator::Graph;
32  
33 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 68 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 68 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 68 ▶use RDF::Trine::Iterator qw(sgrep);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use RDF::Trine::Iterator::Graph::Materialized;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 68 ▶use base qw(RDF::Trine::Iterator);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 ######################################################################
47  
48 68 our ($VERSION);
49 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
50 68 $VERSION = '1.019';
51 }
52  
53 ######################################################################
54  
55  
56 =item C<new ( \@results, %args )>
57  
58 =item C<new ( \&results, %args )>
59  
60 Returns a new SPARQL Result interator object. Results must be either
61 an reference to an array containing results or a CODE reference that
62 acts as an iterator, returning successive items when called, and
63 returning undef when the iterator is exhausted.
64  
65 $type should be one of: bindings, boolean, graph.
66  
67 =cut
68  
69 4265 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
70 4265 my $class = shift;
71 4265 ▶ my $stream = shift || sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
shift() || sub { undef; }
l!l&&r!l&&!r
39862790
MC/DC0%0/2
shift() || sub { undef; }
shift() sub { undef; }
Truth table66%2/3
A || B
A= shift()B= sub { undef; }
ABresult
1X1
011
000
72 # Carp::confess unless (scalar(@_) % 2 == 0);
73 4265 my %args = @_;
74
75 4265 my $type = 'graph';
76 4265 return $class->SUPER::new( $stream, $type, [], %args );
77 }
78  
79 39 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
80 39 my $class = shift;
81 39 my $stream = shift;
82 39 my $type = shift;
83 39 my $names = shift;
84 39 my %args = @_;
85 39 return $class->new( $stream, %args );
86 }
87  
88 =item C<< as_bindings ( $s, $p, $o ) >>
89  
90 Returns the iterator as a Bindings iterator, using the supplied triple nodes to
91 determine the variable bindings.
92  
93 =cut
94  
95 3 ▶sub as_bindings {
Subroutine as_bindings: called CC=5 SCAR=16.1
Pod documented
96 3 my $self = shift;
97 3 my @nodes = @_;
98 3 my @names = qw(subject predicate object context);
99 3 my %bindings;
100 3 foreach my $i (0 .. $#names) {
101 12 ▶ if (not($nodes[ $i ]) or not($nodes[ $i ]->isa('RDF::Trine::Node::Variable'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($nodes[$i] and $nodes[$i]->isa("RDF::Trine::Node::Variable")) 9 3 12
Condition66%2/3
$nodes[$i] and $nodes[$i]->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
903
MC/DC50%1/2
$nodes[$i] and $nodes[$i]->isa("RDF::Trine::Node::Variable")
$nodes[$i] $nodes[$i]->isa("RDF::Trine::Node::Variable")
Truth table66%2/3
A and B
A= $nodes[$i]B= $nodes[$i]->isa("RDF::Trine::Node::Variable")
ABresult
0X0
100
111
102 9 $nodes[ $i ] = RDF::Trine::Node::Variable->new( $names[ $i ] );
103 }
104 }
105 3 foreach my $i (0 .. $#nodes) {
106 12 my $n = $nodes[ $i ];
107 12 $bindings{ $n->name } = $names[ $i ];
108 }
109 3 my $context = $nodes[ 3 ]->name;
110
111 3 ▶ my $sub = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
112 3 my $statement = $self->next;
113 3 ▶ return unless ($statement);
Branch: 1 branch
BranchTrueFalseTotal
unless $statement 0 3 3
114 my %values = map {
115 9 my $method = $bindings{ $_ };
116 9 $_ => $statement->$method()
117 3 ▶ } grep { ($statement->isa('RDF::Trine::Statement::Quad')) ? 1 : ($_ ne $context) } (keys %bindings);
Branch: 1 branch
BranchTrueFalseTotal
$statement->isa('RDF::Trine::Statement::Quad') ? : 0 12 12
118 3 return \%values;
119 3 };
120 3 return RDF::Trine::Iterator::Bindings->new( $sub, [ keys %bindings ] );
121 }
122  
123 =item C<< materialize >>
124  
125 Returns a materialized version of the current graph iterator.
126 The materialization process will leave this iterator empty. The materialized
127 iterator that is returned should be used for any future need for the iterator's
128 data.
129  
130 =cut
131  
132 2 ▶sub materialize {
Subroutine materialize: called CC=1 SCAR=0.0
Pod documented
133 2 my $self = shift;
134 2 my @data = $self->get_all;
135 2 my @args = $self->construct_args;
136 2 return $self->_mclass->_new( \@data, @args );
137 }
138  
139 2 ▶sub _mclass {
Subroutine _mclass: called CC=1 SCAR=0.0
140 2 return 'RDF::Trine::Iterator::Graph::Materialized';
141 }
142  
143  
144 =item C<< unique >>
145  
146 Returns a Graph iterator that ensures the returned statements are unique. While
147 the underlying RDF graph is the same regardless of uniqueness, the iterator's
148 serialization methods assume the results are unique, and so use this method
149 before serialization.
150  
151 Uniqueness is opt-in for efficiency concerns -- this method requires O(n) memory,
152 and so may have noticeable effects on large graphs.
153  
154 =cut
155  
156 13 ▶sub unique {
Subroutine unique: called CC=1 SCAR=0.0
Pod documented
157 13 my $self = shift;
158 13 my %seen;
159 68 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
160 27 ▶ my $stream = sgrep( sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 27 my $s = $_;
162 27 my $str = $s->as_string;
163 27 not($seen{ $str }++)
164 13 }, $self);
165 13 return $stream;
166 }
167  
168 =item C<is_graph>
169  
170 Returns true if the underlying result is an RDF graph.
171  
172 =cut
173  
174 1 ▶sub is_graph {
Subroutine is_graph: called CC=1 SCAR=0.0
Pod documented
175 1 my $self = shift;
176 1 return 1;
177 }
178  
179 =item C<as_string ( $max_size [, \$count] )>
180  
181 Returns a string table serialization of the stream data.
182  
183 =cut
184  
185 0 ▶sub as_string {
Subroutine as_string: not called CC=9 SCAR=45.0
Pod documented
186 0 my $self = shift;
187 0 ▶ my $max_result_size = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
188 0 my $rescount = shift;
189 0 my @names = qw(subject predicate object);
190 0 my $headers = \@names;
191 0 my @rows;
192 0 my $count = 0;
193 0 ▶ while (my $row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next) 0 0 0
194 0 ▶ push(@rows, [ map { blessed($_) ? $_->as_string : '' } map { $row->$_() } qw(subject predicate object) ]);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 0 0 0
195 0 ▶ last if ($max_result_size and ++$count >= $max_result_size);
Branch: 1 branch
BranchTrueFalseTotal
if $max_result_size and ++$count >= $max_result_size 0 0 0
Condition0%0/3
$max_result_size and ++$count >= $max_result_size
!ll&&!rl&&r
000
MC/DC0%0/2
$max_result_size and ++$count >= $max_result_size
$max_result_size ++$count >= $max_result_size
Truth table0%0/3
A and B
A= $max_result_sizeB= ++$count >= $max_result_size
ABresult
0X0
100
111
196 }
197 # my $rows = [ map { [ map { blessed($_) ? $_->as_string : '' } @{$_}{ @names } ] } @nodes ];
198 0 ▶ if (ref($rescount)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rescount) 0 0 0
199 0 $$rescount = scalar(@rows);
200 }
201
202 0 my @rule = qw(- +);
203 0 my @headers = (\q"| ");
204 0 push(@headers, map { $_ => \q" | " } @$headers);
205 0 pop @headers;
206 0 push @headers => (\q" |");
207
208 0 ▶ if ('ARRAY' eq ref $rows[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $rows[0]) { } 0 0 0
209 0 ▶ if (@$headers == @{ $rows[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$headers == @{$rows[0];}) { } 0 0 0
210 0 my $table = Text::Table->new(@headers);
211 0 $table->rule(@rule);
212 0 $table->body_rule(@rule);
213 0 $table->load(@rows);
214
215 return join('',
216 $table->rule(@rule),
217 $table->title,
218 $table->rule(@rule),
219 0 map({ $table->body($_) } 0 .. @rows),
220 $table->rule(@rule)
221 );
222 } else {
223 0 die("make_table() rows must be an AoA with rows being same size as headers");
224 }
225 } else {
226 0 return '';
227 }
228 }
229  
230 =item C<as_xml ( $max_size )>
231  
232 Returns an XML serialization of the stream data.
233  
234 =cut
235  
236 6 ▶sub as_xml {
Subroutine as_xml: called CC=2 SCAR=6.9
Pod documented
237 6 my $self = shift;
238 6 ▶ my $max_result_size = shift || 0;
Condition100%2/2
shift() || 0
l!l
24
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
239 6 my $string = '';
240 6 open( my $fh, '>', \$string );
241 6 $self->print_xml( $fh, $max_result_size );
242 6 return $string;
243 }
244  
245 =item C<< print_xml ( $fh, $max_size ) >>
246  
247 Prints an XML serialization of the stream data to the filehandle $fh.
248  
249 =cut
250  
251 6 ▶sub print_xml {
Subroutine print_xml: called CC=10 SCAR=23.6
Pod documented
252 6 my $self = shift;
253 6 my $fh = shift;
254 6 ▶ my $max_result_size = shift || 0;
Condition100%2/2
shift() || 0
l!l
24
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
255 6 my $graph = $self->unique();
256
257 6 my $count = 0;
258 6 print {$fh} <<"END";
259 <?xml version="1.0" encoding="utf-8"?>
260 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
261 END
262 6 ▶ while (my $stmt = $graph->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stmt = $graph->next) 9 5 14
263 9 ▶ if ($max_result_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_result_size) 4 5 9
264 4 ▶ last if ($count++ >= $max_result_size);
Branch: 1 branch
BranchTrueFalseTotal
if $count++ >= $max_result_size 1 3 4
265 }
266 8 my $p = $stmt->predicate->uri_value;
267 8 my $pos = max( rindex( $p, '/' ), rindex( $p, '#' ) );
268 8 my $ns = substr($p,0,$pos+1);
269 8 my $local = substr($p, $pos+1);
270 8 my $subject = $stmt->subject;
271 8 ▶ my $subjstr = ($subject->is_resource)
Branch: 1 branch
BranchTrueFalseTotal
$subject->is_resource ? : 8 0 8
272 ? 'rdf:about="' . $subject->uri_value . '"'
273 : 'rdf:nodeID="' . $subject->blank_identifier . '"';
274 8 my $object = $stmt->object;
275
276 8 print {$fh} qq[<rdf:Description $subjstr>\n];
277 8 ▶ if ($object->is_resource) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object->is_resource) { } 5 3 8
elsif ($object->isa('RDF::Trine::Node::Blank')) { } 0 3 3
278 5 my $uri = $object->uri_value;
279 5 print {$fh} qq[\t<${local} xmlns="${ns}" rdf:resource="$uri"/>\n];
280 } elsif ($object->isa('RDF::Trine::Node::Blank')) {
281 0 my $id = $object->blank_identifier;
282 0 print {$fh} qq[\t<${local} xmlns="${ns}" rdf:nodeID="$id"/>\n];
283 } else {
284 3 my $value = $object->literal_value;
285 # escape < and & and ' and " and >
286 3 $value =~ s/&/&amp;/g;
287 3 $value =~ s/'/&apos;/g;
288 3 $value =~ s/"/&quot;/g;
289 3 $value =~ s/</&lt;/g;
290 3 $value =~ s/>/&gt;/g;
291
292 3 my $tag = qq[${local} xmlns="${ns}"];
293 3 ▶ if (defined($object->literal_value_language)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $object->literal_value_language) { } 0 3 3
elsif (defined $object->literal_datatype) { } 0 3 3
294 0 my $lang = $object->literal_value_language;
295 0 $tag .= qq[ xml:lang="${lang}"];
296 } elsif (defined($object->literal_datatype)) {
297 0 my $dt = $object->literal_datatype;
298 0 $tag .= qq[ rdf:datatype="${dt}"];
299 }
300 3 print {$fh} qq[\t<${tag}>${value}</${local}>\n];
301 }
302 8 print {$fh} qq[</rdf:Description>\n];
303 }
304 6 print {$fh} "</rdf:RDF>\n";
305 }
306  
307 =item C<as_json ( $max_size )>
308  
309 Returns a JSON serialization of the stream data.
310  
311 =cut
312  
313 1 ▶sub as_json {
Subroutine as_json: called CC=1 SCAR=0.0
Pod documented
314 1 my $self = shift;
315 1 my $max_result_size = shift;
316 1 throw RDF::Trine::Error::SerializationError ( -text => 'There is no JSON serialization specified for graph query results' );
317 }
318  
319 =item C<< as_hashref >>
320  
321 Returns a hashref representing the model in an RDF/JSON-like manner.
322  
323 See C<< as_hashref >> at L<RDF::Trine::Model> for full documentation of the
324 hashref format.
325  
326 =cut
327  
328 2 ▶sub as_hashref {
Subroutine as_hashref: called CC=3 SCAR=11.0
Pod documented
329 2 my $self = shift;
330 2 my $index = {};
331 2 ▶ while (my $statement = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $statement = $self->next) 6 2 8
332
333 6 ▶ my $s = $statement->subject->isa('RDF::Trine::Node::Blank') ?
Branch: 1 branch
BranchTrueFalseTotal
$statement->subject->isa('RDF::Trine::Node::Blank') ? : 0 6 6
334 ('_:'.$statement->subject->blank_identifier) :
335 $statement->subject->uri ;
336 6 my $p = $statement->predicate->uri ;
337 6 push @{ $index->{$s}->{$p} }, $statement->object->as_hashref;
338 }
339 2 return $index;
340 }
341  
342 =item C<< construct_args >>
343  
344 Returns the arguments necessary to pass to the stream constructor _new
345 to re-create this stream (assuming the same closure as the first
346  
347 =cut
348  
349 39 ▶sub construct_args {
Subroutine construct_args: called CC=2 SCAR=7.0
Pod documented
350 39 my $self = shift;
351 39 my $type = $self->type;
352 39 ▶ my $args = $self->_args || {};
Condition50%1/2
$self->_args || {}
l!l
390
MC/DC0%0/1
$self->_args || {}
$self->_args
Truth table50%1/2
A
A= $self->_args
Aresult
11
00
353 39 return ($type, [], %{ $args });
354 }
355  
356  
357 68 1;
358  
359 __END__
« RDF/Trine/Parser/NTriples.pm ↑ Summary RDF/Trine/Serializer/TriG.pm »