Attean/Plan.pm

filter: Statementstmt 32.3% 751 / 2321 Branchbran 4.5% 37 / 812 Conditioncond 3.9% 7 / 179 MC/DCmcdc 1.8% 2 / 108 Subroutinesub 59.3% 172 / 290 Podpod 0.0% 0 / 91 totaltotal 25.4% 969 / 3801 CC 525 SCAR 99.5
CC
525
Cov
24%
evaluate_expression
65725.8
evaluate_aggregate
1722.0
__ANON__
506.0
CRAP 20962.4
↑ Summary Attean/SimpleQueryEvaluator.pm »
1 51 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 51 ▶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.041
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 51 ▶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 51 package Attean::Plan::Quad 0.041 {
36 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 51 ▶ use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
38 51 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 51 ▶ use AtteanX::Functions::CompositeLists;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 51 ▶ use AtteanX::Functions::CompositeMaps;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 51 has 'subject' => (is => 'ro', required => 1);
44 51 has 'predicate' => (is => 'ro', required => 1);
45 51 has 'object' => (is => 'ro', required => 1);
46 51 has 'graph' => (is => 'ro', required => 1);
47
48 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::NullaryQueryTree';
49 51 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 51 };
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 51 ▶ 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 51 package Attean::Plan::NestedLoopJoin 0.041 {
145 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
146 51 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
147 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 51 with 'Attean::API::BindingSubstitutionPlan';
150 51 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 51 package Attean::Plan::HashJoin 0.041 {
234 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
235 51 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
236 51 ▶ 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 51 with 'Attean::API::BindingSubstitutionPlan';
246 51 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 51 package Attean::Plan::Construct 0.041 {
375 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
376 51 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
377 51 ▶ use Types::Standard qw(Str ArrayRef ConsumerOf InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
378 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
379 51 has 'triples' => (is => 'ro', 'isa' => ArrayRef[ConsumerOf['Attean::API::TripleOrQuadPattern']], required => 1);
380  
381 51 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 51 package Attean::Plan::Describe 0.041 {
486 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
487 51 ▶ use Attean::RDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
488 51 ▶ use List::Util qw(all);
Subroutine BEGIN: called CC=1 SCAR=0.0
489 51 ▶ use Types::Standard qw(Str ArrayRef ConsumerOf InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
490 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
491  
492 51 has 'graph' => (is => 'ro');
493 51 has 'terms' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::TermOrVariable']]);
494  
495 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
496 51 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 51 package Attean::Plan::EBVFilter 0.041 {
572 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
573 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
574 51 ▶ use Types::Standard qw(Str ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
575 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
576
577 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
578 51 with 'Attean::API::UnionScopeVariablesPlan';
579  
580 51 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 51 package Attean::Plan::Merge 0.041 {
632 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
633 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
634 51 ▶ use Types::Standard qw(Str ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
635 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
636
637 51 with 'Attean::API::Plan', 'Attean::API::BinaryQueryTree';
638 51 with 'Attean::API::UnionScopeVariablesPlan';
639  
640 51 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 51 package Attean::Plan::Union 0.041 {
661 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
662 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
663 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
664
665 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::BinaryQueryTree';
666 51 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 51 package Attean::Plan::Extend 0.041 {
732 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
733 51 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
734 51 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
735 51 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
736 51 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
737 51 ▶ use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
738 51 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
739 51 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
740 51 ▶ use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
741 51 ▶ use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
742 51 ▶ use List::Util qw(uniq all);
Subroutine BEGIN: called CC=1 SCAR=0.0
743 51 ▶ use Types::Standard qw(ConsumerOf ArrayRef InstanceOf HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
744 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
745  
746 51 with 'MooX::Log::Any';
747 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
748 51 has 'expressions' => (is => 'ro', isa => HashRef[ConsumerOf['Attean::API::Expression']], required => 1);
749 51 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 # around 'evaluate_expression' => sub {
759 # my $orig = shift;
760 # my $self = shift;
761 # my $model = shift;
762 # my $expr = shift;
763 # my $r = shift;
764 #
765 # my $debug = 1;
766 # my $value = $orig->($self, $model, $expr, $r, @_);
767 # if ($debug) {
768 # print STDERR sprintf("[TRACE] %-60s <- %s\n", $value->as_string, $expr->as_string);
769 # }
770 # return $value;
771 # };
772
773 10 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.0
Pod undocumented
774 10 my $class = shift;
775 10 my %args = @_;
776 10 my $exprs = $args{ expressions };
777 10 my @vars = map { @{ $_->in_scope_variables } } @{ $args{ children } };
778 10 my @evars = (@vars, keys %$exprs);
779
780 10 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 10 10
781 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
782 }
783 10 $args{in_scope_variables} = [@evars];
784 10 return $class->SUPER::BUILDARGS(%args);
785 }
786
787 0 ▶ sub evaluate_expression {
Subroutine evaluate_expression: not called CC=258 SCAR=110.9
Pod undocumented
788 0 my $self = shift;
789 0 my $model = shift;
790 0 my $expr = shift;
791 0 my $r = shift;
792 0 ▶ Carp::confess unless ($expr->can('operator'));
Branch: 1 branch
BranchTrueFalseTotal
unless $expr->can("operator") 0 0 0
793 0 my $op = $expr->operator;
794  
795 0 state $true = Attean::Literal->true;
796 0 state $false = Attean::Literal->false;
797 0 state $type_roles = { qw(URI IRI IRI IRI BLANK Blank LITERAL Literal NUMERIC NumericLiteral TRIPLE Triple) };
798 0 state $type_classes = { qw(URI Attean::IRI IRI Attean::IRI STR Attean::Literal) };
799
800 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
801 0 my $datatype = $expr->datatype->value;
802 0 my ($child) = @{ $expr->children };
803 0 my $term = $self->evaluate_expression($model, $child, $r);
804  
805 # TODO: the code below duplicates code in Attean::SimpleQueryEvaluator. It should be refactored.
806 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
807 0 my $value = $term->value;
808 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
809 0 return Attean::Literal->new(value => $term->value);
810 } elsif ($term->datatype->value eq 'http://www.w3.org/2001/XMLSchema#boolean') {
811 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
812 0 return Attean::Literal->new(value => $v);
813 } elsif ($term->does('Attean::API::NumericLiteral')) {
814 0 my $v = $term->numeric_value();
815 0 ▶ if ($v == int($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v == int $v) 0 0 0
816 0 return Attean::Literal->new(value => int($v));
817 }
818 }
819
820 0 return Attean::Literal->new(value => $value);
821 }
822  
823 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
824 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
825 0 my $value = $term->value;
826 0 my $num;
827 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
828 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
829 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
830 } elsif ($term->does('Attean::API::NumericLiteral')) {
831 0 my $v = $term->numeric_value();
832 0 $v =~ s/[.].*$//;
833 0 $value = int($v);
834 } elsif ($value =~ /^[-+]\d+$/) {
835 0 my ($v) = "$value";
836 0 $v =~ s/[.].*$//;
837 0 $value = int($v);
838 }
839 0 $num = $value;
840 } elsif ($datatype eq 'http://www.w3.org/2001/XMLSchema#decimal') {
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';
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' ? : 0 0 0
843 } elsif ($term->does('Attean::API::NumericLiteral')) {
844 0 $value = $term->numeric_value;
845 } elsif (looks_like_number($value)) {
846 0 ▶ if ($value =~ /[eE]/) { # double
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /[eE]/u) 0 0 0
847 0 die "cannot cast to xsd:decimal as precision would be lost";
848 }
849 0 $value = +$value;
850 }
851 0 $num = "$value";
852 0 $num =~ s/[.]0+$/.0/;
853 0 $num =~ s/[.](\d+)0*$/.$1/;
854 } elsif ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#(float|double)$>) {
855 0 my $typename = $1;
856 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
857 0 ▶ $value = ($value eq 'true') ? '1.0' : '0.0';
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'true' ? : 0 0 0
858 } elsif ($term->does('Attean::API::NumericLiteral')) {
859 # no-op
860 } elsif (looks_like_number($value)) {
861 0 $value = +$value;
862 } else {
863 0 die "cannot cast unrecognized value '$value' to xsd:$typename";
864 }
865 0 $num = sprintf("%e", $value);
866 }
867 0 my $c = Attean::Literal->new(value => $num, datatype => $expr->datatype);
868 0 ▶ if (my $term = $c->canonicalized_term_strict()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $term = $c->canonicalized_term_strict) { } 0 0 0
869 0 return $term;
870 } else {
871 0 die "Term value is not a valid lexical form for $datatype";
872 }
873 } elsif ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#boolean$>) {
874 0 ▶ if ($term->does('Attean::API::NumericLiteral')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does('Attean::API::NumericLiteral')) { } 0 0 0
875 0 my $value = $term->numeric_value;
876 0 ▶ return ($value == 0) ? Attean::Literal->false : Attean::Literal->true;
Branch: 1 branch
BranchTrueFalseTotal
$value == 0 ? : 0 0 0
877 } else {
878 0 my $value = $term->value;
879 0 ▶ if ($value =~ m/^(true|false|0|1)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^(true|false|0|1)$/u) { } 0 0 0
880 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
881 } else {
882 0 die "Bad lexical form for xsd:boolean: '$value'";
883 }
884 }
885 } elsif ($datatype =~ m<^http://www.w3.org/2001/XMLSchema#dateTime$>) {
886 0 my $value = $term->value;
887 0 my $c = Attean::Literal->new(value => $value, datatype => $expr->datatype);
888 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
889 0 return $c;
890 } else {
891 0 die "Bad lexical form for xsd:dateTime: '$value'";
892 }
893 }
894 0 $self->log->warn("Cast expression unimplemented for $datatype: " . Dumper($expr));
895 } elsif ($expr->isa('Attean::ValueExpression')) {
896 0 my $node = $expr->value;
897 0 ▶ if ($node->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->does('Attean::API::Variable')) { } 0 0 0
898 0 my $value = $r->value($node->value);
899 0 ▶ unless (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($value)) 0 0 0
900 0 die "Variable " . $node->as_string . " is unbound in expression " . $expr->as_string;
901 }
902 0 return $value;
903 } else {
904 0 return $node;
905 }
906 } elsif ($expr->isa('Attean::UnaryExpression')) {
907 0 my ($child) = @{ $expr->children };
908 0 my $term = $self->evaluate_expression($model, $child, $r);
909 0 ▶ if ($op eq '!') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op eq '!') { } 0 0 0
elsif ($op eq '-' or $op eq '+') { } 0 0 0
910 0 ▶ return ($term->ebv) ? $false : $true;
Branch: 1 branch
BranchTrueFalseTotal
$term->ebv ? : 0 0 0
911 ▶ } 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
912 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
913 0 my $v = $term->numeric_value;
914 0 return Attean::Literal->new( value => eval "$op$v", datatype => $term->datatype );
915 }
916 0 die "Unimplemented UnaryExpression evaluation: " . $expr->operator;
917 } elsif ($expr->isa('Attean::BinaryExpression')) {
918 0 my $op = $expr->operator;
919 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
920 0 foreach my $child (@{ $expr->children }) {
921 0 my $term = $self->evaluate_expression($model, $child, $r);
922 0 ▶ unless ($term->ebv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($term->ebv) 0 0 0
923 0 return $false;
924 }
925 }
926 0 return $true;
927 } elsif ($op eq '||') {
928 0 foreach my $child (@{ $expr->children }) {
929 0 my $term = $self->evaluate_expression($model, $child, $r);
930 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
931 0 return $true;
932 }
933 }
934 0 return $false;
935 } elsif ($op eq '=') {
936 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
937 0 my $eq = $lhs->equals($rhs);
938 0 ▶ return $eq ? $true : $false; # TODO: this may not be using value-space comparision for numerics...
Branch: 1 branch
BranchTrueFalseTotal
$eq ? : 0 0 0
939 } elsif ($op eq '!=') {
940 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
941 0 my $neq = not($lhs->equals($rhs));
942 0 ▶ return $neq ? $true : $false; # TODO: this may not be using value-space comparision for numerics...
Branch: 1 branch
BranchTrueFalseTotal
$neq ? : 0 0 0
943 } elsif ($op =~ m#[<>]=?#) {
944 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
945 0 my $cmp = $lhs->compare($rhs);
946 0 ▶ if ($cmp < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cmp < 0) { } 0 0 0
elsif ($cmp > 0) { } 0 0 0
947 0 ▶ return ($op =~ /^<=?/) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /^<=?/u ? : 0 0 0
948 } elsif ($cmp > 0) {
949 0 ▶ return ($op =~ /^>=?/) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /^>=?/u ? : 0 0 0
950 } else {
951 0 ▶ return ($op =~ /=/) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$op =~ /=/u ? : 0 0 0
952 }
953 } elsif ($op =~ m<^[-+*/]$>) {
954 0 my ($lhs, $rhs) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
955 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
956 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
957 0 my $type = $lhs->binary_promotion_type($rhs, $op);
958 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
959 0 return Attean::Literal->new(value => ($lv + $rv), datatype => $type);
960 } elsif ($op eq '-') {
961 0 return Attean::Literal->new(value => ($lv - $rv), datatype => $type);
962 } elsif ($op eq '*') {
963 0 return Attean::Literal->new(value => ($lv * $rv), datatype => $type);
964 } elsif ($op eq '/') {
965 0 return Attean::Literal->new(value => ($lv / $rv), datatype => $type);
966 }
967 }
968
969 0 $self->log->warn("Binary operator $op expression evaluation unimplemented: " . Dumper($expr));
970 0 die "Expression evaluation unimplemented: " . $expr->as_string;
971 } elsif ($expr->isa('Attean::FunctionExpression')) {
972 0 my $func = $expr->operator;
973 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
974 0 my ($check, @children) = @{ $expr->children };
975 0 my ($term) = $self->evaluate_expression($model, $check, $r);
976 0 ▶ $self->log->warn($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
977 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
978 0 my $value = $self->evaluate_expression($model, $expr, $r);
979 # warn '############# ' . $value->as_string;
980 0 return $value;
981 } elsif ($func eq 'COALESCE') {
982 # warn "COALESCE: . " . $r->as_string . "\n";
983 0 foreach my $child (@{ $expr->children }) {
984 # warn '- ' . $child->as_string . "\n";
985 0 my $term = eval { $self->evaluate_expression($model, $child, $r) };
986 # warn $@ if $@;
987 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
988 # warn ' returning ' . $term->as_string . "\n";
989 0 return $term;
990 }
991 }
992 # warn " no value\n";
993 0 return;
994 } elsif ($func eq 'BOUND') {
995 0 my ($child) = @{ $expr->children };
996 0 my ($term) = eval { $self->evaluate_expression($model, $child, $r) };
997 0 ▶ return blessed($term) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($term) ? : 0 0 0
998 }
999
1000 0 ▶ if ($func eq 'INVOKE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($func eq 'INVOKE') { } 0 0 0
1001 0 my @children = @{ $expr->children };
1002 0 my $furi = $self->evaluate_expression($model, shift(@children), $r)->value;
1003 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
1004 0 my @operands = map { $self->evaluate_expression($model, $_, $r) } @children;
1005 0 my $rr = eval { $func->($model, $self->active_graphs, @operands) };
1006 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1007 # warn "INVOKE error <$furi>: $@" if $@;
1008 0 return;
1009 }
1010 0 return $rr;
1011 } elsif (my $fform = Attean->get_global_functional_form($furi)) {
1012 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
1013 0 my $rr = eval { $fform->($model, $self->active_graphs, @operands) };
1014 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1015 # warn "INVOKE error <$furi>: $@" if $@;
1016 0 return $r;
1017 }
1018 0 return $rr;
1019 } else {
1020 0 die "No extension registered for <$furi>";
1021 }
1022 } else {
1023 0 my @terms = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1024 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
1025 0 my $role = "Attean::API::$type_roles->{$1}";
1026 0 my $t = shift(@terms);
1027 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
1028 0 ▶ return $ok ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
1029 } elsif ($func eq 'REGEX') {
1030 0 my ($string, $pattern, $flags) = @terms;
1031 # my ($string, $pattern, $flags) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1032 # TODO: ensure that $string is a literal
1033 0 ▶ unless ($string->does('Attean::API::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string->does("Attean::API::Literal")) 0 0 0
1034 0 die 'REGEX called with non-literal value';
1035 }
1036 0 ▶ unless ($pattern->does('Attean::API::Literal')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pattern->does("Attean::API::Literal")) 0 0 0
1037 0 die 'REGEX called with non-literal pattern';
1038 }
1039 0 ▶ ($string, $pattern, $flags) = map { blessed($_) ? $_->value : '' } ($string, $pattern, $flags);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 0 0 0
1040 0 my $re;
1041 0 my $flag_str = '';
1042 0 ▶ $flag_str .= 'i' if ($flags =~ /i/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /i/u 0 0 0
1043 0 ▶ $flag_str .= 's' if ($flags =~ /s/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /s/u 0 0 0
1044 0 ▶ $flag_str .= 'm' if ($flags =~ /m/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /m/u 0 0 0
1045 0 ▶ $flag_str .= 'x' if ($flags =~ /x/);
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /x/u 0 0 0
1046 0 ▶ if ($flags =~ /q/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags =~ /q/u) 0 0 0
1047 0 $pattern = quotemeta($pattern);
1048 }
1049 0 $re = qr/(?${flag_str}:$pattern)/;
1050 0 ▶ return ($string =~ $re) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /$re/u ? : 0 0 0
1051 } elsif ($func =~ /^(NOT)?IN$/) {
1052 0 ▶ my $ok = ($func eq 'IN') ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$func eq 'IN' ? : 0 0 0
1053 0 ▶ my $notok = ($func eq 'IN') ? $false : $true;
Branch: 1 branch
BranchTrueFalseTotal
$func eq 'IN' ? : 0 0 0
1054 # my @children = @{ $expr->children };
1055 0 my ($term, @children) = @terms;
1056 # my ($term) = $self->evaluate_expression($model, shift(@children), $r);
1057 # foreach my $child (@{ $expr->children }) {
1058 0 foreach my $value (@children) {
1059 # my $value = $self->evaluate_expression($model, $child, $r);
1060 0 ▶ if (eval { $term->equals($value) }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $term->equals($value) } }) 0 0 0
1061 0 return $ok;
1062 }
1063 }
1064 0 return $notok;
1065 } elsif ($func eq 'NOW') {
1066 0 my $dt = DateTime->now;
1067 0 my $value = DateTime::Format::W3CDTF->new->format_datetime( $dt );
1068 0 return Attean::Literal->new(value => $value, datatype => 'http://www.w3.org/2001/XMLSchema#dateTime');
1069 } elsif ($func eq 'STR') {
1070 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1071 0 return Attean::Literal->new(value => $term->value);
1072 } elsif ($func =~ /^[UI]RI$/) { # IRI URI
1073 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1074 0 return Attean::IRI->new(value => $term->value, base => $expr->base);
1075 } elsif ($func eq 'ABS') {
1076 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1077 0 my $value = abs($string->numeric_value);
1078 0 return Attean::Literal->new(value => $value, datatype => $string->datatype);
1079 } elsif ($func eq 'ROUND') {
1080 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1081 0 my $value = $string->numeric_value;
1082 0 my $mult = 1;
1083 0 ▶ if ($value < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value < 0) 0 0 0
1084 0 $mult = -1;
1085 0 $value = -$value;
1086 }
1087 0 my $round = $mult * POSIX::floor($value + 0.50000000000008);
1088 0 return Attean::Literal->new(value => $round, datatype => $string->datatype);
1089 } elsif ($func eq 'CEIL') {
1090 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1091 0 my $value = ceil($string->numeric_value);
1092 0 return Attean::Literal->new(value => $value, datatype => $string->datatype);
1093 } elsif ($func eq 'FLOOR') {
1094 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1095 0 my $value = floor($string->numeric_value);
1096 0 return Attean::Literal->new(value => $value, datatype => $string->datatype);
1097 } elsif ($func eq 'CONCAT') {
1098 0 my @strings = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1099 0 ▶ if (scalar(@strings) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @strings == 0) 0 0 0
1100 0 return Attean::Literal->new(value => '');
1101 }
1102 # die "CONCAT called with terms that are not argument compatible" unless ($strings[0]->argument_compatible(@strings));
1103 0 my %args;
1104 0 my $lang_key = $strings[0]->lang_dir();
1105 0 foreach my $s (@strings) {
1106 0 ▶ die unless ($s->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $s->does("Attean::API::Literal") 0 0 0
1107 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
1108 0 my $new_lang_key = $s->lang_dir();
1109 0 ▶ if ($new_lang_key ne $lang_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_lang_key ne $lang_key) 0 0 0
1110 0 $lang_key = '';
1111 }
1112 }
1113 0 ▶ if ($lang_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lang_key) 0 0 0
1114 0 my ($lang, $dir) = split('--', $lang_key);
1115 0 $args{language} = $lang;
1116 0 ▶ if ($dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir) 0 0 0
1117 0 $args{base_dir} = $dir;
1118 }
1119 }
1120 0 my $c = Attean::Literal->new(value => join('', map { $_->value } @strings), %args);
1121 0 return $c;
1122 } elsif ($func eq 'DATATYPE') {
1123 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1124 0 ▶ die unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1125 0 return $string->datatype;
1126 } elsif ($func eq 'LANG') {
1127 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1128 0 ▶ die unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1129 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
1130 0 return Attean::Literal->new(value => $value);
1131 } elsif ($func eq 'HASLANG') {
1132 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1133 0 ▶ return $false unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1134 0 ▶ return $string->has_language ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$string->has_language ? : 0 0 0
1135 } elsif ($func eq 'HASLANGDIR') {
1136 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1137 0 ▶ return $false unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1138 0 ▶ return $string->has_base_dir ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$string->has_base_dir ? : 0 0 0
1139 } elsif ($func eq 'LANGDIR') {
1140 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1141 0 ▶ die unless ($string->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $string->does("Attean::API::Literal") 0 0 0
1142 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
1143 0 return Attean::Literal->new(value => $dir);
1144 } elsif ($func eq 'LANGMATCHES') {
1145 0 my ($term, $pat) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1146 0 my $lang = $term->value;
1147 0 my $match = $pat->value;
1148 0 ▶ if ($match eq '*') {
Branch: 1 branch
BranchTrueFalseTotal
if ($match eq '*') { } 0 0 0
1149 # """A language-range of "*" matches any non-empty language-tag string."""
1150 0 ▶ return $lang ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$lang ? : 0 0 0
1151 } else {
1152 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
1153 }
1154
1155 } elsif ($func eq 'ENCODE_FOR_URI') {
1156 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1157 0 return Attean::Literal->new(value => uri_escape_utf8($string->value));
1158 } elsif ($func =~ /^[LU]CASE$/) {
1159 0 my $term = shift(@terms);
1160 0 ▶ my $value = ($func eq 'LCASE') ? lc($term->value) : uc($term->value);
Branch: 1 branch
BranchTrueFalseTotal
$func eq 'LCASE' ? : 0 0 0
1161 0 return Attean::Literal->new(value => $value, $term->construct_args);
1162 } elsif ($func eq 'STRLANG') {
1163 0 my ($term, $lang) = @terms;
1164 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1165 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
1166 0 ▶ die if ($term->language);
Branch: 1 branch
BranchTrueFalseTotal
if $term->language 0 0 0
1167 0 ▶ die unless ($lang->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $lang->value 0 0 0
1168 0 return Attean::Literal->new(value => $term->value, language => $lang->value);
1169 } elsif ($func eq 'STRLANGDIR') {
1170 0 my ($term, $lang, $dir) = @terms;
1171 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1172 0 ▶ die unless ($lang->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $lang->does("Attean::API::Literal") 0 0 0
1173 0 ▶ die unless ($dir->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->does("Attean::API::Literal") 0 0 0
1174 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
1175 0 ▶ die unless ($lang->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $lang->value 0 0 0
1176 0 ▶ die unless ($dir->value =~ /^(ltr|rtl)$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->value =~ /^(ltr|rtl)$/u 0 0 0
1177 0 my $v = Attean::Literal->new(value => $term->value, language => $lang->value, base_dir => $dir->value);
1178 0 return $v;
1179 } elsif ($func eq 'STRDT') {
1180 0 my ($term, $dt) = @terms;
1181 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1182 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
1183 0 ▶ die if ($term->language);
Branch: 1 branch
BranchTrueFalseTotal
if $term->language 0 0 0
1184 # my ($term, $dt) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1185 0 return Attean::Literal->new(value => $term->value, datatype => $dt->value);
1186 } elsif ($func eq 'REPLACE') {
1187 0 my ($term, $pat, $rep, $flags_term) = @terms;
1188 0 ▶ die unless ($term->does('Attean::API::Literal'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::Literal") 0 0 0
1189 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
1190 # my ($term, $pat, $rep) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1191 0 my $value = $term->value;
1192 0 my $pattern = $pat->value;
1193 0 my $replace = $rep->value;
1194 0 ▶ my $flags = blessed($flags_term) ? $flags_term->value : '';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($flags_term) ? : 0 0 0
1195 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
1196 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
1197 0 ▶ die "REPLACE() called with unescaped \$ character at the start of replacement string without trailing digits" if ($replace =~ m<^\$(?!\d)>x); # starting with a '$' without following digits
Branch: 1 branch
BranchTrueFalseTotal
if $replace =~ /^\$(?!\d)/ux 0 0 0
1198 0 ▶ die "REPLACE() called with unescaped \$ character in replacement string without trailing digits" if ($replace =~ m<[^\\]\$(?!\d) >x); # a '$' not at the start, without a preceding escape backslash, and without following digits
Branch: 1 branch
BranchTrueFalseTotal
if $replace =~ /[^\\]\$(?!\d) /ux 0 0 0
1199  
1200 0 '' =~ /(?:$pattern)|/; # match against an empty string to determine how many capture groups there are
1201 0 my $capture_groups = $#+;
1202
1203 0 $replace =~ s/"/\\"/g;
1204 0 $replace = qq["$replace"];
1205 51 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
1206
1207 0 ▶ if ($flags eq 'i') {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags eq "i") 0 0 0
1208 0 $pattern = '(?i)' . $pattern;
1209 }
1210  
1211 # TODO: This only does one round of stripping off trailing digits to find a valid capture group,
1212 # but the F&O semantics say to continue doing that until it works.
1213 0 my $EMPTY = '';
1214 0 my @group_vars = ($replace =~ /(?<!\\)\$(\d+)/);
1215 0 foreach my $v (@group_vars) {
1216 0 ▶ if ($v == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v == 0) { } 0 0 0
elsif ($v > $capture_groups and length $v > 1) { } 0 0 0
elsif ($v > $capture_groups) { } 0 0 0
1217 # $0 as the whole-match variable
1218 0 $replace =~ s/(?<!\\)\$${v}/\$&/;
1219 ▶ } elsif ($v > $capture_groups and length($v) > 1) {
Condition0%0/3
$v > $capture_groups and length $v > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$v > $capture_groups and length $v > 1
$v > $capture_groups length $v > 1
Truth table0%0/3
A and B
A= $v > $capture_groupsB= length $v > 1
ABresult
0X0
100
111
1220 # If the var is too big to reference a capture group,
1221 # strip off the last digit as a literal and try again.
1222 # warn "variable \$${v} does not reference any of the $capture_groups capture groups\n";
1223 0 my ($head, $tail) = ($v =~ /^(.+)(.)$/);
1224
1225 # Insert the $EMPTY variable (to be interpolated during replacement) between
1226 # the length-1 prefix and the 1-digit suffix of the variable number, so it may
1227 # reference an actual capture group (e.g. replacing "a(b)c" with "$12" in "abc"
1228 # should yield "ab2c" since $12 is not a valid capture group var, but $1 is,
1229 # and so the trailing 2 becomes a literal '2' in the replacement string).
1230 0 $replace =~ s/(?<!\\)\$${head}${tail}/\$${head}\${EMPTY}${tail}/;
1231 } elsif ($v > $capture_groups) {
1232 # for single-digit vars that are larger than the number of capture groups, replace with the empty string
1233 0 $replace =~ s/(?<!\\)\$${v}//;
1234 }
1235 }
1236  
1237 0 $value =~ s/$pattern/"$replace"/eeg;
1238 # warn "==> " . Dumper($value);
1239 0 return Attean::Literal->new(value => $value, $term->construct_args);
1240 } elsif ($func eq 'SUBSTR') {
1241 0 my ($term, @args) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1242 0 my $value = $term->value;
1243 0 my @nums;
1244 0 foreach my $i (0 .. $#args) {
1245 0 my $arg = $args[ $i ];
1246 0 push(@nums, $arg->numeric_value);
1247 }
1248 0 my $start = $nums[0]-1; # adjust from 1-based API
1249 0 ▶ my @len = scalar(@nums > 1) ? ($nums[1]) : ();
Branch: 1 branch
BranchTrueFalseTotal
scalar(@nums > 1) ? : 0 0 0
1250 0 ▶ if ($start < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 0 0 0
1251 0 ▶ if (scalar(@len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @len) 0 0 0
1252 0 my $diff = abs($start);
1253 0 $len[0] -= $diff;
1254 }
1255 0 $start = 0;
1256
1257 }
1258 0 ▶ if (scalar(@len) and $len[0] < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @len and $len[0] < 0) 0 0 0
Condition0%0/3
scalar @len and $len[0] < 0
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @len and $len[0] < 0
scalar @len $len[0] < 0
Truth table0%0/3
A and B
A= scalar @lenB= $len[0] < 0
ABresult
0X0
100
111
1259 # negative length, just produce the empty string
1260 0 $len[0] = 0;
1261 }
1262 0 my $lex = $term->value;
1263 51 ▶ no warnings 'substr';
Subroutine BEGIN: called CC=1 SCAR=0.0
1264 0 ▶ my $v = scalar(@len) ? substr($lex, $start, $len[0]) : substr($lex, $start);
Branch: 1 branch
BranchTrueFalseTotal
scalar @len ? : 0 0 0
1265 0 ▶ return Attean::Literal->new(value => ($v // ''), $term->construct_args);
Condition0%0/2
$v // ""
l!l
00
MC/DC0%0/1
$v // ""
$v
Truth table0%0/2
A
A= $v
Aresult
11
00
1266 } elsif ($func eq 'CONTAINS') {
1267 0 my ($term, $pattern) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1268 0 my $t_key = $term->lang_dir();
1269 0 my $p_key = $pattern->lang_dir();
1270 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
1271 0 die "CONTAINS called with literals of different languages and/or base direction";
1272 }
1273 0 my ($string, $pat) = map { $_->value } ($term, $pattern);
1274 0 my $pos = index($string, $pat);
1275 0 ▶ return ($pos >= 0) ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$pos >= 0 ? : 0 0 0
1276 } elsif ($func eq 'STRSTARTS') {
1277 0 my (@terms) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1278 0 ▶ unless ($terms[0]->argument_compatible($terms[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terms[0]->argument_compatible($terms[1])) 0 0 0
1279 0 die "TypeError: ${func} operands are not argument compatible";
1280 }
1281 0 my ($string, $pat) = map { $_->value } @terms;
1282 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
1283 } elsif ($func eq 'STRENDS') {
1284 0 my (@terms) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1285 0 ▶ unless ($terms[0]->argument_compatible($terms[1])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($terms[0]->argument_compatible($terms[1])) 0 0 0
1286 0 die "TypeError: ${func} operands are not argument compatible";
1287 }
1288 0 my ($string, $pat) = map { $_->value } @terms;
1289 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
1290 } elsif ($func eq 'STRAFTER') {
1291 0 my ($term, $pat) = @terms;
1292 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
1293 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
1294 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
1295 # TODO: check that the terms are argument compatible
1296 0 my $value = $term->value;
1297 0 my $match = $pat->value;
1298 0 my $i = index($value, $match, 0);
1299 0 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) { } 0 0 0
1300 0 return Attean::Literal->new(value => '');
1301 } else {
1302 0 return Attean::Literal->new(value => substr($value, $i+length($match)), $term->construct_args);
1303 }
1304 } elsif ($func eq 'STRBEFORE') {
1305 0 my ($term, $pat) = @terms;
1306 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
1307 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
1308 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
1309 # TODO: check that the terms are argument compatible
1310 0 my $value = $term->value;
1311 0 my $match = $pat->value;
1312 0 my $i = index($value, $match, 0);
1313 0 ▶ if ($i < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 0) { } 0 0 0
1314 0 return Attean::Literal->new(value => '');
1315 } else {
1316 0 return Attean::Literal->new(value => substr($value, 0, $i), $term->construct_args);
1317 }
1318 } elsif ($func eq 'STRLEN') {
1319 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1320 0 return Attean::Literal->new(value => length($string->value), datatype => 'http://www.w3.org/2001/XMLSchema#integer');
1321 } elsif ($func eq 'MD5') {
1322 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1323 0 my $bytes = encode('UTF-8', $string->value, Encode::FB_CROAK);
1324 0 return Attean::Literal->new(value => md5_hex($bytes));
1325 } elsif ($func =~ /^SHA(\d+)$/) {
1326 0 my $sha = Digest::SHA->new($1);
1327 0 my ($string) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1328 0 my $bytes = encode('UTF-8', $string->value, Encode::FB_CROAK);
1329 0 $sha->add($bytes);
1330 0 return Attean::Literal->new(value => $sha->hexdigest);
1331 } elsif ($func eq 'RAND') {
1332 0 return Attean::Literal->new(value => rand(), datatype => 'http://www.w3.org/2001/XMLSchema#double');
1333 } elsif ($func =~ /^(YEAR|MONTH|DAY|HOUR|MINUTE)S?$/) {
1334 0 my $method = lc($1);
1335 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1336 0 my $dt = $term->datetime;
1337 0 return Attean::Literal->new(value => $dt->$method(), datatype => 'http://www.w3.org/2001/XMLSchema#integer');
1338 } elsif ($func eq 'SECONDS') {
1339 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1340 0 my $dt = $term->datetime;
1341 0 return Attean::Literal->new(value => $dt->second, datatype => 'http://www.w3.org/2001/XMLSchema#decimal');
1342 } elsif ($func eq 'TIMEZONE') {
1343 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1344 0 my $dt = $term->datetime;
1345 0 my $tz = $dt->time_zone;
1346 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
1347 0 my $offset = $tz->offset_for_datetime( $dt );
1348 0 my $minus = '';
1349 0 ▶ if ($offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 0 0 0
1350 0 $minus = '-';
1351 0 $offset = -$offset;
1352 }
1353  
1354 0 my $duration = "${minus}PT";
1355 0 ▶ if ($offset >= 60*60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 3600) 0 0 0
1356 0 my $h = int($offset / (60*60));
1357 0 ▶ $duration .= "${h}H" if ($h > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $h > 0 0 0 0
1358 0 $offset = $offset % (60*60);
1359 }
1360 0 ▶ if ($offset >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 60) 0 0 0
1361 0 my $m = int($offset / 60);
1362 0 ▶ $duration .= "${m}M" if ($m > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $m > 0 0 0 0
1363 0 $offset = $offset % 60;
1364 }
1365 0 my $s = int($offset);
1366 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
1367
1368 0 return Attean::Literal->new(value => $duration, datatype => 'http://www.w3.org/2001/XMLSchema#dayTimeDuration');
1369 } elsif ($func eq 'TZ') {
1370 0 my ($term) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1371 0 my $dt = $term->datetime;
1372 0 my $tz = $dt->time_zone;
1373 0 ▶ return Attean::Literal->new(value =>'') if ($tz->is_floating);
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_floating 0 0 0
1374 0 ▶ return Attean::Literal->new('Z') if ($tz->is_utc);
Branch: 1 branch
BranchTrueFalseTotal
if $tz->is_utc 0 0 0
1375 0 my $offset = $tz->offset_for_datetime( $dt );
1376 0 my $hours = 0;
1377 0 my $minutes = 0;
1378 0 my $minus = '+';
1379 0 ▶ if ($offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) 0 0 0
1380 0 $minus = '-';
1381 0 $offset = -$offset;
1382 }
1383  
1384 0 ▶ if ($offset >= 60*60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 3600) 0 0 0
1385 0 $hours = int($offset / (60*60));
1386 0 $offset = $offset % (60*60);
1387 }
1388 0 ▶ if ($offset >= 60) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset >= 60) 0 0 0
1389 0 $minutes = int($offset / 60);
1390 0 $offset = $offset % 60;
1391 }
1392 0 my $seconds = int($offset);
1393 0 return Attean::Literal->new(value => sprintf('%s%02d:%02d', $minus, $hours, $minutes));
1394 } elsif ($func eq 'UUID') {
1395 0 my $uuid = 'urn:uuid:' . uc(uuid_to_string(create_uuid()));
1396 0 return Attean::IRI->new(value => $uuid);
1397 } elsif ($func eq 'STRUUID') {
1398 0 return Attean::Literal->new(value => uc(uuid_to_string(create_uuid())));
1399 } elsif ($func eq 'BNODE') {
1400 0 ▶ if (scalar(@{ $expr->children })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$expr->children;}) { } 0 0 0
1401 0 my $string = $self->evaluate_expression($model, $expr->children->[0], $r);
1402 0 my $value = $string->value;
1403 my $b = (exists $r->eval_stash->{'sparql:bnode'}{$value})
1404 0 ▶ ? $r->eval_stash->{'sparql:bnode'}{$value}
Branch: 1 branch
BranchTrueFalseTotal
exists $r->eval_stash->{'sparql:bnode'}{$value} ? : 0 0 0
1405 : Attean::Blank->new();
1406 0 $r->eval_stash->{'sparql:bnode'}{$value} = $b;
1407 0 return $b;
1408 } else {
1409 0 return Attean::Blank->new();
1410 }
1411 } elsif ($func eq 'SAMETERM') {
1412 0 my @operands = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1413 0 my ($a, $b) = @operands;
1414 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
1415 0 ▶ if ($a->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a->does('Attean::API::Binding')) { } 0 0 0
1416 0 my $ok = ($a->sameTerms($b));
1417 0 ▶ return $ok ? $true : $false;
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 0 0 0
1418 } else {
1419 0 ▶ return $false unless ($a->value eq $b->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $a->value eq $b->value 0 0 0
1420 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
1421 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
1422 0 my $ad = $a->datatype;
1423 0 my $bd = $b->datatype;
1424 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
1425 0 my $bd = $b->datatype;
1426 0 ▶ return $false unless ($ad->value eq $bd->value);
Branch: 1 branch
BranchTrueFalseTotal
unless $ad->value eq $bd->value 0 0 0
1427 ▶ } 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
1428 0 return $false;
1429 }
1430 }
1431 0 return $true;
1432 }
1433 } elsif ($func =~ /^(SUBJECT|PREDICATE|OBJECT)$/) {
1434 0 my @operands = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1435 0 my $pos = lc($func);
1436 0 my $term = $operands[0]->$pos();
1437 0 return $term;
1438 } elsif ($func eq 'TRIPLE') {
1439 0 my ($s, $p, $o) = map { $self->evaluate_expression($model, $_, $r) } @{ $expr->children };
1440 0 ▶ unless ($s->does('Attean::API::BlankOrIRI')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s->does("Attean::API::BlankOrIRI")) 0 0 0
1441 0 die 'TRIPLE() constructor failed with bad subject type';
1442 }
1443 0 ▶ unless ($p->does('Attean::API::IRI')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->does("Attean::API::IRI")) 0 0 0
1444 0 die 'TRIPLE() constructor failed with bad predicate type';
1445 }
1446 0 ▶ unless ($o->does('Attean::API::TermOrTriple')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($o->does("Attean::API::TermOrTriple")) 0 0 0
1447 0 die 'TRIPLE() constructor failed with bad object type';
1448 }
1449 0 return Attean::Triple->new($s, $p, $o);
1450 } else {
1451 0 warn "Expression evaluation unimplemented: " . $expr->as_string;
1452 0 $self->log->warn("Expression evaluation unimplemented: " . $expr->as_string);
1453 0 die "Expression evaluation unimplemented: " . $expr->as_string;
1454 }
1455 }
1456 } elsif ($expr->isa('Attean::ExistsPlanExpression')) {
1457 0 my $plan = $expr->plan;
1458 0 my $impl = $plan->substitute_impl($model, $r);
1459 0 my $iter = $impl->();
1460 0 my $found = 0;
1461 0 ▶ if (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 0 0 0
1462 # warn "EXISTS found row: " . $row->as_string;
1463 0 $found++;
1464 }
1465
1466 0 ▶ return $found ? Attean::Literal->true : Attean::Literal->false;
Branch: 1 branch
BranchTrueFalseTotal
$found ? : 0 0 0
1467 } else {
1468 0 $self->log->warn("Expression evaluation unimplemented: " . $expr->as_string);
1469 0 die "Expression evaluation unimplemented: " . $expr->as_string;
1470 }
1471 }
1472
1473 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=1 SCAR=6.9
Pod undocumented
1474 0 my $self = shift;
1475 0 my $model = shift;
1476 0 my $bind = shift;
1477 0 my %exprs = %{ $self->expressions };
1478 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
1479 # TODO: substitute variables in the expression
1480 0 return $self->_impl($model, $impl, %exprs);
1481 }
1482
1483 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1484 0 my $self = shift;
1485 0 my $model = shift;
1486 0 my %exprs = %{ $self->expressions };
1487 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1488 0 return $self->_impl($model, $impl, %exprs);
1489 }
1490
1491 0 ▶ sub _impl {
Subroutine _impl: not called CC=1 SCAR=6.9
1492 0 my $self = shift;
1493 0 my $model = shift;
1494 0 my $impl = shift;
1495 0 my %exprs = @_;
1496 0 my $iter_variables = $self->in_scope_variables;
1497  
1498 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1499 0 my $iter = $impl->();
1500 return Attean::CodeIterator->new(
1501 item_type => 'Attean::API::Result',
1502 variables => $iter_variables,
1503 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
1504 0 ▶ ROW: while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
1505 # warn 'Extend Row -------------------------------> ' . $r->as_string . "\n";
1506 0 my %row = map { $_ => $r->value($_) } $r->variables;
1507 0 foreach my $var (keys %exprs) {
1508 0 my $expr = $exprs{$var};
1509 # warn "-> $var => " . $expr->as_string . "\n";
1510 0 my $term = eval { $self->evaluate_expression($model, $expr, $r) };
1511 # if ($@) {
1512 # warn "EXTEND expression evaluation error: $@";
1513 # warn '- expression: ' . $expr->as_string;
1514 # }
1515 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
1516 # warn "===> " . $term->as_string . "\n";
1517 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
1518 0 next ROW;
1519 }
1520
1521 0 ▶ if ($term->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Binding")) 0 0 0
1522 # patterns need to be made ground to be bound as values (e.g. TriplePattern -> Triple)
1523 0 $term = eval{ $term->ground($r) };
1524 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1525 # validation failed ; skip this variable
1526 0 next;
1527 }
1528 }
1529
1530 0 $row{ $var } = $term;
1531 }
1532 }
1533 0 return Attean::Result->new( bindings => \%row, eval_stash => $r->eval_stash );
1534 }
1535 0 return;
1536 }
1537 0 );
1538 0 };
1539 }
1540 }
1541  
1542 =item * L<Attean::Plan::HashDistinct>
1543  
1544 Evaluates a sub-plan, and returns distinct results by checking a persistent
1545 hash of already-seen results.
1546  
1547 =cut
1548  
1549 51 package Attean::Plan::HashDistinct 0.041 {
1550 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1551 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1552
1553 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1554 51 with 'Attean::API::UnionScopeVariablesPlan';
1555  
1556 0 ▶ sub plan_as_string { return 'HashDistinct' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1557
1558 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1559 0 my $self = shift;
1560 0 my $model = shift;
1561 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1562 0 my %seen;
1563 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1564 0 my $iter = $impl->();
1565 0 ▶ return $iter->grep(sub { return not($seen{ shift->as_string }++); });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1566 0 };
1567 }
1568 }
1569  
1570 =item * L<Attean::Plan::Unique>
1571  
1572 Evaluates an already-ordered sub-plan, and returns distinct results by
1573 filtering out sequential duplicates.
1574  
1575 =cut
1576  
1577 51 package Attean::Plan::Unique 0.041 {
1578 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1579 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1580
1581 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1582 51 with 'Attean::API::UnionScopeVariablesPlan';
1583  
1584 0 ▶ sub plan_as_string { return 'Unique' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1585
1586 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1587 0 my $self = shift;
1588 0 my $model = shift;
1589 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1590 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1591 0 my $iter = $impl->();
1592 0 my $last = '';
1593 0 ▶ return $iter->grep(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1594 0 my $r = shift;
1595 0 my $s = $r->as_string;
1596 0 my $ok = $s ne $last;
1597 0 $last = $s;
1598 0 return $ok;
1599 0 });
1600 0 };
1601 }
1602 }
1603  
1604 =item * L<Attean::Plan::Slice>
1605  
1606 Evaluates a sub-plan, and returns the results after optionally skipping some
1607 number of results ("offset") and limiting the total number of returned results
1608 ("limit").
1609  
1610 =cut
1611  
1612 51 package Attean::Plan::Slice 0.041 {
1613 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1614 51 ▶ use Types::Standard qw(Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
1615 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1616
1617 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1618 51 with 'Attean::API::UnionScopeVariablesPlan';
1619  
1620 51 has 'limit' => (is => 'ro', isa => Int, default => -1);
1621 51 has 'offset' => (is => 'ro', isa => Int, default => 0);
1622  
1623 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=3 SCAR=24.8
Pod undocumented
1624 0 my $self = shift;
1625 0 my @str;
1626 0 ▶ push(@str, "Limit=" . $self->limit) if ($self->limit >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->limit >= 0 0 0 0
1627 0 ▶ push(@str, "Offset=" . $self->offset) if ($self->offset > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $self->offset > 0 0 0 0
1628 0 return sprintf('Slice { %s }', join(' ', @str));
1629 }
1630
1631 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1632 0 my $self = shift;
1633 0 my $model = shift;
1634 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1635 0 my $offset = $self->offset;
1636 0 my $limit = $self->limit;
1637 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1638 0 my $iter = $impl->();
1639 0 ▶ $iter = $iter->offset($offset) if ($offset > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $offset > 0 0 0 0
1640 0 ▶ $iter = $iter->limit($limit) if ($limit >= 0);
Branch: 1 branch
BranchTrueFalseTotal
if $limit >= 0 0 0 0
1641 0 return $iter;
1642 0 };
1643 }
1644 }
1645  
1646 =item * L<Attean::Plan::Project>
1647  
1648 Evaluates a sub-plan and returns projected results by only keeping a fixed-set
1649 of variable bindings in each result.
1650  
1651 =cut
1652  
1653 51 package Attean::Plan::Project 0.041 {
1654 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1655 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
1656 51 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1657 51 has 'variables' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']], required => 1);
1658  
1659 10 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.1
Pod undocumented
1660 10 my $class = shift;
1661 10 my %args = @_;
1662 10 my @vars = map { $_->value } @{ $args{variables} };
1663
1664 10 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 10 10
1665 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
1666 }
1667 10 $args{in_scope_variables} = \@vars;
1668  
1669 10 return $class->SUPER::BUILDARGS(%args);
1670 }
1671
1672 # sub BUILD {
1673 # my $self = shift;
1674 # my @vars = map { $_->value } @{ $self->variables };
1675 # unless (scalar(@vars)) {
1676 # Carp::confess "No vars in project?";
1677 # }
1678 # }
1679
1680 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1681 0 my $self = shift;
1682 0 return sprintf('Project { %s }', join(' ', map { '?' . $_->value } @{ $self->variables }));
1683 }
1684 0 ▶ sub tree_attributes { return qw(variables) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1685
1686 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=1 SCAR=6.9
Pod undocumented
1687 0 my $self = shift;
1688 0 my $model = shift;
1689 0 my $bind = shift;
1690 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
1691 0 my @vars = map { $_->value } @{ $self->variables };
1692 0 my $iter_variables = $self->in_scope_variables;
1693  
1694 # TODO: substitute variables in the projection where appropriate
1695 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1696 0 my $iter = $impl->();
1697 0 ▶ return $iter->map(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1698 0 my $r = shift;
1699 0 ▶ my $b = { map { my $t = $r->value($_); $t ? ($_ => $t) : () } @vars };
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 0 0 0
1700 0 return Attean::Result->new( bindings => $b );
1701 0 }, $iter->item_type, variables => $iter_variables);
1702 0 };
1703 }
1704
1705 2 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
1706 2 my $self = shift;
1707 2 my $model = shift;
1708 2 my ($impl) = map { $_->impl($model) } @{ $self->children };
1709 2 my @vars = map { $_->value } @{ $self->variables };
1710 2 my $iter_variables = $self->in_scope_variables;
1711  
1712 2 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1713 2 my $iter = $impl->();
1714 2 ▶ return $iter->map(sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
1715 2 my $r = shift;
1716 2 ▶ my $b = { map { my $t = $r->value($_); $t ? ($_ => $t) : () } @vars };
Branch: 1 branch
BranchTrueFalseTotal
$t ? : 2 0 2
1717 2 return Attean::Result->new( bindings => $b );
1718 2 }, $iter->item_type, variables => $iter_variables);
1719 2 };
1720 }
1721 }
1722  
1723 =item * L<Attean::Plan::OrderBy>
1724  
1725 Evaluates a sub-plan and returns the results after fully materializing and
1726 sorting is applied.
1727  
1728 =cut
1729  
1730 51 package Attean::Plan::OrderBy 0.041 {
1731 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1732 51 ▶ use Types::Standard qw(HashRef ArrayRef InstanceOf Bool Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
1733 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
1734 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1735
1736 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1737 51 with 'Attean::API::UnionScopeVariablesPlan';
1738  
1739 51 has 'variables' => (is => 'ro', isa => ArrayRef[Str], required => 1);
1740 51 has 'ascending' => (is => 'ro', isa => HashRef[Bool], required => 1);
1741  
1742 1 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=2 SCAR=7.0
Pod undocumented
1743 1 my $self = shift;
1744 1 my @vars = @{ $self->variables };
1745 1 my $ascending = $self->ascending;
1746 1 ▶ my @strings = map { sprintf('%s(?%s)', ($ascending->{$_} ? 'ASC' : 'DESC'), $_) } @vars;
Branch: 1 branch
BranchTrueFalseTotal
$ascending->{$_} ? : 1 0 1
1747 1 return sprintf('Order { %s }', join(', ', @strings));
1748 }
1749
1750 0 ▶ sub sort_rows {
Subroutine sort_rows: not called CC=14 SCAR=53.5
Pod undocumented
1751 0 my $self = shift;
1752 0 my $vars = shift;
1753 0 my $ascending = shift;
1754 0 my $rows = shift;
1755 0 local($Attean::API::Binding::ALLOW_IRI_COMPARISON) = 1;
1756 0 my @sorted = map { $_->[0] } sort {
1757 0 my ($ar, $avalues) = @$a;
1758 0 my ($br, $bvalues) = @$b;
1759 0 my $c = 0;
1760 0 foreach my $i (0 .. $#{ $vars }) {
1761 0 my $ascending = $ascending->{ $vars->[$i] };
1762 0 my ($av, $bv) = map { $_->[$i] } ($avalues, $bvalues);
1763  
1764 # Mirrors code in Attean::SimpleQueryEvaluator->evaluate
1765 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
1766 0 $c = 1;
1767 ▶ } 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
1768 0 $c = -1;
1769 } else {
1770 0 ▶ $c = eval { $av ? $av->compare($bv) : 1 };
Branch: 1 branch
BranchTrueFalseTotal
$av ? : 0 0 0
1771 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1772 0 $c = 1;
1773 }
1774 }
1775 0 ▶ $c *= -1 unless ($ascending);
Branch: 1 branch
BranchTrueFalseTotal
unless $ascending 0 0 0
1776 0 ▶ last unless ($c == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $c == 0 0 0 0
1777 }
1778 $c
1779 } map {
1780 0 my $r = $_;
1781 0 [$r, [map { $r->value($_) } @$vars]]
1782 } @$rows;
1783 0 return @sorted;
1784 }
1785
1786 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1787 0 my $self = shift;
1788 0 my $model = shift;
1789 0 my $vars = $self->variables;
1790 0 my $ascending = $self->ascending;
1791 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
1792 0 my $iter_variables = $self->in_scope_variables;
1793  
1794 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1795 0 my $iter = $impl->();
1796 0 my @rows = $iter->elements;
1797 0 my @sorted = $self->sort_rows($vars, $ascending, \@rows);
1798 0 return Attean::ListIterator->new(
1799 values => \@sorted,
1800 variables => $iter_variables,
1801 item_type => $iter->item_type
1802 );
1803 }
1804 0 }
1805 }
1806  
1807 =item * L<Attean::Plan::Service>
1808  
1809 Evaluates a SPARQL query against a remote endpoint.
1810  
1811 =cut
1812  
1813 51 package Attean::Plan::Service 0.041 {
1814 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1815 51 ▶ use Types::Standard qw(ConsumerOf Bool Str InstanceOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1816 51 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
1817 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
1818 51 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
1819 51 ▶ use Attean::SPARQLClient;
Subroutine BEGIN: called CC=1 SCAR=0.0
1820 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1821  
1822 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1823  
1824 51 has 'endpoint' => (is => 'ro', isa => ConsumerOf['Attean::API::TermOrVariable'], required => 1);
1825 51 has 'silent' => (is => 'ro', isa => Bool, default => 0);
1826 51 has 'sparql' => (is => 'ro', isa => Str, required => 1);
1827 51 has 'user_agent' => (is => 'rw', isa => InstanceOf['LWP::UserAgent']);
1828 51 has 'request_signer' => (is => 'rw');
1829 51 has 'client' => (is => 'rw', required => 0);
1830  
1831 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
1832 0 my $self = shift;
1833 0 my $sparql = $self->sparql;
1834 0 $sparql =~ s/\s+/ /g;
1835 0 return sprintf('Service <%s> %s', $self->endpoint->as_string, $sparql);
1836 }
1837
1838 0 ▶ sub tree_attributes { return qw(endpoint) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1839 0 ▶ sub impl {
Subroutine impl: not called CC=3 SCAR=24.8
Pod undocumented
1840 0 my $self = shift;
1841 0 my $model = shift;
1842  
1843 0 my $endpoint = $self->endpoint->value;
1844 0 my $sparql = $self->sparql;
1845 0 my $silent = $self->silent;
1846 0 my %args = (
1847 endpoint => $endpoint,
1848 silent => $silent,
1849 request_signer => $self->request_signer,
1850 );
1851 0 ▶ $args{user_agent} = $self->user_agent if ($self->user_agent);
Branch: 1 branch
BranchTrueFalseTotal
if $self->user_agent 0 0 0
1852 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
1853 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1854 0 return $client->query($sparql);
1855 0 };
1856 }
1857 }
1858  
1859 =item * L<Attean::Plan::Table>
1860  
1861 Returns a constant set of results.
1862  
1863 =cut
1864  
1865 51 package Attean::Plan::Table 0.041 {
1866 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1867 51 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1868 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1869  
1870 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1871  
1872 51 has variables => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']]);
1873 51 has rows => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Result']]);
1874  
1875 0 ▶ sub tree_attributes { return qw(variables rows) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1876 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=2 SCAR=17.9
Pod undocumented
1877 0 my $self = shift;
1878 0 my $level = shift;
1879 0 my $indent = ' ' x ($level + 1);
1880 0 my $vars = join(', ', map { "?$_" } @{ $self->in_scope_variables });
1881 0 my $s = "Table (" . $vars . ")";
1882 0 foreach my $row (@{ $self->rows }) {
1883 0 $s .= "\n-${indent} " . $row->as_string;
1884 }
1885 0 return $s;
1886 }
1887
1888 8 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.3
Pod undocumented
1889 8 my $class = shift;
1890 8 my %args = @_;
1891 0 my @vars = map { $_->value } @{ $args{variables} };
1892
1893 8 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 8 8
1894 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
1895 }
1896 8 $args{in_scope_variables} = \@vars;
1897  
1898 8 return $class->SUPER::BUILDARGS(%args);
1899 }
1900
1901 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
1902 0 my $self = shift;
1903 0 my $model = shift;
1904 0 my $rows = $self->rows;
1905 0 my $iter_variables = $self->in_scope_variables;
1906  
1907 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1908 0 return Attean::ListIterator->new(
1909 item_type => 'Attean::API::Result',
1910 variables => $iter_variables,
1911 values => $rows
1912 );
1913 0 };
1914 }
1915 }
1916  
1917 =item * L<Attean::Plan::Iterator>
1918  
1919 Returns a constant set of results.
1920  
1921 Be aware that if the iterator being wrapped is not repeatable (consuming the
1922 L<Attean::API::RepeatableIterator> role), then this plan may only be evaluated
1923 once.
1924  
1925 A size estimate may be given if it is available. If the iterator is an
1926 L<Attean::ListIterator>, the size of that iterator will be used.
1927  
1928 =cut
1929  
1930 51 package Attean::Plan::Iterator 0.041 {
1931 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1932 51 ▶ use Types::Standard qw(ArrayRef ConsumerOf Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
1933 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1934  
1935 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
1936  
1937 51 has iterator => (is => 'ro', isa => ConsumerOf['Attean::API::ResultIterator']);
1938 51 has size_estimate => (is => 'lazy', isa => Int, predicate => 1);
1939  
1940 1 ▶ sub _build_size_estimate {
Subroutine _build_size_estimate: called CC=2 SCAR=7.0
1941 1 my $self = shift;
1942 1 my $iter = $self->iterator;
1943 1 ▶ if ($iter->isa('Attean::ListIterator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->isa("Attean::ListIterator")) 1 0 1
1944 1 return $iter->size;
1945 }
1946 }
1947  
1948  
1949 0 ▶ sub tree_attributes { return qw(iterator) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
1950 4 ▶ sub plan_as_string {
Subroutine plan_as_string: called CC=2 SCAR=6.9
Pod undocumented
1951 4 my $self = shift;
1952 4 my $level = shift;
1953 4 my $indent = ' ' x ($level + 1);
1954 4 my $string = 'Iterator (';
1955 4 $string .= join(', ', map { "?$_" } @{ $self->in_scope_variables });
1956 4 ▶ if ($self->has_size_estimate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_size_estimate) 3 1 4
1957 3 $string .= ' with ' . $self->size_estimate . ' elements';
1958 }
1959 4 $string .= ')';
1960 4 return $string;
1961 }
1962
1963 5 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=2 SCAR=7.1
Pod undocumented
1964 5 my $class = shift;
1965 5 my %args = @_;
1966 5 my $vars = $args{iterator}->variables;
1967
1968 5 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 5 5
1969 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
1970 }
1971 5 $args{in_scope_variables} = $vars;
1972  
1973 5 return $class->SUPER::BUILDARGS(%args);
1974 }
1975
1976 1 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
1977 1 my $self = shift;
1978 1 my $model = shift;
1979 1 my $iter = $self->iterator;
1980  
1981 1 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
1982 1 ▶ if ($iter->does('Attean::API::RepeatableIterator')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter->does("Attean::API::RepeatableIterator")) 1 0 1
1983 1 $iter->reset;
1984 }
1985 1 return $iter;
1986 1 };
1987 }
1988 }
1989  
1990 =item * L<Attean::Plan::ALPPath>
1991  
1992 =cut
1993  
1994 51 package Attean::Plan::ALPPath 0.041 {
1995 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
1996 51 ▶ use Attean::TreeRewriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
1997 51 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
1998 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
1999  
2000 51 has 'subject' => (is => 'ro', required => 1);
2001 51 has 'object' => (is => 'ro', required => 1);
2002 51 has 'graph' => (is => 'ro', required => 1);
2003 51 has 'step_begin' => (is => 'ro', required => 1);
2004 51 has 'step_end' => (is => 'ro', required => 1);
2005 51 has 'skip' => (is => 'ro', required => 1, default => 0);
2006 # has 'children' => (is => 'ro', isa => ConsumerOf['Attean::API::BindingSubstitutionPlan'], required => 1);
2007
2008 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::NullaryQueryTree';
2009  
2010 0 ▶ sub tree_attributes { return qw(subject object graph) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2011 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2012
2013 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2014 0 my $self = shift;
2015 0 my @strings;
2016 0 push(@strings, sprintf('%s ← %s', map { $_->ntriples_string } ($self->subject, $self->step_begin)));
2017 0 push(@strings, sprintf('%s ← %s', map { $_->ntriples_string } ($self->object, $self->step_end)));
2018 0 return sprintf('ALPPath %s', join(', ', @strings));
2019 }
2020
2021 0 ▶ sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
2022 0 my $class = shift;
2023 0 my %args = @_;
2024 0 my @vars = map { $_->value } grep { $_->does('Attean::API::Variable') } (@args{qw(subject object)});
2025
2026 0 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 0 0
2027 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
2028 }
2029 0 $args{in_scope_variables} = \@vars;
2030  
2031 0 return $class->SUPER::BUILDARGS(%args);
2032 }
2033
2034 0 ▶ sub alp {
Subroutine alp: not called CC=5 SCAR=34.0
Pod undocumented
2035 0 my $model = shift;
2036 0 my $graph = shift;
2037 0 my $skip = shift;
2038 0 my $x = shift;
2039 0 my $path = shift;
2040 0 my $v = shift;
2041 0 my $start = shift;
2042 0 my $end = shift;
2043 0 my $bind = shift;
2044 0 ▶ if (exists $v->{$x->as_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$v{$x->as_string}) 0 0 0
2045 0 return;
2046 }
2047 0 my $binding = Attean::Result->new( bindings => { $start => $x } )->join($bind);
2048 0 ▶ unless ($binding) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($binding) 0 0 0
2049 0 return;
2050 }
2051
2052 0 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) { } 0 0 0
2053 0 $skip--;
2054 } else {
2055 0 $v->{$x->as_string} = $x;
2056 }
2057
2058 0 my $impl = $path->substitute_impl($model, $binding);
2059 0 my $iter = $impl->();
2060 0 ▶ while (my $row = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 0 0 0
2061 0 my $n = $row->value($end);
2062 0 alp($model, $graph, $skip, $n, $path, $v, $start, $end, $bind);
2063 }
2064 }
2065
2066 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=8 SCAR=42.8
Pod undocumented
2067 0 my $self = shift;
2068 0 my $model = shift;
2069 0 my $bind = shift;
2070 0 my $path = $self->children->[0];
2071 0 my $subject = $self->subject;
2072 0 my $object = $self->object;
2073 0 my $graph = $self->graph;
2074 0 my $start = $self->step_begin->value;
2075 0 my $end = $self->step_end->value;
2076 0 my $skip = $self->skip;
2077 0 my $iter_variables = $self->in_scope_variables;
2078  
2079 0 for ($subject, $object) {
2080 0 ▶ if ($_->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->does("Attean::API::Variable")) 0 0 0
2081 0 my $name = $_->value;
2082 0 ▶ if (my $node = $bind->value($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $bind->value($name)) 0 0 0
2083 0 $_ = $node;
2084 }
2085 }
2086 }
2087
2088 0 my $s_var = $subject->does('Attean::API::Variable');
2089 0 my $o_var = $object->does('Attean::API::Variable');
2090 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
2091 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2092 0 my $nodes = $model->graph_nodes($graph);
2093 0 my @rows;
2094 0 ▶ while (my $n = $nodes->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $nodes->next) 0 0 0
2095 0 my %seen;
2096 0 alp($model, $graph, $skip, $n, $path, \%seen, $start, $end, $bind);
2097 0 foreach my $term (values %seen) {
2098 0 my $b = Attean::Result->new( bindings => {
2099 $subject->value => $n,
2100 $object->value => $term,
2101 } );
2102 0 push(@rows, $b);
2103 }
2104 }
2105 0 return Attean::ListIterator->new(
2106 item_type => 'Attean::API::Result',
2107 variables => $iter_variables,
2108 values => \@rows,
2109 );
2110 0 };
2111 } elsif ($o_var) {
2112 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2113 0 my %seen;
2114 0 alp($model, $graph, $skip, $subject, $path, \%seen, $start, $end, $bind);
2115 0 my @rows = map { Attean::Result->new( bindings => { $object->value => $_ } ) } (values %seen);
2116 0 return Attean::ListIterator->new(
2117 item_type => 'Attean::API::Result',
2118 variables => $iter_variables,
2119 values => \@rows,
2120 );
2121 0 };
2122 } elsif ($s_var) {
2123 0 die "ALP for FB should never occur in a plan (should be inversed during planning)";
2124 } else {
2125 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2126 0 my %seen;
2127 0 alp($model, $graph, $skip, $subject, $path, \%seen, $start, $end, $bind);
2128 0 ▶ if (exists $seen{ $object->as_string }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$object->as_string}) { } 0 0 0
2129 0 return Attean::ListIterator->new(
2130 item_type => 'Attean::API::Result',
2131 variables => $iter_variables,
2132 values => [Attean::Result->new()]
2133 );
2134 } else {
2135 0 return Attean::ListIterator->new(
2136 item_type => 'Attean::API::Result',
2137 variables => $iter_variables,
2138 values => []
2139 );
2140 }
2141 0 };
2142 }
2143 }
2144 }
2145  
2146 51 package Attean::Plan::ZeroOrOnePath 0.041 {
2147 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2148 51 ▶ use Attean::TreeRewriter;
Subroutine BEGIN: called CC=1 SCAR=0.0
2149 51 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
2150 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2151  
2152 51 has 'subject' => (is => 'ro', required => 1);
2153 51 has 'object' => (is => 'ro', required => 1);
2154 51 has 'graph' => (is => 'ro', required => 1);
2155
2156 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::NullaryQueryTree';
2157  
2158 0 ▶ sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
2159 0 my $class = shift;
2160 0 my %args = @_;
2161 0 my @vars = map { $_->value } grep { $_->does('Attean::API::Variable') } (@args{qw(subject object)});
2162
2163 0 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 0 0
2164 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
2165 }
2166 0 $args{in_scope_variables} = \@vars;
2167  
2168 0 return $class->SUPER::BUILDARGS(%args);
2169 }
2170
2171 0 ▶ sub tree_attributes { return qw(subject object) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2172 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2173
2174 0 ▶ sub plan_as_string { return 'ZeroOrOnePath' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2175
2176 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=4 SCAR=30.0
Pod undocumented
2177 0 my $self = shift;
2178 0 my $model = shift;
2179 0 my $bind = shift;
2180 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
2181 0 my $iter_variables = $self->in_scope_variables;
2182  
2183 0 my $subject = $self->subject;
2184 0 my $object = $self->object;
2185 0 my $graph = $self->graph;
2186 0 for ($subject, $object) {
2187 0 ▶ if ($_->does('Attean::API::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->does("Attean::API::Variable")) 0 0 0
2188 0 my $name = $_->value;
2189 0 ▶ if (my $node = $bind->value($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $node = $bind->value($name)) 0 0 0
2190 0 $_ = $node;
2191 }
2192 }
2193 }
2194  
2195 0 my $s_var = $subject->does('Attean::API::Variable');
2196 0 my $o_var = $object->does('Attean::API::Variable');
2197 0 ▶ return sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
2198 0 my @extra;
2199 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
2200 0 my $nodes = $model->graph_nodes($graph);
2201 0 ▶ while (my $n = $nodes->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $nodes->next) 0 0 0
2202 0 push(@extra, Attean::Result->new( bindings => { map { $_->value => $n } ($subject, $object) } ));
2203 }
2204 } elsif ($s_var) {
2205 0 push(@extra, Attean::Result->new( bindings => { $subject->value => $object } ));
2206 } elsif ($o_var) {
2207 0 push(@extra, Attean::Result->new( bindings => { $object->value => $subject } ));
2208 } else {
2209 0 ▶ if (0 == $subject->compare($object)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $subject->compare($object)) 0 0 0
2210 0 push(@extra, Attean::Result->new( bindings => {} ));
2211 }
2212 }
2213 0 my $iter = $impl->();
2214 0 my %seen;
2215 return Attean::CodeIterator->new(
2216 item_type => 'Attean::API::Result',
2217 variables => $iter_variables,
2218 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
2219 0 ▶ while (scalar(@extra)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @extra) 0 0 0
2220 0 my $r = shift(@extra);
2221 0 ▶ unless ($seen{$r->as_string}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$r->as_string}++) 0 0 0
2222 0 return $r;
2223 }
2224 }
2225 0 ▶ while (my $r = $iter->next()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
2226 0 ▶ return unless ($r);
Branch: 1 branch
BranchTrueFalseTotal
unless $r 0 0 0
2227 0 ▶ if ($seen{$r->as_string}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$r->as_string}++) 0 0 0
2228 0 next;
2229 }
2230 0 return $r;
2231 }
2232 }
2233 0 );
2234 0 };
2235 }
2236 }
2237  
2238 =item * L<Attean::Plan::Exists>
2239  
2240 Returns an iterator containing a single boolean term indicating whether any
2241 results were produced by evaluating the sub-plan.
2242  
2243 =cut
2244  
2245 51 package Attean::Plan::Exists 0.041 {
2246 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2247 51 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
2248 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2249
2250 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2251 51 with 'Attean::API::UnionScopeVariablesPlan';
2252  
2253 51 has variables => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']]);
2254 51 has rows => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Result']]);
2255  
2256 0 ▶ sub tree_attributes { return qw(variables rows) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2257 0 ▶ sub plan_as_string { return 'Exists' }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2258
2259 4 ▶ sub impl {
Subroutine impl: called CC=1 SCAR=0.0
Pod undocumented
2260 4 my $self = shift;
2261 4 my $model = shift;
2262 4 my ($impl) = map { $_->impl($model) } @{ $self->children };
2263 4 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2264 4 my $iter = $impl->();
2265 4 my $result = $iter->next;
2266 # if ($result) {
2267 # warn "EXISTS: " . $result->as_string;
2268 # }
2269 4 ▶ my $term = $result ? Attean::Literal->true : Attean::Literal->false;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 2 2 4
2270 4 return Attean::ListIterator->new(values => [$term], item_type => 'Attean::API::Term');
2271 }
2272 4 }
2273 }
2274  
2275 =item * L<Attean::Plan::Aggregate>
2276  
2277 =cut
2278  
2279 51 package Attean::Plan::Aggregate 0.041 {
2280 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2281 51 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
2282 51 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
2283 51 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
2284 51 ▶ use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
2285 51 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
2286 51 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
2287 51 ▶ use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
2288 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2289 51 ▶ use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
2290 51 ▶ use Types::Standard qw(ConsumerOf InstanceOf HashRef ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2291 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2292  
2293 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2294 51 has 'aggregates' => (is => 'ro', isa => HashRef[ConsumerOf['Attean::API::Expression']], required => 1);
2295 51 has 'groups' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Expression']], required => 1);
2296 51 has 'active_graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::IRI']], required => 1);
2297
2298 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2299 0 my $self = shift;
2300 0 my @astrings = map { sprintf('?%s ← %s', $_, $self->aggregates->{$_}->as_string) } keys %{ $self->aggregates };
2301 0 my @gstrings = map { sprintf('%s', $_->as_string) } @{ $self->groups };
2302 0 return sprintf('Aggregate { %s } Groups { %s }', join(', ', @astrings), join(', ', @gstrings));
2303 }
2304 0 ▶ sub tree_attributes { return qw(aggregates groups) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2305
2306 4 ▶ sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
2307 # Ensure that the CT extensions are registered
2308 4 AtteanX::Functions::CompositeLists->register();
2309 4 AtteanX::Functions::CompositeMaps->register();
2310 }
2311  
2312 4 ▶ sub BUILDARGS {
Subroutine BUILDARGS: called CC=3 SCAR=11.1
Pod undocumented
2313 4 my $class = shift;
2314 4 my %args = @_;
2315 4 my $aggs = $args{ aggregates };
2316 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
2317 4 my @evars = (@vars, keys %$aggs);
2318 4 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 4 4
2319 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
2320 }
2321 4 $args{in_scope_variables} = [@evars];
2322 4 return $class->SUPER::BUILDARGS(%args);
2323 }
2324
2325 0 ▶ sub evaluate_aggregate {
Subroutine evaluate_aggregate: not called CC=41 SCAR=74.5
Pod undocumented
2326 0 my $self = shift;
2327 0 my $model = shift;
2328 0 my $expr = shift;
2329 0 my $rows = shift;
2330 0 my $op = $expr->operator;
2331 0 my ($e) = @{ $expr->children };
2332 # my @children = map { Attean::Plan::Extend->evaluate_expression($model, $_, $r) } @{ $expr->children };
2333 # warn "$op — " . join(' ', map { $_->as_string } @children);
2334 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
2335 0 my $count = 0;
2336 0 foreach my $r (@$rows) {
2337 0 ▶ if ($e) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e) { } 0 0 0
2338 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2339 0 ▶ if ($term) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term) 0 0 0
2340 0 $count++;
2341 }
2342 } else {
2343 # This is the special-case branch for COUNT(*)
2344 0 $count++;
2345 }
2346 }
2347 0 return Attean::Literal->new(value => $count, datatype => 'http://www.w3.org/2001/XMLSchema#integer');
2348 } elsif ($op eq 'SUM') {
2349 0 my @cmp;
2350 my @terms;
2351 0 foreach my $r (@$rows) {
2352 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2353 0 ▶ if ($term->does('Attean::API::NumericLiteral')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::NumericLiteral")) 0 0 0
2354 0 push(@terms, $term);
2355 }
2356 }
2357 0 my $lhs = shift(@terms);
2358 0 ▶ while (my $rhs = shift(@terms)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rhs = shift @terms) 0 0 0
2359 0 my $type = $lhs->binary_promotion_type($rhs, '+');
2360 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
2361 0 $lhs = Attean::Literal->new(value => ($lv + $rv), datatype => $type);
2362 }
2363 0 return $lhs;
2364 } elsif ($op eq 'AVG') {
2365 0 my @cmp;
2366 0 my $count = 0;
2367 0 my $all_ints = 1;
2368 0 my @terms;
2369 0 ▶ if (scalar(@$rows) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$rows == 0) 0 0 0
2370 0 return Attean::Literal->integer(0);
2371 }
2372 0 foreach my $r (@$rows) {
2373 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2374 0 ▶ die unless ($term->does('Attean::API::NumericLiteral'));
Branch: 1 branch
BranchTrueFalseTotal
unless $term->does("Attean::API::NumericLiteral") 0 0 0
2375 0 push(@terms, $term);
2376 0 $count++;
2377 }
2378 0 my $lhs = shift(@terms);
2379 0 ▶ while (my $rhs = shift(@terms)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rhs = shift @terms) 0 0 0
2380 0 my $type = $lhs->binary_promotion_type($rhs, '+');
2381 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
2382 0 $lhs = Attean::Literal->new(value => ($lv + $rv), datatype => $type);
2383 }
2384
2385 0 my $rhs = Attean::Literal->new(value => $count, datatype => 'http://www.w3.org/2001/XMLSchema#integer');
2386 0 my ($lv, $rv) = map { $_->numeric_value } ($lhs, $rhs);
2387 0 my $type = $lhs->binary_promotion_type($rhs, '/');
2388 0 return Attean::Literal->new(value => ($lv / $rv), datatype => $type);
2389 } elsif ($op eq 'SAMPLE') {
2390 0 foreach my $r (@$rows) {
2391 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2392 0 ▶ return $term if (blessed($term));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($term) 0 0 0
2393 }
2394 } elsif ($op =~ /^(MIN|MAX)$/) {
2395 0 my @cmp;
2396 0 foreach my $r (@$rows) {
2397 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2398 0 push(@cmp, $term);
2399 }
2400 0 @cmp = sort { $a->compare($b) } @cmp;
2401 0 ▶ return ($op eq 'MIN') ? shift(@cmp) : pop(@cmp);
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'MIN' ? : 0 0 0
2402 } elsif ($op eq 'GROUP_CONCAT') {
2403 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
2404 0 my @values;
2405 my %seen;
2406 0 foreach my $r (@$rows) {
2407 0 my $term = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2408 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
2409 0 ▶ if ($expr->distinct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->distinct) { } 0 0 0
2410 0 ▶ unless ($seen{ $term->value }++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$term->value}++) 0 0 0
2411 0 push(@values, $term->value);
2412 }
2413 } else {
2414 0 push(@values, $term->value);
2415 }
2416 }
2417 0 my $string = join($sep, @values);
2418 0 return Attean::Literal->new(value => $string);
2419 } elsif ($op eq 'FOLD') {
2420 0 my @arg_exprs = @{ $expr->children };
2421 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
2422 0 my @cmps = @$order;
2423 0 my @exprs = map { $_->[1] } @cmps;
2424 0 my @dirs = map { $_->[0] eq 'ASC' } @cmps;
2425 0 my $l = eval {
2426 0 ▶ if (scalar(@$order)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$order) 0 0 0
2427 # sort $rows by the order condition
2428 0 ▶ my $fold_cmp = sub {
Subroutine __ANON__: not called CC=16 SCAR=56.1
2429 0 my ($ar, $avalues) = @$a;
2430 0 my ($br, $bvalues) = @$b;
2431  
2432 0 my $c = 0;
2433 0 foreach my $i (0 .. $#cmps) {
2434 0 my ($av, $bv) = map { $_->[$i] } ($avalues, $bvalues);
2435 # Mirrors code in Attean::Plan::OrderBy->sort_rows
2436 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
2437 0 $c = -1;
2438 } elsif (not(blessed($av))) {
2439 0 $c = 1;
2440 ▶ } 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
2441 0 $c = 1;
2442 ▶ } 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
2443 0 $c = -1;
2444 } else {
2445 0 ▶ $c = eval { $av ? $av->compare($bv) : 1 };
Branch: 1 branch
BranchTrueFalseTotal
$av ? : 0 0 0
2446 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
2447 0 $c = 1;
2448 }
2449 }
2450 0 ▶ $c *= -1 if ($dirs[$i] == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $dirs[$i] == 0 0 0 0
2451 0 ▶ last unless ($c == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $c == 0 0 0 0
2452 }
2453 $c
2454 0 };
2455
2456 0 my @sorted = map { $_->[0] } sort { $fold_cmp->() }
2457 0 map { my $r = $_; [$r, [map { eval { Attean::Plan::Extend->evaluate_expression( $model, $_, $r ) } } @exprs]] }
2458 @$rows;
2459  
2460 0 $rows = \@sorted;
2461 }
2462
2463 0 my %seen;
2464 0 ▶ if (scalar(@arg_exprs) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arg_exprs > 1) { } 0 0 0
2465 # map
2466 0 my @values;
2467 0 foreach my $r (@$rows) {
2468 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
2469 0 ▶ if (defined($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
2470 0 push(@values, $key, $value);
2471 }
2472 }
2473 0 my $func = Attean->get_global_functional_form($AtteanX::Functions::CompositeMaps::MAP_TYPE_IRI);
2474 0 $func->(undef, undef, @values);
2475 } else {
2476 0 my @values;
2477 0 foreach my $r (@$rows) {
2478 0 my $term = eval { Attean::Plan::Extend->evaluate_expression($model, $e, $r) };
2479 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
2480 0 ▶ next if ($seen{ $term->as_string }++);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$term->as_string}++ 0 0 0
2481 }
2482 0 push(@values, $term);
2483 }
2484 0 my $func = Attean->get_global_functional_form($AtteanX::Functions::CompositeLists::LIST_TYPE_IRI);
2485 0 $func->(undef, undef, @values);
2486 }
2487 };
2488 0 ▶ warn $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
2489 0 return $l;
2490 } elsif ($op eq 'CUSTOM') {
2491 0 my $iri = $expr->custom_iri;
2492 0 my $data = Attean->get_global_aggregate($iri);
2493 0 ▶ unless ($data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data) 0 0 0
2494 0 die "No extension aggregate registered for <$iri>";
2495 }
2496 0 my $start = $data->{'start'};
2497 0 my $process = $data->{'process'};
2498 0 my $finalize = $data->{'finalize'};
2499  
2500 0 my $thunk = $start->($model, $self->active_graphs);
2501 0 foreach my $r (@$rows) {
2502 0 my $t = Attean::Plan::Extend->evaluate_expression($model, $e, $r);
2503 0 $process->($thunk, $t);
2504 }
2505 0 return $finalize->($thunk);
2506 } else {
2507 0 warn "Unexpected aggregate expression: $op";
2508 }
2509 0 die "$op not implemented";
2510 }
2511
2512 0 ▶ sub impl {
Subroutine impl: not called CC=3 SCAR=24.8
Pod undocumented
2513 0 my $self = shift;
2514 0 my $model = shift;
2515 0 my %aggs = %{ $self->aggregates };
2516 0 my @groups = @{ $self->groups };
2517 0 my $iter_variables = $self->in_scope_variables;
2518  
2519 0 ▶ my $group_template_generator = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
2520 0 my $r = shift;
2521 0 my %components;
2522 0 foreach my $g (@groups) {
2523 0 ▶ if ($g->isa('Attean::ValueExpression')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->isa("Attean::ValueExpression")) 0 0 0
2524 0 my $value = $g->value;
2525 0 ▶ if ($value->isa('Attean::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value->isa("Attean::Variable")) 0 0 0
2526 0 my $var = $value->value;
2527 0 my $value = eval { Attean::Plan::Extend->evaluate_expression($model, $g, $r) };
2528 0 ▶ if (blessed($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value)) 0 0 0
2529 0 $components{$var} = $value;
2530 }
2531 }
2532 }
2533 }
2534 0 return %components;
2535 0 };
2536 0 ▶ my $group_key_generator = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
2537 0 my $r = shift;
2538 0 my @components;
2539 0 foreach my $g (@groups) {
2540 0 my $value = eval { Attean::Plan::Extend->evaluate_expression($model, $g, $r) };
2541 0 ▶ my $key = blessed($value) ? $value->as_string : '';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($value) ? : 0 0 0
2542 0 push(@components, $key);
2543 }
2544 0 my $group = join('|', @components);
2545 0 return $group;
2546 0 };
2547
2548 0 my $rank;
2549 0 ▶ while (my($var, $agg) = each(%aggs)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %aggs) 0 0 0
2550 0 ▶ if ($agg->operator eq 'RANK') {
Branch: 1 branch
BranchTrueFalseTotal
if ($agg->operator eq "RANK") 0 0 0
2551 0 $rank = $var;
2552 }
2553 }
2554
2555 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
2556 0 my %row_groups;
2557 my %group_templates;
2558 0 ▶ return sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
2559 0 my $iter = $impl->();
2560 0 ▶ while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
2561 0 my $group_key = $group_key_generator->($r);
2562 0 push(@{ $row_groups{ $group_key } }, $r);
2563 0 ▶ unless (exists $group_templates{ $group_key }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $group_templates{$group_key}) 0 0 0
2564 0 $group_templates{ $group_key } = { $group_template_generator->($r) };
2565 }
2566 }
2567 0 my @group_keys = keys %row_groups;
2568
2569 # SPARQL evaluation of aggregates over an empty input sequence should
2570 # result in an empty result <http://answers.semanticweb.com/questions/17410/semantics-of-sparql-aggregates>
2571
2572 0 my @results;
2573 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
2574 0 push(@group_keys, '');
2575 0 $row_groups{''} = [];
2576 0 $group_templates{''} = {};
2577 }
2578 0 foreach my $group (@group_keys) {
2579 0 my %row = %{ $group_templates{ $group } };
2580 0 my $rows = $row_groups{$group};
2581 0 ▶ if (defined $rank) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rank) { } 0 0 0
2582 0 my $agg = $aggs{$rank};
2583 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
2584 0 my $vars = [map { $_->value->value } @{ $agg->children }];
2585 # TODO: support ordering by complex expressions in $vars, not just ValueExpressions with variables
2586 0 my @sorted = Attean::Plan::OrderBy->sort_rows($vars, $ascending, $rows);
2587 0 my $ord = 0;
2588 0 foreach my $row (@sorted) {
2589 0 my %b = %{ $row->bindings };
2590 0 $b{ $rank } = Attean::Literal->integer($ord++);
2591 0 my $r = Attean::Result->new( bindings => \%b );
2592 0 push(@results, $r);
2593 }
2594 } else {
2595 0 foreach my $var (keys %aggs) {
2596 0 my $expr = $aggs{$var};
2597 0 my $value = eval { $self->evaluate_aggregate($model, $expr, $rows) };
2598 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
2599 0 $row{$var} = $value;
2600 }
2601 }
2602 0 my $result = Attean::Result->new( bindings => \%row );
2603 0 push(@results, $result);
2604 }
2605 }
2606 0 return Attean::ListIterator->new(
2607 values => \@results,
2608 variables => $iter_variables,
2609 item_type => 'Attean::API::Result'
2610 );
2611 0 };
2612 }
2613 }
2614  
2615 51 package Attean::Plan::Sequence 0.041 {
2616 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2617 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2618 51 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2619 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2620
2621 51 with 'Attean::API::Plan', 'Attean::API::QueryTree';
2622 51 with 'Attean::API::UnionScopeVariablesPlan';
2623  
2624 0 ▶ sub plan_as_string { return 'Sequence'; }
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2625
2626 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2627 0 my $self = shift;
2628 0 my $model = shift;
2629 0 my @children = map { $_->impl($model) } @{ $self->children };
2630 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2631 0 foreach my $child (@children) {
2632 0 my $iter = $child->();
2633 0 $iter->elements;
2634 }
2635 0 return Attean::ListIterator->new(values => [Attean::Literal->true], item_type => 'Attean::API::Term');
2636 0 };
2637 }
2638 }
2639  
2640 51 package Attean::Plan::Clear 0.041 {
2641 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2642 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2643 51 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2644 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2645
2646 51 with 'Attean::API::Plan', 'Attean::API::NullaryQueryTree';
2647 51 with 'Attean::API::UnionScopeVariablesPlan';
2648  
2649 51 has 'graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Term']]);
2650  
2651 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=2 SCAR=17.9
Pod undocumented
2652 0 my $self = shift;
2653 0 my $level = shift;
2654 0 my $indent = ' ' x (1+$level);
2655 0 my $s = sprintf("Clear { %d graphs }", scalar(@{ $self->graphs }));
2656 0 foreach my $g (@{ $self->graphs }) {
2657 0 my $name = $g->as_sparql;
2658 0 chomp($name);
2659 0 $s .= "\n-${indent} $name";
2660 }
2661 0 return $s;
2662 }
2663
2664 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2665 0 my $self = shift;
2666 0 my $model = shift;
2667 0 my $graphs = $self->graphs;
2668 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2669 0 foreach my $g (@$graphs) {
2670 0 $model->clear_graph($g);
2671 }
2672 0 return Attean::ListIterator->new(values => [Attean::Literal->true], item_type => 'Attean::API::Term');
2673 0 };
2674 }
2675 }
2676  
2677 51 package Attean::Plan::Drop 0.041 {
2678 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2679 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2680 51 ▶ use Types::Standard qw(ConsumerOf ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2681 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2682
2683 51 with 'Attean::API::Plan', 'Attean::API::NullaryQueryTree';
2684 51 with 'Attean::API::UnionScopeVariablesPlan';
2685  
2686 51 has 'graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Term']]);
2687  
2688 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=2 SCAR=17.9
Pod undocumented
2689 0 my $self = shift;
2690 0 my $level = shift;
2691 0 my $indent = ' ' x (1+$level);
2692 0 my $s = sprintf("Drop { %d graphs }", scalar(@{ $self->graphs }));
2693 0 foreach my $g (@{ $self->graphs }) {
2694 0 $s .= "\n-${indent} " . $g->as_sparql;
2695 }
2696 0 return $s;
2697 }
2698
2699 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2700 0 my $self = shift;
2701 0 my $model = shift;
2702 0 my $graphs = $self->graphs;
2703 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
2704 0 foreach my $g (@$graphs) {
2705 0 $model->drop_graph($g);
2706 }
2707 0 return Attean::ListIterator->new(values => [Attean::Literal->true], item_type => 'Attean::API::Term');
2708 0 };
2709 }
2710 }
2711  
2712 51 package Attean::Plan::TripleTemplateToModelQuadMethod 0.041 {
2713 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2714 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2715 51 ▶ use Types::Standard qw(ConsumerOf Str ArrayRef HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2716 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2717
2718 51 with 'Attean::API::Plan', 'Attean::API::UnaryQueryTree';
2719 51 with 'Attean::API::UnionScopeVariablesPlan';
2720  
2721 51 has 'order' => (is => 'ro', isa => ArrayRef[Str], required => 1);
2722 51 has 'patterns' => (is => 'ro', isa => HashRef[ArrayRef[ConsumerOf['Attean::API::TripleOrQuadPattern']]], required => 1);
2723 51 has 'graph' => (is => 'ro', isa => ConsumerOf['Attean::API::Term']);
2724  
2725 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=3 SCAR=24.8
Pod undocumented
2726 0 my $self = shift;
2727 0 my $level = shift;
2728 0 my $indent = ' ' x (1+$level);
2729 0 my $s = sprintf("Template-to-Model { Default graph: %s }", $self->graph->as_string);
2730 0 foreach my $method (@{ $self->order }) {
2731 0 my $pattern = $self->patterns->{ $method };
2732 0 $s .= "\n-${indent} Method: ${method}";
2733 0 foreach my $p (@$pattern) {
2734 0 $s .= "\n-${indent} " . $p->as_string;
2735 }
2736 }
2737 0 return $s;
2738 }
2739
2740 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2741 0 my $self = shift;
2742 0 my $model = shift;
2743 0 my $child = $self->children->[0]->impl($model);
2744
2745 0 my $graph = $self->graph;
2746 0 my @order = @{ $self->order };
2747 0 my $method = shift(@order);
2748 0 my $pattern = $self->patterns->{ $method };
2749  
2750 0 ▶ return sub {
Subroutine __ANON__: not called CC=11 SCAR=48.8
2751 0 my $iter = $child->();
2752 0 my @results;
2753 0 ▶ while (my $t = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 0 0 0
2754 0 my $bnode_mapper = Attean::TermMap->uuid_blank_map; # used to create fresh bnodes in the pattern for each input row
2755 0 ▶ if (scalar(@order)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @order) 0 0 0
2756 0 push(@results, $t);
2757 }
2758 0 foreach my $p (@$pattern) {
2759 0 my $q = $p->apply_map($bnode_mapper)->apply_bindings($t);
2760 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
2761 0 ▶ if ($quad->is_ground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quad->is_ground) { } 0 0 0
2762 # warn "# $method: " . $quad->as_string . "\n";
2763 0 $model->$method($quad->as_quad);
2764 } else {
2765 # warn "not ground: " . $quad->as_string;
2766 }
2767 }
2768 }
2769 0 foreach my $method (@order) {
2770 0 my $pattern = $self->patterns->{ $method };
2771 0 foreach my $t (@results) {
2772 0 my $bnode_mapper = Attean::TermMap->uuid_blank_map; # used to create fresh bnodes in the pattern for each input row
2773 0 foreach my $p (@$pattern) {
2774 0 my $q = $p->apply_map($bnode_mapper)->apply_bindings($t);
2775 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
2776 0 ▶ if ($quad->is_ground) {
Branch: 1 branch
BranchTrueFalseTotal
if ($quad->is_ground) { } 0 0 0
2777 # warn "# $method: " . $quad->as_string . "\n";
2778 0 $model->$method($quad->as_quad);
2779 } else {
2780 # warn "not ground: " . $quad->as_string;
2781 }
2782 }
2783 }
2784 }
2785 0 return Attean::ListIterator->new(values => [Attean::Literal->integer($model->size)], item_type => 'Attean::API::Term');
2786 0 };
2787 }
2788 }
2789  
2790 51 package Attean::Plan::Load 0.041 {
2791 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2792 51 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
2793 51 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
2794 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
2795 51 ▶ use Types::Standard qw(Bool Str);
Subroutine BEGIN: called CC=1 SCAR=0.0
2796 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2797
2798 51 with 'Attean::API::Plan', 'Attean::API::NullaryQueryTree';
2799 51 with 'Attean::API::UnionScopeVariablesPlan';
2800  
2801 51 has 'silent' => (is => 'ro', isa => Bool, default => 0);
2802 51 has 'url' => (is => 'ro', isa => Str);
2803  
2804 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2805 0 my $self = shift;
2806 0 return sprintf("Load { %s }", $self->url);
2807 }
2808
2809 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2810 0 my $self = shift;
2811 0 my $url = $self->url;
2812 0 my $ua = LWP::UserAgent->new();
2813 0 my $silent = $self->silent;
2814 0 my $accept = Attean->acceptable_parsers( handles => 'Attean::API::Triple' );
2815 0 $ua->default_headers->push_header( 'Accept' => $accept );
2816 0 ▶ return sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
2817 0 my $resp = $ua->get( $url );
2818 0 ▶ if ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) 0 0 0
2819 0 my $ct = $resp->header('Content-Type');
2820 0 my $pclass = Attean->get_parser( media_type => $ct );
2821 0 ▶ $pclass = Attean->get_parser( filename => $url ) unless ($pclass);
Branch: 1 branch
BranchTrueFalseTotal
unless $pclass 0 0 0
2822 0 ▶ $pclass = Attean->get_parser( 'Turtle' ) unless ($pclass); # default to Turtle if no media type is provided
Branch: 1 branch
BranchTrueFalseTotal
unless $pclass 0 0 0
2823 0 my $p = $pclass->new();
2824 0 my $str = $resp->decoded_content;
2825 0 my $bytes = encode('UTF-8', $str, Encode::FB_CROAK);
2826 0 my $iter = $p->parse_iter_from_bytes( $bytes );
2827 0 return $iter;
2828 }
2829
2830 0 ▶ if ($silent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($silent) { } 0 0 0
2831 0 return Attean::ListIterator->new(values => [], item_type => 'Attean::API::Triple');
2832 } else {
2833 0 die "Failed to load url: " . $resp->status_line;
2834 }
2835 0 };
2836 }
2837 }
2838  
2839  
2840  
2841 =item * L<Attean::Plan::Unfold>
2842  
2843 =cut
2844  
2845 51 package Attean::Plan::Unfold 0.032 {
2846 51 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
2847 51 ▶ use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
2848 51 ▶ use UUID::Tiny ':std';
Subroutine BEGIN: called CC=1 SCAR=0.0
2849 51 ▶ use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
2850 51 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
2851 51 ▶ use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
2852 51 ▶ use POSIX qw(ceil floor);
Subroutine BEGIN: called CC=1 SCAR=0.0
2853 51 ▶ use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
2854 51 ▶ use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
2855 51 ▶ use Scalar::Util qw(blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
2856 51 ▶ use List::Util qw(uniq all);
Subroutine BEGIN: called CC=1 SCAR=0.0
2857 51 ▶ use Types::Standard qw(ConsumerOf ArrayRef InstanceOf HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
2858 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
2859  
2860 51 with 'MooX::Log::Any';
2861 51 with 'Attean::API::BindingSubstitutionPlan', 'Attean::API::UnaryQueryTree';
2862
2863 51 has 'variables' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::Variable']], required => 1);
2864 51 has 'expression' => (is => 'ro', isa => ConsumerOf['Attean::API::Expression'], required => 1);
2865 51 has 'active_graphs' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::IRI']], required => 1);
2866
2867 0 ▶ sub plan_as_string {
Subroutine plan_as_string: not called CC=1 SCAR=6.9
Pod undocumented
2868 0 my $self = shift;
2869 0 my @vars = map { $_->as_string } @{ $self->variables };
2870 0 my $vars = '(' . join(', ', @vars) . ')';
2871 0 return sprintf('Unfold { %s ← %s }', $vars, $self->expression->as_string);
2872 }
2873 0 ▶ sub tree_attributes { return qw(variable expression) };
Subroutine tree_attributes: not called CC=1 SCAR=6.9
Pod undocumented
2874
2875 0 ▶ sub BUILDARGS {
Subroutine BUILDARGS: not called CC=2 SCAR=17.9
Pod undocumented
2876 0 my $class = shift;
2877 0 my %args = @_;
2878 0 my $exprs = $args{ expressions };
2879 0 my @vars = map { @{ $_->in_scope_variables } } @{ $args{ children } };
2880 0 my @evars = (@vars, keys %$exprs);
2881
2882 0 ▶ if (exists $args{in_scope_variables}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"in_scope_variables"}) 0 0 0
2883 0 Carp::confess "in_scope_variables is computed automatically, and must not be specified in the $class constructor";
2884 }
2885 0 $args{in_scope_variables} = [@evars];
2886 0 return $class->SUPER::BUILDARGS(%args);
2887 }
2888
2889 0 ▶ sub substitute_impl {
Subroutine substitute_impl: not called CC=1 SCAR=6.9
Pod undocumented
2890 0 my $self = shift;
2891 0 my $model = shift;
2892 0 my $bind = shift;
2893 0 my $expr = $self->expression;
2894 0 my $vars = $self->variables;
2895 0 my ($impl) = map { $_->substitute_impl($model, $bind) } @{ $self->children };
2896 # TODO: substitute variables in the expression
2897 0 return $self->_impl($model, $impl, $expr, @$vars);
2898 }
2899
2900 0 ▶ sub impl {
Subroutine impl: not called CC=1 SCAR=6.9
Pod undocumented
2901 0 my $self = shift;
2902 0 my $model = shift;
2903 0 my $expr = $self->expression;
2904 0 my $vars = $self->variables;
2905 0 my ($impl) = map { $_->impl($model) } @{ $self->children };
2906 0 return $self->_impl($model, $impl, $expr, @$vars);
2907 }
2908
2909 0 ▶ sub _impl {
Subroutine _impl: not called CC=2 SCAR=17.9
2910 0 my $self = shift;
2911 0 my $model = shift;
2912 0 my $impl = shift;
2913 0 ▶ Carp::confess unless (defined($impl));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $impl 0 0 0
2914 0 my $expr = shift;
2915 0 my @vars = @_;
2916 0 my $iter_variables = $self->in_scope_variables;
2917  
2918 0 my $var = $vars[0];
2919 0 my $index_var = $vars[1];
2920 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2921 0 my $iter = $impl->();
2922 0 my @buffer;
2923 return Attean::CodeIterator->new(
2924 item_type => 'Attean::API::Result',
2925 variables => $iter_variables,
2926 0 ▶ generator => sub {
Subroutine __ANON__: not called CC=21 SCAR=61.4
2927 0 ▶ if (scalar(@buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @buffer) 0 0 0
2928 0 return shift(@buffer);
2929 }
2930 0 ▶ ROW: while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
2931 0 my %base = map { $_ => $r->value($_) } $r->variables;
2932 0 my $cdt = eval { Attean::Plan::Extend->evaluate_expression($model, $expr, $r) };
2933 0 ▶ warn "UNFOLD expression evaluation error: $@" if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
2934 0 ▶ unless ($cdt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cdt) 0 0 0
2935 0 return $r;
2936 }
2937
2938 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
2939 0 my @values = AtteanX::Functions::CompositeLists::lex_to_list($cdt);
2940 0 foreach my $index (0 .. $#values) {
2941 0 my %row = %base;
2942 0 my $term = $values[$index];
2943 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
2944 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
2945 0 next ROW;
2946 }
2947
2948 0 ▶ if ($term->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Binding")) 0 0 0
2949 # patterns need to be made ground to be bound as values (e.g. TriplePattern -> Triple)
2950 0 $term = $term->ground($r);
2951 }
2952
2953 0 $row{ $var->value } = $term;
2954 0 ▶ if (defined $index_var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index_var) 0 0 0
2955 0 $row{ $index_var->value } = Attean::Literal->integer(1+$index);
2956 }
2957 }
2958 0 push(@buffer, Attean::Result->new( bindings => \%row, eval_stash => $r->eval_stash ));
2959 }
2960 ▶ } 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
2961 0 my @values = AtteanX::Functions::CompositeMaps::lex_to_maplist($cdt);
2962 0 ▶ while (my @pair = splice(@values, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pair) = splice(@values, 0, 2)) 0 0 0
2963 0 my ($index, $term) = @pair;
2964 0 my %row = %base;
2965 0 ▶ if (blessed($term)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($term)) 0 0 0
2966 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
2967 0 next ROW;
2968 }
2969
2970 0 ▶ if ($term->does('Attean::API::Binding')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->does("Attean::API::Binding")) 0 0 0
2971 # patterns need to be made ground to be bound as values (e.g. TriplePattern -> Triple)
2972 0 $term = $term->ground($r);
2973 }
2974
2975 0 ▶ if (defined $index_var) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $index_var) { } 0 0 0
2976 # 2-var
2977 0 $row{ $index_var->value } = $term;
2978 0 $row{ $var->value } = $index;
2979 } else {
2980 # 1-var
2981 0 $row{ $var->value } = $term;
2982 }
2983 }
2984 0 push(@buffer, Attean::Result->new( bindings => \%row, eval_stash => $r->eval_stash ));
2985 }
2986 }
2987 0 return shift(@buffer);
2988 }
2989 0 return;
2990 }
2991 0 );
2992 0 };
2993 }
2994 }
2995 # Create(iri)
2996  
2997 51 1;
2998  
2999 __END__
↑ Summary Attean/SimpleQueryEvaluator.pm »