Query/Algebra/Project.pm

filter: Statementstmt 64.2% 140 / 218 Branchbran 47.2% 34 / 72 Conditioncond 33.3% 7 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 80.0% 20 / 25 Podpod 100% 14 / 14 totaltotal 59.6% 216 / 362 CC 53 SCAR 35.6
CC
53
Cov
58%
as_sparql
71.2
_from_sse
42.0
bind_variables
30.0
CRAP 35.2
« Query/Federate/Plan.pm ↑ Summary Query/Plan/Triple.pm »
1 # RDF::Query::Algebra::Project
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Algebra::Project - Algebra class for projection
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Algebra::Project version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Algebra::Project;
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 Set::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use Scalar::Util qw(reftype blessed refaddr);
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 RDF::Trine::Iterator qw(sgrep);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 ######################################################################
28  
29 36 our ($VERSION);
30 36 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 36 $VERSION = '2.919';
32 }
33  
34 ######################################################################
35  
36 =head1 METHODS
37  
38 Beyond the methods documented below, this class inherits methods from the
39 L<RDF::Query::Algebra> class.
40  
41 =over 4
42  
43 =cut
44  
45 =item C<< new ( $pattern, \@vars_and_exprs ) >>
46  
47 Returns a new Project structure.
48  
49 =cut
50  
51 175 ▶sub new {
Subroutine new: called CC=6 SCAR=19.1
Pod documented
52 175 my $class = shift;
53 175 my $pattern = shift;
54 175 my $vars = shift;
55 175 ▶ unless (blessed($pattern)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($pattern)) 0 175 175
56 0 throw RDF::Query::Error::MethodInvocationError -text => "Sub-pattern in RDF::Query::Algebra::Project constructor must be a valid algebra object";
57 }
58 175 ▶ unless (reftype($vars) eq 'ARRAY' and not(blessed($vars))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($vars) eq "ARRAY" and not builtin::blessed($vars)) 0 175 175
Condition33%1/3
builtin::reftype($vars) eq "ARRAY" and not builtin::blessed($vars)
!ll&&!rl&&r
00175
MC/DC0%0/2
builtin::reftype($vars) eq "ARRAY" and not builtin::blessed($vars)
builtin::reftype($vars) eq "ARRAY" not builtin::blessed($vars)
Truth table33%1/3
A and not(B)
A= builtin::reftype($vars) eq "ARRAY"B= not builtin::blessed($vars)
ABresult
0X0
100
111
59 0 throw RDF::Query::Error::MethodInvocationError -text => "Variable list in RDF::Query::Algebra::Project constructor must be an ARRAY reference";
60 }
61 175 my @vars;
62 175 foreach my $v (@$vars) {
63 280 ▶ if ($v->isa('RDF::Query::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa('RDF::Query::Node::Variable')) { } 251 29 280
64 251 push(@vars, $v);
65 } else {
66 29 push(@vars, $v->alias);
67 }
68 }
69 175 return bless( [ $pattern, \@vars ], $class );
70 }
71  
72 =item C<< construct_args >>
73  
74 Returns a list of arguments that, passed to this class' constructor,
75 will produce a clone of this algebra pattern.
76  
77 =cut
78  
79 440 ▶sub construct_args {
Subroutine construct_args: called CC=1 SCAR=0.0
Pod documented
80 440 my $self = shift;
81 440 my $pattern = $self->pattern;
82 440 my $vars = $self->vars;
83 440 return ($pattern, $vars);
84 }
85  
86 =item C<< pattern >>
87  
88 Returns the pattern to be sorted.
89  
90 =cut
91  
92 834 ▶sub pattern {
Subroutine pattern: called CC=2 SCAR=7.6
Pod documented
93 834 my $self = shift;
94 834 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 834 834
95 0 $self->[0] = shift;
96 }
97 834 return $self->[0];
98 }
99  
100 =item C<< vars >>
101  
102 Returns the vars to be projected to.
103  
104 =cut
105  
106 803 ▶sub vars {
Subroutine vars: called CC=1 SCAR=0.0
Pod documented
107 803 my $self = shift;
108 803 return $self->[1];
109 }
110  
111 =item C<< sse >>
112  
113 Returns the SSE string for this algebra expression.
114  
115 =cut
116  
117 167 ▶sub sse {
Subroutine sse: called CC=4 SCAR=14.0
Pod documented
118 167 my $self = shift;
119 167 my $context = shift;
120 167 ▶ my $prefix = shift || '';
Condition100%2/2
shift() || ''
l!l
32135
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
121 167 ▶ my $indent = $context->{indent} || ' ';
Condition50%1/2
$context->{'indent'} || ' '
l!l
1670
MC/DC0%0/1
$context->{'indent'} || ' '
$context->{'indent'}
Truth table50%1/2
A
A= $context->{'indent'}
Aresult
11
00
122
123 my $vars = join(' ',
124 map {
125 281 ▶ ($_->isa('RDF::Query::Node::Variable')) ? '?' . $_->name : $_->sse( $context )
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('RDF::Query::Node::Variable') ? : 281 0 281
126 167 } @{ $self->vars }
127 );
128 167 return sprintf(
129 "(project (%s)\n${prefix}${indent}%s\n${prefix})",
130 $vars,
131 $self->pattern->sse( $context, "${prefix}${indent}" ),
132 );
133 }
134  
135 0 ▶sub _from_sse {
Subroutine _from_sse: not called CC=6 SCAR=37.4
136 0 my $class = shift;
137 0 my $context = $_[1];
138 0 for ($_[0]) {
139 0 ▶ if (m/^[(]project\s+[(]\s*/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[(]project\s+[(]\s*/) { } 0 0 0
140 0 my @nodes;
141 0 s/^[(]project\s+[(]\s*//;
142 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
/\s*[)]/ or do { do { push @nodes, "RDF::Trine::Node"->from_sse($_[0], $context) } } 0 0 0
143 0 push(@nodes, RDF::Trine::Node->from_sse( $_[0], $context ));
144 } until (m/\s*[)]/);
145 0 ▶ if (m/^\s*[)]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*[)]/) { } 0 0 0
146 0 s/^\s*[)]\s*//;
147 } else {
148 0 throw RDF::Trine::Error -text => "Cannot parse end-of-project-vars from SSE string: >>$_<<";
149 }
150
151 0 my ($pattern) = RDF::Query::Algebra->from_sse( $context, $_[0] );
152
153 0 ▶ if (m/^\s*[)]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*[)]/) { } 0 0 0
154 0 s/^\s*[)]\s*//;
155 0 warn "project: " . Dumper(\@nodes);
156 0 return RDF::Query::Algebra::Project->new( $pattern, \@nodes );
157 } else {
158 0 throw RDF::Trine::Error -text => "Cannot parse end-of-project from SSE string: >>$_<<";
159 }
160 } else {
161 0 throw RDF::Trine::Error -text => "Cannot parse project from SSE string: >>$_<<";
162 }
163 }
164 }
165  
166 =item C<< as_sparql >>
167  
168 Returns the SPARQL string for this algebra expression.
169  
170 =cut
171  
172 29 ▶sub as_sparql {
Subroutine as_sparql: called CC=31 SCAR=42.7
Pod documented
173 29 my $self = shift;
174 29 ▶ my $context = shift || {};
Condition50%1/2
shift() || {}
l!l
290
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
175 29 my $indent = shift;
176
177 29 my $pattern = $self->pattern;
178 29 $context->{ force_ggp_braces }++;
179
180 29 my ($vars, $_sparql);
181 29 my $vlist = $self->vars;
182 29 my (@vars);
183 29 foreach my $k (@$vlist) {
184 38 ▶ if ($k->isa('RDF::Query::Expression')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k->isa('RDF::Query::Expression')) { } 0 38 38
elsif ($k->isa('RDF::Query::Node::Variable')) { } 38 0 38
185 0 push(@vars, $k->sse({}, ''));
186 } elsif ($k->isa('RDF::Query::Node::Variable')) {
187 38 push(@vars, '?' . $k->name);
188 } else {
189 0 push(@vars, $k);
190 }
191 }
192
193 29 my $aggregate = 0;
194 29 my $group = '';
195 29 my $having = '';
196 29 my $order = '';
197 29 my %agg_projections;
198 29 my @aggs = $pattern->subpatterns_of_type( 'RDF::Query::Algebra::Aggregate' );
199 29 ▶ if (@aggs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@aggs) 1 28 29
200 # aggregate check
201 1 my $p = $pattern;
202 1 ▶ if ($p->isa('RDF::Query::Algebra::Sort')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->isa("RDF::Query::Algebra::Sort")) 1 0 1
203 1 $context->{ skip_sort }++;
204 1 $order = $p->_as_sparql_order_exprs( $context, $indent );
205 1 $p = $p->pattern
206 }
207 1 ▶ if ($p->isa('RDF::Query::Algebra::Filter')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->isa("RDF::Query::Algebra::Filter")) 1 0 1
208 1 $context->{ skip_filter }++;
209 1 $having = $p->expr->as_sparql( $context, $indent );
210 1 $p = $p->pattern;
211 }
212 1 ▶ $p = ($p->patterns)[0] if ($p->isa('RDF::Query::Algebra::GroupGraphPattern') and scalar(@{[$p->patterns]}) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $p->isa("RDF::Query::Algebra::GroupGraphPattern") and scalar @{[$p->patterns];} == 1 1 0 1
Condition33%1/3
$p->isa("RDF::Query::Algebra::GroupGraphPattern") and scalar @{[$p->patterns];} == 1
!ll&&!rl&&r
001
MC/DC0%0/2
$p->isa("RDF::Query::Algebra::GroupGraphPattern") and scalar @{[$p->patterns];} == 1
$p->isa("RDF::Query::Algebra::GroupGraphPattern") scalar @{[$p->patterns];} == 1
Truth table33%1/3
A and B
A= $p->isa("RDF::Query::Algebra::GroupGraphPattern")B= scalar @{[$p->patterns];} == 1
ABresult
0X0
100
111
213 1 ▶ if ($p->isa('RDF::Query::Algebra::Extend') and $p->pattern->isa('RDF::Query::Algebra::Aggregate')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->isa("RDF::Query::Algebra::Extend") and $p->pattern->isa("RDF::Query::Algebra::Aggregate")) 1 0 1
Condition33%1/3
$p->isa("RDF::Query::Algebra::Extend") and $p->pattern->isa("RDF::Query::Algebra::Aggregate")
!ll&&!rl&&r
001
MC/DC0%0/2
$p->isa("RDF::Query::Algebra::Extend") and $p->pattern->isa("RDF::Query::Algebra::Aggregate")
$p->isa("RDF::Query::Algebra::Extend") $p->pattern->isa("RDF::Query::Algebra::Aggregate")
Truth table33%1/3
A and B
A= $p->isa("RDF::Query::Algebra::Extend")B= $p->pattern->isa("RDF::Query::Algebra::Aggregate")
ABresult
0X0
100
111
214 1 my $pp = $p->pattern;
215 1 $context->{ skip_extend }++;
216 1 my $vlist = $p->vars;
217 1 foreach my $k (@$vlist) {
218 1 ▶ if ($k->isa('RDF::Query::Expression::Alias')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k->isa('RDF::Query::Expression::Alias')) { } 1 0 1
219 1 my $var = $k->name;
220 1 my $expr = $k->expression;
221 1 my $exprstr;
222 1 ▶ if ($expr->isa('RDF::Query::Expression::Binary')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->isa('RDF::Query::Expression::Binary')) { } 0 1 1
223 0 $exprstr = $expr->as_sparql( $context, $indent );
224 } else {
225 1 $exprstr = $k->expression->name;
226 }
227 1 my $str = "($exprstr AS ?$var)";
228 1 $agg_projections{ '?' . $var } = $str;
229 } else {
230 0 warn Dumper($k) . ' ';
231 }
232 }
233
234 1 my @groups = $pp->groupby;
235 1 ▶ if (@groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@groups) 1 0 1
236 1 $group = join(' ', map { $_->as_sparql($context, $indent) } @groups);
237 }
238 }
239 }
240
241 29 ▶ if ($pattern->isa('RDF::Query::Algebra::Extend')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern->isa('RDF::Query::Algebra::Extend')) { } 0 29 29
242 0 my %seen;
243 0 my $vlist = $pattern->vars;
244 0 foreach my $k (@$vlist) {
245 0 ▶ if ($k->isa('RDF::Query::Expression::Alias')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($k->isa('RDF::Query::Expression::Alias')) { } 0 0 0
elsif ($k->isa('RDF::Query::Expression')) { } 0 0 0
elsif ($k->isa('RDF::Query::Node::Variable')) { } 0 0 0
246 0 $seen{ '?' . $k->name } = $k->as_sparql({}, '');
247 } elsif ($k->isa('RDF::Query::Expression')) {
248 0 push(@vars, $k->as_sparql({}, ''));
249 } elsif ($k->isa('RDF::Query::Node::Variable')) {
250 0 push(@vars, '?' . $k->name);
251 } else {
252 0 push(@vars, $k);
253 }
254 }
255 0 ▶ @vars = map { exists($seen{$_}) ? $seen{$_} : $_ } @vars;
Branch: 1 branch
BranchTrueFalseTotal
exists $seen{$_} ? : 0 0 0
256 0 $vars = join(' ', @vars);
257 0 my $pp = $pattern->pattern;
258 0 ▶ if ($pp->isa('RDF::Query::Algebra::Aggregate')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pp->isa('RDF::Query::Algebra::Aggregate')) { } 0 0 0
259 0 $_sparql = $pp->pattern->as_sparql( $context, $indent );
260 0 my @groups = $pp->groupby;
261 0 ▶ if (@groups) {
Branch: 1 branch
BranchTrueFalseTotal
if (@groups) 0 0 0
262 0 $group = join(' ', map { $_->as_sparql($context, $indent) } @groups);
263 }
264 } else {
265 0 $_sparql = $pp->as_sparql( $context, $indent );
266 }
267 } else {
268 29 ▶ my $pvars = join(' ', map { my $agg = $agg_projections{ "?$_" }; defined($agg) ? $agg : "?$_" } sort $self->pattern->referenced_variables);
Branch: 1 branch
BranchTrueFalseTotal
defined $agg ? : 1 53 54
269 29 ▶ my $svars = join(' ', map { my $agg = $agg_projections{ $_ }; defined($agg) ? $agg : $_ } sort @vars);
Branch: 1 branch
BranchTrueFalseTotal
defined $agg ? : 1 37 38
270 29 ▶ $vars = ($pvars eq $svars) ? '*' : join(' ', map { my $agg = $agg_projections{ $_ }; defined($agg) ? $agg : $_ } @vars);
Branch: 2 branches
BranchTrueFalseTotal
$pvars eq $svars ? : 15 14 29
defined $agg ? : 1 14 15
271 29 $_sparql = $pattern->as_sparql( $context, $indent );
272 }
273 29 my $sparql = sprintf("%s WHERE %s", $vars, $_sparql);
274 29 ▶ if ($group) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group) 1 28 29
275 1 $sparql .= "\n${indent}GROUP BY $group";
276 }
277 29 ▶ if ($having) {
Branch: 1 branch
BranchTrueFalseTotal
if ($having) 1 28 29
278 1 $sparql .= "\n${indent}HAVING $having";
279 }
280 29 ▶ if ($order) {
Branch: 1 branch
BranchTrueFalseTotal
if ($order) 1 28 29
281 1 $sparql .= "\n${indent}ORDER BY $order";
282 }
283 29 return $sparql;
284 }
285  
286 =item C<< as_hash >>
287  
288 Returns the query as a nested set of plain data structures (no objects).
289  
290 =cut
291  
292 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
293 0 my $self = shift;
294 0 my $context = shift;
295 return {
296 type => lc($self->type),
297 0 variables => [ map { $_->as_hash } @{ $self->vars } ],
298 pattern => $self->pattern->as_hash,
299 };
300 }
301  
302 =item C<< as_spin ( $model ) >>
303  
304 Adds statements to the given model to represent this algebra object in the
305 SPARQL Inferencing Notation (L<http://www.spinrdf.org/>).
306  
307 =cut
308  
309 0 ▶sub as_spin {
Subroutine as_spin: not called CC=1 SCAR=6.9
Pod documented
310 0 my $self = shift;
311 0 my $model = shift;
312 0 my $spin = RDF::Trine::Namespace->new('http://spinrdf.org/spin#');
313 0 my $rdf = RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
314 0 my $q = RDF::Query::Node::Blank->new();
315 0 my @nodes = $self->pattern->as_spin( $model );
316
317 0 $model->add_statement( RDF::Trine::Statement->new($q, $rdf->type, $spin->Select) );
318
319 0 my @vars = map { RDF::Query::Node::Blank->new( "variable_" . $_->name ) } @{ $self->vars };
320 0 my $vlist = $model->add_list( @vars );
321 0 $model->add_statement( RDF::Trine::Statement->new($q, $spin->resultVariables, $vlist) );
322
323 0 my $list = $model->add_list( @nodes );
324 0 $model->add_statement( RDF::Trine::Statement->new($q, $spin->where, $list) );
325 0 return $q;
326 }
327  
328 =item C<< type >>
329  
330 Returns the type of this algebra expression.
331  
332 =cut
333  
334 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
335 0 return 'PROJECT';
336 }
337  
338 =item C<< referenced_variables >>
339  
340 Returns a list of the variable names used in this algebra expression.
341  
342 =cut
343  
344 32 ▶sub referenced_variables {
Subroutine referenced_variables: called CC=3 SCAR=11.2
Pod documented
345 32 my $self = shift;
346 32 my @vars = $self->pattern->referenced_variables;
347 32 foreach my $v (@{ $self->vars }) {
348 41 ▶ if ($v->isa('RDF::Query::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa('RDF::Query::Node::Variable')) { } 41 0 41
349 41 push(@vars, $v->name);
350 } else {
351 0 push(@vars, $v->referenced_variables);
352 }
353 }
354 32 return RDF::Query::_uniq(@vars);
355 }
356  
357 =item C<< bind_variables ( \%bound ) >>
358  
359 Returns a new algebra pattern with variables named in %bound replaced by their corresponding bound values.
360  
361 =cut
362  
363 0 ▶sub bind_variables {
Subroutine bind_variables: not called CC=5 SCAR=34.0
Pod documented
364 0 my $self = shift;
365 0 my $class = ref($self);
366 0 my $bound = shift;
367 0 my $pattern = $self->pattern->bind_variables( $bound );
368 0 my $vars = $self->vars;
369 0 my @vars;
370 0 foreach my $v (@$vars) {
371 0 ▶ if (blessed($v) and $v->isa('RDF::Query::Node::Variable') and exists $bound->{ $v->name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($v) and $v->isa('RDF::Query::Node::Variable') and exists $$bound{$v->name}) { } 0 0 0
Condition0%0/6
builtin::blessed($v) and $v->isa("RDF::Query::Node::Variable") and exists $$bound{$v->name}
!ll&&!rl&&r
000
builtin::blessed($v) and $v->isa("RDF::Query::Node::Variable")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($v) and $v->isa("RDF::Query::Node::Variable") and exists $$bound{$v->name}
builtin::blessed($v) $v->isa("RDF::Query::Node::Variable") exists $$bound{$v->name}
Truth table0%0/4
A and B and C
A= builtin::blessed($v)B= $v->isa("RDF::Query::Node::Variable")C= exists $$bound{$v->name}
ABCresult
0XX0
10X0
1100
1111
372 0 push(@vars, $bound->{ $v->name });
373 } else {
374 0 push(@vars, $v);
375 }
376 }
377 0 return $class->new( $pattern, \@vars );
378 }
379  
380 =item C<< potentially_bound >>
381  
382 Returns a list of the variable names used in this algebra expression that will
383 bind values during execution.
384  
385 =cut
386  
387 1 ▶sub potentially_bound {
Subroutine potentially_bound: called CC=3 SCAR=11.2
Pod documented
388 1 my $self = shift;
389 1 my @vars;
390 # push(@vars, $self->pattern->potentially_bound);
391 1 foreach my $v (@{ $self->vars }) {
392 1 ▶ if ($v->isa('RDF::Query::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v->isa('RDF::Query::Node::Variable')) { } 1 0 1
393 1 push(@vars, $v->name);
394 } else {
395 0 push(@vars, $v->potentially_bound);
396 }
397 }
398 1 return RDF::Query::_uniq(@vars);
399 }
400  
401 =item C<< definite_variables >>
402  
403 Returns a list of the variable names that will be bound after evaluating this algebra expression.
404  
405 =cut
406  
407 3 ▶sub definite_variables {
Subroutine definite_variables: called CC=1 SCAR=0.0
Pod documented
408 3 my $self = shift;
409 3 return $self->pattern->definite_variables;
410 }
411  
412 =item C<< is_solution_modifier >>
413  
414 Returns true if this node is a solution modifier.
415  
416 =cut
417  
418 134 ▶sub is_solution_modifier {
Subroutine is_solution_modifier: called CC=1 SCAR=0.0
Pod documented
419 134 return 1;
420 }
421  
422  
423 36 1;
424  
425 __END__
« Query/Federate/Plan.pm ↑ Summary Query/Plan/Triple.pm »