Attean/API/Query.pm

filter: Statementstmt 87.9% 381 / 433 Branchbran 79.4% 81 / 102 Conditioncond 97.1% 34 / 35 MC/DCmcdc 94.1% 16 / 17 Subroutinesub 92.0% 58 / 63 Podpod 0.0% 0 / 28 totaltotal 84.0% 570 / 678 CC 81 SCAR 29.6
CC
81
Cov
87%
query_tokens
49.4
__ANON__
16.2
__ANON__
12.0
CRAP 19.4
« Attean/API/Binding.pm ↑ Summary Attean/API.pm »
1 50 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 Attean::API::Query - Utility package defining query-related roles
7  
8 =head1 VERSION
9  
10 This document describes Attean::API::Query version 0.040
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16  
17 =head1 DESCRIPTION
18  
19 This is a utility package for defining query-related roles:
20  
21 =over 4
22  
23 =item * L<Attean::API::DirectedAcyclicGraph>
24  
25 =cut
26  
27 50 package Attean::API::DirectedAcyclicGraph 0.040 {
28 50 ▶ use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 # =item C<< children >>
34 #
35 # An ARRAY reference of L<Attean::API::DirectedAcyclicGraph> objects.
36 #
37 # =back
38 #
39 # =cut
40  
41 has 'children' => (
42 is => 'ro',
43 isa => ArrayRef[ConsumerOf['Attean::API::DirectedAcyclicGraph']],
44 1894 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 50 );
46
47 # =item C<< is_leaf >>
48 #
49 # Returns true if the referent has zero C<< children >>, false otherwise.
50 #
51 # =cut
52  
53 3 ▶ sub is_leaf {
Subroutine is_leaf: called CC=1 SCAR=0.0
Pod undocumented
54 3 my $self = shift;
55 3 return not(scalar(@{ $self->children }));
56 }
57
58 # =item C<< walk( prefix => \&pre_cb, postfix => \&pre_cb ) >>
59 #
60 # Walks the graph rooted at the referent, calling C<< &pre_cb >> (if supplied)
61 # before descending, and C<< &post_cb >> (if supplied) after descending. The
62 # callback functions are passed the current graph walk node as the single
63 # argument.
64 #
65 # =cut
66  
67 150 ▶ sub walk {
Subroutine walk: called CC=5 SCAR=16.1
Pod undocumented
68 150 my $self = shift;
69 150 my %args = @_;
70 150 ▶ my $level = $args{ level } // 0;
Condition100%2/2
$args{"level"} // 0
l!l
9951
MC/DC100%1/1
$args{"level"} // 0
$args{"level"}
Truth table100%2/2
A
A= $args{"level"}
Aresult
11
00
71 150 my $parent = $args{ parent };
72 150 ▶ if (my $cb = $args{ prefix }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $args{"prefix"}) 150 0 150
73 150 $cb->( $self, $level, $parent );
74 }
75 150 foreach my $c (@{ $self->children }) {
76 99 $c->walk( %args, level => (1+$level), parent => $self );
77 }
78 150 ▶ if (my $cb = $args{ postfix }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $args{"postfix"}) 2 148 150
79 2 $cb->( $self, $level, $parent );
80 }
81 }
82
83 # =item C<< has_only_subtree_types( @classes ) >>
84 #
85 # Returns true if the invocant and all of its sub-trees are instances of only
86 # the listed classes, false otherwise.
87 #
88 # =cut
89  
90 2 ▶ sub has_only_subtree_types {
Subroutine has_only_subtree_types: called CC=4 SCAR=13.9
Pod undocumented
91 2 my $self = shift;
92 2 my @types = @_;
93 2 my %types = map { $_ => 1 } @types;
94 2 ▶ return 0 unless (exists $types{ ref($self) });
Branch: 1 branch
BranchTrueFalseTotal
unless exists $types{ref $self} 1 1 2
95
96 1 my %classes;
97 1 ▶ $self->walk( prefix => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 1 my $plan = shift;
99 1 $classes{ref($plan)}++;
100 1 });
101 1 foreach my $type (@types) {
102 1 delete $classes{$type};
103 }
104 1 my @keys = keys %classes;
105 1 ▶ return (scalar(@keys) == 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar @keys == 0 ? : 1 0 1
106 }
107  
108 # =item C<< cover( prefix => \&pre_cb, postfix => \&pre_cb ) >>
109 #
110 # Similar to C<< walk >>, walks the graph rooted at the referent, calling
111 # C<< &pre_cb >> (if supplied) before descending, and C<< &post_cb >> (if
112 # supplied) after descending. However, unlike C<< walk >>, each node in the graph
113 # is visited only once.
114 #
115 # =cut
116  
117 1 ▶ sub cover {
Subroutine cover: called CC=1 SCAR=0.0
Pod undocumented
118 1 my $self = shift;
119 1 return $self->_cover({}, @_);
120 }
121
122 3 ▶ sub _cover {
Subroutine _cover: called CC=5 SCAR=16.4
123 3 my $self = shift;
124 3 my $seen = shift;
125 3 my %cb = @_;
126 3 ▶ return if ($seen->{refaddr($self)}++);
Branch: 1 branch
BranchTrueFalseTotal
if $$seen{builtin::refaddr($self)}++ 1 2 3
127 2 ▶ if (my $cb = $cb{ prefix }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $cb{"prefix"}) 2 0 2
128 2 $cb->( $self );
129 }
130 2 foreach my $c (@{ $self->children }) {
131 2 $c->_cover( $seen, %cb );
132 }
133 2 ▶ if (my $cb = $cb{ postfix }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $cb{"postfix"}) 0 2 2
134 0 $cb->( $self );
135 }
136 }
137  
138 11 ▶ sub subpatterns_of_type {
Subroutine subpatterns_of_type: called CC=1 SCAR=0.0
Pod undocumented
139 11 my $self = shift;
140 11 my @types = @_;
141 11 my @p;
142 43 ▶ $self->walk( prefix => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
143 43 my $a = shift;
144 43 foreach my $t (@types) {
145 43 ▶ push(@p, $a) if ($a->isa($t) or $a->does($t));
Branch: 1 branch
BranchTrueFalseTotal
if $a->isa($t) or $a->does($t) 21 22 43
Condition66%2/3
$a->isa($t) or $a->does($t)
l!l&&r!l&&!r
21022
MC/DC50%1/2
$a->isa($t) or $a->does($t)
$a->isa($t) $a->does($t)
Truth table66%2/3
A or B
A= $a->isa($t)B= $a->does($t)
ABresult
1X1
011
000
146 }
147 11 });
148 11 return @p;
149 }
150 }
151  
152 50 package Attean::API::SPARQLSerializable 0.040 {
153 50 ▶ use AtteanX::SPARQL::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 50 ▶ use AtteanX::SPARQL::Token;
Subroutine BEGIN: called CC=1 SCAR=0.0
155 50 ▶ use Encode qw(decode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
156 50 ▶ use Attean::API::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
157 50 ▶ use Attean::API::Serializer;
Subroutine BEGIN: called CC=1 SCAR=0.0
158 50 ▶ use AtteanX::Serializer::SPARQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 50 requires 'sparql_tokens';
163
164 54 ▶ sub as_sparql {
Subroutine as_sparql: called CC=1 SCAR=0.0
Pod undocumented
165 54 my $self = shift;
166 54 my $s = AtteanX::Serializer::SPARQL->new();
167 54 my $i = $self->sparql_tokens;
168 54 my $bytes = $s->serialize_iter_to_bytes($i);
169 54 my $sparql = decode_utf8($bytes);
170 54 return $sparql
171 }
172
173 29 ▶ sub sparql_subtokens {
Subroutine sparql_subtokens: called CC=2 SCAR=11.6
Pod undocumented
174 29 my $self = shift;
175 29 ▶ if ($self->does('Attean::API::SPARQLQuerySerializable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->does('Attean::API::SPARQLQuerySerializable')) { } 0 29 29
176 0 my $l = AtteanX::SPARQL::Token->lbrace;
177 0 my $r = AtteanX::SPARQL::Token->rbrace;
178 0 my @tokens;
179 0 push(@tokens, $l);
180 0 push(@tokens, $self->sparql_tokens->elements);
181 0 push(@tokens, $r);
182 0 return Attean::ListIterator->new( values => \@tokens, item_type => 'AtteanX::SPARQL::Token' );
183 } else {
184 29 return $self->sparql_tokens;
185 }
186 }
187
188 33 ▶ sub dataset_tokens {
Subroutine dataset_tokens: called CC=6 SCAR=17.9
Pod undocumented
189 33 my $self = shift;
190 33 my $dataset = shift;
191 33 ▶ my @default = @{ $dataset->{ default } || [] };
Condition100%2/2
$dataset->{'default'} || []
l!l
429
MC/DC100%1/1
$dataset->{'default'} || []
$dataset->{'default'}
Truth table100%2/2
A
A= $dataset->{'default'}
Aresult
11
00
192 33 ▶ my @named = @{ $dataset->{ named } || [] };
Condition100%2/2
$dataset->{'named'} || []
l!l
429
MC/DC100%1/1
$dataset->{'named'} || []
$dataset->{'named'}
Truth table100%2/2
A
A= $dataset->{'named'}
Aresult
11
00
193 33 my $has_dataset = (scalar(@default) + scalar(@named));
194 33 my @tokens;
195 33 ▶ if ($has_dataset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_dataset) 4 29 33
196 4 my $from = AtteanX::SPARQL::Token->keyword('FROM');
197 4 my $named = AtteanX::SPARQL::Token->keyword('NAMED');
198 0 foreach my $i (sort { $a->as_string cmp $b->as_string } @default) {
199 4 push(@tokens, $from);
200 4 push(@tokens, $i->sparql_tokens->elements);
201 }
202 0 foreach my $i (sort { $a->as_string cmp $b->as_string } @named) {
203 4 push(@tokens, $from);
204 4 push(@tokens, $named);
205 4 push(@tokens, $i->sparql_tokens->elements);
206 }
207 }
208 33 return Attean::ListIterator->new( values => \@tokens, item_type => 'AtteanX::SPARQL::Token' );
209 }
210
211 33 ▶ sub query_tokens {
Subroutine query_tokens: called CC=49 SCAR=39.0
Pod undocumented
212 33 my $self = shift;
213 33 my %args = @_;
214 33 ▶ my $dataset = $args{dataset} || {};
Condition100%2/2
$args{'dataset'} || {}
l!l
429
MC/DC100%1/1
$args{'dataset'} || {}
$args{'dataset'}
Truth table100%2/2
A
A= $args{'dataset'}
Aresult
11
00
215
216 33 my $as = AtteanX::SPARQL::Token->keyword('AS');
217 33 my $lparen = AtteanX::SPARQL::Token->lparen;
218 33 my $rparen = AtteanX::SPARQL::Token->rparen;
219
220 33 my $algebra = $self;
221
222 33 my %modifiers;
223 33 my $form = 'SELECT';
224 33 ▶ if ($algebra->isa('Attean::Algebra::Ask')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($algebra->isa('Attean::Algebra::Ask')) { } 3 30 33
elsif ($algebra->isa('Attean::Algebra::Describe')) { } 3 27 30
elsif ($algebra->isa('Attean::Algebra::Construct')) { } 3 24 27
225 3 $form = 'ASK';
226 3 ($algebra) = @{ $algebra->children };
227 } elsif ($algebra->isa('Attean::Algebra::Describe')) {
228 3 $form = 'DESCRIBE';
229 3 $modifiers{describe} = $algebra->terms;
230 3 ($algebra) = @{ $algebra->children };
231 } elsif ($algebra->isa('Attean::Algebra::Construct')) {
232 3 $form = 'CONSTRUCT';
233 3 $modifiers{construct} = $algebra->triples;
234 3 ($algebra) = @{ $algebra->children };
235 }
236
237 33 ▶ unless ($form eq 'CONSTRUCT' or $form eq 'DESCRIBE') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($form eq "CONSTRUCT" or $form eq "DESCRIBE") 27 6 33
Condition100%3/3
$form eq "CONSTRUCT" or $form eq "DESCRIBE"
l!l&&r!l&&!r
3327
MC/DC100%2/2
$form eq "CONSTRUCT" or $form eq "DESCRIBE"
$form eq "CONSTRUCT" $form eq "DESCRIBE"
Truth table100%3/3
A or B
A= $form eq "CONSTRUCT"B= $form eq "DESCRIBE"
ABresult
1X1
011
000
238 27 ▶ while ($algebra->isa('Attean::Algebra::Extend') or $algebra->isa('Attean::Algebra::Group') or $algebra->isa('Attean::Algebra::OrderBy') or $algebra->isa('Attean::Algebra::Distinct') or $algebra->isa('Attean::Algebra::Reduced') or $algebra->isa('Attean::Algebra::Slice') or $algebra->isa('Attean::Algebra::Project')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group") or $algebra->isa("Attean::Algebra::OrderBy") or $algebra->isa("Attean::Algebra::Distinct") or $algebra->isa("Attean::Algebra::Reduced") or $algebra->isa("Attean::Algebra::Slice") or $algebra->isa("Attean::Algebra::Project")) 30 27 57
Condition100%18/18
$algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group") or $algebra->isa("Attean::Algebra::OrderBy") or $algebra->isa("Attean::Algebra::Distinct") or $algebra->isa("Attean::Algebra::Reduced") or $algebra->isa("Attean::Algebra::Slice") or $algebra->isa("Attean::Algebra::Project")
l!l&&r!l&&!r
181227
$algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group") or $algebra->isa("Attean::Algebra::OrderBy") or $algebra->isa("Attean::Algebra::Distinct") or $algebra->isa("Attean::Algebra::Reduced") or $algebra->isa("Attean::Algebra::Slice")
l!l&&r!l&&!r
12639
$algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group") or $algebra->isa("Attean::Algebra::OrderBy") or $algebra->isa("Attean::Algebra::Distinct") or $algebra->isa("Attean::Algebra::Reduced")
l!l&&r!l&&!r
10245
$algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group") or $algebra->isa("Attean::Algebra::OrderBy") or $algebra->isa("Attean::Algebra::Distinct")
l!l&&r!l&&!r
6447
$algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group") or $algebra->isa("Attean::Algebra::OrderBy")
l!l&&r!l&&!r
5151
$algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group")
l!l&&r!l&&!r
4152
MC/DC100%7/7
$algebra->isa("Attean::Algebra::Extend") or $algebra->isa("Attean::Algebra::Group") or $algebra->isa("Attean::Algebra::OrderBy") or $algebra->isa("Attean::Algebra::Distinct") or $algebra->isa("Attean::Algebra::Reduced") or $algebra->isa("Attean::Algebra::Slice") or $algebra->isa("Attean::Algebra::Project")
$algebra->isa("Attean::Algebra::Extend") $algebra->isa("Attean::Algebra::Group") $algebra->isa("Attean::Algebra::OrderBy") $algebra->isa("Attean::Algebra::Distinct") $algebra->isa("Attean::Algebra::Reduced") $algebra->isa("Attean::Algebra::Slice") $algebra->isa("Attean::Algebra::Project")
Truth table100%8/8
A or B or C or D or E or F or G
A= $algebra->isa("Attean::Algebra::Extend")B= $algebra->isa("Attean::Algebra::Group")C= $algebra->isa("Attean::Algebra::OrderBy")D= $algebra->isa("Attean::Algebra::Distinct")E= $algebra->isa("Attean::Algebra::Reduced")F= $algebra->isa("Attean::Algebra::Slice")G= $algebra->isa("Attean::Algebra::Project")
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
000001X1
00000011
00000000
239 # TODO: Handle HAVING
240 # TODO: Error if Slice appears before distinct/reduced
241 30 ▶ if ($algebra->isa('Attean::Algebra::Distinct')) {
Branch: 7 branches
BranchTrueFalseTotal
if ($algebra->isa('Attean::Algebra::Distinct')) { } 4 26 30
elsif ($algebra->isa('Attean::Algebra::Reduced')) { } 2 24 26
elsif ($algebra->isa('Attean::Algebra::Slice')) { } 6 18 24
elsif ($algebra->isa('Attean::Algebra::OrderBy')) { } 1 17 18
elsif ($algebra->isa('Attean::Algebra::Extend')) { } 4 13 17
elsif ($algebra->isa('Attean::Algebra::Project')) { } 12 1 13
elsif ($algebra->isa('Attean::Algebra::Group')) { } 1 0 1
242 4 $modifiers{ distinct } = 1;
243 } elsif ($algebra->isa('Attean::Algebra::Reduced')) {
244 2 $modifiers{ reduced } = 1;
245 } elsif ($algebra->isa('Attean::Algebra::Slice')) {
246 6 ▶ if ($algebra->limit >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->limit >= 0) 6 0 6
247 6 $modifiers{ limit } = $algebra->limit;
248 }
249 6 ▶ if ($algebra->offset > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->offset > 0) 4 2 6
250 4 $modifiers{ offset } = $algebra->offset;
251 }
252 } elsif ($algebra->isa('Attean::Algebra::OrderBy')) {
253 1 $modifiers{order} = $algebra->comparators;
254 } elsif ($algebra->isa('Attean::Algebra::Extend')) {
255 4 my $v = $algebra->variable;
256 4 my $name = $v->value;
257 4 my $expr = $algebra->expression;
258 4 my @tokens;
259 4 push(@tokens, $lparen);
260 4 push(@tokens, $expr->sparql_tokens->elements);
261 4 push(@tokens, $as);
262 4 push(@tokens, $v->sparql_tokens->elements);
263 4 push(@tokens, $rparen);
264 4 $modifiers{project_expression_tokens}{$name} = \@tokens;
265 4 ▶ unless ($modifiers{project_variables}{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($modifiers{"project_variables"}{$name}++) 0 4 4
266 0 unshift(@{ $modifiers{project_variables_order} }, $v->value);
267 }
268 } elsif ($algebra->isa('Attean::Algebra::Project')) {
269 12 my $vars = $algebra->variables;
270 12 my ($child) = @{ $algebra->children };
271 12 my @vars = sort(map { $_->value } @$vars);
272 12 my @subvars = sort($child->in_scope_variables);
273 12 ▶ if (scalar(@vars) == scalar(@subvars) and join('.', @vars) eq join('.', @subvars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @vars == scalar @subvars and join('.', @vars) eq join('.', @subvars)) { } 5 7 12
Condition100%3/3
scalar @vars == scalar @subvars and join(".", @vars) eq join(".", @subvars)
!ll&&!rl&&r
525
MC/DC100%2/2
scalar @vars == scalar @subvars and join(".", @vars) eq join(".", @subvars)
scalar @vars == scalar @subvars join(".", @vars) eq join(".", @subvars)
Truth table100%3/3
A and B
A= scalar @vars == scalar @subvarsB= join(".", @vars) eq join(".", @subvars)
ABresult
0X0
100
111
274 # this is a SELECT * query
275 } else {
276 7 foreach my $v (@$vars) {
277 13 my $name = $v->value;
278 13 ▶ unless ($modifiers{project_variables}{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($modifiers{"project_variables"}{$name}++) 13 0 13
279 13 push(@{ $modifiers{project_variables_order} }, $name);
280 }
281 }
282 }
283 } elsif ($algebra->isa('Attean::Algebra::Group')) {
284 1 my $aggs = $algebra->aggregates;
285 1 my $groups = $algebra->groupby;
286 1 foreach my $agg (@$aggs) {
287 1 my $v = $agg->variable;
288 1 my $name = $v->value;
289 1 my @tokens;
290 1 push(@tokens, $lparen);
291 1 push(@tokens, $agg->sparql_tokens->elements);
292 1 push(@tokens, $as);
293 1 push(@tokens, $v->sparql_tokens->elements);
294 1 push(@tokens, $rparen);
295 1 ▶ unless ($modifiers{project_variables}{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($modifiers{"project_variables"}{$name}++) 1 0 1
296 1 push(@{ $modifiers{project_variables_order} }, $name);
297 }
298 1 $modifiers{project_expression_tokens}{$name} = \@tokens;
299 }
300 1 foreach my $group (@$groups) {
301 1 push(@{ $modifiers{groups} }, $group->sparql_tokens->elements);
302 }
303 } else {
304 0 die "Unexpected pattern type encountered in query_tokens: " . ref($algebra);
305 }
306 30 ($algebra) = @{ $algebra->children };
307 }
308 }
309
310 33 my @tokens;
311  
312 33 my $where = AtteanX::SPARQL::Token->keyword('WHERE');
313 33 my $lbrace = AtteanX::SPARQL::Token->lbrace;
314 33 my $rbrace = AtteanX::SPARQL::Token->rbrace;
315  
316  
317 33 ▶ if ($form eq 'SELECT') {
Branch: 4 branches
BranchTrueFalseTotal
if ($form eq 'SELECT') { } 24 9 33
elsif ($form eq 'DESCRIBE') { } 3 6 9
elsif ($form eq 'CONSTRUCT') { } 3 3 6
elsif ($form eq 'ASK') { } 3 0 3
318 24 push(@tokens, AtteanX::SPARQL::Token->keyword('SELECT'));
319 24 ▶ if ($modifiers{distinct}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($modifiers{'distinct'}) { } 4 20 24
elsif ($modifiers{'reduced'}) { } 2 18 20
320 4 push(@tokens, AtteanX::SPARQL::Token->keyword('DISTINCT'));
321 } elsif ($modifiers{reduced}) {
322 2 push(@tokens, AtteanX::SPARQL::Token->keyword('REDUCED'));
323 }
324
325 24 ▶ if (my $p = $modifiers{project_variables_order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $p = $modifiers{'project_variables_order'}) { } 8 16 24
326 8 foreach my $name (@$p) {
327 14 ▶ if (my $etokens = $modifiers{project_expression_tokens}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $etokens = $modifiers{'project_expression_tokens'}{$name}) { } 5 9 14
328 5 push(@tokens, @$etokens);
329 } else {
330 9 my $v = Attean::Variable->new( value => $name );
331 9 push(@tokens, $v->sparql_tokens->elements);
332 }
333 }
334 } else {
335 16 push(@tokens, AtteanX::SPARQL::Token->star);
336 }
337
338 24 push(@tokens, $self->dataset_tokens($dataset)->elements);
339
340 24 push(@tokens, $where);
341 24 ▶ if ($algebra->isa('Attean::Algebra::Join')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($algebra->isa('Attean::Algebra::Join')) { } 1 23 24
342 # don't emit extraneous braces at the top-level
343 1 push(@tokens, $algebra->sparql_tokens->elements);
344 } else {
345 23 push(@tokens, $lbrace);
346 23 push(@tokens, $algebra->sparql_tokens->elements);
347 23 push(@tokens, $rbrace);
348 }
349 24 ▶ if (my $groups = $modifiers{groups}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $groups = $modifiers{"groups"}) 1 23 24
350 1 push(@tokens, AtteanX::SPARQL::Token->keyword('GROUP'));
351 1 push(@tokens, AtteanX::SPARQL::Token->keyword('BY'));
352 1 push(@tokens, @$groups);
353 }
354 24 ▶ if (my $expr = $modifiers{having}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $expr = $modifiers{"having"}) 0 24 24
355 0 push(@tokens, AtteanX::SPARQL::Token->keyword('HAVING'));
356 0 push(@tokens, $expr->sparql_tokens->elements);
357 }
358 24 ▶ if (my $comps = $modifiers{order}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $comps = $modifiers{"order"}) 1 23 24
359 1 push(@tokens, AtteanX::SPARQL::Token->keyword('ORDER'));
360 1 push(@tokens, AtteanX::SPARQL::Token->keyword('BY'));
361 1 foreach my $c (@$comps) {
362 1 push(@tokens, $c->sparql_tokens->elements);
363 }
364 }
365 24 ▶ if (exists $modifiers{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $modifiers{"limit"}) 6 18 24
366 6 push(@tokens, AtteanX::SPARQL::Token->keyword('LIMIT'));
367 6 push(@tokens, AtteanX::SPARQL::Token->integer($modifiers{limit}));
368 }
369 24 ▶ if (exists $modifiers{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $modifiers{"offset"}) 4 20 24
370 4 push(@tokens, AtteanX::SPARQL::Token->keyword('OFFSET'));
371 4 push(@tokens, AtteanX::SPARQL::Token->integer($modifiers{offset}));
372 }
373 } elsif ($form eq 'DESCRIBE') {
374 3 push(@tokens, AtteanX::SPARQL::Token->keyword('DESCRIBE'));
375 3 foreach my $t (@{ $modifiers{describe} }) {
376 5 push(@tokens, $t->sparql_tokens->elements);
377 }
378 3 push(@tokens, $self->dataset_tokens($dataset)->elements);
379 3 push(@tokens, $where);
380 3 push(@tokens, $lbrace);
381 3 push(@tokens, $algebra->sparql_tokens->elements);
382 3 push(@tokens, $rbrace);
383 } elsif ($form eq 'CONSTRUCT') {
384 3 push(@tokens, AtteanX::SPARQL::Token->keyword('CONSTRUCT'));
385 3 push(@tokens, $lbrace);
386 3 foreach my $t (@{ $modifiers{construct} }) {
387 3 push(@tokens, $t->sparql_tokens->elements);
388 3 push(@tokens, AtteanX::SPARQL::Token->dot);
389 }
390 3 push(@tokens, $rbrace);
391 3 push(@tokens, $self->dataset_tokens($dataset)->elements);
392 3 push(@tokens, $where);
393 3 push(@tokens, $lbrace);
394 3 push(@tokens, $algebra->sparql_tokens->elements);
395 3 push(@tokens, $rbrace);
396 } elsif ($form eq 'ASK') {
397 3 push(@tokens, AtteanX::SPARQL::Token->keyword('ASK'));
398 3 push(@tokens, $self->dataset_tokens($dataset)->elements);
399 3 push(@tokens, $lbrace);
400 3 push(@tokens, $algebra->sparql_tokens->elements);
401 3 push(@tokens, $rbrace);
402 } else {
403 0 die "Unexpected query for '$form' in query_tokens";
404 }
405 33 return Attean::ListIterator->new( values => \@tokens, item_type => 'AtteanX::SPARQL::Token' );
406 }
407 }
408  
409 50 package Attean::API::SPARQLQuerySerializable 0.040 {
410 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
411 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
412 50 with 'Attean::API::SPARQLSerializable';
413  
414 26 ▶ sub sparql_tokens {
Subroutine sparql_tokens: called CC=1 SCAR=0.0
Pod undocumented
415 26 my $self = shift;
416 26 return $self->query_tokens;
417 }
418 }
419  
420 =item * L<Attean::API::Algebra>
421  
422 =cut
423  
424 50 package Attean::API::Algebra 0.040 {
425 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
426 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
427
428 50 with 'Attean::API::SPARQLSerializable';
429 511 ▶ has 'hints' => (is => 'rw', isa => ArrayRef[ArrayRef[ConsumerOf['Attean::API::Term']]], default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
430
431 50 requires 'as_sparql';
432 50 requires 'in_scope_variables'; # variables that will be in-scope after this operation is evaluated
433
434 2 ▶ sub unary {
Subroutine unary: called CC=2 SCAR=6.9
Pod undocumented
435 2 my $self = shift;
436 2 ▶ return unless (scalar(@{ $self->children }) == 1);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->children;} == 1 1 1 2
437 1 return $self->children->[0];
438 }
439
440 0 ▶ sub algebra_as_string {
Subroutine algebra_as_string: not called CC=1 SCAR=6.9
Pod undocumented
441 0 my $self = shift;
442 0 return "$self";
443 }
444
445 20 ▶ sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod undocumented
446 20 my $self = shift;
447 20 my $string = '';
448 74 ▶ $self->walk( prefix => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
449 74 my $a = shift;
450 74 my $level = shift;
451 74 my $parent = shift;
452 74 my $indent = ' ' x $level;
453 74 $string .= "-$indent " . $a->algebra_as_string($level) . "\n";
454 20 });
455 20 return $string;
456 }
457
458 4 ▶ sub blank_nodes {
Subroutine blank_nodes: called CC=1 SCAR=2.1
Pod undocumented
459 4 my $self = shift;
460 4 my %blanks;
461 4 ▶ $self->walk( prefix => sub {
Subroutine __ANON__: called CC=5 SCAR=27.8
462 4 my $a = shift;
463 4 ▶ if ($a->isa('Attean::Algebra::BGP')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($a->isa('Attean::Algebra::BGP')) { } 0 4 4
elsif ($a->isa('Attean::Algebra::Path')) { } 0 4 4
464 0 my @triples = @{ $a->triples };
465 0 my @nodes = grep { $_->does('Attean::API::Blank') } map { $_->values } @triples;
466 0 foreach my $b (@nodes) {
467 0 $blanks{ $b->value } = $b;
468 }
469 } elsif ($a->isa('Attean::Algebra::Path')) {
470 0 my @nodes = grep { $_->does('Attean::API::Blank') } ($a->subject, $a->object);
471 0 foreach my $b (@nodes) {
472 0 $blanks{ $b->value } = $b;
473 }
474 }
475 4 });
476 4 return values %blanks;
477 }
478
479 500 ▶ sub BUILD {}
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
480 50 ▶ if ($ENV{ATTEAN_TYPECHECK}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"ATTEAN_TYPECHECK"}) 0 50 50
481 0 ▶ around 'BUILD' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
482 0 my $orig = shift;
483 0 my $self = shift;
484 0 $self->$orig(@_);
485 0 my $name = ref($self);
486 0 $name =~ s/^.*://;
487 0 ▶ if ($self->can('arity')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("arity")) 0 0 0
488 0 my $arity = $self->arity;
489 0 my $children = $self->children;
490 0 my $size = scalar(@$children);
491 0 ▶ unless ($size == $arity) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size == $arity) 0 0 0
492 0 Carp::confess "${name} algebra construction with bad number of children (expected $arity, but got $size)";
493 }
494 }
495 }
496 0 }
497 }
498  
499 =item * L<Attean::API::QueryTree>
500  
501 =cut
502  
503 50 package Attean::API::QueryTree 0.040 {
504 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
505 50 with 'Attean::API::DirectedAcyclicGraph';
506 }
507  
508 =item * L<Attean::API::NullaryQueryTree>
509  
510 =cut
511  
512 50 package Attean::API::NullaryQueryTree 0.040 {
513 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
514 1 ▶ sub arity { return 0 }
Subroutine arity: called CC=1 SCAR=0.0
Pod undocumented
515 50 with 'Attean::API::QueryTree';
516 }
517  
518 =item * L<Attean::API::UnaryQueryTree>
519  
520 =cut
521  
522 50 package Attean::API::UnaryQueryTree 0.040 {
523 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
524 2 ▶ sub arity { return 1 }
Subroutine arity: called CC=1 SCAR=0.0
Pod undocumented
525 50 with 'Attean::API::QueryTree';
526
527 36 ▶ sub child {
Subroutine child: called CC=1 SCAR=0.0
Pod undocumented
528 36 my $self = shift;
529 36 return $self->children->[0];
530 }
531 }
532  
533 =item * L<Attean::API::BinaryQueryTree>
534  
535 =cut
536  
537 50 package Attean::API::BinaryQueryTree 0.040 {
538 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
539 0 ▶ sub arity { return 2 }
Subroutine arity: not called CC=1 SCAR=6.9
Pod undocumented
540 50 with 'Attean::API::QueryTree';
541 }
542  
543 =item * L<Attean::API::PropertyPath>
544  
545 =cut
546  
547 50 package Attean::API::PropertyPath 0.040 {
548 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
549 50 with 'Attean::API::QueryTree';
550 50 requires 'as_string';
551 50 requires 'as_sparql';
552 }
553  
554 =item * L<Attean::API::UnaryPropertyPath>
555  
556 =cut
557  
558 50 package Attean::API::UnaryPropertyPath 0.040 {
559 50 ▶ use Types::Standard qw(ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
560  
561 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
562  
563 0 ▶ sub arity { return 1 }
Subroutine arity: not called CC=1 SCAR=6.9
Pod undocumented
564 # has 'path' => (is => 'ro', isa => ConsumerOf['Attean::API::PropertyPath'], required => 1);
565 3 ▶ sub prefix_name { "" }
Subroutine prefix_name: called CC=1 SCAR=0.0
Pod undocumented
566 4 ▶ sub postfix_name { "" }
Subroutine postfix_name: called CC=1 SCAR=0.0
Pod undocumented
567 5 ▶ sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod undocumented
568 5 my $self = shift;
569 5 my ($path) = @{ $self->children };
570 5 my $pstr = $path->as_string;
571 5 ▶ if ($path->does('Attean::API::UnaryPropertyPath')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path->does("Attean::API::UnaryPropertyPath")) 1 4 5
572 1 $pstr = "($pstr)";
573 }
574 5 my $str = sprintf("%s%s%s", $self->prefix_name, $pstr, $self->postfix_name);
575 5 return $str;
576 }
577 2 ▶ sub algebra_as_string {
Subroutine algebra_as_string: called CC=1 SCAR=0.0
Pod undocumented
578 2 my $self = shift;
579 2 return "Property Path " . $self->prefix_name . $self->postfix_name;
580 }
581 50 with 'Attean::API::PropertyPath', 'Attean::API::UnaryQueryTree';
582 }
583  
584 =item * L<Attean::API::NaryPropertyPath>
585  
586 =cut
587  
588 50 package Attean::API::NaryPropertyPath 0.040 {
589 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf);
Subroutine BEGIN: called CC=1 SCAR=0.0
590  
591 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
592  
593 # has 'children' => (is => 'ro', isa => ArrayRef[ConsumerOf['Attean::API::PropertyPath']], required => 1);
594 50 requires 'separator';
595 7 ▶ sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod undocumented
596 7 my $self = shift;
597 7 my @children = @{ $self->children };
598 7 ▶ if (scalar(@children) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @children == 1) { } 2 5 7
599 2 return $children[0]->as_string;
600 } else {
601 5 return sprintf("(%s)", join($self->separator, map { $_->as_string } @children));
602 }
603 }
604 2 ▶ sub algebra_as_string {
Subroutine algebra_as_string: called CC=1 SCAR=0.0
Pod undocumented
605 2 my $self = shift;
606 2 return "Property Path " . $self->separator;
607 }
608 50 with 'Attean::API::PropertyPath';
609 }
610  
611 =item * L<Attean::API::UnionScopeVariables>
612  
613 =cut
614  
615 50 package Attean::API::UnionScopeVariables 0.040 {
616 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
617 22 ▶ sub in_scope_variables {
Subroutine in_scope_variables: called CC=2 SCAR=6.9
Pod undocumented
618 22 my $self = shift;
619 22 my $set = Set::Scalar->new();
620 22 foreach my $c (@{ $self->children }) {
621 29 $set->insert( $c->in_scope_variables );
622 }
623 22 return $set->elements;
624 }
625 }
626  
627 =item * L<Attean::API::IntersectionScopeVariables>
628  
629 =cut
630  
631 50 package Attean::API::IntersectionScopeVariables 0.040 {
632 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
633 0 ▶ sub in_scope_variables {
Subroutine in_scope_variables: not called CC=3 SCAR=24.8
Pod undocumented
634 0 my $self = shift;
635 0 my @c = @{ $self->children };
636 0 ▶ return unless scalar(@c);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @c 0 0 0
637 0 my $set = Set::Scalar->new(shift(@c)->in_scope_variables);
638 0 foreach my $c (@c) {
639 0 my $rhs = Set::Scalar->new($c->in_scope_variables);
640 0 $set = $set->intersection($rhs);
641 }
642 0 return $set->elements;
643 }
644 }
645  
646 50 1;
647  
648 =back
649  
650 =head1 BUGS
651  
652 Please report any bugs or feature requests to through the GitHub web interface
653 at L<https://github.com/kasei/attean/issues>.
654  
655 =head1 SEE ALSO
656  
657  
658  
659 =head1 AUTHOR
660  
661 Gregory Todd Williams C<< <gwilliams@cpan.org> >>
662  
663 =head1 COPYRIGHT
664  
665 Copyright (c) 2014--2026 Gregory Todd Williams.
666 This program is free software; you can redistribute it and/or modify it under
667 the same terms as Perl itself.
668  
669 =cut
« Attean/API/Binding.pm ↑ Summary Attean/API.pm »