Query/Algebra/BasicGraphPattern.pm

filter: Statementstmt 73.3% 143 / 195 Branchbran 75.0% 27 / 36 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 77.4% 24 / 31 Podpod 100% 18 / 18 totaltotal 74.4% 219 / 294 CC 42 SCAR 24.0
CC
42
Cov
73%
bf
56.0
connected
15.0
subsumes
8.2
CRAP 11.0
« Query/Plan/ComputedStatement.pm ↑ Summary Query/Plan/Clear.pm »
1 # RDF::Query::Algebra::BasicGraphPattern
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Algebra::BasicGraphPattern - Algebra class for BasicGraphPattern patterns
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Algebra::BasicGraphPattern version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Algebra::BasicGraphPattern;
15  
16 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶use base qw(RDF::Query::Algebra);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 36 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Scalar::Util qw(blessed refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 36 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 36 ▶use RDF::Trine::Iterator qw(smap swatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 ######################################################################
29  
30 36 our ($VERSION);
31 36 my %AS_SPARQL;
32 36 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 36 $VERSION = '2.919';
34 }
35  
36 ######################################################################
37  
38 =head1 METHODS
39  
40 Beyond the methods documented below, this class inherits methods from the
41 L<RDF::Query::Algebra> class.
42  
43 =over 4
44  
45 =cut
46  
47 =item C<new ( @triples )>
48  
49 Returns a new BasicGraphPattern structure.
50  
51 =cut
52  
53 233 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
54 233 my $class = shift;
55 233 my @triples = @_;
56 233 foreach my $t (@triples) {
57 419 ▶ unless ($t->isa('RDF::Trine::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($t->isa("RDF::Trine::Statement")) 0 419 419
58 0 throw RDF::Query::Error::QueryPatternError -text => "Patterns belonging to a BGP must be graph statements";
59 }
60 }
61 233 return bless( [ @triples ] );
62 }
63  
64 =item C<< construct_args >>
65  
66 Returns a list of arguments that, passed to this class' constructor,
67 will produce a clone of this algebra pattern.
68  
69 =cut
70  
71 546 ▶sub construct_args {
Subroutine construct_args: called CC=1 SCAR=0.0
Pod documented
72 546 my $self = shift;
73 546 return ($self->triples);
74 }
75  
76 =item C<< triples >>
77  
78 Returns a list of triples belonging to this BGP.
79  
80 =cut
81  
82 1697 ▶sub triples {
Subroutine triples: called CC=1 SCAR=0.0
Pod documented
83 1697 my $self = shift;
84 1697 return @$self;
85 }
86  
87 =item C<< quads >>
88  
89 Returns a list of the (implicit) quads belonging to this BGP.
90  
91 =cut
92  
93 14 ▶sub quads {
Subroutine quads: called CC=5 SCAR=16.2
Pod documented
94 14 my $self = shift;
95 14 my @triples = $self->triples;
96 14 my @quads;
97 14 foreach my $t (@triples) {
98 35 my @nodes = $t->nodes;
99 35 foreach my $i (0 .. 3) {
100 140 my $n = $nodes[ $i ];
101 140 ▶ if (not blessed($n)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($n)) 21 119 140
102 21 ▶ if ($i == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 3) { } 21 0 21
103 21 $nodes[ $i ] = RDF::Trine::Node::Nil->new();
104 } else {
105 0 $nodes[ $i ] = RDF::Query::Node::Variable->new();
106 }
107 }
108 }
109 35 my $st = RDF::Trine::Statement::Quad->new( @nodes );
110 35 push(@quads, $st);
111 }
112 14 return @quads;
113 }
114  
115 =item C<< sse >>
116  
117 Returns the SSE string for this algebra expression.
118  
119 =cut
120  
121 634 ▶sub sse {
Subroutine sse: called CC=3 SCAR=11.0
Pod documented
122 634 my $self = shift;
123 634 my $context = shift;
124 634 ▶ my $prefix = shift || '';
Condition100%2/2
shift() || ''
l!l
339295
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
125 634 ▶ my $indent = $context->{indent} || ' ';
Condition100%2/2
$context->{'indent'} || ' '
l!l
6331
MC/DC100%1/1
$context->{'indent'} || ' '
$context->{'indent'}
Truth table100%2/2
A
A= $context->{'indent'}
Aresult
11
00
126
127 634 my @triples = sort map { $_->sse( $context ) } $self->triples;
128 634 return sprintf(
129 "(BGP\n${prefix}${indent}%s\n${prefix})",
130 join("\n${prefix}${indent}", @triples)
131 );
132 }
133  
134 =item C<< explain >>
135  
136 Returns a string serialization of the algebra appropriate for display on the
137 command line.
138  
139 =cut
140  
141 0 ▶sub explain {
Subroutine explain: not called CC=2 SCAR=17.9
Pod documented
142 0 my $self = shift;
143 0 my $s = shift;
144 0 my $count = shift;
145 0 my $indent = $s x $count;
146 0 my $string = "${indent}basic graph pattern\n";
147
148 0 foreach my $t ($self->triples) {
149 0 $string .= "${indent}${s}" . $t->as_sparql . "\n";
150 }
151 0 return $string;
152 }
153  
154  
155 =item C<< as_sparql >>
156  
157 Returns the SPARQL string for this algebra expression.
158  
159 =cut
160  
161 55 ▶sub as_sparql {
Subroutine as_sparql: called CC=4 SCAR=13.9
Pod documented
162 55 my $self = shift;
163 55 ▶ if (exists $AS_SPARQL{ refaddr( $self ) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $AS_SPARQL{builtin::refaddr($self)}) { } 11 44 55
164 11 return $AS_SPARQL{ refaddr( $self ) };
165 } else {
166 44 my $context = shift;
167 # if (ref($context)) {
168 # $context = { %$context };
169 # }
170 44 ▶ my $indent = shift || '';
Condition50%1/2
shift() || ''
l!l
440
MC/DC0%0/1
shift() || ''
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
171 44 my @triples;
172 44 foreach my $t ($self->triples) {
173 51 push(@triples, $t->as_sparql( $context, $indent ));
174 }
175 44 my $string = join("\n${indent}", @triples);
176 44 $AS_SPARQL{ refaddr( $self ) } = $string;
177 44 return $string;
178 }
179 }
180  
181 =item C<< as_hash >>
182  
183 Returns the query as a nested set of plain data structures (no objects).
184  
185 =cut
186  
187 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
188 0 my $self = shift;
189 0 my $context = shift;
190 return {
191 type => lc($self->type),
192 0 patterns => [ map { $_->as_hash } $self->triples ],
193 };
194 }
195  
196 =item C<< as_spin ( $model ) >>
197  
198 Adds statements to the given model to represent this algebra object in the
199 SPARQL Inferencing Notation (L<http://www.spinrdf.org/>).
200  
201 =cut
202  
203 0 ▶sub as_spin {
Subroutine as_spin: not called CC=1 SCAR=6.9
Pod documented
204 0 my $self = shift;
205 0 my $model = shift;
206 0 my @t = $self->triples;
207 0 my @nodes = map { $_->as_spin( $model ) } @t;
208 0 return @nodes;
209 }
210  
211 =item C<< type >>
212  
213 Returns the type of this algebra expression.
214  
215 =cut
216  
217 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
218 0 return 'BGP';
219 }
220  
221 =item C<< referenced_variables >>
222  
223 Returns a list of the variable names used in this algebra expression.
224  
225 =cut
226  
227 67 ▶sub referenced_variables {
Subroutine referenced_variables: called CC=1 SCAR=0.0
Pod documented
228 67 my $self = shift;
229 67 return RDF::Query::_uniq(map { $_->referenced_variables } $self->triples);
230 }
231  
232 =item C<< potentially_bound >>
233  
234 Returns a list of the variable names used in this algebra expression that will
235 bind values during execution.
236  
237 =cut
238  
239 33 ▶sub potentially_bound {
Subroutine potentially_bound: called CC=1 SCAR=0.0
Pod documented
240 33 my $self = shift;
241 33 return RDF::Query::_uniq(map { $_->potentially_bound } $self->triples);
242 }
243  
244 =item C<< definite_variables >>
245  
246 Returns a list of the variable names that will be bound after evaluating this algebra expression.
247  
248 =cut
249  
250 4 ▶sub definite_variables {
Subroutine definite_variables: called CC=1 SCAR=0.0
Pod documented
251 4 my $self = shift;
252 4 return RDF::Query::_uniq(map { $_->definite_variables } $self->triples);
253 }
254  
255 193 ▶sub _referenced_blanks {
Subroutine _referenced_blanks: called CC=3 SCAR=11.0
256 193 my $self = shift;
257 193 my %seen;
258 193 foreach my $t ($self->triples) {
259 336 my @blanks = $t->referenced_blanks;
260 336 foreach my $b (@blanks) {
261 61 $seen{ $b }++;
262 }
263 }
264 193 return [keys %seen];
265 }
266  
267 =item C<< connected >>
268  
269 Returns true if the pattern is connected through shared variables, false otherwise.
270  
271 =cut
272  
273 7 ▶sub connected {
Subroutine connected: called CC=15 SCAR=27.1
Pod documented
274 7 my $self = shift;
275 7 my @triples = $self->triples;
276 7 ▶ return 1 unless (scalar(@triples) > 1);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @triples > 1 1 6 7
277
278 6 my %index;
279 my %variables;
280 6 foreach my $i (0 .. $#triples) {
281 22 my $t = $triples[ $i ];
282 22 $index{ $t->as_string } = $i;
283 22 foreach my $n ($t->nodes) {
284 66 ▶ next unless ($n->isa('RDF::Trine::Node::Variable'));
Branch: 1 branch
BranchTrueFalseTotal
unless $n->isa("RDF::Trine::Node::Variable") 21 45 66
285 45 push( @{ $variables{ $n->name } }, $t );
286 }
287 }
288
289 6 my @connected;
290 6 foreach my $i (0 .. $#triples) {
291 22 foreach my $j (0 .. $#triples) {
292 90 ▶ $connected[ $i ][ $j ] = ($i == $j) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$i == $j ? : 22 68 90
293 }
294 }
295
296 6 my %seen;
297 6 my @queue = $triples[0];
298 6 ▶ while (my $t = shift(@queue)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = shift @queue) 77 6 83
299 77 my $string = $t->as_string;
300 77 ▶ next if ($seen{ $string }++);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$string}++ 59 18 77
301 18 my @vars = map { $_->name } grep { $_->isa('RDF::Trine::Node::Variable') } $t->nodes;
302 18 my @connected_to = map { @{ $variables{ $_ } } } @vars;
303 18 foreach my $c (@connected_to) {
304 71 my $cstring = $c->as_string;
305 71 my $i = $index{$string};
306
307 71 my $k = $index{ $cstring };
308 71 my @conn = @{ $connected[$i] };
309 71 $conn[ $k ] = 1;
310 71 foreach my $j (0 .. $#triples) {
311 299 ▶ if ($conn[ $j ] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn[$j] == 1) 225 74 299
312 225 $connected[ $k ][ $j ] = 1;
313 225 $connected[ $j ][ $k ] = 1;
314 }
315 }
316 71 push(@queue, $c);
317 }
318 }
319
320 6 foreach my $i (0 .. $#triples) {
321 20 ▶ return 0 unless ($connected[0][$i] == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless $connected[0][$i] == 1 2 18 20
322 }
323 4 return 1;
324 }
325  
326 =item C<< subsumes ( $pattern ) >>
327  
328 Returns true if the bgp subsumes the pattern, false otherwise.
329  
330 =cut
331  
332 5 ▶sub subsumes {
Subroutine subsumes: called CC=8 SCAR=21.1
Pod documented
333 5 my $self = shift;
334 5 my $pattern = shift;
335 5 ▶ if ($pattern->isa('RDF::Trine::Statement')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pattern->isa('RDF::Trine::Statement')) { } 3 2 5
elsif ($pattern->isa('RDF::Query::Algebra::BasicGraphPattern')) { } 2 0 2
336 3 foreach my $t ($self->triples) {
337 5 ▶ return 1 if ($t->subsumes($pattern));
Branch: 1 branch
BranchTrueFalseTotal
if $t->subsumes($pattern) 2 3 5
338 }
339 1 return 0;
340 } elsif ($pattern->isa('RDF::Query::Algebra::BasicGraphPattern')) {
341 2 OUTER: foreach my $p ($pattern->triples) {
342 4 foreach my $t ($self->triples) {
343 6 ▶ next OUTER if ($t->subsumes($p));
Branch: 1 branch
BranchTrueFalseTotal
if $t->subsumes($p) 4 2 6
344 }
345 0 return 0;
346 }
347 2 return 1;
348 } else {
349 0 return 0;
350 }
351 }
352  
353 =item C<< bf () >>
354  
355 Returns a string representing the state of the nodes of the triple (bound or free).
356  
357 =cut
358  
359 0 ▶sub bf {
Subroutine bf: not called CC=7 SCAR=40.3
Pod documented
360 0 my $self = shift;
361 0 my @bf;
362 my %var_to_num;
363 0 my %use_count;
364 0 my $counter = 1;
365 0 foreach my $t ($self->triples) {
366 0 my $bf = $t->bf;
367 0 ▶ if ($bf =~ /f/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bf =~ /f/) 0 0 0
368 0 $bf = '';
369 0 foreach my $n ($t->nodes) {
370 0 ▶ if ($n->isa('RDF::Query::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa('RDF::Query::Node::Variable')) { } 0 0 0
371 0 my $name = $n->name;
372 0 ▶ my $num = ($var_to_num{ $name } ||= $counter++);
Condition0%0/3
$var_to_num{$name} ||= $counter++
l!l&&r!l&&!r
000
MC/DC0%0/2
$var_to_num{$name} ||= $counter++
$var_to_num{$name} $counter++
Truth table0%0/3
A ||= B
A= $var_to_num{$name}B= $counter++
ABresult
1X1
011
000
373 0 $use_count{ $name }++;
374 0 $bf .= "{${num}}";
375 } else {
376 0 $bf .= 'b';
377 }
378 }
379 }
380 0 push(@bf, $bf);
381 }
382 0 my $bf = join(',',@bf);
383 0 ▶ if ($counter <= 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counter <= 10) 0 0 0
384 0 $bf =~ s/[{}]//g;
385 }
386 0 return $bf;
387 }
388  
389 =item C<< clone >>
390  
391 =cut
392  
393 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
394 0 my $self = shift;
395 0 my $class = ref($self);
396 0 return $class->new( map { $_->clone } $self->triples );
397 }
398  
399 =item C<< bind_variables ( \%bound ) >>
400  
401 Returns a new algebra pattern with variables named in %bound replaced by their corresponding bound values.
402  
403 =cut
404  
405 0 ▶sub bind_variables {
Subroutine bind_variables: not called CC=1 SCAR=6.9
Pod documented
406 0 my $self = shift;
407 0 my $class = ref($self);
408 0 my $bound = shift;
409 0 return $class->new( map { $_->bind_variables( $bound ) } $self->triples );
410 }
411  
412 234 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
413 234 my $self = shift;
414 234 delete $AS_SPARQL{ refaddr( $self ) };
415 }
416  
417 36 1;
418  
419 __END__
« Query/Plan/ComputedStatement.pm ↑ Summary Query/Plan/Clear.pm »