RDF/Trine/Iterator/Bindings.pm

filter: Statementstmt 78.2% 262 / 335 Branchbran 53.1% 50 / 94 Conditioncond 50.0% 16 / 32 MC/DCmcdc 26.3% 5 / 19 Subroutinesub 95.3% 41 / 43 Podpod 100% 20 / 20 totaltotal 72.5% 394 / 543 CC 65 SCAR 27.2
CC
65
Cov
71%
as_string
90.0
format_node_json
30.0
join_streams
25.8
CRAP 15.1
« RDF/Trine/Parser/RDFJSON.pm ↑ Summary RDF/Trine/Model/Dataset.pm »
1 # RDF::Trine::Iterator::Bindings
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Iterator::Bindings - Iterator class for bindings query results
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Iterator::Bindings version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Iterator::Bindings;
15
16 my $iterator = RDF::Trine::Iterator::Bindings->new( \&data, \@names );
17 while (my $row = $iterator->next) {
18 # $row is a HASHref containing variable name -> RDF Term bindings
19 my @vars = keys %$row;
20 print $row->{ 'var' }->as_string;
21 }
22  
23 =head1 METHODS
24  
25 Beyond the methods documented below, this class inherits methods from the
26 L<RDF::Trine::Iterator> class.
27  
28 =over 4
29  
30 =cut
31  
32 package RDF::Trine::Iterator::Bindings;
33  
34 68 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
38 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 68 ▶use JSON 2.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶use Text::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
44 68 ▶use RDF::Trine::Iterator::Bindings::Materialized;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 68 ▶use RDF::Trine::Serializer::Turtle;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 68 ▶use RDF::Trine::Iterator qw(smap);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 68 ▶use base qw(RDF::Trine::Iterator);
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 68 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 68 our ($VERSION);
53 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
54 68 $VERSION = '1.019';
55 }
56  
57 =item C<new ( \@results, \@names, %args )>
58  
59 =item C<new ( \&results, \@names, %args )>
60  
61 Returns a new SPARQL Result interator object. Results must be either
62 a reference to an array containing results or a CODE reference that
63 acts as an iterator, returning successive items when called, and
64 returning undef when the iterator is exhausted.
65  
66 =cut
67  
68 1926 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
69 1926 my $class = shift;
70 1926 ▶ my $stream = shift || sub { undef };
Subroutine __ANON__: called CC=1 SCAR=0.1
Condition66%2/3
shift() || sub { undef; }
l!l&&r!l&&!r
192510
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
71 1926 ▶ my $names = shift || [];
Condition100%2/2
shift() || []
l!l
1021905
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
72 1926 my %args = @_;
73
74 1926 my $type = 'bindings';
75 1926 my $self = $class->SUPER::new( $stream, $type, $names, %args );
76
77 1926 ▶ my $s = $args{ sorted_by } || [];
Condition100%2/2
$args{'sorted_by'} || []
l!l
9231003
MC/DC100%1/1
$args{'sorted_by'} || []
$args{'sorted_by'}
Truth table100%2/2
A
A= $args{'sorted_by'}
Aresult
11
00
78 1926 $self->{sorted_by} = $s;
79 1926 return $self;
80 }
81  
82 10 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
83 10 my $class = shift;
84 10 my $stream = shift;
85 10 my $type = shift;
86 10 my $names = shift;
87 10 my %args = @_;
88 10 return $class->new( $stream, $names, %args );
89 }
90  
91 =item C<< materialize >>
92  
93 Returns a materialized version of the current binding iterator.
94 The materialization process will leave this iterator empty. The materialized
95 iterator that is returned should be used for any future need for the iterator's
96 data.
97  
98 =cut
99  
100 3 ▶sub materialize {
Subroutine materialize: called CC=1 SCAR=0.0
Pod documented
101 3 my $self = shift;
102 3 my @data = $self->get_all;
103 3 my @args = $self->construct_args;
104 3 return $self->_mclass->_new( \@data, @args );
105 }
106  
107 3 ▶sub _mclass {
Subroutine _mclass: called CC=1 SCAR=0.0
108 3 return 'RDF::Trine::Iterator::Bindings::Materialized';
109 }
110  
111 =item C<< project ( @columns ) >>
112  
113 Returns a new stream that projects the current bindings to only the given columns.
114  
115 =cut
116  
117 8 ▶sub project {
Subroutine project: called CC=1 SCAR=0.0
Pod documented
118 8 my $self = shift;
119 8 my $class = ref($self);
120 8 my @proj = @_;
121
122 30 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
123 30 my $row = $self->next;
124 30 ▶ return unless ($row);
Branch: 1 branch
BranchTrueFalseTotal
unless $row 8 22 30
125 22 my $p = { map { $_ => $row->{ $_ } } @proj };
126 22 return $p;
127 8 };
128
129 8 my $args = $self->_args();
130 8 my $proj = $class->new( $sub, [@proj], %$args );
131 8 return $proj;
132 }
133  
134 =item C<join_streams ( $stream, $stream )>
135  
136 Performs a natural, nested loop join of the two streams, returning a new stream
137 of joined results.
138  
139 =cut
140  
141 3 ▶sub join_streams {
Subroutine join_streams: called CC=7 SCAR=32.5
Pod documented
142 3 my $self = shift;
143 3 my $a = shift;
144 3 my $b = shift;
145 # my $bridge = shift;
146 3 my %args = @_;
147
148 # Carp::confess unless ($a->isa('RDF::Trine::Iterator::Bindings'));
149 # Carp::confess unless ($b->isa('RDF::Trine::Iterator::Bindings'));
150 3 my $l = Log::Log4perl->get_logger("rdf.trine.iterator.bindings");
151
152 3 my @join_sorted_by;
153 3 ▶ if (my $o = $args{ orderby }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $args{"orderby"}) 0 3 3
154 0 my $req_sort = join(',', map { $_->[1]->name => $_->[0] } @$o);
155 0 my $a_sort = join(',', $a->sorted_by);
156 0 my $b_sort = join(',', $b->sorted_by);
157
158 0 ▶ if ($l->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_debug) 0 0 0
159 0 $l->debug('---------------------------');
160 0 $l->debug('REQUESTED SORT in JOIN: ' . Dumper($req_sort));
161 0 $l->debug('JOIN STREAM SORTED BY: ' . Dumper($a_sort));
162 0 $l->debug('JOIN STREAM SORTED BY: ' . Dumper($b_sort));
163 }
164 0 my $actual_sort;
165 0 ▶ if (substr( $a_sort, 0, length($req_sort) ) eq $req_sort) {
Branch: 2 branches
BranchTrueFalseTotal
if (substr($a_sort, 0, length $req_sort) eq $req_sort) { } 0 0 0
elsif (substr($b_sort, 0, length $req_sort) eq $req_sort) { } 0 0 0
166 0 $l->debug("first stream is already sorted. using it in the outer loop.");
167 } elsif (substr( $b_sort, 0, length($req_sort) ) eq $req_sort) {
168 0 $l->debug("second stream is already sorted. using it in the outer loop.");
169 0 ($a,$b) = ($b,$a);
170 } else {
171 0 my $a_common = join('!', $a_sort, $req_sort);
172 0 my $b_common = join('!', $b_sort, $req_sort);
173 0 ▶ if ($a_common =~ qr[^([^!]+)[^!]*!\1]) { # shared prefix between $a_sort and $req_sort?
Branch: 2 branches
BranchTrueFalseTotal
if ($a_common =~ qr/^([^!]+)[^!]*!\1/) { } 0 0 0
elsif ($b_common =~ qr/^([^!]+)[^!]*!\1/) { } 0 0 0
174 0 $l->debug("first stream is closely sorted ($1).");
175 } elsif ($b_common =~ qr[^([^!]+)[^!]*!\1]) { # shared prefix between $b_sort and $req_sort?
176 0 $l->debug("second stream is closely sorted ($1).");
177 0 ($a,$b) = ($b,$a);
178 }
179 }
180 0 @join_sorted_by = ($a->sorted_by, $b->sorted_by);
181 }
182
183 3 my $stream = $self->nested_loop_join( $a, $b, %args );
184 3 $l->debug("JOINED stream is sorted by: " . join(',', @join_sorted_by));
185 3 $stream->{sorted_by} = \@join_sorted_by;
186 3 return $stream;
187 }
188  
189 =item C<< nested_loop_join ( $outer, $inner ) >>
190  
191 Performs a natural, nested loop join of the two streams, returning a new stream
192 of joined results.
193  
194 Note that the values from the C<< $inner >> iterator are fully materialized for
195 this join, and the results of the join are in the order of values from the
196 C<< $outer >> iterator. This suggests that:
197  
198 * If sorting needs to be preserved, the C<< $outer >> iterator should be used to
199 determine the result ordering.
200  
201 * If one iterator is much smaller than the other, it should likely be used as
202 the C<< $inner >> iterator since materialization will require less total memory.
203  
204 =cut
205  
206 3 ▶sub nested_loop_join {
Subroutine nested_loop_join: called CC=1 SCAR=0.0
Pod documented
207 3 my $self = shift;
208 3 my $astream = shift;
209 3 my $bstream = shift;
210 # my $bridge = shift;
211 3 my %args = @_;
212
213 # Carp::confess unless ($astream->isa('RDF::Trine::Iterator::Bindings'));
214 # Carp::confess unless ($bstream->isa('RDF::Trine::Iterator::Bindings'));
215 3 my $l = Log::Log4perl->get_logger("rdf.trine.iterator.bindings");
216
217 3 my @names = RDF::Trine::_uniq( map { $_->binding_names() } ($astream, $bstream) );
218 3 my $a = $astream->project( @names );
219 3 my $b = $bstream->project( @names );
220
221
222 3 my @data = $b->get_all();
223 68 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
224
225 3 my $inner_index;
226 my $rowa;
227 3 my $need_new_a = 1;
228 15 ▶ my $sub = sub {
Subroutine __ANON__: called CC=12 SCAR=24.9
229 15 OUTER: while (1) {
230 24 ▶ if ($need_new_a) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_new_a) 12 12 24
231 12 $l->debug("### fetching new outer tuple");
232 12 $rowa = $a->next;
233 12 $inner_index = 0;
234 12 $need_new_a = 0;
235 }
236 24 $l->debug("OUTER: " . Dumper($rowa));
237 24 ▶ return unless ($rowa);
Branch: 1 branch
BranchTrueFalseTotal
unless $rowa 3 21 24
238 21 ▶ LOOP: while ($inner_index <= $#data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inner_index <= $#data) 28 9 37
239 28 my $rowb = $data[ $inner_index++ ];
240 28 $l->debug("- INNER[ $inner_index ]: " . Dumper($rowb));
241 28 ▶ $l->debug("[--JOIN--] " . join(' ', map { my $row = $_; '{' . join(', ', map { join('=', $_, ($row->{$_}) ? $row->{$_}->as_string : '(undef)') } (keys %$row)) . '}' } ($rowa, $rowb)));
Branch: 1 branch
BranchTrueFalseTotal
$row->{$_} ? : 80 32 112
242 28 my %keysa = map {$_=>1} (keys %$rowa);
243 28 my @shared = grep { $keysa{ $_ } } (keys %$rowb);
244 28 foreach my $key (@shared) {
245 43 my $val_a = $rowa->{ $key };
246 43 my $val_b = $rowb->{ $key };
247 43 my $defined = 0;
248 43 foreach my $n ($val_a, $val_b) {
249 86 ▶ $defined++ if (defined($n));
Branch: 1 branch
BranchTrueFalseTotal
if defined $n 67 19 86
250 }
251 43 ▶ if ($defined == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($defined == 2) 24 19 43
252 24 my $equal = $val_a->equal( $val_b );
253 24 ▶ unless ($equal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($equal) 16 8 24
254 16 $l->debug("can't join because mismatch of $key (" . join(' <==> ', map {$_->as_string} ($val_a, $val_b)) . ")");
255 16 next LOOP;
256 }
257 }
258 }
259
260 12 my $row = { (map { $_ => $rowa->{$_} } grep { defined($rowa->{$_}) } keys %$rowa), (map { $_ => $rowb->{$_} } grep { defined($rowb->{$_}) } keys %$rowb) };
261 12 ▶ if ($l->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_debug) 0 12 12
262 0 $l->debug("JOINED:");
263 0 foreach my $key (keys %$row) {
264 0 $l->debug("$key\t=> " . $row->{ $key }->as_string);
265 }
266 }
267 12 return $row;
268 }
269 9 $need_new_a = 1;
270 }
271 3 };
272
273 3 my $args = $astream->_args;
274 3 return $astream->_new( $sub, 'bindings', \@names, %$args );
275 }
276  
277  
278 =item C<< sorted_by >>
279  
280 =cut
281  
282 21 ▶sub sorted_by {
Subroutine sorted_by: called CC=1 SCAR=0.0
Pod documented
283 21 my $self = shift;
284 21 my $sorted = $self->{sorted_by};
285 21 return @$sorted;
286 }
287  
288 =item C<< binding_value_by_name ( $name ) >>
289  
290 Returns the binding of the named variable in the current result.
291  
292 =cut
293  
294 4 ▶sub binding_value_by_name {
Subroutine binding_value_by_name: called CC=3 SCAR=11.8
Pod documented
295 4 my $self = shift;
296 4 my $name = shift;
297 4 ▶ my $row = ($self->open) ? $self->current : $self->next;
Branch: 1 branch
BranchTrueFalseTotal
$self->open ? : 4 0 4
298 4 ▶ if (exists( $row->{ $name } )) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $row->{$name}) { } 4 0 4
299 4 return $row->{ $name };
300 } else {
301 # warn "No variable named '$name' is present in query results.\n";
302 0 return;
303 }
304 }
305  
306 =item C<< binding_value ( $i ) >>
307  
308 Returns the binding of the $i-th variable in the current result.
309  
310 =cut
311  
312 2 ▶sub binding_value {
Subroutine binding_value: called CC=1 SCAR=0.0
Pod documented
313 2 my $self = shift;
314 2 my $val = shift;
315 2 my @names = $self->binding_names;
316 2 return $self->binding_value_by_name( $names[ $val ] );
317 }
318  
319  
320 =item C<binding_values>
321  
322 Returns a list of the binding values from the current result.
323  
324 =cut
325  
326 2 ▶sub binding_values {
Subroutine binding_values: called CC=2 SCAR=7.0
Pod documented
327 2 my $self = shift;
328 2 ▶ my $row = ($self->open) ? $self->current : $self->next;
Branch: 1 branch
BranchTrueFalseTotal
$self->open ? : 2 0 2
329 2 return @{ $row }{ $self->binding_names };
330 }
331  
332  
333 =item C<binding_names>
334  
335 Returns a list of the binding names.
336  
337 =cut
338  
339 22 ▶sub binding_names {
Subroutine binding_names: called CC=1 SCAR=0.0
Pod documented
340 22 my $self = shift;
341 22 my $names = $self->{_names};
342 22 return @$names;
343 }
344  
345 =item C<binding_name ( $i )>
346  
347 Returns the name of the $i-th result column.
348  
349 =cut
350  
351 14 ▶sub binding_name {
Subroutine binding_name: called CC=1 SCAR=0.0
Pod documented
352 14 my $self = shift;
353 14 my $val = shift;
354 14 my $names = $self->{_names};
355 14 return $names->[ $val ];
356 }
357  
358  
359 =item C<bindings_count>
360  
361 Returns the number of variable bindings in the current result.
362  
363 =cut
364  
365 5 ▶sub bindings_count {
Subroutine bindings_count: called CC=2 SCAR=6.9
Pod documented
366 5 my $self = shift;
367 5 my $names = $self->{_names};
368 5 ▶ return scalar( @$names ) if (scalar(@$names));
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$names 4 1 5
369 1 return 0;
370 }
371  
372 =item C<is_bindings>
373  
374 Returns true if the underlying result is a set of variable bindings.
375  
376 =cut
377  
378 3 ▶sub is_bindings {
Subroutine is_bindings: called CC=1 SCAR=0.0
Pod documented
379 3 my $self = shift;
380 3 return 1;
381 }
382  
383 =item C<as_json ( $max_size )>
384  
385 Returns a JSON serialization of the stream data.
386  
387 =cut
388  
389 2 ▶sub as_json {
Subroutine as_json: called CC=9 SCAR=26.7
Pod documented
390 2 my $self = shift;
391 2 ▶ my $max_result_size = shift || 0;
Condition50%1/2
shift() || 0
l!l
02
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
392 2 my $width = $self->bindings_count;
393 # my $bridge = $self->bridge;
394
395 2 my @variables;
396 2 ▶ for (my $i=0; $i < $width; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $width) 3 2 5
397 3 my $name = $self->binding_name($i);
398 3 ▶ push(@variables, $name) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 3 0 3
399 }
400
401 2 my $count = 0;
402 2 my @sorted = $self->sorted_by;
403 2 ▶ my $order = scalar(@sorted) ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
scalar @sorted ? : 0 2 2
404 2 ▶ my $dist = $self->_args->{distinct} ? JSON::true : JSON::false;
Branch: 1 branch
BranchTrueFalseTotal
$self->_args->{'distinct'} ? : 0 2 2
405
406 2 my $data = {
407 head => { vars => \@variables },
408 results => { ordered => $order, distinct => $dist, bindings => [] },
409 };
410 2 my @bindings;
411 2 ▶ while (my $row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next) 0 2 2
412 0 my %row = map { format_node_json($row->{$_}, $_) } (keys %$row);
413 0 push(@{ $data->{results}{bindings} }, \%row);
414 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
415 }
416
417 2 return to_json( $data );
418 }
419  
420 =item C<as_xml ( $max_size )>
421  
422 Returns an XML serialization of the stream data.
423  
424 =cut
425  
426 1 ▶sub as_xml {
Subroutine as_xml: called CC=2 SCAR=7.0
Pod documented
427 1 my $self = shift;
428 1 ▶ my $max_result_size = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
429 1 my $string = '';
430 1 open( my $fh, '>', \$string );
431 1 $self->print_xml( $fh, $max_result_size );
432 1 close($fh);
433 1 return $string;
434 }
435  
436 =item C<as_string ( $max_size [, \$count] )>
437  
438 Returns a string table serialization of the stream data.
439  
440 =cut
441  
442 0 ▶sub as_string {
Subroutine as_string: not called CC=9 SCAR=45.0
Pod documented
443 0 my $self = shift;
444 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
445 0 my $rescount = shift;
446 0 my @names = $self->binding_names;
447 0 my $headers = \@names;
448 0 my @rows;
449 0 my $count = 0;
450 0 ▶ while (my $row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next) 0 0 0
451 0 ▶ push(@rows, [ map { blessed($_) ? RDF::Trine::Serializer::Turtle->node_as_concise_string($_) : '' } @{ $row }{ @names } ]);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 0 0 0
452 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
453 }
454 0 ▶ if (ref($rescount)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $rescount) 0 0 0
455 0 $$rescount = scalar(@rows);
456 }
457
458 0 my @rule = qw(- +);
459 0 my @headers = (\q"| ");
460 0 push(@headers, map { $_ => \q" | " } @$headers);
461 0 pop @headers;
462 0 push @headers => (\q" |");
463
464 0 ▶ if ('ARRAY' eq ref $rows[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $rows[0]) { } 0 0 0
465 0 ▶ if (@$headers == @{ $rows[0] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$headers == @{$rows[0];}) { } 0 0 0
466 0 my $table = Text::Table->new(@headers);
467 0 $table->rule(@rule);
468 0 $table->body_rule(@rule);
469 0 $table->load(@rows);
470
471 return join('',
472 $table->rule(@rule),
473 $table->title,
474 $table->rule(@rule),
475 0 map({ $table->body($_) } 0 .. @rows),
476 $table->rule(@rule)
477 );
478 } else {
479 0 die("make_table() rows must be an AoA with rows being same size as headers");
480 }
481 } else {
482 0 return '';
483 }
484 }
485  
486 =item C<< as_statements ( $pattern | @names ) >>
487  
488 Returns a L<RDF::Trine::Iterator::Graph> with the statements of the stream.
489  
490 If C<$pattern>, an RDF::Trine::Pattern object, is given as an argument, each of
491 its triples are instantiated with variable bindings from each row of the
492 iterator, and returned as RDF::Trine::Statement objects from a new
493 RDF::Trine::Iterator::Graph iterator.
494  
495 If 3 variable C<@names> are supplied, their corresponding variable bindings
496 in each row of the iterator are used (in order) as the subject, predicate, and
497 object of new RDF::Trine::Statement objects and returned from a new
498 RDF::Trine::Iterator::Graph iterator.
499  
500 =cut
501  
502 867 ▶sub as_statements {
Subroutine as_statements: called CC=3 SCAR=11.0
Pod documented
503 867 my $self = shift;
504 867 my @names = @_;
505 867 ▶ if (scalar(@names) == 1 and $names[0]->can('triples')) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @names == 1 and $names[0]->can('triples')) { } 1 866 867
Condition66%2/3
scalar @names == 1 and $names[0]->can("triples")
!ll&&!rl&&r
86601
MC/DC50%1/2
scalar @names == 1 and $names[0]->can("triples")
scalar @names == 1 $names[0]->can("triples")
Truth table66%2/3
A and B
A= scalar @names == 1B= $names[0]->can("triples")
ABresult
0X0
100
111
506 1 my $pattern = shift;
507 1 my @triples = $pattern->triples;
508 1 my @queue;
509 4 ▶ my $sub = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
510 4 while (1) {
511 6 ▶ if (scalar(@queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @queue) 3 3 6
512 3 return shift(@queue);
513 }
514 3 my $row = $self->next;
515 3 ▶ return unless (defined $row);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 1 2 3
516 2 foreach my $t (@triples) {
517 4 my $st = $t->bind_variables($row);
518 4 ▶ if ($st->rdf_compatible) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->rdf_compatible) 3 1 4
519 3 push(@queue, $st);
520 }
521 }
522 }
523 1 };
524 1 return RDF::Trine::Iterator::Graph->new( $sub );
525 } else {
526 2516 ▶ my $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
527 2516 my $row = $self->next;
528 2516 ▶ return unless (defined $row);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row 866 1650 2516
529 1650 my @values = @{ $row }{ @names };
530 1650 ▶ my $statement = (scalar(@values) == 3 or not(defined($values[3])))
Branch: 1 branch
BranchTrueFalseTotal
scalar @values == 3 || !defined($values[3]) ? : 227 1423 1650
Condition100%3/3
scalar @values == 3 or not defined $values[3]
l!l&&r!l&&!r
211161423
MC/DC100%2/2
scalar @values == 3 or not defined $values[3]
scalar @values == 3 not defined $values[3]
Truth table100%3/3
A or not(B)
A= scalar @values == 3B= not defined $values[3]
ABresult
1X1
011
000
531 ? RDF::Trine::Statement->new( @values[ 0 .. 2 ] )
532 : RDF::Trine::Statement::Quad->new( @values );
533 1650 return $statement;
534 866 };
535 866 return RDF::Trine::Iterator::Graph->new( $sub );
536 }
537 }
538  
539 =item C<< print_xml ( $fh, $max_size ) >>
540  
541 Prints an XML serialization of the stream data to the filehandle $fh.
542  
543 =cut
544  
545 1 ▶sub print_xml {
Subroutine print_xml: called CC=9 SCAR=22.1
Pod documented
546 1 my $self = shift;
547 1 my $fh = shift;
548 1 ▶ my $max_result_size = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
549 1 my $width = $self->bindings_count;
550
551 1 my @variables;
552 1 ▶ for (my $i=0; $i < $width; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $width) 3 1 4
553 3 my $name = $self->binding_name($i);
554 3 ▶ push(@variables, $name) if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 3 0 3
555 }
556
557 1 print {$fh} <<"END";
558 <?xml version="1.0" encoding="utf-8"?>
559 <sparql xmlns="http://www.w3.org/2005/sparql-results#">
560 <head>
561 END
562
563 1 my $t = join("\n", map { qq(\t<variable name="$_"/>) } @variables);
564
565 1 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 1 0 1
566 1 print {$fh} "${t}\n";
567 }
568
569 1 print {$fh} <<"END";
570 </head>
571 <results>
572 END
573
574 1 my $count = 0;
575 1 ▶ while (my $row = $self->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $self->next) 2 1 3
576 2 my @row;
577 2 print {$fh} "\t\t<result>\n";
578 2 ▶ for (my $i = 0; $i < $width; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $width) 6 2 8
579 6 my $name = $self->binding_name($i);
580 6 my $value = $row->{ $name };
581 6 print {$fh} "\t\t\t" . $self->format_node_xml($value, $name) . "\n";
582 }
583 2 print {$fh} "\t\t</result>\n";
584
585 2 ▶ last if ($max_result_size and ++$count >= $max_result_size);
Branch: 1 branch
BranchTrueFalseTotal
if $max_result_size and ++$count >= $max_result_size 0 2 2
Condition33%1/3
$max_result_size and ++$count >= $max_result_size
!ll&&!rl&&r
200
MC/DC0%0/2
$max_result_size and ++$count >= $max_result_size
$max_result_size ++$count >= $max_result_size
Truth table33%1/3
A and B
A= $max_result_sizeB= ++$count >= $max_result_size
ABresult
0X0
100
111
586 }
587
588 1 print {$fh} "</results>\n";
589 1 print {$fh} "</sparql>\n";
590 }
591  
592 =begin private
593  
594 =item C<format_node_json ( $node, $name )>
595  
596 Returns a string representation of C<$node> for use in a JSON serialization.
597  
598 =end private
599  
600 =cut
601  
602 0 ▶sub format_node_json {
Subroutine format_node_json: not called CC=5 SCAR=34.0
Pod documented
603 # my $bridge = shift;
604 # return unless ($bridge);
605
606 0 my $node = shift;
607 0 my $name = shift;
608 0 my $node_label;
609
610 0 ▶ if(!defined $node) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $node) { } 0 0 0
elsif ($node->isa('RDF::Trine::Node::Resource')) { } 0 0 0
elsif ($node->isa('RDF::Trine::Node::Literal')) { } 0 0 0
elsif ($node->isa('RDF::Trine::Node::Blank')) { } 0 0 0
611 0 return;
612 } elsif ($node->isa('RDF::Trine::Node::Resource')) {
613 0 $node_label = $node->uri_value;
614 0 return $name => { type => 'uri', value => $node_label };
615 } elsif ($node->isa('RDF::Trine::Node::Literal')) {
616 0 $node_label = $node->literal_value;
617 0 return $name => { type => 'literal', value => $node_label };
618 } elsif ($node->isa('RDF::Trine::Node::Blank')) {
619 0 $node_label = $node->blank_identifier;
620 0 return $name => { type => 'bnode', value => $node_label };
621 } else {
622 0 return;
623 }
624 }
625  
626 =item C<< construct_args >>
627  
628 Returns the arguments necessary to pass to the stream constructor _new
629 to re-create this stream (assuming the same closure as the first
630 argument).
631  
632 =cut
633  
634 8 ▶sub construct_args {
Subroutine construct_args: called CC=2 SCAR=7.0
Pod documented
635 8 my $self = shift;
636 8 my $type = $self->type;
637 8 my @names = $self->binding_names;
638 8 ▶ my $args = $self->_args || {};
Condition50%1/2
$self->_args || {}
l!l
80
MC/DC0%0/1
$self->_args || {}
$self->_args
Truth table50%1/2
A
A= $self->_args
Aresult
11
00
639 8 return ($type, \@names, %{ $args });
640 }
641  
642  
643 68 1;
644  
645 __END__
« RDF/Trine/Parser/RDFJSON.pm ↑ Summary RDF/Trine/Model/Dataset.pm »