Attean/Plan.pm

filter: Statementstmt 32.5% 748 / 2296 Branchbran 4.6% 37 / 792 Conditioncond 4.0% 7 / 171 MC/DCmcdc 1.9% 2 / 103 Subroutinesub 59.1% 171 / 289 Podpod 0.0% 0 / 91 totaltotal 25.7% 965 / 3742 CC 511 SCAR 98.2
CC
511
Cov
24%
evaluate_expression
59751.1
evaluate_aggregate
1722.0
__ANON__
506.0
CRAP 18454.4
↑ Summary Attean/SimpleQueryEvaluator.pm »
1 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 50 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Attean::Plan - Representation of SPARQL query plan operators
8  
9 =head1 VERSION
10  
11 This document describes Attean::Plan version 0.040
12  
13 =head1 SYNOPSIS
14  
15 use v5.14;
16 use Attean;
17  
18 =head1 DESCRIPTION
19  
20 This is a utility package that defines all the Attean query plan classes
21 in the Attean::Plan namespace:
22  
23 =over 4
24  
25 =cut
26  
27 50 ▶use Attean::API::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =item * L<Attean::Plan::Quad>
30  
31 Evaluates a quad pattern against the model.
32  
33 =cut
34  
35 50 package Attean::Plan::Quad 0.040 {
36 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 50 ▶ use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 50 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 50 ▶ use AtteanX::Functions::CompositeLists;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 50 ▶ use AtteanX::Functions::CompositeMaps;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 50 has 'subject' => (is => 'ro', required => 1);
44 50 has 'predicate' => (is => 'ro', required => 1);
45 50 has 'object' => (is => 'ro', required => 1);
46 50 has 'graph' => (is => 'ro', required => 1);
47
48 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::NullaryQueryTree';
49 50 with 'Attean::API::QuadPattern';
50  
51 101 ▶ around 'BUILDARGS' => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
52 101 my $orig = shift;
53 101 my $class = shift;
54 101 my $args = $orig->( $class, @_ );
55 101 ▶ if (exists $args->{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"in_scope_variables"}) 0 101 101
56 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
57 }
58
59 101 my %vars;
60 101 foreach my $pos (qw(subject predicate object graph)) {
61 404 my $term = $args->{$pos};
62 404 ▶ if (blessed($term) and $term->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term) and $term->does("Attean::API::Variable")) 146 258 404
Condition100%3/3
builtin::blessed($term) and $term->does("Attean::API::Variable")
!ll&&!rl&&r
100158146
MC/DC100%2/2
builtin::blessed($term) and $term->does("Attean::API::Variable")
builtin::blessed($term) $term->does("Attean::API::Variable")
Truth table100%3/3
A and B
A= builtin::blessed($term)B= $term->does("Attean::API::Variable")
ABresult
0X0
100
111
63 146 $vars{$term->value} = $term;
64 }
65 }
66
67 101 my @vars = keys %vars;
68 101 $args->{in_scope_variables} = [@vars];
69  
70 101 return $args;
71 50 };
72  
73 10 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=6 SCAR=19.2
Pod undocumented
74 10 my $self = shift;
75 10 my @nodes = $self->values;
76 10 my @strings;
77 10 foreach my $t (@nodes) {
78 40 ▶ if (ref($t) eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $t eq 'ARRAY') { } 7 33 40
elsif ($t->does('Attean::API::TermOrVariable')) { } 33 0 33
elsif ($t->does('Attean::API::TriplePattern')) { } 0 0 0
79 7 my @tstrings = map { $_->ntriples_string } @$t;
80 7 ▶ if (scalar(@tstrings) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @tstrings == 1) { } 7 0 7
81 7 push(@strings, @tstrings);
82 } else {
83 0 push(@strings, '[' . join(', ', @tstrings) . ']');
84 }
85 } elsif ($t->does('Attean::API::TermOrVariable')) {
86 33 push(@strings, $t->ntriples_string);
87 } elsif ($t->does('Attean::API::TriplePattern')) {
88 0 push(@strings, $t->ntriples_string);
89 } else {
90 50 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 0 die "Unrecognized node in quad pattern: " . Dumper($t);
92 }
93 }
94 10 return sprintf('Quad { %s }', join(', ', @strings));
95 }
96
97 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=7 SCAR=40.3
Pod undocumented
98 0 my $self = shift;
99 0 my $model = shift;
100 0 my $b = shift;
101 0 my @values = $self->values;
102 0 foreach my $i (0 .. $#values) {
103 0 my $value = $values[$i];
104 0 ▶ if (reftype($value) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::reftype($value) eq 'ARRAY') { } 0 0 0
elsif ($value->does('Attean::API::Variable')) { } 0 0 0
105 0 my @values;
106 0 foreach my $value (@{ $value }) {
107 0 my $name = $value->value;
108 0 ▶ if (my $node = $b->value($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $b->value($name)) { } 0 0 0
109 0 push(@values, $node);
110 } else {
111 0 push(@values, $value);
112 }
113 0 $values[$i] = \@values;
114 }
115 } elsif ($value->does('Attean::API::Variable')) {
116 0 my $name = $value->value;
117 0 ▶ if (my $node = $b->value($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $b->value($name)) 0 0 0
118 0 $values[$i] = $node;
119 }
120 }
121 }
122
123 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 return $model->get_bindings( @values );
125 }
126 0 }
127  
128 11 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
129 11 my $self = shift;
130 11 my $model = shift;
131 11 my @values = $self->values;
132 11 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
133 11 return $model->get_bindings( @values );
134 }
135 11 }
136 }
137  
138 =item * L<Attean::Plan::NestedLoopJoin>
139  
140 Evaluates a join (natural-, anti-, or left-) using a nested loop.
141  
142 =cut
143  
144 50 package Attean::Plan::NestedLoopJoin 0.040 {
145 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 50 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
147 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 50 with 'Attean::API::BindingSubstitutionPlan';
150 50 with 'Attean::API::Plan::Join';
151 1 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=3 SCAR=13.3
Pod undocumented
152 1 my $self = shift;
153 1 ▶ if ($self->left) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->left) { } 0 1 1
elsif ($self->anti) { } 0 1 1
154 0 return 'NestedLoop Left Join';
155 } elsif ($self->anti) {
156 0 return 'NestedLoop Anti Join';
157 } else {
158 1 return 'NestedLoop Join';
159 }
160 }
161  
162 4 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
163 4 my $self = shift;
164 4 my $model = shift;
165 4 my @children = map { $_->impl($model) } @{ $self->children };
166 4 return $self->_impl($model, @children);
167 }
168
169 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=2 SCAR=17.9
Pod undocumented
170 0 my $self = shift;
171 0 my $model = shift;
172 0 my $b = shift;
173 0 ▶ unless (all { $_->does('Attean::API::BindingSubstitutionPlan') } @{ $self->children }) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&all(sub { $_->does("Attean::API::BindingSubstitutionPlan"); } , @{$self->children;})) 0 0 0
174 0 die "Plan children do not all consume BindingSubstitutionPlan role:\n" . $self->as_string;
175 }
176
177 0 my @children = map { $_->substitute_impl($model, $b) } @{ $self->children };
178 0 return $self->_impl($model, @children);
179 }
180
181 4 ▶ sub _impl {
Subroutine _impl: called CC=1 SCAR=0.3
182 4 my $self = shift;
183 4 my $model = shift;
184 4 my @children = @_;
185 4 my $left = $self->left;
186 4 my $anti = $self->anti;
187 4 my $iter_variables = $self->in_scope_variables;
188
189 4 ▶ return sub {
Subroutine __ANON__: called CC=12 SCAR=29.9
190 4 my ($lhs, $rhs) = map { $_->() } @children;
191 4 my @right = $rhs->elements;
192 4 my @results;
193 4 ▶ while (my $l = $lhs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $lhs->next) 2 4 6
194 2 my $seen = 0;
195 2 foreach my $r (@right) {
196 2 my @shared = $l->shared_domain($r);
197 2 ▶ if ($anti and scalar(@shared) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anti and scalar @shared == 0) 0 2 2
Condition33%1/3
$anti and scalar @shared == 0
!ll&&!rl&&r
200
MC/DC0%0/2
$anti and scalar @shared == 0
$anti scalar @shared == 0
Truth table33%1/3
A and B
A= $antiB= scalar @shared == 0
ABresult
0X0
100
111
198 # in a MINUS, two results that have disjoint domains are considered not to be joinable
199 0 next;
200 }
201 2 ▶ if (my $j = $l->join($r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $j = $l->join($r)) 2 0 2
202 2 $seen++;
203 2 ▶ if ($left) {
Branch: 2 branches
BranchTrueFalseTotal
if ($left) { } 0 2 2
elsif ($anti) { } 0 2 2
204 # TODO: filter with expression
205 0 push(@results, $j);
206 } elsif ($anti) {
207 } else {
208 2 push(@results, $j);
209 }
210 }
211 }
212 2 ▶ if ($left and not($seen)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($left and not $seen) { } 0 2 2
elsif ($anti and not $seen) { } 0 4 4
Condition33%1/3
$left and not $seen
!ll&&!rl&&r
200
MC/DC0%0/2
$left and not $seen
$left not $seen
Truth table33%1/3
A and not(B)
A= $leftB= not $seen
ABresult
0X0
100
111
213 0 push(@results, $l);
214 ▶ } elsif ($anti and not($seen)) {
Condition33%1/3
$anti and not $seen
!ll&&!rl&&r
200
MC/DC0%0/2
$anti and not $seen
$anti not $seen
Truth table33%1/3
A and not(B)
A= $antiB= not $seen
ABresult
0X0
100
111
215 0 push(@results, $l);
216 }
217 }
218 4 return Attean::ListIterator->new(
219 item_type => 'Attean::API::Result',
220 variables => $iter_variables,
221 values => \@results,
222 );
223 }
224 4 }
225 }
226  
227 =item * L<Attean::Plan::HashJoin>
228  
229 Evaluates a join (natural-, anti-, or left-) using a hash join.
230  
231 =cut
232  
233 50 package Attean::Plan::HashJoin 0.040 {
234 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 50 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
236 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
237
238 622 ▶ sub BUILD {
Subroutine BUILD: called CC=2 SCAR=8.1
Pod undocumented
239 622 my $self = shift;
240 622 ▶ if ($self->anti) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->anti) 0 622 622
241 0 die "Cannot use a HashJoin for anti-joins";
242 }
243 }
244
245 50 with 'Attean::API::BindingSubstitutionPlan';
246 50 with 'Attean::API::Plan::Join';
247 1 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=2 SCAR=7.1
Pod undocumented
248 1 my $self = shift;
249 1 my $name;
250 1 ▶ if ($self->left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->left) { } 0 1 1
251 0 $name = "Hash Left Join";
252 } else {
253 1 $name = "Hash Join";
254 }
255 1 return sprintf('%s { %s }', $name, join(', ', @{$self->join_variables}));
256 }
257  
258 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
259 0 my $self = shift;
260 0 my $model = shift;
261 0 my @children = map { $_->impl($model) } @{ $self->children };
262 0 return $self->_impl($model, @children);
263 }
264
265 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=2 SCAR=17.9
Pod undocumented
266 0 my $self = shift;
267 0 my $model = shift;
268 0 my $b = shift;
269
270 0 ▶ unless (all { $_->does('Attean::API::BindingSubstitutionPlan') } @{ $self->children }) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&all(sub { $_->does("Attean::API::BindingSubstitutionPlan"); } , @{$self->children;})) 0 0 0
271 0 die "Plan children do not all consume BindingSubstitutionPlan role:\n" . $self->as_string;
272 }
273
274 0 my @children = map { $_->substitute_impl($model, $b) } @{ $self->children };
275 0 return $self->_impl($model, @children);
276 }
277
278 0 ▶ sub _impl {
Subroutine _impl: not called CC=1 SCAR=6.9
279 0 my $self = shift;
280 0 my $model = shift;
281 0 my @children = @_;
282 0 my $left = $self->left;
283 0 my $iter_variables = $self->in_scope_variables;
284  
285 0 ▶ return sub {
Subroutine __ANON__: not called CC=22 SCAR=62.3
286 0 my %hash;
287 0 my @vars = @{ $self->join_variables };
288 0 my $rhs = $children[1]->();
289 0 ▶ while (my $r = $rhs->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $rhs->next) 0 0 0
290 0 my $has_unbound_right_join_var = 0;
291 0 my @values;
292 0 foreach my $var (@vars) {
293 0 my $value = $r->value($var);
294 0 ▶ unless (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
295 0 $has_unbound_right_join_var++;
296 }
297 0 push(@values, $value);
298 }
299 0 ▶ if ($has_unbound_right_join_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_unbound_right_join_var) { } 0 0 0
300 # this is a RHS row that doesn't have a term bound to one of the join variables.
301 # this will make it impossible to compute the proper hash key to access the row bucket,
302 # so we add this row to the null bucket (hash key '') which we try to join all LHS rows
303 # against.
304 0 push(@{ $hash{''} }, $r);
305 } else {
306 0 ▶ my $key = join(',', map { ref($_) ? $_->as_string : '' } @values);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
307 0 push(@{ $hash{$key} }, $r);
308 }
309 }
310
311 0 my @results;
312 0 my $lhs = $children[0]->();
313 0 ▶ while (my $l = $lhs->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $lhs->next) 0 0 0
314 0 my $seen = 0;
315 0 my @values;
316 0 my $has_unbound_left_join_var = 0;
317 0 foreach my $var (@vars) {
318 0 my $value = $l->value($var);
319 0 ▶ unless (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
320 0 $has_unbound_left_join_var++;
321 }
322 0 push(@values, $value);
323 }
324
325 0 my @buckets;
326 0 ▶ if (my $b = $hash{''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $b = $hash{""}) 0 0 0
327 0 push(@buckets, $b);
328 }
329
330 0 ▶ if ($has_unbound_left_join_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_unbound_left_join_var) { } 0 0 0
331 0 ▶ my $pattern = join(',', map { ref($_) ? quotemeta($_->as_string) : '.*' } @values);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
332 0 foreach my $key (keys %hash) {
333 0 ▶ if ($key =~ /^${pattern}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^$pattern$/u) 0 0 0
334 0 push(@buckets, $hash{$key});
335 }
336 }
337 } else {
338 0 ▶ my $key = join(',', map { ref($_) ? $_->as_string : '' } @values);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
339 0 ▶ if (my $rows = $hash{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rows = $hash{$key}) 0 0 0
340 0 push(@buckets, $rows);
341 }
342 }
343
344 0 foreach my $rows (@buckets) {
345 0 foreach my $r (@$rows) {
346 0 ▶ if (my $j = $l->join($r)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $j = $l->join($r)) 0 0 0
347 0 $seen++;
348 0 ▶ if ($left) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left) { } 0 0 0
349 # TODO: filter with expression
350 0 push(@results, $j);
351 } else {
352 0 push(@results, $j);
353 }
354 }
355 }
356 }
357 0 ▶ if ($left and not($seen)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left and not $seen) 0 0 0
Condition0%0/3
$left and not $seen
!ll&&!rl&&r
000
MC/DC0%0/2
$left and not $seen
$left not $seen
Truth table0%0/3
A and not(B)
A= $leftB= not $seen
ABresult
0X0
100
111
358 0 push(@results, $l);
359 }
360 }
361 0 return Attean::ListIterator->new(
362 item_type => 'Attean::API::Result',
363 variables => $iter_variables,
364 values => \@results
365 );
366 }
367 0 }
368 }
369  
370 =item * L<Attean::Plan::Construct>
371  
372 =cut
373  
374 50 package Attean::Plan::Construct 0.040 {
375 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
376 50 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
377 50 ▶ use Types::Standard qw(Str ArrayRef ConsumerOf InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
378 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
379 50 has 'triples' => (is => 'ro', 'isa' => ArrayRef[ConsumerOf['Attean::API::TripleOrQuadPattern']], required => 1);
380  
381 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
382  
383 3 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=1 SCAR=0.0
Pod undocumented
384 3 my $self = shift;
385 3 my $triples = $self->triples;
386 3 return sprintf('Construct { %s }', join(' . ', map { $_->as_string } @$triples));
387 }
388  
389 1 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod undocumented
390 # TODO: this code is repeated in several plan classes; figure out a way to share it.
391 1 my $class = shift;
392 1 my %args = @_;
393 1 my %vars = map { $_ => 1 } map { @{ $_->in_scope_variables } } @{ $args{ children } };
394 1 my @vars = keys %vars;
395
396 1 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 1 1
397 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
398 }
399 1 $args{in_scope_variables} = \@vars;
400  
401 1 return $class->SUPER::BUILDARGS(%args);
402 }
403  
404 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
405 0 my $self = shift;
406 0 my $model = shift;
407 0 my @children = map { $_->impl($model) } @{ $self->children };
408 0 return $self->_impl($model, @children);
409 }
410
411 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=2 SCAR=17.9
Pod undocumented
412 0 my $self = shift;
413 0 my $model = shift;
414 0 my $b = shift;
415 0 ▶ unless (all { $_->does('Attean::API::BindingSubstitutionPlan') } @{ $self->children }) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&all(sub { $_->does("Attean::API::BindingSubstitutionPlan"); } , @{$self->children;})) 0 0 0
416 0 die "Plan children do not all consume BindingSubstitutionPlan role:\n" . $self->as_string;
417 }
418
419 0 warn "TODO: fix substitute_impl to substitute construct triples";
420 0 my @children = map { $_->substitute_impl($model, $b) } @{ $self->children };
421 0 return $self->_impl($model, @children);
422 }
423
424 # replace blank nodes in all the triple patterns with fresh ones
425 0 ▶ sub refresh_triples {
Subroutine refresh_triples: not called CC=5 SCAR=34.0
Pod undocumented
426 0 my $self = shift;
427 0 my @t;
428 my %mapping;
429 0 foreach my $t (@_) {
430 0 foreach my $term ($t->values) {
431 0 ▶ if ($term->does('Attean::API::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Blank")) 0 0 0
432 0 $mapping{$term->as_string} = Attean::Blank->new();
433 }
434 }
435 }
436 0 my $mapper = Attean::TermMap->rewrite_map(\%mapping);
437 0 foreach my $t (@_) {
438 0 push(@t, $t->apply_map($mapper));
439 }
440 0 return @t;
441 }
442
443 0 ▶ sub _impl {
Subroutine _impl: not called CC=1 SCAR=6.9
444 0 my $self = shift;
445 0 my $model = shift;
446 0 my $child = shift;
447
448 0 my @triples = @{ $self->triples };
449
450 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
451 0 my $iter = $child->();
452 0 my @buffer;
453 my %seen;
454 return Attean::CodeIterator->new(
455 item_type => 'Attean::API::Triple',
456 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
457 0 ▶ if (scalar(@buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @buffer) 0 0 0
458 0 return shift(@buffer);
459 }
460 0 ▶ while (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 0 0 0
461 0 foreach my $tp ($self->refresh_triples(@triples)) {
462 0 my $tp = $tp->apply_bindings($row);
463 0 my $t = eval { $tp->as_triple };
464 0 ▶ if ($t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t) 0 0 0
465 0 push(@buffer, $t);
466 }
467 }
468 0 ▶ if (scalar(@buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @buffer) 0 0 0
469 0 my $t = shift(@buffer);
470 0 return $t;
471 }
472 }
473 }
474 0 ▶ )->grep(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
475 0 return not $seen{$_->as_string}++;
476 0 });
477 }
478 0 }
479 }
480  
481 =item * L<Attean::Plan::Describe>
482  
483 =cut
484  
485 50 package Attean::Plan::Describe 0.040 {
486 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 50 ▶ use Attean::RDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
488 50 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
489 50 ▶ use Types::Standard qw(Str ArrayRef ConsumerOf InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
490 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
491  
492 50 has 'graph' => (is => 'ro');
493 50 has 'terms' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::TermOrVariable']]);
494  
495 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
496 50 with 'Attean::API::UnionScopeVariablesPlan';
497  
498 1 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=1 SCAR=0.0
Pod undocumented
499 1 my $self = shift;
500 1 my $terms = $self->terms;
501 1 return sprintf('Describe { %s }', join(' . ', map { $_->as_string } @$terms));
502 }
503  
504 1 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
505 1 my $self = shift;
506 1 my $model = shift;
507 1 my @children = map { $_->impl($model) } @{ $self->children };
508 1 return $self->_impl($model, @children);
509 }
510
511 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=2 SCAR=17.9
Pod undocumented
512 0 my $self = shift;
513 0 my $model = shift;
514 0 my $b = shift;
515 0 ▶ unless (all { $_->does('Attean::API::BindingSubstitutionPlan') } @{ $self->children }) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless (&all(sub { $_->does("Attean::API::BindingSubstitutionPlan"); } , @{$self->children;})) 0 0 0
516 0 die "Plan children do not all consume BindingSubstitutionPlan role:\n" . $self->as_string;
517 }
518
519 0 warn "TODO: fix substitute_impl to substitute describe terms";
520 0 my @children = map { $_->substitute_impl($model, $b) } @{ $self->children };
521 0 return $self->_impl($model, @children);
522 }
523
524 1 ▶ sub _impl {
Subroutine _impl: called CC=1 SCAR=0.1
525 1 my $self = shift;
526 1 my $model = shift;
527 1 my $child = shift;
528
529 1 my $graph = $self->graph;
530 1 my @terms = @{ $self->terms };
531 # TODO: Split @terms into ground terms and variables.
532 # Only call get_quads once for ground terms.
533 # For variable terms, call get_quads for each variable-result combination.
534 1 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.1
535 1 my $iter = $child->();
536 1 my @buffer;
537 my %seen;
538 return Attean::CodeIterator->new(
539 item_type => 'Attean::API::Triple',
540 1 ▶ generator => sub {
Subroutine __ANON__: called CC=6 SCAR=19.1
541 1 ▶ if (scalar(@buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @buffer) 0 1 1
542 0 return shift(@buffer);
543 }
544 1 ▶ while (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 1 0 1
545 1 foreach my $term (@terms) {
546 1 my $value = $term->apply_binding($row);
547 1 ▶ if ($value->does('Attean::API::Term')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->does("Attean::API::Term")) 1 0 1
548 1 my $iter = $model->get_quads( $value, variable('predicate'), variable('object'), $graph );
549 1 push(@buffer, $iter->elements);
550 }
551 1 ▶ if (scalar(@buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @buffer) 1 0 1
552 1 return shift(@buffer);
553 }
554 }
555 }
556 }
557 1 ▶ )->grep(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
558 1 return not $seen{$_->as_string}++;
559 1 });
560 }
561 1 }
562 }
563  
564 =item * L<Attean::Plan::EBVFilter>
565  
566 Filters results from a sub-plan based on the effective boolean value of a
567 named variable binding.
568  
569 =cut
570  
571 50 package Attean::Plan::EBVFilter 0.040 {
572 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
573 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
574 50 ▶ use Types::Standard qw(Str ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
575 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
576
577 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
578 50 with 'Attean::API::UnionScopeVariablesPlan';
579  
580 50 has 'variable' => (is => 'ro', isa => Str, required => 1);
581  
582 1 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=1 SCAR=0.0
Pod undocumented
583 1 my $self = shift;
584 1 return sprintf('EBVFilter { ?%s }', $self->variable);
585 }
586 0 ▶ sub tree_attributes { return qw(expression) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
587
588 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=1 SCAR=6.9
Pod undocumented
589 0 my $self = shift;
590 0 my $model = shift;
591 0 my $bind = shift;
592 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
593 0 my $var = $self->variable;
594  
595 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
596 0 my $iter = $impl->();
597 0 ▶ return $iter->grep(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
598 0 my $r = shift;
599 0 my $term = $r->value($var);
600 0 ▶ return 0 unless (blessed($term) and $term->does('Attean::API::Term'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($term) and $term->does("Attean::API::Term") 0 0 0
Condition0%0/3
builtin::blessed($term) and $term->does("Attean::API::Term")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($term) and $term->does("Attean::API::Term")
builtin::blessed($term) $term->does("Attean::API::Term")
Truth table0%0/3
A and B
A= builtin::blessed($term)B= $term->does("Attean::API::Term")
ABresult
0X0
100
111
601 0 return $term->ebv;
602 0 });
603 0 };
604 }
605
606 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
607 0 my $self = shift;
608 0 my $model = shift;
609 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
610 0 my $var = $self->variable;
611 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
612 0 my $iter = $impl->();
613 0 ▶ return $iter->grep(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
614 0 my $r = shift;
615 0 my $term = $r->value($var);
616 0 ▶ return 0 unless (blessed($term) and $term->does('Attean::API::Term'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($term) and $term->does("Attean::API::Term") 0 0 0
Condition0%0/3
builtin::blessed($term) and $term->does("Attean::API::Term")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($term) and $term->does("Attean::API::Term")
builtin::blessed($term) $term->does("Attean::API::Term")
Truth table0%0/3
A and B
A= builtin::blessed($term)B= $term->does("Attean::API::Term")
ABresult
0X0
100
111
617 0 ▶ my $ok = eval { $term->ebv } || 0;
Condition0%0/2
eval { do { $term->ebv } } || 0
l!l
00
MC/DC0%0/1
eval { do { $term->ebv } } || 0
eval { do { $term->ebv } }
Truth table0%0/2
A
A= eval { do { $term->ebv } }
Aresult
11
00
618 0 return $ok;
619 0 });
620 0 };
621 }
622 }
623  
624 =item * L<Attean::Plan::Merge>
625  
626 Evaluates a set of sub-plans, returning the merged union of results, preserving
627 ordering.
628  
629 =cut
630  
631 50 package Attean::Plan::Merge 0.040 {
632 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
633 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
634 50 ▶ use Types::Standard qw(Str ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
635 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
636
637 50 with 'Attean::API::Plan', 'Attean::API::BinaryQueryTree';
638 50 with 'Attean::API::UnionScopeVariablesPlan';
639  
640 50 has 'variables' => (is => 'ro', isa => ArrayRef[Str], required => 1);
641  
642 0 ▶ sub plan_as_string { return 'Merge' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
643
644 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
645 0 my $self = shift;
646 0 my $model = shift;
647 0 my @children = map { $_->impl($model) } @{ $self->children };
648 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
649 0 die "Unimplemented";
650 0 };
651 }
652 }
653  
654 =item * L<Attean::Plan::Union>
655  
656 Evaluates a set of sub-plans, returning the union of results.
657  
658 =cut
659  
660 50 package Attean::Plan::Union 0.040 {
661 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
662 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
663 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
664
665 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::BinaryQueryTree';
666 50 with 'Attean::API::UnionScopeVariablesPlan';
667  
668 1 ▶ sub plan_as_string { return 'Union' }
Subroutine plan_as_string: called CC=1 SCAR=0.0
Pod undocumented
669
670 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
671 0 my $self = shift;
672 0 my $model = shift;
673 0 my @children = map { $_->impl($model) } @{ $self->children };
674 0 return $self->_impl($model, @children);
675 }
676
677 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=2 SCAR=17.9
Pod undocumented
678 0 my $self = shift;
679 0 my $model = shift;
680 0 my $b = shift;
681 0 ▶ unless (all { $_->does('Attean::API::BindingSubstitutionPlan') } @{ $self->children }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (do { $_->does("Attean::API::BindingSubstitutionPlan") }->all(@{$self->children;})) 0 0 0
682 0 die "Plan children do not all consume BindingSubstitutionPlan role:\n" . $self->as_string;
683 }
684
685 0 my @children = map { $_->substitute_impl($model, $b) } @{ $self->children };
686 0 return $self->_impl($model, @children);
687 }
688
689 0 ▶ sub _impl {
Subroutine _impl: not called CC=1 SCAR=6.9
690 0 my $self = shift;
691 0 my $model = shift;
692 0 my @children = @_;
693 0 my $iter_variables = $self->in_scope_variables;
694  
695 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
696 0 ▶ if (my $current = shift(@children)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current = shift @children) { } 0 0 0
697 0 my $iter = $current->();
698 return Attean::CodeIterator->new(
699 item_type => 'Attean::API::Result',
700 variables => $iter_variables,
701 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
702 0 ▶ while (blessed($iter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($iter)) 0 0 0
703 0 my $row = $iter->next();
704 0 ▶ if ($row) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row) { } 0 0 0
705 0 return $row;
706 } else {
707 0 $current = shift(@children);
708 0 ▶ if ($current) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current) { } 0 0 0
709 0 $iter = $current->();
710 } else {
711 0 undef $iter;
712 }
713 }
714 }
715 },
716 0 );
717 } else {
718 0 return Attean::ListIterator->new( item_type => 'Attean::API::Result', variables => [], values => [], );
719 }
720 0 };
721 }
722 }
723  
724 =item * L<Attean::Plan::Extend>
725  
726 Evaluates a sub-plan, and extends each result by evaluating a set of
727 expressions, binding the produced values to new variables.
728  
729 =cut
730  
731 50 package Attean::Plan::Extend 0.040 {
732 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
733 50 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
734 50 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
735 50 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
736 50 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
737 50 ▶ use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
738 50 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
739 50 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
740 50 ▶ use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
741 50 ▶ use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
742 50 ▶ use List::Util qw(uniq all);
Subroutine BEGIN: called CC=1 SCAR=0.0
743 50 ▶ use Types::Standard qw(ConsumerOf ArrayRef InstanceOf HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
744 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
745  
746 50 with 'MooX::Log::Any';
747 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
748 50 has 'expressions' => (is => 'ro', isa => HashRef[ConsumerOf['Attean::API::Expression']], required => 1);
749 50 has 'active_graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::IRI']], required => 1);
750
751 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
752 0 my $self = shift;
753 0 my @strings = map { sprintf('?%s ← %s', $_, $self->expressions->{$_}->as_string) } keys %{ $self->expressions };
754 0 return sprintf('Extend { %s }', join(', ', @strings));
755 }
756 0 ▶ sub tree_attributes { return qw(variable expression) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
757
758 10 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod undocumented
759 10 my $class = shift;
760 10 my %args = @_;
761 10 my $exprs = $args{ expressions };
762 10 my @vars = map { @{ $_->in_scope_variables } } @{ $args{ children } };
763 10 my @evars = (@vars, keys %$exprs);
764
765 10 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 10 10
766 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
767 }
768 10 $args{in_scope_variables} = [@evars];
769 10 return $class->SUPER::BUILDARGS(%args);
770 }
771
772 0 ▶ sub evaluate_expression {
Subroutine evaluate_expression: not called CC=245 SCAR=110.0
Pod undocumented
773 0 my $self = shift;
774 0 my $model = shift;
775 0 my $expr = shift;
776 0 my $r = shift;
777 0 ▶ Carp::confess unless ($expr->can('operator'));
Branch: 1 branch
BranchTrueFalseTotal
unless $expr->can("operator") 0 0 0
778 0 my $op = $expr->operator;
779  
780 0 state $true = Attean::Literal->true;
781 0 state $false = Attean::Literal->false;
782 0 state $type_roles = { qw(URI IRI IRI IRI BLANK Blank LITERAL Literal NUMERIC NumericLiteral TRIPLE Triple) };
783 0 state $type_classes = { qw(URI Attean::IRI IRI Attean::IRI STR Attean::Literal) };
784
785 0 ▶ if ($expr->isa('Attean::CastExpression')) {
Branch: 6 branches
BranchTrueFalseTotal
if ($expr->isa('Attean::CastExpression')) { } 0 0 0
elsif ($expr->isa('Attean::ValueExpression')) { } 0 0 0
elsif ($expr->isa('Attean::UnaryExpression')) { } 0 0 0
elsif ($expr->isa('Attean::BinaryExpression')) { } 0 0 0
elsif ($expr->isa('Attean::FunctionExpression')) { } 0 0 0
elsif ($expr->isa('Attean::ExistsPlanExpression')) { } 0 0 0
786 0 my $datatype = $expr->datatype->value;
787 0 my ($child) = @{ $expr->children };
788 0 my $term = $self->evaluate_expression($model, $child, $r);
789  
790 # TODO: the code below duplicates code in Attean::SimpleQueryEvaluator. It should be refactored.
791 0 ▶ if ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#string$>) {
Branch: 1 branch
BranchTrueFalseTotal
if ($datatype =~ m[^http://www.w3.org/2001/XMLSchema#string$]u) 0 0 0
792 0 my $value = $term->value;
793 0 ▶ if ($term->does('Attean::API::IRI')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($term->does('Attean::API::IRI')) { } 0 0 0
elsif ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 0 0 0
elsif ($term->does('Attean::API::NumericLiteral')) { } 0 0 0
794 0 return Attean::Literal->new(value => $term->value);
795 } elsif ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') {
796 0 ▶ my $v = ($value eq 'true' or $value eq '1') ? 'true' : 'false';
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' || $value eq '1' ? : 0 0 0
Condition0%0/3
$value eq "true" or $value eq 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$value eq "true" or $value eq 1
$value eq "true" $value eq 1
Truth table0%0/3
A or B
A= $value eq "true"B= $value eq 1
ABresult
1X1
011
000
797 0 return Attean::Literal->new(value => $v);
798 } elsif ($term->does('Attean::API::NumericLiteral')) {
799 0 my $v = $term->numeric_value();
800 0 ▶ if ($v == int($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == int $v) 0 0 0
801 0 return Attean::Literal->new(value => int($v));
802 }
803 }
804
805 0 return Attean::Literal->new(value => $value);
806 }
807  
808 0 ▶ die "TypeError $op" unless (blessed($term) and $term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($term) and $term->does("Attean::API::Literal") 0 0 0
Condition0%0/3
builtin::blessed($term) and $term->does("Attean::API::Literal")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($term) and $term->does("Attean::API::Literal")
builtin::blessed($term) $term->does("Attean::API::Literal")
Truth table0%0/3
A and B
A= builtin::blessed($term)B= $term->does("Attean::API::Literal")
ABresult
0X0
100
111
809 0 ▶ if ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#(integer|float|double|decimal)>) {
Branch: 3 branches
BranchTrueFalseTotal
if ($datatype =~ m[^http://www.w3.org/2001/XMLSchema#(integer|float|double|decimal)]u) { } 0 0 0
elsif ($datatype =~ m[^http://www.w3.org/2001/XMLSchema#boolean$]u) { } 0 0 0
elsif ($datatype =~ m[^http://www.w3.org/2001/XMLSchema#dateTime$]u) { } 0 0 0
810 0 my $value = $term->value;
811 0 my $num;
812 0 ▶ if ($datatype eq 'http://www.w3.org/2001/XMLSchema#integer') {
Branch: 3 branches
BranchTrueFalseTotal
if ($datatype eq 'http://www.w3.org/2001/XMLSchema#integer') { } 0 0 0
elsif ($datatype eq 'http://www.w3.org/2001/XMLSchema#decimal') { } 0 0 0
elsif ($datatype =~ m[^http://www.w3.org/2001/XMLSchema#(float|double)$]u) { } 0 0 0
813 0 ▶ if ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') {
Branch: 3 branches
BranchTrueFalseTotal
if ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 0 0 0
elsif ($term->does('Attean::API::NumericLiteral')) { } 0 0 0
elsif ($value =~ /^[-+]\d+$/u) { } 0 0 0
814 0 ▶ $value = ($value eq 'true' or $value eq '1') ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' || $value eq '1' ? : 0 0 0
Condition0%0/3
$value eq "true" or $value eq 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$value eq "true" or $value eq 1
$value eq "true" $value eq 1
Truth table0%0/3
A or B
A= $value eq "true"B= $value eq 1
ABresult
1X1
011
000
815 } elsif ($term->does('Attean::API::NumericLiteral')) {
816 0 my $v = $term->numeric_value();
817 0 $v =~ s/[.].*$//;
818 0 $value = int($v);
819 } elsif ($value =~ /^[-+]\d+$/) {
820 0 my ($v) = "$value";
821 0 $v =~ s/[.].*$//;
822 0 $value = int($v);
823 }
824 0 $num = $value;
825 } elsif ($datatype eq 'http://www.w3.org/2001/XMLSchema#decimal') {
826 0 ▶ if ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') {
Branch: 3 branches
BranchTrueFalseTotal
if ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 0 0 0
elsif ($term->does('Attean::API::NumericLiteral')) { } 0 0 0
elsif (&looks_like_number($value)) { } 0 0 0
827 0 ▶ $value = ($value eq 'true') ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' ? : 0 0 0
828 } elsif ($term->does('Attean::API::NumericLiteral')) {
829 0 $value = $term->numeric_value;
830 } elsif (looks_like_number($value)) {
831 0 ▶ if ($value =~ /[eE]/) { # double
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /[eE]/u) 0 0 0
832 0 die "cannot cast to xsd:decimal as precision would be lost";
833 }
834 0 $value = +$value;
835 }
836 0 $num = "$value";
837 0 $num =~ s/[.]0+$/.0/;
838 0 $num =~ s/[.](\d+)0*$/.$1/;
839 } elsif ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#(float|double)$>) {
840 0 my $typename = $1;
841 0 ▶ if ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') {
Branch: 3 branches
BranchTrueFalseTotal
if ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') { } 0 0 0
elsif ($term->does('Attean::API::NumericLiteral')) { } 0 0 0
elsif (&looks_like_number($value)) { } 0 0 0
842 0 ▶ $value = ($value eq 'true') ? '1.0' : '0.0';
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' ? : 0 0 0
843 } elsif ($term->does('Attean::API::NumericLiteral')) {
844 # no-op
845 } elsif (looks_like_number($value)) {
846 0 $value = +$value;
847 } else {
848 0 die "cannot cast unrecognized value '$value' to xsd:$typename";
849 }
850 0 $num = sprintf("%e", $value);
851 }
852 0 my $c = Attean::Literal->new(value => $num, datatype => $expr->datatype);
853 0 ▶ if (my $term = $c->canonicalized_term_strict()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $term = $c->canonicalized_term_strict) { } 0 0 0
854 0 return $term;
855 } else {
856 0 die "Term value is not a valid lexical form for $datatype";
857 }
858 } elsif ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#boolean$>) {
859 0 ▶ if ($term->does('Attean::API::NumericLiteral')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does('Attean::API::NumericLiteral')) { } 0 0 0
860 0 my $value = $term->numeric_value;
861 0 ▶ return ($value == 0) ? Attean::Literal->false : Attean::Literal->true;
Branch: 1 branch
BranchTrueFalseTotal
$value == 0 ? : 0 0 0
862 } else {
863 0 my $value = $term->value;
864 0 ▶ if ($value =~ m/^(true|false|0|1)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|false|0|1)$/u) { } 0 0 0
865 0 ▶ return ($value eq 'true' or $value eq '1') ? Attean::Literal->true : Attean::Literal->false;
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' || $value eq '1' ? : 0 0 0
Condition0%0/3
$value eq "true" or $value eq 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$value eq "true" or $value eq 1
$value eq "true" $value eq 1
Truth table0%0/3
A or B
A= $value eq "true"B= $value eq 1
ABresult
1X1
011
000
866 } else {
867 0 die "Bad lexical form for xsd:boolean: '$value'";
868 }
869 }
870 } elsif ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#dateTime$>) {
871 0 my $value = $term->value;
872 0 my $c = Attean::Literal->new(value => $value, datatype => $expr->datatype);
873 0 ▶ if ($c->does('Attean::API::DateTimeLiteral') and $c->datetime) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->does('Attean::API::DateTimeLiteral') and $c->datetime) { } 0 0 0
Condition0%0/3
$c->does("Attean::API::DateTimeLiteral") and $c->datetime
!ll&&!rl&&r
000
MC/DC0%0/2
$c->does("Attean::API::DateTimeLiteral") and $c->datetime
$c->does("Attean::API::DateTimeLiteral") $c->datetime
Truth table0%0/3
A and B
A= $c->does("Attean::API::DateTimeLiteral")B= $c->datetime
ABresult
0X0
100
111
874 0 return $c;
875 } else {
876 0 die "Bad lexical form for xsd:dateTime: '$value'";
877 }
878 }
879 0 $self->log->warn("Cast expression unimplemented for $datatype: " . Dumper($expr));
880 } elsif ($expr->isa('Attean::ValueExpression')) {
881 0 my $node = $expr->value;
882 0 ▶ if ($node->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->does('Attean::API::Variable')) { } 0 0 0
883 0 my $value = $r->value($node->value);
884 0 ▶ unless (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($value)) 0 0 0
885 0 die "Variable " . $node->as_string . " is unbound in expression " . $expr->as_string;
886 }
887 0 return $value;
888 } else {
889 0 return $node;
890 }
891 } elsif ($expr->isa('Attean::UnaryExpression')) {
892 0 my ($child) = @{ $expr->children };
893 0 my $term = $self->evaluate_expression($model, $child, $r);
894 0 ▶ if ($op eq '!') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '!') { } 0 0 0
elsif ($op eq '-' or $op eq '+') { } 0 0 0
895 0 ▶ return ($term->ebv) ? $false : $true;
Branch: 1 branch
BranchTrueFalseTotal
$term->ebv ? : 0 0 0
896 ▶ } elsif ($op eq '-' or $op eq '+') {
Condition0%0/3
$op eq "-" or $op eq "+"
l!l&&r!l&&!r
000
MC/DC0%0/2
$op eq "-" or $op eq "+"
$op eq "-" $op eq "+"
Truth table0%0/3
A or B
A= $op eq "-"B= $op eq "+"
ABresult
1X1
011
000
897 0 ▶ die "TypeError $op" unless (blessed($term) and $term->does('Attean::API::NumericLiteral'));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($term) and $term->does("Attean::API::NumericLiteral") 0 0 0
Condition0%0/3
builtin::blessed($term) and $term->does("Attean::API::NumericLiteral")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($term) and $term->does("Attean::API::NumericLiteral")
builtin::blessed($term) $term->does("Attean::API::NumericLiteral")
Truth table0%0/3
A and B
A= builtin::blessed($term)B= $term->does("Attean::API::NumericLiteral")
ABresult
0X0
100
111
898 0 my $v = $term->numeric_value;
899 0 return Attean::Literal->new( value => eval "$op$v", datatype => $term->datatype );
900 }
901 0 die "Unimplemented UnaryExpression evaluation: " . $expr->operator;
902 } elsif ($expr->isa('Attean::BinaryExpression')) {
903 0 my $op = $expr->operator;
904 0 ▶ if ($op eq '&&') {
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '&&') { } 0 0 0
elsif ($op eq '||') { } 0 0 0
elsif ($op eq '=') { } 0 0 0
elsif ($op eq '!=') { } 0 0 0
elsif ($op =~ /[<>]=?/u) { } 0 0 0
elsif ($op =~ m[^[-+*/]$]u) { } 0 0 0
905 0 foreach my $child (@{ $expr->children }) {
906 0 my $term = $self->evaluate_expression($model, $child, $r);
907 0 ▶ unless ($term->ebv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term->ebv) 0 0 0
908 0 return $false;
909 }
910 }
911 0 return $true;
912 } elsif ($op eq '||') {
913 0 foreach my $child (@{ $expr->children }) {
914 0 my $term = $self->evaluate_expression($model, $child, $r);
915 0 ▶ if (blessed($term) and $term->ebv) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term) and $term->ebv) 0 0 0
Condition0%0/3
builtin::blessed($term) and $term->ebv
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($term) and $term->ebv
builtin::blessed($term) $term->ebv
Truth table0%0/3
A and B
A= builtin::blessed($term)B= $term->ebv
ABresult
0X0
100
111
916 0 return $true;
917 }
918 }
919 0 return $false;
920 } elsif ($op eq '=') {
921 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
922 0 my $eq = $lhs->equals($rhs);
923 0 ▶ return $eq ? $true : $false; # TODO: this may not be using value-space comparision for numerics...
Branch: 1 branch
BranchTrueFalseTotal
$eq ? : 0 0 0
924 } elsif ($op eq '!=') {
925 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
926 0 my $neq = not($lhs->equals($rhs));
927 0 ▶ return $neq ? $true : $false; # TODO: this may not be using value-space comparision for numerics...
Branch: 1 branch
BranchTrueFalseTotal
$neq ? : 0 0 0
928 } elsif ($op =~ m#[<>]=?#) {
929 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
930 0 my $cmp = $lhs->compare($rhs);
931 0 ▶ if ($cmp < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp < 0) { } 0 0 0
elsif ($cmp > 0) { } 0 0 0
932 0 ▶ return ($op =~ /^<=?/) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /^<=?/u ? : 0 0 0
933 } elsif ($cmp > 0) {
934 0 ▶ return ($op =~ /^>=?/) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /^>=?/u ? : 0 0 0
935 } else {
936 0 ▶ return ($op =~ /=/) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /=/u ? : 0 0 0
937 }
938 } elsif ($op =~ m<^[-+*/]$>) {
939 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
940 0 ▶ die "TypeError $op" unless all { blessed($_) and $_->does('Attean::API::NumericLiteral') } ($lhs, $rhs);
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
unless &all(sub { builtin::blessed($_) and $_->does("Attean::API::NumericLiteral"); } , $lhs, $rhs) 0 0 0
builtin::blessed($_) and $_->does("Attean::API::NumericLiteral") 0 0 0
941 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
942 0 my $type = $lhs->binary_promotion_type($rhs, $op);
943 0 ▶ if ($op eq '+') {
Branch: 4 branches
BranchTrueFalseTotal
if ($op eq '+') { } 0 0 0
elsif ($op eq '-') { } 0 0 0
elsif ($op eq '*') { } 0 0 0
elsif ($op eq '/') { } 0 0 0
944 0 return Attean::Literal->new(value => ($lv + $rv), datatype => $type);
945 } elsif ($op eq '-') {
946 0 return Attean::Literal->new(value => ($lv - $rv), datatype => $type);
947 } elsif ($op eq '*') {
948 0 return Attean::Literal->new(value => ($lv * $rv), datatype => $type);
949 } elsif ($op eq '/') {
950 0 return Attean::Literal->new(value => ($lv / $rv), datatype => $type);
951 }
952 }
953
954 0 $self->log->warn("Binary operator $op expression evaluation unimplemented: " . Dumper($expr));
955 0 die "Expression evaluation unimplemented: " . $expr->as_string;
956 } elsif ($expr->isa('Attean::FunctionExpression')) {
957 0 my $func = $expr->operator;
958 0 ▶ if ($func eq 'IF') {
Branch: 3 branches
BranchTrueFalseTotal
if ($func eq 'IF') { } 0 0 0
elsif ($func eq 'COALESCE') { } 0 0 0
elsif ($func eq 'BOUND') { } 0 0 0
959 0 my ($check, @children) = @{ $expr->children };
960 0 my ($term) = $self->evaluate_expression($model, $check, $r);
961 0 ▶ $self->log->warn($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
962 0 ▶ my $expr = $children[ (blessed($term) and $term->ebv) ? 0 : 1 ];
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($term) && $term->ebv ? : 0 0 0
Condition0%0/3
builtin::blessed($term) and $term->ebv
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($term) and $term->ebv
builtin::blessed($term) $term->ebv
Truth table0%0/3
A and B
A= builtin::blessed($term)B= $term->ebv
ABresult
0X0
100
111
963 0 my $value = $self->evaluate_expression($model, $expr, $r);
964 # warn '############# ' . $value->as_string;
965 0 return $value;
966 } elsif ($func eq 'COALESCE') {
967 # warn "COALESCE: . " . $r->as_string . "\n";
968 0 foreach my $child (@{ $expr->children }) {
969 # warn '- ' . $child->as_string . "\n";
970 0 my $term = eval { $self->evaluate_expression($model, $child, $r) };
971 # warn $@ if $@;
972 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
973 # warn ' returning ' . $term->as_string . "\n";
974 0 return $term;
975 }
976 }
977 # warn " no value\n";
978 0 return;
979 } elsif ($func eq 'BOUND') {
980 0 my ($child) = @{ $expr->children };
981 0 my ($term) = eval { $self->evaluate_expression($model, $child, $r) };
982 0 ▶ return blessed($term) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($term) ? : 0 0 0
983 }
984
985 0 ▶ if ($func eq 'INVOKE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq 'INVOKE') { } 0 0 0
986 0 my @children = @{ $expr->children };
987 0 my $furi = $self->evaluate_expression($model, shift(@children), $r)->value;
988 0 ▶ if (my $func = Attean->get_global_function($furi)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $func = 'Attean'->get_global_function($furi)) { } 0 0 0
elsif (my $fform = 'Attean'->get_global_functional_form($furi)) { } 0 0 0
989 0 my @operands = map { $self->evaluate_expression($model, $_, $r) } @children;
990 0 my $rr = eval { $func->($model, $self->active_graphs, @operands) };
991 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
992 0 ▶ warn "INVOKE error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
993 0 return;
994 }
995 0 return $rr;
996 } elsif (my $fform = Attean->get_global_functional_form($furi)) {
997 0 ▶ my @operands = map { eval { $self->evaluate_expression($model, $_, $r) } || undef } @children;
Condition0%0/2
eval { do { $self->evaluate_expression($model, $_, $r) } } || undef
l!l
00
MC/DC0%0/1
eval { do { $self->evaluate_expression($model, $_, $r) } } || undef
eval { do { $self->evaluate_expression($model, $_, $r) } }
Truth table0%0/2
A
A= eval { do { $self->evaluate_expression($model, $_, $r) } }
Aresult
11
00
998 0 my $rr = eval { $fform->($model, $self->active_graphs, @operands) };
999 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1000 0 ▶ warn "INVOKE error: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1001 0 return $r;
1002 }
1003 0 return $rr;
1004 } else {
1005 0 die "No extension registered for <$furi>";
1006 }
1007 } else {
1008 0 my @terms = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1009 0 ▶ if ($func =~ /^IS([UI]RI|BLANK|LITERAL|NUMERIC|TRIPLE)$/) {
Branch: 43 branches
BranchTrueFalseTotal
if ($func =~ /^IS([UI]RI|BLANK|LITERAL|NUMERIC|TRIPLE)$/u) { } 0 0 0
elsif ($func eq 'REGEX') { } 0 0 0
elsif ($func =~ /^(NOT)?IN$/u) { } 0 0 0
elsif ($func eq 'NOW') { } 0 0 0
elsif ($func eq 'STR') { } 0 0 0
elsif ($func =~ /^[UI]RI$/u) { } 0 0 0
elsif ($func eq 'ABS') { } 0 0 0
elsif ($func eq 'ROUND') { } 0 0 0
elsif ($func eq 'CEIL') { } 0 0 0
elsif ($func eq 'FLOOR') { } 0 0 0
elsif ($func eq 'CONCAT') { } 0 0 0
elsif ($func eq 'DATATYPE') { } 0 0 0
elsif ($func eq 'LANG') { } 0 0 0
elsif ($func eq 'HASLANG') { } 0 0 0
elsif ($func eq 'HASLANGDIR') { } 0 0 0
elsif ($func eq 'LANGDIR') { } 0 0 0
elsif ($func eq 'LANGMATCHES') { } 0 0 0
elsif ($func eq 'ENCODE_FOR_URI') { } 0 0 0
elsif ($func =~ /^[LU]CASE$/u) { } 0 0 0
elsif ($func eq 'STRLANG') { } 0 0 0
elsif ($func eq 'STRLANGDIR') { } 0 0 0
elsif ($func eq 'STRDT') { } 0 0 0
elsif ($func eq 'REPLACE') { } 0 0 0
elsif ($func eq 'SUBSTR') { } 0 0 0
elsif ($func eq 'CONTAINS') { } 0 0 0
elsif ($func eq 'STRSTARTS') { } 0 0 0
elsif ($func eq 'STRENDS') { } 0 0 0
elsif ($func eq 'STRAFTER') { } 0 0 0
elsif ($func eq 'STRBEFORE') { } 0 0 0
elsif ($func eq 'STRLEN') { } 0 0 0
elsif ($func eq 'MD5') { } 0 0 0
elsif ($func =~ /^SHA(\d+)$/u) { } 0 0 0
elsif ($func eq 'RAND') { } 0 0 0
elsif ($func =~ /^(YEAR|MONTH|DAY|HOUR|MINUTE)S?$/u) { } 0 0 0
elsif ($func eq 'SECONDS') { } 0 0 0
elsif ($func eq 'TIMEZONE') { } 0 0 0
elsif ($func eq 'TZ') { } 0 0 0
elsif ($func eq 'UUID') { } 0 0 0
elsif ($func eq 'STRUUID') { } 0 0 0
elsif ($func eq 'BNODE') { } 0 0 0
elsif ($func eq 'SAMETERM') { } 0 0 0
elsif ($func =~ /^(SUBJECT|PREDICATE|OBJECT)$/u) { } 0 0 0
elsif ($func eq 'TRIPLE') { } 0 0 0
1010 0 my $role = "Attean::API::$type_roles->{$1}";
1011 0 my $t = shift(@terms);
1012 0 ▶ my $ok = (blessed($t) and $t->does($role));
Condition0%0/3
builtin::blessed($t) && $t->does($role)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($t) && $t->does($role)
builtin::blessed($t) $t->does($role)
Truth table0%0/3
A && B
A= builtin::blessed($t)B= $t->does($role)
ABresult
0X0
100
111
1013 0 ▶ return $ok ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
1014 } elsif ($func eq 'REGEX') {
1015 0 my ($string, $pattern, $flags) = @terms;
1016 # my ($string, $pattern, $flags) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1017 # TODO: ensure that $string is a literal
1018 0 ▶ unless ($string->does('Attean::API::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string->does("Attean::API::Literal")) 0 0 0
1019 0 die 'REGEX called with non-literal value';
1020 }
1021 0 ▶ unless ($pattern->does('Attean::API::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern->does("Attean::API::Literal")) 0 0 0
1022 0 die 'REGEX called with non-literal pattern';
1023 }
1024 0 ▶ ($string, $pattern, $flags) = map { blessed($_) ? $_->value : '' } ($string, $pattern, $flags);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 0 0 0
1025 0 my $re;
1026 0 my $flag_str = '';
1027 0 ▶ $flag_str .= 'i' if ($flags =~ /i/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /i/u 0 0 0
1028 0 ▶ $flag_str .= 's' if ($flags =~ /s/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /s/u 0 0 0
1029 0 ▶ $flag_str .= 'm' if ($flags =~ /m/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /m/u 0 0 0
1030 0 ▶ $flag_str .= 'x' if ($flags =~ /x/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /x/u 0 0 0
1031 0 ▶ if ($flags =~ /q/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /q/u) 0 0 0
1032 0 $pattern = quotemeta($pattern);
1033 }
1034 0 $re = qr/(?${flag_str}:$pattern)/;
1035 0 ▶ return ($string =~ $re) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /$re/u ? : 0 0 0
1036 } elsif ($func =~ /^(NOT)?IN$/) {
1037 0 ▶ my $ok = ($func eq 'IN') ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$func eq 'IN' ? : 0 0 0
1038 0 ▶ my $notok = ($func eq 'IN') ? $false : $true;
Branch: 1 branch
BranchTrueFalseTotal
$func eq 'IN' ? : 0 0 0
1039 # my @children = @{ $expr->children };
1040 0 my ($term, @children) = @terms;
1041 # my ($term) = $self->evaluate_expression($model, shift(@children), $r);
1042 # foreach my $child (@{ $expr->children }) {
1043 0 foreach my $value (@children) {
1044 # my $value = $self->evaluate_expression($model, $child, $r);
1045 0 ▶ if (eval { $term->equals($value) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $term->equals($value) } }) 0 0 0
1046 0 return $ok;
1047 }
1048 }
1049 0 return $notok;
1050 } elsif ($func eq 'NOW') {
1051 0 my $dt = DateTime->now;
1052 0 my $value = DateTime::Format::W3CDTF->new->format_datetime( $dt );
1053 0 return Attean::Literal->new(value => $value, datatype => 'http://www.w3.org/2001/XMLSchema#dateTime');
1054 } elsif ($func eq 'STR') {
1055 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1056 0 return Attean::Literal->new(value => $term->value);
1057 } elsif ($func =~ /^[UI]RI$/) { # IRI URI
1058 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1059 0 return Attean::IRI->new(value => $term->value, base => $expr->base);
1060 } elsif ($func eq 'ABS') {
1061 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1062 0 my $value = abs($string->numeric_value);
1063 0 return Attean::Literal->new(value => $value, datatype => $string->datatype);
1064 } elsif ($func eq 'ROUND') {
1065 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1066 0 my $value = $string->numeric_value;
1067 0 my $mult = 1;
1068 0 ▶ if ($value < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) 0 0 0
1069 0 $mult = -1;
1070 0 $value = -$value;
1071 }
1072 0 my $round = $mult * POSIX::floor($value + 0.50000000000008);
1073 0 return Attean::Literal->new(value => $round, datatype => $string->datatype);
1074 } elsif ($func eq 'CEIL') {
1075 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1076 0 my $value = ceil($string->numeric_value);
1077 0 return Attean::Literal->new(value => $value, datatype => $string->datatype);
1078 } elsif ($func eq 'FLOOR') {
1079 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1080 0 my $value = floor($string->numeric_value);
1081 0 return Attean::Literal->new(value => $value, datatype => $string->datatype);
1082 } elsif ($func eq 'CONCAT') {
1083 0 my @strings = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1084 0 ▶ if (scalar(@strings) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @strings == 0) 0 0 0
1085 0 return Attean::Literal->new(value => '');
1086 }
1087 # die "CONCAT called with terms that are not argument compatible" unless ($strings[0]->argument_compatible(@strings));
1088 0 my %args;
1089 0 my $lang_key = $strings[0]->lang_dir();
1090 0 foreach my $s (@strings) {
1091 0 ▶ die unless ($s->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $s->does("Attean::API::Literal") 0 0 0
1092 0 ▶ die if ($s->datatype and not($s->datatype->value =~ m<http://www.w3.org/(1999/02/22-rdf-syntax-ns#(lang|dirLang)String|2001/XMLSchema#string)>));
Branch: 1 branch
BranchTrueFalseTotal
if $s->datatype and not $s->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#(lang|dirLang)String|2001/XMLSchema#string)]u 0 0 0
Condition0%0/3
$s->datatype and not $s->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#(lang|dirLang)String|2001/XMLSchema#string)]u
!ll&&!rl&&r
000
MC/DC0%0/2
$s->datatype and not $s->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#(lang|dirLang)String|2001/XMLSchema#string)]u
$s->datatype not $s->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#(lang|dirLang)String|2001/XMLSchema#string)]u
Truth table0%0/3
A and not(B)
A= $s->datatypeB= not $s->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#(lang|dirLang)String|2001/XMLSchema#string)]u
ABresult
0X0
100
111
1093 0 my $new_lang_key = $s->lang_dir();
1094 0 ▶ if ($new_lang_key ne $lang_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_lang_key ne $lang_key) 0 0 0
1095 0 $lang_key = '';
1096 }
1097 }
1098 0 ▶ if ($lang_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang_key) 0 0 0
1099 0 my ($lang, $dir) = split('--', $lang_key);
1100 0 $args{language} = $lang;
1101 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 0 0
1102 0 $args{base_dir} = $dir;
1103 }
1104 }
1105 0 my $c = Attean::Literal->new(value => join('', map { $_->value } @strings), %args);
1106 0 return $c;
1107 } elsif ($func eq 'DATATYPE') {
1108 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1109 0 ▶ die unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1110 0 return $string->datatype;
1111 } elsif ($func eq 'LANG') {
1112 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1113 0 ▶ die unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1114 0 ▶ my $value = $string->language // '';
Condition0%0/2
$string->language // ""
l!l
00
MC/DC0%0/1
$string->language // ""
$string->language
Truth table0%0/2
A
A= $string->language
Aresult
11
00
1115 0 return Attean::Literal->new(value => $value);
1116 } elsif ($func eq 'HASLANG') {
1117 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1118 0 ▶ return $false unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1119 0 ▶ return $string->has_language ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$string->has_language ? : 0 0 0
1120 } elsif ($func eq 'HASLANGDIR') {
1121 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1122 0 ▶ return $false unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1123 0 ▶ return $string->has_base_dir ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$string->has_base_dir ? : 0 0 0
1124 } elsif ($func eq 'LANGDIR') {
1125 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1126 0 ▶ die unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1127 0 ▶ my $dir = $string->base_dir // '';
Condition0%0/2
$string->base_dir // ""
l!l
00
MC/DC0%0/1
$string->base_dir // ""
$string->base_dir
Truth table0%0/2
A
A= $string->base_dir
Aresult
11
00
1128 0 return Attean::Literal->new(value => $dir);
1129 } elsif ($func eq 'LANGMATCHES') {
1130 0 my ($term, $pat) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1131 0 my $lang = $term->value;
1132 0 my $match = $pat->value;
1133 0 ▶ if ($match eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($match eq '*') { } 0 0 0
1134 # """A language-range of "*" matches any non-empty language-tag string."""
1135 0 ▶ return $lang ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$lang ? : 0 0 0
1136 } else {
1137 0 ▶ return (I18N::LangTags::is_dialect_of( $lang, $match )) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
I18N::LangTags::is_dialect_of($lang, $match) ? : 0 0 0
1138 }
1139
1140 } elsif ($func eq 'ENCODE_FOR_URI') {
1141 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1142 0 return Attean::Literal->new(value => uri_escape_utf8($string->value));
1143 } elsif ($func =~ /^[LU]CASE$/) {
1144 0 my $term = shift(@terms);
1145 0 ▶ my $value = ($func eq 'LCASE') ? lc($term->value) : uc($term->value);
Branch: 1 branch
BranchTrueFalseTotal
$func eq 'LCASE' ? : 0 0 0
1146 0 return Attean::Literal->new(value => $value, $term->construct_args);
1147 } elsif ($func eq 'STRLANG') {
1148 0 my ($term, $lang) = @terms;
1149 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1150 0 ▶ die unless ($term->datatype->value =~ m<http://www.w3.org/2001/XMLSchema#string>);
Branch: 1 branch
BranchTrueFalseTotal
unless $term->datatype->value =~ m[http://www.w3.org/2001/XMLSchema#string]u 0 0 0
1151 0 ▶ die if ($term->language);
Branch: 1 branch
BranchTrueFalseTotal
if $term->language 0 0 0
1152 0 ▶ die unless ($lang->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $lang->value 0 0 0
1153 0 return Attean::Literal->new(value => $term->value, language => $lang->value);
1154 } elsif ($func eq 'STRLANGDIR') {
1155 0 my ($term, $lang, $dir) = @terms;
1156 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1157 0 ▶ die unless ($lang->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $lang->does("Attean::API::Literal") 0 0 0
1158 0 ▶ die unless ($dir->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->does("Attean::API::Literal") 0 0 0
1159 0 ▶ die unless ($term->datatype->value =~ m<http://www.w3.org/2001/XMLSchema#string>);
Branch: 1 branch
BranchTrueFalseTotal
unless $term->datatype->value =~ m[http://www.w3.org/2001/XMLSchema#string]u 0 0 0
1160 0 ▶ die unless ($lang->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $lang->value 0 0 0
1161 0 ▶ die unless ($dir->value =~ /^(ltr|rtl)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->value =~ /^(ltr|rtl)$/u 0 0 0
1162 0 my $v = Attean::Literal->new(value => $term->value, language => $lang->value, base_dir => $dir->value);
1163 0 return $v;
1164 } elsif ($func eq 'STRDT') {
1165 0 my ($term, $dt) = @terms;
1166 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1167 0 ▶ die unless ($term->datatype->value =~ m<http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)>);
Branch: 1 branch
BranchTrueFalseTotal
unless $term->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)]u 0 0 0
1168 0 ▶ die if ($term->language);
Branch: 1 branch
BranchTrueFalseTotal
if $term->language 0 0 0
1169 # my ($term, $dt) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1170 0 return Attean::Literal->new(value => $term->value, datatype => $dt->value);
1171 } elsif ($func eq 'REPLACE') {
1172 0 my ($term, $pat, $rep, $flags_term) = @terms;
1173 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1174 0 ▶ die unless ($term->language or $term->datatype->value =~ m<http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)>);
Branch: 1 branch
BranchTrueFalseTotal
unless $term->language or $term->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)]u 0 0 0
Condition0%0/3
$term->language or $term->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)]u
l!l&&r!l&&!r
000
MC/DC0%0/2
$term->language or $term->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)]u
$term->language $term->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)]u
Truth table0%0/3
A or B
A= $term->languageB= $term->datatype->value =~ m[http://www.w3.org/(1999/02/22-rdf-syntax-ns#langString|2001/XMLSchema#string)]u
ABresult
1X1
011
000
1175 # my ($term, $pat, $rep) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1176 0 my $value = $term->value;
1177 0 my $pattern = $pat->value;
1178 0 my $replace = $rep->value;
1179 0 ▶ my $flags = blessed($flags_term) ? $flags_term->value : '';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($flags_term) ? : 0 0 0
1180 0 ▶ die 'REPLACE() called with unsafe ?{} match pattern' if (index($pattern, '(?{') != -1 or index($pattern, '(??{') != -1);
Branch: 1 branch
BranchTrueFalseTotal
if index($pattern, "(?{") != -1 or index($pattern, "(??{") != -1 0 0 0
Condition0%0/3
index($pattern, "(?{") != -1 or index($pattern, "(??{") != -1
l!l&&r!l&&!r
000
MC/DC0%0/2
index($pattern, "(?{") != -1 or index($pattern, "(??{") != -1
index($pattern, "(?{") != -1 index($pattern, "(??{") != -1
Truth table0%0/3
A or B
A= index($pattern, "(?{") != -1B= index($pattern, "(??{") != -1
ABresult
1X1
011
000
1181 0 ▶ die 'REPLACE() called with unsafe ?{} replace pattern' if (index($replace, '(?{') != -1 or index($replace, '(??{') != -1);
Branch: 1 branch
BranchTrueFalseTotal
if index($replace, "(?{") != -1 or index($replace, "(??{") != -1 0 0 0
Condition0%0/3
index($replace, "(?{") != -1 or index($replace, "(??{") != -1
l!l&&r!l&&!r
000
MC/DC0%0/2
index($replace, "(?{") != -1 or index($replace, "(??{") != -1
index($replace, "(?{") != -1 index($replace, "(??{") != -1
Truth table0%0/3
A or B
A= index($replace, "(?{") != -1B= index($replace, "(??{") != -1
ABresult
1X1
011
000
1182  
1183 0 $replace =~ s/\\/\\\\/g;
1184 0 $replace =~ s/\$(\d+)/\$$1/g;
1185 0 $replace =~ s/"/\\"/g;
1186 0 $replace = qq["$replace"];
1187 50 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1188  
1189 0 ▶ if ($flags eq 'i') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags eq "i") 0 0 0
1190 0 $pattern = '(?i)' . $pattern;
1191 }
1192  
1193 0 $value =~ s/$pattern/"$replace"/eeg;
1194 # warn "==> " . Dumper($value);
1195 0 return Attean::Literal->new(value => $value, $term->construct_args);
1196 } elsif ($func eq 'SUBSTR') {
1197 0 my ($term, @args) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1198 0 my $value = $term->value;
1199 0 my @nums;
1200 0 foreach my $i (0 .. $#args) {
1201 0 my $argnum = $i + 2;
1202 0 my $arg = $args[ $i ];
1203 0 push(@nums, $arg->numeric_value);
1204 }
1205 0 $nums[0]--;
1206 0 ▶ my $substring = (scalar(@nums) > 1) ? substr($value, $nums[0], $nums[1]) : substr($value, $nums[0]);
Branch: 1 branch
BranchTrueFalseTotal
scalar @nums > 1 ? : 0 0 0
1207 0 return Attean::Literal->new(value => $substring, $term->construct_args);
1208 } elsif ($func eq 'CONTAINS') {
1209 0 my ($term, $pattern) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1210 0 my $t_key = $term->lang_dir();
1211 0 my $p_key = $pattern->lang_dir();
1212 0 ▶ if ($t_key ne $p_key and $p_key ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t_key ne $p_key and $p_key ne "") 0 0 0
Condition0%0/3
$t_key ne $p_key and $p_key ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
$t_key ne $p_key and $p_key ne ""
$t_key ne $p_key $p_key ne ""
Truth table0%0/3
A and B
A= $t_key ne $p_keyB= $p_key ne ""
ABresult
0X0
100
111
1213 0 die "CONTAINS called with literals of different languages and/or base direction";
1214 }
1215 0 my ($string, $pat) = map { $_->value } ($term, $pattern);
1216 0 my $pos = index($string, $pat);
1217 0 ▶ return ($pos >= 0) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$pos >= 0 ? : 0 0 0
1218 } elsif ($func eq 'STRSTARTS') {
1219 0 my (@terms) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1220 0 my ($string, $pat) = map { $_->value } @terms;
1221 0 ▶ return (substr($string, 0, length($pat)) eq $pat) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
substr($string, 0, length $pat) eq $pat ? : 0 0 0
1222 } elsif ($func eq 'STRENDS') {
1223 0 my (@terms) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1224 0 my ($string, $pat) = map { $_->value } @terms;
1225 0 ▶ return (substr($string, length($string) - length($pat)) eq $pat) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
substr($string, length($string) - length($pat)) eq $pat ? : 0 0 0
1226 } elsif ($func eq 'STRAFTER') {
1227 0 my ($term, $pat) = @terms;
1228 0 ▶ die "STRAFTER called without a literal" unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1229 0 ▶ die "STRAFTER called without a plain literal" unless ($term->language or $term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#string');
Branch: 1 branch
BranchTrueFalseTotal
unless $term->language or $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string" 0 0 0
Condition0%0/3
$term->language or $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
l!l&&r!l&&!r
000
MC/DC0%0/2
$term->language or $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
$term->language $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
Truth table0%0/3
A or B
A= $term->languageB= $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
ABresult
1X1
011
000
1230 0 ▶ die "$func arguments are not term compatible: " . join(', ', map { $_->as_string } @terms) unless ($term->argument_compatible($pat));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->argument_compatible($pat) 0 0 0
1231 # TODO: check that the terms are argument compatible
1232 0 my $value = $term->value;
1233 0 my $match = $pat->value;
1234 0 my $i = index($value, $match, 0);
1235 0 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) { } 0 0 0
1236 0 return Attean::Literal->new(value => '');
1237 } else {
1238 0 return Attean::Literal->new(value => substr($value, $i+length($match)), $term->construct_args);
1239 }
1240 } elsif ($func eq 'STRBEFORE') {
1241 0 my ($term, $pat) = @terms;
1242 0 ▶ die "STRBEFORE called without a literal" unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1243 0 ▶ die "STRBEFORE called without a plain literal" unless ($term->language or $term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#string');
Branch: 1 branch
BranchTrueFalseTotal
unless $term->language or $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string" 0 0 0
Condition0%0/3
$term->language or $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
l!l&&r!l&&!r
000
MC/DC0%0/2
$term->language or $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
$term->language $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
Truth table0%0/3
A or B
A= $term->languageB= $term->datatype->value eq "http://www.w3.org/2001/XMLSchema#string"
ABresult
1X1
011
000
1244 0 ▶ die "$func arguments are not term compatible: " . join(', ', map { $_->as_string } @terms) unless ($term->argument_compatible($pat));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->argument_compatible($pat) 0 0 0
1245 # TODO: check that the terms are argument compatible
1246 0 my $value = $term->value;
1247 0 my $match = $pat->value;
1248 0 my $i = index($value, $match, 0);
1249 0 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) { } 0 0 0
1250 0 return Attean::Literal->new(value => '');
1251 } else {
1252 0 return Attean::Literal->new(value => substr($value, 0, $i), $term->construct_args);
1253 }
1254 } elsif ($func eq 'STRLEN') {
1255 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1256 0 return Attean::Literal->new(value => length($string->value), datatype => 'http://www.w3.org/2001/XMLSchema#integer');
1257 } elsif ($func eq 'MD5') {
1258 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1259 0 my $bytes = encode('UTF-8', $string->value, Encode::FB_CROAK);
1260 0 return Attean::Literal->new(value => md5_hex($bytes));
1261 } elsif ($func =~ /^SHA(\d+)$/) {
1262 0 my $sha = Digest::SHA->new($1);
1263 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1264 0 my $bytes = encode('UTF-8', $string->value, Encode::FB_CROAK);
1265 0 $sha->add($bytes);
1266 0 return Attean::Literal->new(value => $sha->hexdigest);
1267 } elsif ($func eq 'RAND') {
1268 0 return Attean::Literal->new(value => rand(), datatype => 'http://www.w3.org/2001/XMLSchema#double');
1269 } elsif ($func =~ /^(YEAR|MONTH|DAY|HOUR|MINUTE)S?$/) {
1270 0 my $method = lc($1);
1271 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1272 0 my $dt = $term->datetime;
1273 0 return Attean::Literal->new(value => $dt->$method(), datatype => 'http://www.w3.org/2001/XMLSchema#integer');
1274 } elsif ($func eq 'SECONDS') {
1275 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1276 0 my $dt = $term->datetime;
1277 0 return Attean::Literal->new(value => $dt->second, datatype => 'http://www.w3.org/2001/XMLSchema#decimal');
1278 } elsif ($func eq 'TIMEZONE') {
1279 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1280 0 my $dt = $term->datetime;
1281 0 my $tz = $dt->time_zone;
1282 0 ▶ die "TIMEZONE called with a dateTime without a timezone" if ($tz->is_floating);
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_floating 0 0 0
1283 0 my $offset = $tz->offset_for_datetime( $dt );
1284 0 my $minus = '';
1285 0 ▶ if ($offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 0 0 0
1286 0 $minus = '-';
1287 0 $offset = -$offset;
1288 }
1289  
1290 0 my $duration = "${minus}PT";
1291 0 ▶ if ($offset >= 60*60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 3600) 0 0 0
1292 0 my $h = int($offset / (60*60));
1293 0 ▶ $duration .= "${h}H" if ($h > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $h > 0 0 0 0
1294 0 $offset = $offset % (60*60);
1295 }
1296 0 ▶ if ($offset >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 60) 0 0 0
1297 0 my $m = int($offset / 60);
1298 0 ▶ $duration .= "${m}M" if ($m > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $m > 0 0 0 0
1299 0 $offset = $offset % 60;
1300 }
1301 0 my $s = int($offset);
1302 0 ▶ $duration .= "${s}S" if ($s > 0 or $duration eq 'PT');
Branch: 1 branch
BranchTrueFalseTotal
if $s > 0 or $duration eq "PT" 0 0 0
Condition0%0/3
$s > 0 or $duration eq "PT"
l!l&&r!l&&!r
000
MC/DC0%0/2
$s > 0 or $duration eq "PT"
$s > 0 $duration eq "PT"
Truth table0%0/3
A or B
A= $s > 0B= $duration eq "PT"
ABresult
1X1
011
000
1303
1304 0 return Attean::Literal->new(value => $duration, datatype => 'http://www.w3.org/2001/XMLSchema#dayTimeDuration');
1305 } elsif ($func eq 'TZ') {
1306 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1307 0 my $dt = $term->datetime;
1308 0 my $tz = $dt->time_zone;
1309 0 ▶ return Attean::Literal->new(value =>'') if ($tz->is_floating);
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_floating 0 0 0
1310 0 ▶ return Attean::Literal->new('Z') if ($tz->is_utc);
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_utc 0 0 0
1311 0 my $offset = $tz->offset_for_datetime( $dt );
1312 0 my $hours = 0;
1313 0 my $minutes = 0;
1314 0 my $minus = '+';
1315 0 ▶ if ($offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 0 0 0
1316 0 $minus = '-';
1317 0 $offset = -$offset;
1318 }
1319  
1320 0 ▶ if ($offset >= 60*60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 3600) 0 0 0
1321 0 $hours = int($offset / (60*60));
1322 0 $offset = $offset % (60*60);
1323 }
1324 0 ▶ if ($offset >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 60) 0 0 0
1325 0 $minutes = int($offset / 60);
1326 0 $offset = $offset % 60;
1327 }
1328 0 my $seconds = int($offset);
1329 0 return Attean::Literal->new(value => sprintf('%s%02d:%02d', $minus, $hours, $minutes));
1330 } elsif ($func eq 'UUID') {
1331 0 my $uuid = 'urn:uuid:' . uc(uuid_to_string(create_uuid()));
1332 0 return Attean::IRI->new(value => $uuid);
1333 } elsif ($func eq 'STRUUID') {
1334 0 return Attean::Literal->new(value => uc(uuid_to_string(create_uuid())));
1335 } elsif ($func eq 'BNODE') {
1336 0 ▶ if (scalar(@{ $expr->children })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$expr->children;}) { } 0 0 0
1337 0 my $string = $self->evaluate_expression($model, $expr->children->[0], $r);
1338 0 my $value = $string->value;
1339 my $b = (exists $r->eval_stash->{'sparql:bnode'}{$value})
1340 0 ▶ ? $r->eval_stash->{'sparql:bnode'}{$value}
Branch: 1 branch
BranchTrueFalseTotal
exists $r->eval_stash->{'sparql:bnode'}{$value} ? : 0 0 0
1341 : Attean::Blank->new();
1342 0 $r->eval_stash->{'sparql:bnode'}{$value} = $b;
1343 0 return $b;
1344 } else {
1345 0 return Attean::Blank->new();
1346 }
1347 } elsif ($func eq 'SAMETERM') {
1348 0 my @operands = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1349 0 my ($a, $b) = @operands;
1350 0 ▶ die "TypeError: SAMETERM" unless (blessed($operands[0]) and blessed($operands[1]));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($operands[0]) and builtin::blessed($operands[1]) 0 0 0
Condition0%0/3
builtin::blessed($operands[0]) and builtin::blessed($operands[1])
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($operands[0]) and builtin::blessed($operands[1])
builtin::blessed($operands[0]) builtin::blessed($operands[1])
Truth table0%0/3
A and B
A= builtin::blessed($operands[0])B= builtin::blessed($operands[1])
ABresult
0X0
100
111
1351 0 ▶ if ($a->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->does('Attean::API::Binding')) { } 0 0 0
1352 0 my $ok = ($a->sameTerms($b));
1353 0 ▶ return $ok ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
1354 } else {
1355 0 ▶ return $false unless ($a->value eq $b->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $a->value eq $b->value 0 0 0
1356 0 ▶ if ($a->does('Attean::API::Literal') and $b->does('Attean::API::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->does("Attean::API::Literal") and $b->does("Attean::API::Literal")) 0 0 0
Condition0%0/3
$a->does("Attean::API::Literal") and $b->does("Attean::API::Literal")
!ll&&!rl&&r
000
MC/DC0%0/2
$a->does("Attean::API::Literal") and $b->does("Attean::API::Literal")
$a->does("Attean::API::Literal") $b->does("Attean::API::Literal")
Truth table0%0/3
A and B
A= $a->does("Attean::API::Literal")B= $b->does("Attean::API::Literal")
ABresult
0X0
100
111
1357 0 ▶ return $false unless ($a->lang_dir eq $b->lang_dir);
Branch: 1 branch
BranchTrueFalseTotal
unless $a->lang_dir eq $b->lang_dir 0 0 0
1358 0 my $ad = $a->datatype;
1359 0 my $bd = $b->datatype;
1360 0 ▶ if ($ad and $bd) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ad and $bd) { } 0 0 0
elsif ($ad or $bd) { } 0 0 0
Condition0%0/3
$ad and $bd
!ll&&!rl&&r
000
MC/DC0%0/2
$ad and $bd
$ad $bd
Truth table0%0/3
A and B
A= $adB= $bd
ABresult
0X0
100
111
1361 0 my $bd = $b->datatype;
1362 0 ▶ return $false unless ($ad->value eq $bd->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $ad->value eq $bd->value 0 0 0
1363 ▶ } elsif ($ad or $bd) {
Condition0%0/3
$ad or $bd
l!l&&r!l&&!r
000
MC/DC0%0/2
$ad or $bd
$ad $bd
Truth table0%0/3
A or B
A= $adB= $bd
ABresult
1X1
011
000
1364 0 return $false;
1365 }
1366 }
1367 0 return $true;
1368 }
1369 } elsif ($func =~ /^(SUBJECT|PREDICATE|OBJECT)$/) {
1370 0 my @operands = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1371 0 my $pos = lc($func);
1372 0 my $term = $operands[0]->$pos();
1373 0 return $term;
1374 } elsif ($func eq 'TRIPLE') {
1375 0 my ($s, $p, $o) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1376 0 ▶ unless ($s->does('Attean::API::BlankOrIRI')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->does("Attean::API::BlankOrIRI")) 0 0 0
1377 0 die 'TRIPLE() constructor failed with bad subject type';
1378 }
1379 0 ▶ unless ($p->does('Attean::API::IRI')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->does("Attean::API::IRI")) 0 0 0
1380 0 die 'TRIPLE() constructor failed with bad predicate type';
1381 }
1382 0 ▶ unless ($o->does('Attean::API::TermOrTriple')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o->does("Attean::API::TermOrTriple")) 0 0 0
1383 0 die 'TRIPLE() constructor failed with bad object type';
1384 }
1385 0 return Attean::Triple->new($s, $p, $o);
1386 } else {
1387 0 warn "Expression evaluation unimplemented: " . $expr->as_string;
1388 0 $self->log->warn("Expression evaluation unimplemented: " . $expr->as_string);
1389 0 die "Expression evaluation unimplemented: " . $expr->as_string;
1390 }
1391 }
1392 } elsif ($expr->isa('Attean::ExistsPlanExpression')) {
1393 0 my $plan = $expr->plan;
1394 0 my $impl = $plan->substitute_impl($model, $r);
1395 0 my $iter = $impl->();
1396 0 my $found = 0;
1397 0 ▶ if (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 0 0 0
1398 # warn "EXISTS found row: " . $row->as_string;
1399 0 $found++;
1400 }
1401
1402 0 ▶ return $found ? Attean::Literal->true : Attean::Literal->false;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 0 0 0
1403 } else {
1404 0 $self->log->warn("Expression evaluation unimplemented: " . $expr->as_string);
1405 0 die "Expression evaluation unimplemented: " . $expr->as_string;
1406 }
1407 }
1408
1409 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=1 SCAR=6.9
Pod undocumented
1410 0 my $self = shift;
1411 0 my $model = shift;
1412 0 my $bind = shift;
1413 0 my %exprs = %{ $self->expressions };
1414 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
1415 # TODO: substitute variables in the expression
1416 0 return $self->_impl($model, $impl, %exprs);
1417 }
1418
1419 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1420 0 my $self = shift;
1421 0 my $model = shift;
1422 0 my %exprs = %{ $self->expressions };
1423 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1424 0 return $self->_impl($model, $impl, %exprs);
1425 }
1426
1427 0 ▶ sub _impl {
Subroutine _impl: not called CC=1 SCAR=6.9
1428 0 my $self = shift;
1429 0 my $model = shift;
1430 0 my $impl = shift;
1431 0 my %exprs = @_;
1432 0 my $iter_variables = $self->in_scope_variables;
1433  
1434 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1435 0 my $iter = $impl->();
1436 return Attean::CodeIterator->new(
1437 item_type => 'Attean::API::Result',
1438 variables => $iter_variables,
1439 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
1440 0 ▶ ROW: while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
1441 # warn 'Extend Row -------------------------------> ' . $r->as_string . "\n";
1442 0 my %row = map { $_ => $r->value($_) } $r->variables;
1443 0 foreach my $var (keys %exprs) {
1444 0 my $expr = $exprs{$var};
1445 # warn "-> $var => " . $expr->as_string . "\n";
1446 0 my $term = eval { $self->evaluate_expression($model, $expr, $r) };
1447 # if ($@) {
1448 # warn "EXTEND expression evaluation error: $@";
1449 # warn '- expression: ' . $expr->as_string;
1450 # }
1451 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
1452 # warn "===> " . $term->as_string . "\n";
1453 0 ▶ if ($row{ $var } and $term->as_string ne $row{ $var }->as_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row{$var} and $term->as_string ne $row{$var}->as_string) 0 0 0
Condition0%0/3
$row{$var} and $term->as_string ne $row{$var}->as_string
!ll&&!rl&&r
000
MC/DC0%0/2
$row{$var} and $term->as_string ne $row{$var}->as_string
$row{$var} $term->as_string ne $row{$var}->as_string
Truth table0%0/3
A and B
A= $row{$var}B= $term->as_string ne $row{$var}->as_string
ABresult
0X0
100
111
1454 0 next ROW;
1455 }
1456
1457 0 ▶ if ($term->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Binding")) 0 0 0
1458 # patterns need to be made ground to be bound as values (e.g. TriplePattern -> Triple)
1459 0 $term = eval{ $term->ground($r) };
1460 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1461 # validation failed ; skip this variable
1462 0 next;
1463 }
1464 }
1465
1466 0 $row{ $var } = $term;
1467 }
1468 }
1469 0 return Attean::Result->new( bindings => \%row, eval_stash => $r->eval_stash );
1470 }
1471 0 return;
1472 }
1473 0 );
1474 0 };
1475 }
1476 }
1477  
1478 =item * L<Attean::Plan::HashDistinct>
1479  
1480 Evaluates a sub-plan, and returns distinct results by checking a persistent
1481 hash of already-seen results.
1482  
1483 =cut
1484  
1485 50 package Attean::Plan::HashDistinct 0.040 {
1486 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1487 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1488
1489 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1490 50 with 'Attean::API::UnionScopeVariablesPlan';
1491  
1492 0 ▶ sub plan_as_string { return 'HashDistinct' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1493
1494 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1495 0 my $self = shift;
1496 0 my $model = shift;
1497 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1498 0 my %seen;
1499 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1500 0 my $iter = $impl->();
1501 0 ▶ return $iter->grep(sub { return not($seen{ shift->as_string }++); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1502 0 };
1503 }
1504 }
1505  
1506 =item * L<Attean::Plan::Unique>
1507  
1508 Evaluates an already-ordered sub-plan, and returns distinct results by
1509 filtering out sequential duplicates.
1510  
1511 =cut
1512  
1513 50 package Attean::Plan::Unique 0.040 {
1514 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1515 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1516
1517 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1518 50 with 'Attean::API::UnionScopeVariablesPlan';
1519  
1520 0 ▶ sub plan_as_string { return 'Unique' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1521
1522 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1523 0 my $self = shift;
1524 0 my $model = shift;
1525 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1526 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1527 0 my $iter = $impl->();
1528 0 my $last = '';
1529 0 ▶ return $iter->grep(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1530 0 my $r = shift;
1531 0 my $s = $r->as_string;
1532 0 my $ok = $s ne $last;
1533 0 $last = $s;
1534 0 return $ok;
1535 0 });
1536 0 };
1537 }
1538 }
1539  
1540 =item * L<Attean::Plan::Slice>
1541  
1542 Evaluates a sub-plan, and returns the results after optionally skipping some
1543 number of results ("offset") and limiting the total number of returned results
1544 ("limit").
1545  
1546 =cut
1547  
1548 50 package Attean::Plan::Slice 0.040 {
1549 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1550 50 ▶ use Types::Standard qw(Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
1551 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1552
1553 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1554 50 with 'Attean::API::UnionScopeVariablesPlan';
1555  
1556 50 has 'limit' => (is => 'ro', isa => Int, default => -1);
1557 50 has 'offset' => (is => 'ro', isa => Int, default => 0);
1558  
1559 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=3 SCAR=24.8
Pod undocumented
1560 0 my $self = shift;
1561 0 my @str;
1562 0 ▶ push(@str, "Limit=" . $self->limit) if ($self->limit >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->limit >= 0 0 0 0
1563 0 ▶ push(@str, "Offset=" . $self->offset) if ($self->offset > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->offset > 0 0 0 0
1564 0 return sprintf('Slice { %s }', join(' ', @str));
1565 }
1566
1567 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1568 0 my $self = shift;
1569 0 my $model = shift;
1570 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1571 0 my $offset = $self->offset;
1572 0 my $limit = $self->limit;
1573 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1574 0 my $iter = $impl->();
1575 0 ▶ $iter = $iter->offset($offset) if ($offset > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $offset > 0 0 0 0
1576 0 ▶ $iter = $iter->limit($limit) if ($limit >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $limit >= 0 0 0 0
1577 0 return $iter;
1578 0 };
1579 }
1580 }
1581  
1582 =item * L<Attean::Plan::Project>
1583  
1584 Evaluates a sub-plan and returns projected results by only keeping a fixed-set
1585 of variable bindings in each result.
1586  
1587 =cut
1588  
1589 50 package Attean::Plan::Project 0.040 {
1590 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1591 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
1592 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1593 50 has 'variables' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']], required => 1);
1594  
1595 10 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.1
Pod undocumented
1596 10 my $class = shift;
1597 10 my %args = @_;
1598 10 my @vars = map { $_->value } @{ $args{variables} };
1599
1600 10 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 10 10
1601 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
1602 }
1603 10 $args{in_scope_variables} = \@vars;
1604  
1605 10 return $class->SUPER::BUILDARGS(%args);
1606 }
1607
1608 # sub BUILD {
1609 # my $self = shift;
1610 # my @vars = map { $_->value } @{ $self->variables };
1611 # unless (scalar(@vars)) {
1612 # Carp::confess "No vars in project?";
1613 # }
1614 # }
1615
1616 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1617 0 my $self = shift;
1618 0 return sprintf('Project { %s }', join(' ', map { '?' . $_->value } @{ $self->variables }));
1619 }
1620 0 ▶ sub tree_attributes { return qw(variables) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1621
1622 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=1 SCAR=6.9
Pod undocumented
1623 0 my $self = shift;
1624 0 my $model = shift;
1625 0 my $bind = shift;
1626 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
1627 0 my @vars = map { $_->value } @{ $self->variables };
1628 0 my $iter_variables = $self->in_scope_variables;
1629  
1630 # TODO: substitute variables in the projection where appropriate
1631 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1632 0 my $iter = $impl->();
1633 0 ▶ return $iter->map(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1634 0 my $r = shift;
1635 0 ▶ my $b = { map { my $t = $r->value($_); $t ? ($_ => $t) : () } @vars };
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 0 0 0
1636 0 return Attean::Result->new( bindings => $b );
1637 0 }, $iter->item_type, variables => $iter_variables);
1638 0 };
1639 }
1640
1641 2 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
1642 2 my $self = shift;
1643 2 my $model = shift;
1644 2 my ($impl) = map { $_->impl($model) } @{ $self->children };
1645 2 my @vars = map { $_->value } @{ $self->variables };
1646 2 my $iter_variables = $self->in_scope_variables;
1647  
1648 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1649 2 my $iter = $impl->();
1650 2 ▶ return $iter->map(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1651 2 my $r = shift;
1652 2 ▶ my $b = { map { my $t = $r->value($_); $t ? ($_ => $t) : () } @vars };
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 2 0 2
1653 2 return Attean::Result->new( bindings => $b );
1654 2 }, $iter->item_type, variables => $iter_variables);
1655 2 };
1656 }
1657 }
1658  
1659 =item * L<Attean::Plan::OrderBy>
1660  
1661 Evaluates a sub-plan and returns the results after fully materializing and
1662 sorting is applied.
1663  
1664 =cut
1665  
1666 50 package Attean::Plan::OrderBy 0.040 {
1667 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1668 50 ▶ use Types::Standard qw(HashRef ArrayRef InstanceOf Bool Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
1669 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
1670 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1671
1672 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1673 50 with 'Attean::API::UnionScopeVariablesPlan';
1674  
1675 50 has 'variables' => (is => 'ro', isa => ArrayRef[Str], required => 1);
1676 50 has 'ascending' => (is => 'ro', isa => HashRef[Bool], required => 1);
1677  
1678 1 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=2 SCAR=7.0
Pod undocumented
1679 1 my $self = shift;
1680 1 my @vars = @{ $self->variables };
1681 1 my $ascending = $self->ascending;
1682 1 ▶ my @strings = map { sprintf('%s(?%s)', ($ascending->{$_} ? 'ASC' : 'DESC'), $_) } @vars;
Branch: 1 branch
BranchTrueFalseTotal
$ascending->{$_} ? : 1 0 1
1683 1 return sprintf('Order { %s }', join(', ', @strings));
1684 }
1685
1686 0 ▶ sub sort_rows {
Subroutine sort_rows: not called CC=14 SCAR=53.5
Pod undocumented
1687 0 my $self = shift;
1688 0 my $vars = shift;
1689 0 my $ascending = shift;
1690 0 my $rows = shift;
1691 0 local($Attean::API::Binding::ALLOW_IRI_COMPARISON) = 1;
1692 0 my @sorted = map { $_->[0] } sort {
1693 0 my ($ar, $avalues) = @$a;
1694 0 my ($br, $bvalues) = @$b;
1695 0 my $c = 0;
1696 0 foreach my $i (0 .. $#{ $vars }) {
1697 0 my $ascending = $ascending->{ $vars->[$i] };
1698 0 my ($av, $bv) = map { $_->[$i] } ($avalues, $bvalues);
1699  
1700 # Mirrors code in Attean::SimpleQueryEvaluator->evaluate
1701 0 ▶ if (blessed($av) and $av->does('Attean::API::Binding') and (not(defined($bv)) or not($bv->does('Attean::API::Binding')))) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($av) and $av->does('Attean::API::Binding') and not defined $bv && $bv->does('Attean::API::Binding')) { } 0 0 0
elsif (builtin::blessed($bv) and $bv->does('Attean::API::Binding') and not defined $av && $av->does('Attean::API::Binding')) { } 0 0 0
Condition0%0/9
builtin::blessed($av) and $av->does("Attean::API::Binding") and not defined $bv && $bv->does("Attean::API::Binding")
!ll&&!rl&&r
000
builtin::blessed($av) and $av->does("Attean::API::Binding")
!ll&&!rl&&r
000
defined $bv && $bv->does('Attean::API::Binding')
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($av) and $av->does("Attean::API::Binding") and not defined $bv && $bv->does("Attean::API::Binding")
builtin::blessed($av) $av->does("Attean::API::Binding") defined $bv $bv->does('Attean::API::Binding')
Truth table0%0/5
A and B and not(C && D)
A= builtin::blessed($av)B= $av->does("Attean::API::Binding")C= defined $bvD= $bv->does('Attean::API::Binding')
ABCDresult
0XXX0
10XX0
11110
110X1
11101
1702 0 $c = 1;
1703 ▶ } elsif (blessed($bv) and $bv->does('Attean::API::Binding') and (not(defined($av)) or not($av->does('Attean::API::Binding')))) {
Condition0%0/9
builtin::blessed($bv) and $bv->does("Attean::API::Binding") and not defined $av && $av->does("Attean::API::Binding")
!ll&&!rl&&r
000
builtin::blessed($bv) and $bv->does("Attean::API::Binding")
!ll&&!rl&&r
000
defined $av && $av->does('Attean::API::Binding')
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($bv) and $bv->does("Attean::API::Binding") and not defined $av && $av->does("Attean::API::Binding")
builtin::blessed($bv) $bv->does("Attean::API::Binding") defined $av $av->does('Attean::API::Binding')
Truth table0%0/5
A and B and not(C && D)
A= builtin::blessed($bv)B= $bv->does("Attean::API::Binding")C= defined $avD= $av->does('Attean::API::Binding')
ABCDresult
0XXX0
10XX0
11110
110X1
11101
1704 0 $c = -1;
1705 } else {
1706 0 ▶ $c = eval { $av ? $av->compare($bv) : 1 };
Branch: 1 branch
BranchTrueFalseTotal
$av ? : 0 0 0
1707 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1708 0 $c = 1;
1709 }
1710 }
1711 0 ▶ $c *= -1 unless ($ascending);
Branch: 1 branch
BranchTrueFalseTotal
unless $ascending 0 0 0
1712 0 ▶ last unless ($c == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $c == 0 0 0 0
1713 }
1714 $c
1715 } map {
1716 0 my $r = $_;
1717 0 [$r, [map { $r->value($_) } @$vars]]
1718 } @$rows;
1719 0 return @sorted;
1720 }
1721
1722 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1723 0 my $self = shift;
1724 0 my $model = shift;
1725 0 my $vars = $self->variables;
1726 0 my $ascending = $self->ascending;
1727 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1728 0 my $iter_variables = $self->in_scope_variables;
1729  
1730 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1731 0 my $iter = $impl->();
1732 0 my @rows = $iter->elements;
1733 0 my @sorted = $self->sort_rows($vars, $ascending, \@rows);
1734 0 return Attean::ListIterator->new(
1735 values => \@sorted,
1736 variables => $iter_variables,
1737 item_type => $iter->item_type
1738 );
1739 }
1740 0 }
1741 }
1742  
1743 =item * L<Attean::Plan::Service>
1744  
1745 Evaluates a SPARQL query against a remote endpoint.
1746  
1747 =cut
1748  
1749 50 package Attean::Plan::Service 0.040 {
1750 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1751 50 ▶ use Types::Standard qw(ConsumerOf Bool Str InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1752 50 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
1753 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
1754 50 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
1755 50 ▶ use Attean::SPARQLClient;
Subroutine BEGIN: called CC=1 SCAR=0.0
1756 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1757  
1758 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1759  
1760 50 has 'endpoint' => (is => 'ro', isa => ConsumerOf['Attean::API::TermOrVariable'], required => 1);
1761 50 has 'silent' => (is => 'ro', isa => Bool, default => 0);
1762 50 has 'sparql' => (is => 'ro', isa => Str, required => 1);
1763 50 has 'user_agent' => (is => 'rw', isa => InstanceOf['LWP::UserAgent']);
1764 50 has 'request_signer' => (is => 'rw');
1765 50 has 'client' => (is => 'rw', required => 0);
1766  
1767 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1768 0 my $self = shift;
1769 0 my $sparql = $self->sparql;
1770 0 $sparql =~ s/\s+/ /g;
1771 0 return sprintf('Service <%s> %s', $self->endpoint->as_string, $sparql);
1772 }
1773
1774 0 ▶ sub tree_attributes { return qw(endpoint) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1775 0 ▶ sub impl {
Subroutine impl: not called CC=3 SCAR=24.8
Pod undocumented
1776 0 my $self = shift;
1777 0 my $model = shift;
1778  
1779 0 my $endpoint = $self->endpoint->value;
1780 0 my $sparql = $self->sparql;
1781 0 my $silent = $self->silent;
1782 0 my %args = (
1783 endpoint => $endpoint,
1784 silent => $silent,
1785 request_signer => $self->request_signer,
1786 );
1787 0 ▶ $args{user_agent} = $self->user_agent if ($self->user_agent);
Branch: 1 branch
BranchTrueFalseTotal
if $self->user_agent 0 0 0
1788 0 ▶ my $client = $self->client || Attean::SPARQLClient->new(%args);
Condition0%0/3
$self->client || 'Attean::SPARQLClient'->new(%args)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->client || 'Attean::SPARQLClient'->new(%args)
$self->client 'Attean::SPARQLClient'->new(%args)
Truth table0%0/3
A || B
A= $self->clientB= 'Attean::SPARQLClient'->new(%args)
ABresult
1X1
011
000
1789 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1790 0 return $client->query($sparql);
1791 0 };
1792 }
1793 }
1794  
1795 =item * L<Attean::Plan::Table>
1796  
1797 Returns a constant set of results.
1798  
1799 =cut
1800  
1801 50 package Attean::Plan::Table 0.040 {
1802 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1803 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1804 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1805  
1806 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1807  
1808 50 has variables => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']]);
1809 50 has rows => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Result']]);
1810  
1811 0 ▶ sub tree_attributes { return qw(variables rows) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1812 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=2 SCAR=17.9
Pod undocumented
1813 0 my $self = shift;
1814 0 my $level = shift;
1815 0 my $indent = ' ' x ($level + 1);
1816 0 my $vars = join(', ', map { "?$_" } @{ $self->in_scope_variables });
1817 0 my $s = "Table (" . $vars . ")";
1818 0 foreach my $row (@{ $self->rows }) {
1819 0 $s .= "\n-${indent} " . $row->as_string;
1820 }
1821 0 return $s;
1822 }
1823
1824 8 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.3
Pod undocumented
1825 8 my $class = shift;
1826 8 my %args = @_;
1827 0 my @vars = map { $_->value } @{ $args{variables} };
1828
1829 8 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 8 8
1830 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
1831 }
1832 8 $args{in_scope_variables} = \@vars;
1833  
1834 8 return $class->SUPER::BUILDARGS(%args);
1835 }
1836
1837 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1838 0 my $self = shift;
1839 0 my $model = shift;
1840 0 my $rows = $self->rows;
1841 0 my $iter_variables = $self->in_scope_variables;
1842  
1843 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1844 0 return Attean::ListIterator->new(
1845 item_type => 'Attean::API::Result',
1846 variables => $iter_variables,
1847 values => $rows
1848 );
1849 0 };
1850 }
1851 }
1852  
1853 =item * L<Attean::Plan::Iterator>
1854  
1855 Returns a constant set of results.
1856  
1857 Be aware that if the iterator being wrapped is not repeatable (consuming the
1858 L<Attean::API::RepeatableIterator> role), then this plan may only be evaluated
1859 once.
1860  
1861 A size estimate may be given if it is available. If the iterator is an
1862 L<Attean::ListIterator>, the size of that iterator will be used.
1863  
1864 =cut
1865  
1866 50 package Attean::Plan::Iterator 0.040 {
1867 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1868 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
1869 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1870  
1871 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1872  
1873 50 has iterator => (is => 'ro', isa => ConsumerOf['Attean::API::ResultIterator']);
1874 50 has size_estimate => (is => 'lazy', isa => Int, predicate => 1);
1875  
1876 1 ▶ sub _build_size_estimate {
Subroutine _build_size_estimate: called CC=2 SCAR=7.0
1877 1 my $self = shift;
1878 1 my $iter = $self->iterator;
1879 1 ▶ if ($iter->isa('Attean::ListIterator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->isa("Attean::ListIterator")) 1 0 1
1880 1 return $iter->size;
1881 }
1882 }
1883  
1884  
1885 0 ▶ sub tree_attributes { return qw(iterator) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1886 4 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=2 SCAR=6.9
Pod undocumented
1887 4 my $self = shift;
1888 4 my $level = shift;
1889 4 my $indent = ' ' x ($level + 1);
1890 4 my $string = 'Iterator (';
1891 4 $string .= join(', ', map { "?$_" } @{ $self->in_scope_variables });
1892 4 ▶ if ($self->has_size_estimate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_size_estimate) 3 1 4
1893 3 $string .= ' with ' . $self->size_estimate . ' elements';
1894 }
1895 4 $string .= ')';
1896 4 return $string;
1897 }
1898
1899 5 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.1
Pod undocumented
1900 5 my $class = shift;
1901 5 my %args = @_;
1902 5 my $vars = $args{iterator}->variables;
1903
1904 5 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 5 5
1905 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
1906 }
1907 5 $args{in_scope_variables} = $vars;
1908  
1909 5 return $class->SUPER::BUILDARGS(%args);
1910 }
1911
1912 1 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
1913 1 my $self = shift;
1914 1 my $model = shift;
1915 1 my $iter = $self->iterator;
1916  
1917 1 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
1918 1 ▶ if ($iter->does('Attean::API::RepeatableIterator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->does("Attean::API::RepeatableIterator")) 1 0 1
1919 1 $iter->reset;
1920 }
1921 1 return $iter;
1922 1 };
1923 }
1924 }
1925  
1926 =item * L<Attean::Plan::ALPPath>
1927  
1928 =cut
1929  
1930 50 package Attean::Plan::ALPPath 0.040 {
1931 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1932 50 ▶ use Attean::TreeRewriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
1933 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1934 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1935  
1936 50 has 'subject' => (is => 'ro', required => 1);
1937 50 has 'object' => (is => 'ro', required => 1);
1938 50 has 'graph' => (is => 'ro', required => 1);
1939 50 has 'step_begin' => (is => 'ro', required => 1);
1940 50 has 'step_end' => (is => 'ro', required => 1);
1941 50 has 'skip' => (is => 'ro', required => 1, default => 0);
1942 # has 'children' => (is => 'ro', isa => ConsumerOf['Attean::API::BindingSubstitutionPlan'], required => 1);
1943
1944 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::NullaryQueryTree';
1945  
1946 0 ▶ sub tree_attributes { return qw(subject object graph) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1947 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1948
1949 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1950 0 my $self = shift;
1951 0 my @strings;
1952 0 push(@strings, sprintf('%s ← %s', map { $_->ntriples_string } ($self->subject, $self->step_begin)));
1953 0 push(@strings, sprintf('%s ← %s', map { $_->ntriples_string } ($self->object, $self->step_end)));
1954 0 return sprintf('ALPPath %s', join(', ', @strings));
1955 }
1956
1957 0 ▶ sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
1958 0 my $class = shift;
1959 0 my %args = @_;
1960 0 my @vars = map { $_->value } grep { $_->does('Attean::API::Variable') } (@args{qw(subject object)});
1961
1962 0 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 0 0
1963 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
1964 }
1965 0 $args{in_scope_variables} = \@vars;
1966  
1967 0 return $class->SUPER::BUILDARGS(%args);
1968 }
1969
1970 0 ▶ sub alp {
Subroutine alp: not called CC=5 SCAR=34.0
Pod undocumented
1971 0 my $model = shift;
1972 0 my $graph = shift;
1973 0 my $skip = shift;
1974 0 my $x = shift;
1975 0 my $path = shift;
1976 0 my $v = shift;
1977 0 my $start = shift;
1978 0 my $end = shift;
1979 0 my $bind = shift;
1980 0 ▶ if (exists $v->{$x->as_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$v{$x->as_string}) 0 0 0
1981 0 return;
1982 }
1983 0 my $binding = Attean::Result->new( bindings => { $start => $x } )->join($bind);
1984 0 ▶ unless ($binding) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($binding) 0 0 0
1985 0 return;
1986 }
1987
1988 0 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) { } 0 0 0
1989 0 $skip--;
1990 } else {
1991 0 $v->{$x->as_string} = $x;
1992 }
1993
1994 0 my $impl = $path->substitute_impl($model, $binding);
1995 0 my $iter = $impl->();
1996 0 ▶ while (my $row = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 0 0 0
1997 0 my $n = $row->value($end);
1998 0 alp($model, $graph, $skip, $n, $path, $v, $start, $end, $bind);
1999 }
2000 }
2001
2002 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=8 SCAR=42.8
Pod undocumented
2003 0 my $self = shift;
2004 0 my $model = shift;
2005 0 my $bind = shift;
2006 0 my $path = $self->children->[0];
2007 0 my $subject = $self->subject;
2008 0 my $object = $self->object;
2009 0 my $graph = $self->graph;
2010 0 my $start = $self->step_begin->value;
2011 0 my $end = $self->step_end->value;
2012 0 my $skip = $self->skip;
2013 0 my $iter_variables = $self->in_scope_variables;
2014  
2015 0 for ($subject, $object) {
2016 0 ▶ if ($_->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->does("Attean::API::Variable")) 0 0 0
2017 0 my $name = $_->value;
2018 0 ▶ if (my $node = $bind->value($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $bind->value($name)) 0 0 0
2019 0 $_ = $node;
2020 }
2021 }
2022 }
2023
2024 0 my $s_var = $subject->does('Attean::API::Variable');
2025 0 my $o_var = $object->does('Attean::API::Variable');
2026 0 ▶ if ($s_var and $o_var) {
Branch: 3 branches
BranchTrueFalseTotal
if ($s_var and $o_var) { } 0 0 0
elsif ($o_var) { } 0 0 0
elsif ($s_var) { } 0 0 0
Condition0%0/3
$s_var and $o_var
!ll&&!rl&&r
000
MC/DC0%0/2
$s_var and $o_var
$s_var $o_var
Truth table0%0/3
A and B
A= $s_varB= $o_var
ABresult
0X0
100
111
2027 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2028 0 my $nodes = $model->graph_nodes($graph);
2029 0 my @rows;
2030 0 ▶ while (my $n = $nodes->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $nodes->next) 0 0 0
2031 0 my %seen;
2032 0 alp($model, $graph, $skip, $n, $path, \%seen, $start, $end, $bind);
2033 0 foreach my $term (values %seen) {
2034 0 my $b = Attean::Result->new( bindings => {
2035 $subject->value => $n,
2036 $object->value => $term,
2037 } );
2038 0 push(@rows, $b);
2039 }
2040 }
2041 0 return Attean::ListIterator->new(
2042 item_type => 'Attean::API::Result',
2043 variables => $iter_variables,
2044 values => \@rows,
2045 );
2046 0 };
2047 } elsif ($o_var) {
2048 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2049 0 my %seen;
2050 0 alp($model, $graph, $skip, $subject, $path, \%seen, $start, $end, $bind);
2051 0 my @rows = map { Attean::Result->new( bindings => { $object->value => $_ } ) } (values %seen);
2052 0 return Attean::ListIterator->new(
2053 item_type => 'Attean::API::Result',
2054 variables => $iter_variables,
2055 values => \@rows,
2056 );
2057 0 };
2058 } elsif ($s_var) {
2059 0 die "ALP for FB should never occur in a plan (should be inversed during planning)";
2060 } else {
2061 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2062 0 my %seen;
2063 0 alp($model, $graph, $skip, $subject, $path, \%seen, $start, $end, $bind);
2064 0 ▶ if (exists $seen{ $object->as_string }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$object->as_string}) { } 0 0 0
2065 0 return Attean::ListIterator->new(
2066 item_type => 'Attean::API::Result',
2067 variables => $iter_variables,
2068 values => [Attean::Result->new()]
2069 );
2070 } else {
2071 0 return Attean::ListIterator->new(
2072 item_type => 'Attean::API::Result',
2073 variables => $iter_variables,
2074 values => []
2075 );
2076 }
2077 0 };
2078 }
2079 }
2080 }
2081  
2082 50 package Attean::Plan::ZeroOrOnePath 0.040 {
2083 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2084 50 ▶ use Attean::TreeRewriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
2085 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
2086 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2087  
2088 50 has 'subject' => (is => 'ro', required => 1);
2089 50 has 'object' => (is => 'ro', required => 1);
2090 50 has 'graph' => (is => 'ro', required => 1);
2091
2092 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::NullaryQueryTree';
2093  
2094 0 ▶ sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
2095 0 my $class = shift;
2096 0 my %args = @_;
2097 0 my @vars = map { $_->value } grep { $_->does('Attean::API::Variable') } (@args{qw(subject object)});
2098
2099 0 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 0 0
2100 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
2101 }
2102 0 $args{in_scope_variables} = \@vars;
2103  
2104 0 return $class->SUPER::BUILDARGS(%args);
2105 }
2106
2107 0 ▶ sub tree_attributes { return qw(subject object) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2108 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2109
2110 0 ▶ sub plan_as_string { return 'ZeroOrOnePath' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2111
2112 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=4 SCAR=30.0
Pod undocumented
2113 0 my $self = shift;
2114 0 my $model = shift;
2115 0 my $bind = shift;
2116 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
2117 0 my $iter_variables = $self->in_scope_variables;
2118  
2119 0 my $subject = $self->subject;
2120 0 my $object = $self->object;
2121 0 my $graph = $self->graph;
2122 0 for ($subject, $object) {
2123 0 ▶ if ($_->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->does("Attean::API::Variable")) 0 0 0
2124 0 my $name = $_->value;
2125 0 ▶ if (my $node = $bind->value($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $bind->value($name)) 0 0 0
2126 0 $_ = $node;
2127 }
2128 }
2129 }
2130  
2131 0 my $s_var = $subject->does('Attean::API::Variable');
2132 0 my $o_var = $object->does('Attean::API::Variable');
2133 0 ▶ return sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
2134 0 my @extra;
2135 0 ▶ if ($s_var and $o_var) {
Branch: 3 branches
BranchTrueFalseTotal
if ($s_var and $o_var) { } 0 0 0
elsif ($s_var) { } 0 0 0
elsif ($o_var) { } 0 0 0
Condition0%0/3
$s_var and $o_var
!ll&&!rl&&r
000
MC/DC0%0/2
$s_var and $o_var
$s_var $o_var
Truth table0%0/3
A and B
A= $s_varB= $o_var
ABresult
0X0
100
111
2136 0 my $nodes = $model->graph_nodes($graph);
2137 0 ▶ while (my $n = $nodes->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $nodes->next) 0 0 0
2138 0 push(@extra, Attean::Result->new( bindings => { map { $_->value => $n } ($subject, $object) } ));
2139 }
2140 } elsif ($s_var) {
2141 0 push(@extra, Attean::Result->new( bindings => { $subject->value => $object } ));
2142 } elsif ($o_var) {
2143 0 push(@extra, Attean::Result->new( bindings => { $object->value => $subject } ));
2144 } else {
2145 0 ▶ if (0 == $subject->compare($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $subject->compare($object)) 0 0 0
2146 0 push(@extra, Attean::Result->new( bindings => {} ));
2147 }
2148 }
2149 0 my $iter = $impl->();
2150 0 my %seen;
2151 return Attean::CodeIterator->new(
2152 item_type => 'Attean::API::Result',
2153 variables => $iter_variables,
2154 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
2155 0 ▶ while (scalar(@extra)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @extra) 0 0 0
2156 0 my $r = shift(@extra);
2157 0 ▶ unless ($seen{$r->as_string}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$r->as_string}++) 0 0 0
2158 0 return $r;
2159 }
2160 }
2161 0 ▶ while (my $r = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
2162 0 ▶ return unless ($r);
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
2163 0 ▶ if ($seen{$r->as_string}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$r->as_string}++) 0 0 0
2164 0 next;
2165 }
2166 0 return $r;
2167 }
2168 }
2169 0 );
2170 0 };
2171 }
2172 }
2173  
2174 =item * L<Attean::Plan::Exists>
2175  
2176 Returns an iterator containing a single boolean term indicating whether any
2177 results were produced by evaluating the sub-plan.
2178  
2179 =cut
2180  
2181 50 package Attean::Plan::Exists 0.040 {
2182 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2183 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
2184 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2185
2186 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2187 50 with 'Attean::API::UnionScopeVariablesPlan';
2188  
2189 50 has variables => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']]);
2190 50 has rows => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Result']]);
2191  
2192 0 ▶ sub tree_attributes { return qw(variables rows) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2193 0 ▶ sub plan_as_string { return 'Exists' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2194
2195 4 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
2196 4 my $self = shift;
2197 4 my $model = shift;
2198 4 my ($impl) = map { $_->impl($model) } @{ $self->children };
2199 4 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2200 4 my $iter = $impl->();
2201 4 my $result = $iter->next;
2202 # if ($result) {
2203 # warn "EXISTS: " . $result->as_string;
2204 # }
2205 4 ▶ my $term = $result ? Attean::Literal->true : Attean::Literal->false;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 2 2 4
2206 4 return Attean::ListIterator->new(values => [$term], item_type => 'Attean::API::Term');
2207 }
2208 4 }
2209 }
2210  
2211 =item * L<Attean::Plan::Aggregate>
2212  
2213 =cut
2214  
2215 50 package Attean::Plan::Aggregate 0.040 {
2216 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2217 50 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
2218 50 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
2219 50 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
2220 50 ▶ use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
2221 50 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
2222 50 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
2223 50 ▶ use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
2224 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2225 50 ▶ use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
2226 50 ▶ use Types::Standard qw(ConsumerOf InstanceOf HashRef ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2227 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2228  
2229 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2230 50 has 'aggregates' => (is => 'ro', isa => HashRef[ConsumerOf['Attean::API::Expression']], required => 1);
2231 50 has 'groups' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Expression']], required => 1);
2232 50 has 'active_graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::IRI']], required => 1);
2233
2234 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2235 0 my $self = shift;
2236 0 my @astrings = map { sprintf('?%s ← %s', $_, $self->aggregates->{$_}->as_string) } keys %{ $self->aggregates };
2237 0 my @gstrings = map { sprintf('%s', $_->as_string) } @{ $self->groups };
2238 0 return sprintf('Aggregate { %s } Groups { %s }', join(', ', @astrings), join(', ', @gstrings));
2239 }
2240 0 ▶ sub tree_attributes { return qw(aggregates groups) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2241
2242 4 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
2243 # Ensure that the CT extensions are registered
2244 4 AtteanX::Functions::CompositeLists->register();
2245 4 AtteanX::Functions::CompositeMaps->register();
2246 }
2247  
2248 4 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=3 SCAR=11.1
Pod undocumented
2249 4 my $class = shift;
2250 4 my %args = @_;
2251 4 my $aggs = $args{ aggregates };
2252 4 ▶ my @vars = map { $_->value } grep { $_->does('Attean::API::Variable') } map { $_->value } @{ $args{groups} // [] };
Condition50%1/2
$args{"groups"} // []
l!l
40
MC/DC0%0/1
$args{"groups"} // []
$args{"groups"}
Truth table50%1/2
A
A= $args{"groups"}
Aresult
11
00
2253 4 my @evars = (@vars, keys %$aggs);
2254 4 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 4 4
2255 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
2256 }
2257 4 $args{in_scope_variables} = [@evars];
2258 4 return $class->SUPER::BUILDARGS(%args);
2259 }
2260
2261 0 ▶ sub evaluate_aggregate {
Subroutine evaluate_aggregate: not called CC=41 SCAR=74.5
Pod undocumented
2262 0 my $self = shift;
2263 0 my $model = shift;
2264 0 my $expr = shift;
2265 0 my $rows = shift;
2266 0 my $op = $expr->operator;
2267 0 my ($e) = @{ $expr->children };
2268 # my @children = map { Attean::Plan::Extend->evaluate_expression($model, $_, $r) } @{ $expr->children };
2269 # warn "$op — " . join(' ', map { $_->as_string } @children);
2270 0 ▶ if ($op eq 'COUNT') {
Branch: 8 branches
BranchTrueFalseTotal
if ($op eq 'COUNT') { } 0 0 0
elsif ($op eq 'SUM') { } 0 0 0
elsif ($op eq 'AVG') { } 0 0 0
elsif ($op eq 'SAMPLE') { } 0 0 0
elsif ($op =~ /^(MIN|MAX)$/u) { } 0 0 0
elsif ($op eq 'GROUP_CONCAT') { } 0 0 0
elsif ($op eq 'FOLD') { } 0 0 0
elsif ($op eq 'CUSTOM') { } 0 0 0
2271 0 my $count = 0;
2272 0 foreach my $r (@$rows) {
2273 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) { } 0 0 0
2274 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2275 0 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) 0 0 0
2276 0 $count++;
2277 }
2278 } else {
2279 # This is the special-case branch for COUNT(*)
2280 0 $count++;
2281 }
2282 }
2283 0 return Attean::Literal->new(value => $count, datatype => 'http://www.w3.org/2001/XMLSchema#integer');
2284 } elsif ($op eq 'SUM') {
2285 0 my @cmp;
2286 my @terms;
2287 0 foreach my $r (@$rows) {
2288 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2289 0 ▶ if ($term->does('Attean::API::NumericLiteral')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::NumericLiteral")) 0 0 0
2290 0 push(@terms, $term);
2291 }
2292 }
2293 0 my $lhs = shift(@terms);
2294 0 ▶ while (my $rhs = shift(@terms)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rhs = shift @terms) 0 0 0
2295 0 my $type = $lhs->binary_promotion_type($rhs, '+');
2296 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
2297 0 $lhs = Attean::Literal->new(value => ($lv + $rv), datatype => $type);
2298 }
2299 0 return $lhs;
2300 } elsif ($op eq 'AVG') {
2301 0 my @cmp;
2302 0 my $count = 0;
2303 0 my $all_ints = 1;
2304 0 my @terms;
2305 0 ▶ if (scalar(@$rows) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$rows == 0) 0 0 0
2306 0 return Attean::Literal->integer(0);
2307 }
2308 0 foreach my $r (@$rows) {
2309 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2310 0 ▶ die unless ($term->does('Attean::API::NumericLiteral'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::NumericLiteral") 0 0 0
2311 0 push(@terms, $term);
2312 0 $count++;
2313 }
2314 0 my $lhs = shift(@terms);
2315 0 ▶ while (my $rhs = shift(@terms)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rhs = shift @terms) 0 0 0
2316 0 my $type = $lhs->binary_promotion_type($rhs, '+');
2317 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
2318 0 $lhs = Attean::Literal->new(value => ($lv + $rv), datatype => $type);
2319 }
2320
2321 0 my $rhs = Attean::Literal->new(value => $count, datatype => 'http://www.w3.org/2001/XMLSchema#integer');
2322 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
2323 0 my $type = $lhs->binary_promotion_type($rhs, '/');
2324 0 return Attean::Literal->new(value => ($lv / $rv), datatype => $type);
2325 } elsif ($op eq 'SAMPLE') {
2326 0 foreach my $r (@$rows) {
2327 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2328 0 ▶ return $term if (blessed($term));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($term) 0 0 0
2329 }
2330 } elsif ($op =~ /^(MIN|MAX)$/) {
2331 0 my @cmp;
2332 0 foreach my $r (@$rows) {
2333 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2334 0 push(@cmp, $term);
2335 }
2336 0 @cmp = sort { $a->compare($b) } @cmp;
2337 0 ▶ return ($op eq 'MIN') ? shift(@cmp) : pop(@cmp);
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'MIN' ? : 0 0 0
2338 } elsif ($op eq 'GROUP_CONCAT') {
2339 0 ▶ my $sep = $expr->scalar_vars->{seperator} // ' ';
Condition0%0/2
$expr->scalar_vars->{"seperator"} // " "
l!l
00
MC/DC0%0/1
$expr->scalar_vars->{"seperator"} // " "
$expr->scalar_vars->{"seperator"}
Truth table0%0/2
A
A= $expr->scalar_vars->{"seperator"}
Aresult
11
00
2340 0 my @values;
2341 my %seen;
2342 0 foreach my $r (@$rows) {
2343 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2344 0 ▶ die "GROUP_CONCAT called with a non-literal argument" unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
2345 0 ▶ if ($expr->distinct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->distinct) { } 0 0 0
2346 0 ▶ unless ($seen{ $term->value }++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$term->value}++) 0 0 0
2347 0 push(@values, $term->value);
2348 }
2349 } else {
2350 0 push(@values, $term->value);
2351 }
2352 }
2353 0 my $string = join($sep, @values);
2354 0 return Attean::Literal->new(value => $string);
2355 } elsif ($op eq 'FOLD') {
2356 0 my @arg_exprs = @{ $expr->children };
2357 0 ▶ my $order = $expr->order || [];
Condition0%0/2
$expr->order || []
l!l
00
MC/DC0%0/1
$expr->order || []
$expr->order
Truth table0%0/2
A
A= $expr->order
Aresult
11
00
2358 0 my @cmps = @$order;
2359 0 my @exprs = map { $_->[1] } @cmps;
2360 0 my @dirs = map { $_->[0] eq 'ASC' } @cmps;
2361 0 my $l = eval {
2362 0 ▶ if (scalar(@$order)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$order) 0 0 0
2363 # sort $rows by the order condition
2364 0 ▶ my $fold_cmp = sub {
Subroutine __ANON__: not called CC=16 SCAR=56.1
2365 0 my ($ar, $avalues) = @$a;
2366 0 my ($br, $bvalues) = @$b;
2367  
2368 0 my $c = 0;
2369 0 foreach my $i (0 .. $#cmps) {
2370 0 my ($av, $bv) = map { $_->[$i] } ($avalues, $bvalues);
2371 # Mirrors code in Attean::Plan::OrderBy->sort_rows
2372 0 ▶ if (not(blessed($av))) {
Branch: 4 branches
BranchTrueFalseTotal
if (not builtin::blessed($av)) { } 0 0 0
elsif (not builtin::blessed($av)) { } 0 0 0
elsif (builtin::blessed($av) and $av->does('Attean::API::Binding') and not defined $bv && $bv->does('Attean::API::Binding')) { } 0 0 0
elsif (builtin::blessed($bv) and $bv->does('Attean::API::Binding') and not defined $av && $av->does('Attean::API::Binding')) { } 0 0 0
2373 0 $c = -1;
2374 } elsif (not(blessed($av))) {
2375 0 $c = 1;
2376 ▶ } elsif (blessed($av) and $av->does('Attean::API::Binding') and (not(defined($bv)) or not($bv->does('Attean::API::Binding')))) {
Condition0%0/9
builtin::blessed($av) and $av->does("Attean::API::Binding") and not defined $bv && $bv->does("Attean::API::Binding")
!ll&&!rl&&r
000
builtin::blessed($av) and $av->does("Attean::API::Binding")
!ll&&!rl&&r
000
defined $bv && $bv->does('Attean::API::Binding')
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($av) and $av->does("Attean::API::Binding") and not defined $bv && $bv->does("Attean::API::Binding")
builtin::blessed($av) $av->does("Attean::API::Binding") defined $bv $bv->does('Attean::API::Binding')
Truth table0%0/5
A and B and not(C && D)
A= builtin::blessed($av)B= $av->does("Attean::API::Binding")C= defined $bvD= $bv->does('Attean::API::Binding')
ABCDresult
0XXX0
10XX0
11110
110X1
11101
2377 0 $c = 1;
2378 ▶ } elsif (blessed($bv) and $bv->does('Attean::API::Binding') and (not(defined($av)) or not($av->does('Attean::API::Binding')))) {
Condition0%0/9
builtin::blessed($bv) and $bv->does("Attean::API::Binding") and not defined $av && $av->does("Attean::API::Binding")
!ll&&!rl&&r
000
builtin::blessed($bv) and $bv->does("Attean::API::Binding")
!ll&&!rl&&r
000
defined $av && $av->does('Attean::API::Binding')
!ll&&!rl&&r
000
MC/DC0%0/4
builtin::blessed($bv) and $bv->does("Attean::API::Binding") and not defined $av && $av->does("Attean::API::Binding")
builtin::blessed($bv) $bv->does("Attean::API::Binding") defined $av $av->does('Attean::API::Binding')
Truth table0%0/5
A and B and not(C && D)
A= builtin::blessed($bv)B= $bv->does("Attean::API::Binding")C= defined $avD= $av->does('Attean::API::Binding')
ABCDresult
0XXX0
10XX0
11110
110X1
11101
2379 0 $c = -1;
2380 } else {
2381 0 ▶ $c = eval { $av ? $av->compare($bv) : 1 };
Branch: 1 branch
BranchTrueFalseTotal
$av ? : 0 0 0
2382 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
2383 0 $c = 1;
2384 }
2385 }
2386 0 ▶ $c *= -1 if ($dirs[$i] == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $dirs[$i] == 0 0 0 0
2387 0 ▶ last unless ($c == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $c == 0 0 0 0
2388 }
2389 $c
2390 0 };
2391
2392 0 my @sorted = map { $_->[0] } sort { $fold_cmp->() }
2393 0 map { my $r = $_; [$r, [map { eval { Attean::Plan::Extend->evaluate_expression( $model, $_, $r ) } } @exprs]] }
2394 @$rows;
2395  
2396 0 $rows = \@sorted;
2397 }
2398
2399 0 my %seen;
2400 0 ▶ if (scalar(@arg_exprs) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arg_exprs > 1) { } 0 0 0
2401 # map
2402 0 my @values;
2403 0 foreach my $r (@$rows) {
2404 0 ▶ my ($key, $value) = map { eval { Attean::Plan::Extend->evaluate_expression($model, $_, $r) } || undef } @arg_exprs;
Condition0%0/2
eval { do { 'Attean::Plan::Extend'->evaluate_expression($model, $_, $r) } } || undef
l!l
00
MC/DC0%0/1
eval { do { 'Attean::Plan::Extend'->evaluate_expression($model, $_, $r) } } || undef
eval { do { 'Attean::Plan::Extend'->evaluate_expression($model, $_, $r) } }
Truth table0%0/2
A
A= eval { do { 'Attean::Plan::Extend'->evaluate_expression($model, $_, $r) } }
Aresult
11
00
2405 0 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
2406 0 push(@values, $key, $value);
2407 }
2408 }
2409 0 my $func = Attean->get_global_functional_form($AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI);
2410 0 $func->(undef, undef, @values);
2411 } else {
2412 0 my @values;
2413 0 foreach my $r (@$rows) {
2414 0 my $term = eval { Attean::Plan::Extend->evaluate_expression($model, $e, $r) };
2415 0 ▶ if ($expr->distinct and blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->distinct and builtin::blessed($term)) 0 0 0
Condition0%0/3
$expr->distinct and builtin::blessed($term)
!ll&&!rl&&r
000
MC/DC0%0/2
$expr->distinct and builtin::blessed($term)
$expr->distinct builtin::blessed($term)
Truth table0%0/3
A and B
A= $expr->distinctB= builtin::blessed($term)
ABresult
0X0
100
111
2416 0 ▶ next if ($seen{ $term->as_string }++);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$term->as_string}++ 0 0 0
2417 }
2418 0 push(@values, $term);
2419 }
2420 0 my $func = Attean->get_global_functional_form($AtteanX::Functions::CompositeLists::LIST_TYPE_IRI);
2421 0 $func->(undef, undef, @values);
2422 }
2423 };
2424 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
2425 0 return $l;
2426 } elsif ($op eq 'CUSTOM') {
2427 0 my $iri = $expr->custom_iri;
2428 0 my $data = Attean->get_global_aggregate($iri);
2429 0 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
2430 0 die "No extension aggregate registered for <$iri>";
2431 }
2432 0 my $start = $data->{'start'};
2433 0 my $process = $data->{'process'};
2434 0 my $finalize = $data->{'finalize'};
2435  
2436 0 my $thunk = $start->($model, $self->active_graphs);
2437 0 foreach my $r (@$rows) {
2438 0 my $t = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2439 0 $process->($thunk, $t);
2440 }
2441 0 return $finalize->($thunk);
2442 } else {
2443 0 warn "Unexpected aggregate expression: $op";
2444 }
2445 0 die "$op not implemented";
2446 }
2447
2448 0 ▶ sub impl {
Subroutine impl: not called CC=3 SCAR=24.8
Pod undocumented
2449 0 my $self = shift;
2450 0 my $model = shift;
2451 0 my %aggs = %{ $self->aggregates };
2452 0 my @groups = @{ $self->groups };
2453 0 my $iter_variables = $self->in_scope_variables;
2454  
2455 0 ▶ my $group_template_generator = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
2456 0 my $r = shift;
2457 0 my %components;
2458 0 foreach my $g (@groups) {
2459 0 ▶ if ($g->isa('Attean::ValueExpression')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->isa("Attean::ValueExpression")) 0 0 0
2460 0 my $value = $g->value;
2461 0 ▶ if ($value->isa('Attean::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa("Attean::Variable")) 0 0 0
2462 0 my $var = $value->value;
2463 0 my $value = eval { Attean::Plan::Extend->evaluate_expression($model, $g, $r) };
2464 0 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 0 0 0
2465 0 $components{$var} = $value;
2466 }
2467 }
2468 }
2469 }
2470 0 return %components;
2471 0 };
2472 0 ▶ my $group_key_generator = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2473 0 my $r = shift;
2474 0 my @components;
2475 0 foreach my $g (@groups) {
2476 0 my $value = eval { Attean::Plan::Extend->evaluate_expression($model, $g, $r) };
2477 0 ▶ my $key = blessed($value) ? $value->as_string : '';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) ? : 0 0 0
2478 0 push(@components, $key);
2479 }
2480 0 my $group = join('|', @components);
2481 0 return $group;
2482 0 };
2483
2484 0 my $rank;
2485 0 ▶ while (my($var, $agg) = each(%aggs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %aggs) 0 0 0
2486 0 ▶ if ($agg->operator eq 'RANK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($agg->operator eq "RANK") 0 0 0
2487 0 $rank = $var;
2488 }
2489 }
2490
2491 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
2492 0 my %row_groups;
2493 my %group_templates;
2494 0 ▶ return sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
2495 0 my $iter = $impl->();
2496 0 ▶ while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
2497 0 my $group_key = $group_key_generator->($r);
2498 0 push(@{ $row_groups{ $group_key } }, $r);
2499 0 ▶ unless (exists $group_templates{ $group_key }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $group_templates{$group_key}) 0 0 0
2500 0 $group_templates{ $group_key } = { $group_template_generator->($r) };
2501 }
2502 }
2503 0 my @group_keys = keys %row_groups;
2504
2505 # SPARQL evaluation of aggregates over an empty input sequence should
2506 # result in an empty result <http://answers.semanticweb.com/questions/17410/semantics-of-sparql-aggregates>
2507
2508 0 my @results;
2509 0 ▶ if (scalar(@group_keys) == 0 and scalar(@groups) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @group_keys == 0 and scalar @groups == 0) 0 0 0
Condition0%0/3
scalar @group_keys == 0 and scalar @groups == 0
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @group_keys == 0 and scalar @groups == 0
scalar @group_keys == 0 scalar @groups == 0
Truth table0%0/3
A and B
A= scalar @group_keys == 0B= scalar @groups == 0
ABresult
0X0
100
111
2510 0 push(@group_keys, '');
2511 0 $row_groups{''} = [];
2512 0 $group_templates{''} = {};
2513 }
2514 0 foreach my $group (@group_keys) {
2515 0 my %row = %{ $group_templates{ $group } };
2516 0 my $rows = $row_groups{$group};
2517 0 ▶ if (defined $rank) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rank) { } 0 0 0
2518 0 my $agg = $aggs{$rank};
2519 0 ▶ my $ascending = $agg->scalar_vars->{ascending} // {};
Condition0%0/2
$agg->scalar_vars->{"ascending"} // {}
l!l
00
MC/DC0%0/1
$agg->scalar_vars->{"ascending"} // {}
$agg->scalar_vars->{"ascending"}
Truth table0%0/2
A
A= $agg->scalar_vars->{"ascending"}
Aresult
11
00
2520 0 my $vars = [map { $_->value->value } @{ $agg->children }];
2521 # TODO: support ordering by complex expressions in $vars, not just ValueExpressions with variables
2522 0 my @sorted = Attean::Plan::OrderBy->sort_rows($vars, $ascending, $rows);
2523 0 my $ord = 0;
2524 0 foreach my $row (@sorted) {
2525 0 my %b = %{ $row->bindings };
2526 0 $b{ $rank } = Attean::Literal->integer($ord++);
2527 0 my $r = Attean::Result->new( bindings => \%b );
2528 0 push(@results, $r);
2529 }
2530 } else {
2531 0 foreach my $var (keys %aggs) {
2532 0 my $expr = $aggs{$var};
2533 0 my $value = eval { $self->evaluate_aggregate($model, $expr, $rows) };
2534 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
2535 0 $row{$var} = $value;
2536 }
2537 }
2538 0 my $result = Attean::Result->new( bindings => \%row );
2539 0 push(@results, $result);
2540 }
2541 }
2542 0 return Attean::ListIterator->new(
2543 values => \@results,
2544 variables => $iter_variables,
2545 item_type => 'Attean::API::Result'
2546 );
2547 0 };
2548 }
2549 }
2550  
2551 50 package Attean::Plan::Sequence 0.040 {
2552 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2553 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2554 50 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2555 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2556
2557 50 with 'Attean::API::Plan', 'Attean::API::QueryTree';
2558 50 with 'Attean::API::UnionScopeVariablesPlan';
2559  
2560 0 ▶ sub plan_as_string { return 'Sequence'; }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2561
2562 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2563 0 my $self = shift;
2564 0 my $model = shift;
2565 0 my @children = map { $_->impl($model) } @{ $self->children };
2566 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2567 0 foreach my $child (@children) {
2568 0 my $iter = $child->();
2569 0 $iter->elements;
2570 }
2571 0 return Attean::ListIterator->new(values => [Attean::Literal->true], item_type => 'Attean::API::Term');
2572 0 };
2573 }
2574 }
2575  
2576 50 package Attean::Plan::Clear 0.040 {
2577 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2578 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2579 50 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2580 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2581
2582 50 with 'Attean::API::Plan', 'Attean::API::NullaryQueryTree';
2583 50 with 'Attean::API::UnionScopeVariablesPlan';
2584  
2585 50 has 'graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Term']]);
2586  
2587 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=2 SCAR=17.9
Pod undocumented
2588 0 my $self = shift;
2589 0 my $level = shift;
2590 0 my $indent = ' ' x (1+$level);
2591 0 my $s = sprintf("Clear { %d graphs }", scalar(@{ $self->graphs }));
2592 0 foreach my $g (@{ $self->graphs }) {
2593 0 my $name = $g->as_sparql;
2594 0 chomp($name);
2595 0 $s .= "\n-${indent} $name";
2596 }
2597 0 return $s;
2598 }
2599
2600 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2601 0 my $self = shift;
2602 0 my $model = shift;
2603 0 my $graphs = $self->graphs;
2604 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2605 0 foreach my $g (@$graphs) {
2606 0 $model->clear_graph($g);
2607 }
2608 0 return Attean::ListIterator->new(values => [Attean::Literal->true], item_type => 'Attean::API::Term');
2609 0 };
2610 }
2611 }
2612  
2613 50 package Attean::Plan::Drop 0.040 {
2614 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2615 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2616 50 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2617 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2618
2619 50 with 'Attean::API::Plan', 'Attean::API::NullaryQueryTree';
2620 50 with 'Attean::API::UnionScopeVariablesPlan';
2621  
2622 50 has 'graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Term']]);
2623  
2624 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=2 SCAR=17.9
Pod undocumented
2625 0 my $self = shift;
2626 0 my $level = shift;
2627 0 my $indent = ' ' x (1+$level);
2628 0 my $s = sprintf("Drop { %d graphs }", scalar(@{ $self->graphs }));
2629 0 foreach my $g (@{ $self->graphs }) {
2630 0 $s .= "\n-${indent} " . $g->as_sparql;
2631 }
2632 0 return $s;
2633 }
2634
2635 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2636 0 my $self = shift;
2637 0 my $model = shift;
2638 0 my $graphs = $self->graphs;
2639 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2640 0 foreach my $g (@$graphs) {
2641 0 $model->drop_graph($g);
2642 }
2643 0 return Attean::ListIterator->new(values => [Attean::Literal->true], item_type => 'Attean::API::Term');
2644 0 };
2645 }
2646 }
2647  
2648 50 package Attean::Plan::TripleTemplateToModelQuadMethod 0.040 {
2649 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2650 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2651 50 ▶ use Types::Standard qw(ConsumerOf Str ArrayRef HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2652 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2653
2654 50 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2655 50 with 'Attean::API::UnionScopeVariablesPlan';
2656  
2657 50 has 'order' => (is => 'ro', isa => ArrayRef[Str], required => 1);
2658 50 has 'patterns' => (is => 'ro', isa => HashRef[ArrayRef[ConsumerOf['Attean::API::TripleOrQuadPattern']]], required => 1);
2659 50 has 'graph' => (is => 'ro', isa => ConsumerOf['Attean::API::Term']);
2660  
2661 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=3 SCAR=24.8
Pod undocumented
2662 0 my $self = shift;
2663 0 my $level = shift;
2664 0 my $indent = ' ' x (1+$level);
2665 0 my $s = sprintf("Template-to-Model { Default graph: %s }", $self->graph->as_string);
2666 0 foreach my $method (@{ $self->order }) {
2667 0 my $pattern = $self->patterns->{ $method };
2668 0 $s .= "\n-${indent} Method: ${method}";
2669 0 foreach my $p (@$pattern) {
2670 0 $s .= "\n-${indent} " . $p->as_string;
2671 }
2672 }
2673 0 return $s;
2674 }
2675
2676 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2677 0 my $self = shift;
2678 0 my $model = shift;
2679 0 my $child = $self->children->[0]->impl($model);
2680
2681 0 my $graph = $self->graph;
2682 0 my @order = @{ $self->order };
2683 0 my $method = shift(@order);
2684 0 my $pattern = $self->patterns->{ $method };
2685  
2686 0 ▶ return sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
2687 0 my $iter = $child->();
2688 0 my @results;
2689 0 ▶ while (my $t = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 0 0 0
2690 0 my $bnode_mapper = Attean::TermMap->uuid_blank_map; # used to create fresh bnodes in the pattern for each input row
2691 0 ▶ if (scalar(@order)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @order) 0 0 0
2692 0 push(@results, $t);
2693 }
2694 0 foreach my $p (@$pattern) {
2695 0 my $q = $p->apply_map($bnode_mapper)->apply_bindings($t);
2696 0 ▶ my $quad = $q->does('Attean::API::QuadPattern') ? $q : $q->as_quad_pattern($graph);
Branch: 1 branch
BranchTrueFalseTotal
$q->does('Attean::API::QuadPattern') ? : 0 0 0
2697 0 ▶ if ($quad->is_ground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quad->is_ground) { } 0 0 0
2698 # warn "# $method: " . $quad->as_string . "\n";
2699 0 $model->$method($quad->as_quad);
2700 } else {
2701 # warn "not ground: " . $quad->as_string;
2702 }
2703 }
2704 }
2705 0 foreach my $method (@order) {
2706 0 my $pattern = $self->patterns->{ $method };
2707 0 foreach my $t (@results) {
2708 0 my $bnode_mapper = Attean::TermMap->uuid_blank_map; # used to create fresh bnodes in the pattern for each input row
2709 0 foreach my $p (@$pattern) {
2710 0 my $q = $p->apply_map($bnode_mapper)->apply_bindings($t);
2711 0 ▶ my $quad = $q->does('Attean::API::QuadPattern') ? $q : $q->as_quad_pattern($graph);
Branch: 1 branch
BranchTrueFalseTotal
$q->does('Attean::API::QuadPattern') ? : 0 0 0
2712 0 ▶ if ($quad->is_ground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quad->is_ground) { } 0 0 0
2713 # warn "# $method: " . $quad->as_string . "\n";
2714 0 $model->$method($quad->as_quad);
2715 } else {
2716 # warn "not ground: " . $quad->as_string;
2717 }
2718 }
2719 }
2720 }
2721 0 return Attean::ListIterator->new(values => [Attean::Literal->integer($model->size)], item_type => 'Attean::API::Term');
2722 0 };
2723 }
2724 }
2725  
2726 50 package Attean::Plan::Load 0.040 {
2727 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2728 50 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
2729 50 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
2730 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2731 50 ▶ use Types::Standard qw(Bool Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
2732 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2733
2734 50 with 'Attean::API::Plan', 'Attean::API::NullaryQueryTree';
2735 50 with 'Attean::API::UnionScopeVariablesPlan';
2736  
2737 50 has 'silent' => (is => 'ro', isa => Bool, default => 0);
2738 50 has 'url' => (is => 'ro', isa => Str);
2739  
2740 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2741 0 my $self = shift;
2742 0 return sprintf("Load { %s }", $self->url);
2743 }
2744
2745 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2746 0 my $self = shift;
2747 0 my $url = $self->url;
2748 0 my $ua = LWP::UserAgent->new();
2749 0 my $silent = $self->silent;
2750 0 my $accept = Attean->acceptable_parsers( handles => 'Attean::API::Triple' );
2751 0 $ua->default_headers->push_header( 'Accept' => $accept );
2752 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
2753 0 my $resp = $ua->get( $url );
2754 0 ▶ if ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) 0 0 0
2755 0 my $ct = $resp->header('Content-Type');
2756 0 ▶ if (my $pclass = Attean->get_parser( media_type => $ct )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pclass = "Attean"->get_parser("media_type", $ct)) 0 0 0
2757 0 my $p = $pclass->new();
2758 0 my $str = $resp->decoded_content;
2759 0 my $bytes = encode('UTF-8', $str, Encode::FB_CROAK);
2760 0 my $iter = $p->parse_iter_from_bytes( $bytes );
2761 0 return $iter;
2762 }
2763 }
2764
2765 0 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 0 0 0
2766 0 return Attean::ListIterator->new(values => [], item_type => 'Attean::API::Triple');
2767 } else {
2768 0 die "Failed to load url: " . $resp->status_line;
2769 }
2770 0 };
2771 }
2772 }
2773  
2774  
2775  
2776 =item * L<Attean::Plan::Unfold>
2777  
2778 =cut
2779  
2780 50 package Attean::Plan::Unfold 0.032 {
2781 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2782 50 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
2783 50 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
2784 50 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
2785 50 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
2786 50 ▶ use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
2787 50 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
2788 50 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
2789 50 ▶ use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
2790 50 ▶ use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
2791 50 ▶ use List::Util qw(uniq all);
Subroutine BEGIN: called CC=1 SCAR=0.0
2792 50 ▶ use Types::Standard qw(ConsumerOf ArrayRef InstanceOf HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2793 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2794  
2795 50 with 'MooX::Log::Any';
2796 50 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
2797
2798 50 has 'variables' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']], required => 1);
2799 50 has 'expression' => (is => 'ro', isa => ConsumerOf['Attean::API::Expression'], required => 1);
2800 50 has 'active_graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::IRI']], required => 1);
2801
2802 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2803 0 my $self = shift;
2804 0 my @vars = map { $_->as_string } @{ $self->variables };
2805 0 my $vars = '(' . join(', ', @vars) . ')';
2806 0 return sprintf('Unfold { %s ← %s }', $vars, $self->expression->as_string);
2807 }
2808 0 ▶ sub tree_attributes { return qw(variable expression) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2809
2810 0 ▶ sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
2811 0 my $class = shift;
2812 0 my %args = @_;
2813 0 my $exprs = $args{ expressions };
2814 0 my @vars = map { @{ $_->in_scope_variables } } @{ $args{ children } };
2815 0 my @evars = (@vars, keys %$exprs);
2816
2817 0 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 0 0
2818 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
2819 }
2820 0 $args{in_scope_variables} = [@evars];
2821 0 return $class->SUPER::BUILDARGS(%args);
2822 }
2823
2824 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=1 SCAR=6.9
Pod undocumented
2825 0 my $self = shift;
2826 0 my $model = shift;
2827 0 my $bind = shift;
2828 0 my $expr = $self->expression;
2829 0 my $vars = $self->variables;
2830 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
2831 # TODO: substitute variables in the expression
2832 0 return $self->_impl($model, $impl, $expr, @$vars);
2833 }
2834
2835 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2836 0 my $self = shift;
2837 0 my $model = shift;
2838 0 my $expr = $self->expression;
2839 0 my $vars = $self->variables;
2840 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
2841 0 return $self->_impl($model, $impl, $expr, @$vars);
2842 }
2843
2844 0 ▶ sub _impl {
Subroutine _impl: not called CC=2 SCAR=17.9
2845 0 my $self = shift;
2846 0 my $model = shift;
2847 0 my $impl = shift;
2848 0 ▶ Carp::confess unless (defined($impl));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $impl 0 0 0
2849 0 my $expr = shift;
2850 0 my @vars = @_;
2851 0 my $iter_variables = $self->in_scope_variables;
2852  
2853 0 my $var = $vars[0];
2854 0 my $index_var = $vars[1];
2855 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2856 0 my $iter = $impl->();
2857 0 my @buffer;
2858 return Attean::CodeIterator->new(
2859 item_type => 'Attean::API::Result',
2860 variables => $iter_variables,
2861 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=21 SCAR=61.4
2862 0 ▶ if (scalar(@buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @buffer) 0 0 0
2863 0 return shift(@buffer);
2864 }
2865 0 ▶ ROW: while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
2866 0 my %base = map { $_ => $r->value($_) } $r->variables;
2867 0 my $cdt = eval { Attean::Plan::Extend->evaluate_expression($model, $expr, $r) };
2868 0 ▶ warn "UNFOLD expression evaluation error: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
2869 0 ▶ unless ($cdt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cdt) 0 0 0
2870 0 return $r;
2871 }
2872
2873 0 ▶ if ($cdt->does('Attean::API::Literal') and $cdt->datatype->value eq $AtteanX::Functions::CompositeLists::LIST_TYPE_IRI) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cdt->does('Attean::API::Literal') and $cdt->datatype->value eq $AtteanX::Functions::CompositeLists::LIST_TYPE_IRI) { } 0 0 0
elsif ($cdt->does('Attean::API::Literal') and $cdt->datatype->value eq $AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI) { } 0 0 0
Condition0%0/3
$cdt->does("Attean::API::Literal") and $cdt->datatype->value eq $AtteanX::Functions::CompositeLists::LIST_TYPE_IRI
!ll&&!rl&&r
000
MC/DC0%0/2
$cdt->does("Attean::API::Literal") and $cdt->datatype->value eq $AtteanX::Functions::CompositeLists::LIST_TYPE_IRI
$cdt->does("Attean::API::Literal") $cdt->datatype->value eq $AtteanX::Functions::CompositeLists::LIST_TYPE_IRI
Truth table0%0/3
A and B
A= $cdt->does("Attean::API::Literal")B= $cdt->datatype->value eq $AtteanX::Functions::CompositeLists::LIST_TYPE_IRI
ABresult
0X0
100
111
2874 0 my @values = AtteanX::Functions::CompositeLists::lex_to_list($cdt);
2875 0 foreach my $index (0 .. $#values) {
2876 0 my %row = %base;
2877 0 my $term = $values[$index];
2878 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
2879 0 ▶ if ($row{ $var } and $term->as_string ne $row{ $var }->as_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row{$var} and $term->as_string ne $row{$var}->as_string) 0 0 0
Condition0%0/3
$row{$var} and $term->as_string ne $row{$var}->as_string
!ll&&!rl&&r
000
MC/DC0%0/2
$row{$var} and $term->as_string ne $row{$var}->as_string
$row{$var} $term->as_string ne $row{$var}->as_string
Truth table0%0/3
A and B
A= $row{$var}B= $term->as_string ne $row{$var}->as_string
ABresult
0X0
100
111
2880 0 next ROW;
2881 }
2882
2883 0 ▶ if ($term->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Binding")) 0 0 0
2884 # patterns need to be made ground to be bound as values (e.g. TriplePattern -> Triple)
2885 0 $term = $term->ground($r);
2886 }
2887
2888 0 $row{ $var->value } = $term;
2889 0 ▶ if (defined $index_var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index_var) 0 0 0
2890 0 $row{ $index_var->value } = Attean::Literal->integer(1+$index);
2891 }
2892 }
2893 0 push(@buffer, Attean::Result->new( bindings => \%row, eval_stash => $r->eval_stash ));
2894 }
2895 ▶ } elsif ($cdt->does('Attean::API::Literal') and $cdt->datatype->value eq $AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI) {
Condition0%0/3
$cdt->does("Attean::API::Literal") and $cdt->datatype->value eq $AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI
!ll&&!rl&&r
000
MC/DC0%0/2
$cdt->does("Attean::API::Literal") and $cdt->datatype->value eq $AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI
$cdt->does("Attean::API::Literal") $cdt->datatype->value eq $AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI
Truth table0%0/3
A and B
A= $cdt->does("Attean::API::Literal")B= $cdt->datatype->value eq $AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI
ABresult
0X0
100
111
2896 0 my @values = AtteanX::Functions::CompositeMaps::lex_to_maplist($cdt);
2897 0 ▶ while (my @pair = splice(@values, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pair) = splice(@values, 0, 2)) 0 0 0
2898 0 my ($index, $term) = @pair;
2899 0 my %row = %base;
2900 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
2901 0 ▶ if ($row{ $var } and $term->as_string ne $row{ $var }->as_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row{$var} and $term->as_string ne $row{$var}->as_string) 0 0 0
Condition0%0/3
$row{$var} and $term->as_string ne $row{$var}->as_string
!ll&&!rl&&r
000
MC/DC0%0/2
$row{$var} and $term->as_string ne $row{$var}->as_string
$row{$var} $term->as_string ne $row{$var}->as_string
Truth table0%0/3
A and B
A= $row{$var}B= $term->as_string ne $row{$var}->as_string
ABresult
0X0
100
111
2902 0 next ROW;
2903 }
2904
2905 0 ▶ if ($term->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Binding")) 0 0 0
2906 # patterns need to be made ground to be bound as values (e.g. TriplePattern -> Triple)
2907 0 $term = $term->ground($r);
2908 }
2909
2910 0 ▶ if (defined $index_var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index_var) { } 0 0 0
2911 # 2-var
2912 0 $row{ $index_var->value } = $term;
2913 0 $row{ $var->value } = $index;
2914 } else {
2915 # 1-var
2916 0 $row{ $var->value } = $term;
2917 }
2918 }
2919 0 push(@buffer, Attean::Result->new( bindings => \%row, eval_stash => $r->eval_stash ));
2920 }
2921 }
2922 0 return shift(@buffer);
2923 }
2924 0 return;
2925 }
2926 0 );
2927 0 };
2928 }
2929 }
2930 # Create(iri)
2931  
2932 50 1;
2933  
2934 __END__
↑ Summary Attean/SimpleQueryEvaluator.pm »