Attean.pm

filter: Statementstmt 89.1% 287 / 322 Branchbran 79.6% 94 / 118 Conditioncond 68.7% 22 / 32 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 86.5% 45 / 52 Podpod 100% 18 / 18 totaltotal 84.8% 477 / 562 CC 102 SCAR 24.7
CC
102
Cov
85%
negotiate_serializer
27.0
import
17.9
acceptable_parsers
15.0
CRAP 11.8
« Attean/TreeRewriter.pm ↑ Summary Attean/API/Model.pm »
1 =head1 NAME
2  
3 Attean - A Semantic Web Framework
4  
5 =head1 VERSION
6  
7 This document describes Attean version 0.041
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 use Attean::RDF qw(iri);
13
14 my $store = Attean->get_store('Memory')->new();
15 my $parser = Attean->get_parser('NTriples')->new();
16
17 # iterator of triples and quads
18 my $iter = $parser->parse_iter_from_io(\*STDIN);
19
20 # add a graph name to all triples
21 my $graph = iri('http://graph-name/');
22 my $quads = $iter->as_quads($graph);
23
24 $store->add_iter($quads);
25 my $model = Attean::QuadModel->new( store => $store );
26 my $iter = $model->get_quads();
27 while (my $quad = $iter->next) {
28 say $quad->object->ntriples_string;
29 }
30  
31 # run a SPARQL query and iterate over the results
32 my $sparql = 'SELECT * WHERE { ?s ?p ?o }';
33 my $s = Attean->get_parser('SPARQL')->new();
34 my ($algebra) = $s->parse($sparql);
35 my $results = $model->evaluate($algebra, $graph);
36 while (my $r = $results->next) {
37 say $r->as_string;
38 }
39  
40 =head1 DESCRIPTION
41  
42 Attean provides APIs for parsing, storing, querying, and serializing
43 Semantic Web (RDF and SPARQL) data.
44  
45 =head1 METHODS
46  
47 =over 4
48  
49 =cut
50  
51 51 package Attean {
52 51 ▶ use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 51 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 51 our $VERSION = '0.041';
55 51 ▶ use Attean::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
56
57 51 ▶ use Attean::Blank;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 51 ▶ use Attean::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 51 ▶ use Attean::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 51 ▶ use Attean::IRI;
Subroutine BEGIN: called CC=1 SCAR=0.0
61
62 51 ▶ use Attean::Triple;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 51 ▶ use Attean::Quad;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 51 ▶ use Attean::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
65
66 51 ▶ use Attean::QuadModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 51 ▶ use Attean::TripleModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 51 ▶ use Attean::BindingEqualityTest;
Subroutine BEGIN: called CC=1 SCAR=0.0
69
70 51 ▶ use Attean::CodeIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 51 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 51 ▶ use Attean::IteratorSequence;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 51 ▶ use Attean::IDPQueryPlanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
75
76 51 ▶ use Attean::TermMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
77
78 51 ▶ use HTTP::Negotiate qw(choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
79 51 ▶ use List::Util qw(any all);
Subroutine BEGIN: called CC=1 SCAR=0.0
80 51 ▶ use Module::Load::Conditional qw(can_load);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 51 ▶ use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
82 51 ▶ use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
83 51 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
84
85 51 ▶ use Module::Pluggable search_path => 'AtteanX::Parser', sub_name => 'parsers', max_depth => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 51 ▶ use Module::Pluggable search_path => 'AtteanX::Serializer', sub_name => 'serializers', max_depth => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 51 ▶ use Module::Pluggable search_path => 'AtteanX::Store', sub_name => 'stores', max_depth => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
88
89 149 ▶ sub import {
Subroutine import: called CC=11 SCAR=28.8
90 149 my $class = shift;
91 149 ▶ if (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 3 146 149
92 3 my %args = @_;
93 3 ▶ foreach my $p (@{ $args{parsers} || [] }) {
Condition50%1/2
$args{'parsers'} || []
l!l
30
MC/DC0%0/1
$args{'parsers'} || []
$args{'parsers'}
Truth table50%1/2
A
A= $args{'parsers'}
Aresult
11
00
94 # warn "Loading $p parser...";
95 3 ▶ $class->get_parser($p) || die "Failed to load parser: $p";
Branch: 1 branch
BranchTrueFalseTotal
$class->get_parser($p) or die "Failed to load parser: $p" 3 0 3
96 }
97 3 ▶ foreach my $s (@{ $args{serializers} || [] }) {
Condition50%1/2
$args{'serializers'} || []
l!l
03
MC/DC0%0/1
$args{'serializers'} || []
$args{'serializers'}
Truth table50%1/2
A
A= $args{'serializers'}
Aresult
11
00
98 # warn "Loading $s serializer...";
99 0 ▶ $class->get_serializer($s) || die "Failed to load serializer: $s";
Branch: 1 branch
BranchTrueFalseTotal
$class->get_serializer($s) or die "Failed to load serializer: $s" 0 0 0
100 }
101 3 ▶ foreach my $s (@{ $args{stores} || [] }) {
Condition50%1/2
$args{'stores'} || []
l!l
03
MC/DC0%0/1
$args{'stores'} || []
$args{'stores'}
Truth table50%1/2
A
A= $args{'stores'}
Aresult
11
00
102 # warn "Loading $s store...";
103 0 ▶ $class->get_store($s) || die "Failed to load store: $s";
Branch: 1 branch
BranchTrueFalseTotal
$class->get_store($s) or die "Failed to load store: $s" 0 0 0
104 }
105 }
106 }
107
108 =item C<< get_store( $NAME ) >>
109  
110 Attempts to find a L<Attean::API::Store> implementation with the
111 given C<< $NAME >>. This is done using L<Module::Pluggable> and will generally
112 be searching for class names C<< AtteanX::Store::$NAME >>.
113  
114 Returns the full class name if a matching implementation is found, otherwise
115 returns undef.
116  
117 =cut
118  
119 76 ▶ sub get_store {
Subroutine get_store: called CC=1 SCAR=0.0
Pod documented
120 76 my $self = shift;
121 76 return $self->_get_plugin('stores', shift);
122 }
123  
124 =item C<< parse_store_args ( $string ) >>
125  
126 Parses the configure C<$string> and returns a list C<($type, \%args, \@files)>
127 containing the store type (suitable for passing to C<get_store>) and constructor
128 arguments for creating an L<Attean::API::Store> object. This string configuration
129 format is suitable for use when only a single scalar value can be used to indicate
130 what store is to be used.
131  
132 The format is comprised of between one and three components joined by ';':
133  
134 * The name of the store type
135 * A list of key=value pairs (joined by ',')
136 * A list of filenames joined by ',' (this can be used by callers to load
137 initial data into the store, but the specific format and use is unspecified)
138  
139 Examples:
140  
141 * Memory;foo=bar,baz=quux;file1,file2
142 * DBI;type=mysql,database=db,user=test,password=pw,host=127.0.0.1,port=3306
143 * DBI;type=sqlite,database=test.sqlite
144  
145 =cut
146  
147 0 ▶ sub parse_store_args {
Subroutine parse_store_args: not called CC=3 SCAR=24.8
Pod documented
148 0 my $class = shift;
149 0 my $conf = shift;
150 0 my ($name, $args, $files) = split(';', $conf, 3);
151 0 ▶ my @files = defined($files) ? split(',', $files) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $files ? : 0 0 0
152 0 ▶ my @args = defined($args) ? split(',', $args) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $args ? : 0 0 0
153 0 my %args = map { split(/=/, $_, 2) } @args;
154 0 return ($name, \%args, \@files);
155 }
156  
157 =item C<< temporary_model >>
158  
159 Returns a temporary, mutable quad model based on a L<AtteanX::Store::Memory> store.
160  
161 =cut
162  
163 3 ▶ sub temporary_model {
Subroutine temporary_model: called CC=1 SCAR=0.0
Pod documented
164 3 my $self = shift;
165 3 return Attean::MutableQuadModel->new( store => $self->get_store('Memory')->new() )
166 }
167  
168  
169  
170 =item C<< get_serializer( $NAME ) >>
171  
172 =item C<< get_serializer( filename => $FILENAME ) >>
173  
174 =item C<< get_serializer( media_type => $MEDIA_TYPE ) >>
175  
176 Attempts to find a L<Attean::API::Serializer> serializer class with the given
177 C<< $NAME >>, or that can serialize files with the C<< $MEDIA_TYPE >> media
178 type.
179  
180 Returns the full class name if a matching implementation is found, otherwise
181 returns undef.
182  
183 =cut
184  
185 67 ▶ sub get_serializer {
Subroutine get_serializer: called CC=12 SCAR=24.9
Pod documented
186 67 my $self = shift;
187 67 my $role = 'Attean::API::Serializer';
188  
189 67 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 48 19 67
190 48 my $name = shift;
191 48 my $p = $self->_get_plugin('serializers', $name, $role);
192 48 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 33 15 48
193
194 15 foreach my $type (qw'filename media_type') {
195 15 my $p = $self->get_serializer($type => $name);
196 15 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 15 0 15
197 }
198 0 return;
199 }
200 19 my $type = shift;
201 19 my %method = (filename => 'file_extensions', media_type => 'media_types');
202 19 ▶ if (my $method = $method{ $type }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $method{$type}) { } 18 1 19
203 18 my $value = shift;
204 18 ▶ $value =~ s/^.*[.]// if ($type eq 'filename');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "filename" 15 3 18
205 18 ▶ $value =~ s/;.*$// if ($type eq 'media_type');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "media_type" 3 15 18
206 18 foreach my $p ($self->serializers()) {
207 127 ▶ if (can_load( modules => { $p => 0 })) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_load("modules", {$p, 0})) 127 0 127
208 127 ▶ next unless ($p->does($role));
Branch: 1 branch
BranchTrueFalseTotal
unless $p->does($role) 14 113 127
209 113 my @exts = @{ $p->$method() };
210 113 ▶ return $p if (any { $value eq $_ } @exts);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $value eq $_; } , @exts) 18 95 113
211 }
212 }
213 0 return;
214 } else {
215 1 die "Not a valid constraint in get_serializer call: $type";
216 }
217 }
218
219 =item C<< get_parser( $NAME ) >>
220  
221 =item C<< get_parser( filename => $FILENAME ) >>
222  
223 =item C<< get_parser( media_type => $MEDIA_TYPE ) >>
224  
225 Attempts to find a L<Attean::API::Parser> parser class with the given
226 C<< $NAME >>, or that can parse files with the same extension as
227 C<< $FILENAME >>, or that can parse files with the C<< $MEDIA_TYPE >> media
228 type.
229  
230 Returns the full class name if a matching implementation is found, otherwise
231 returns undef.
232  
233 =cut
234  
235 190 ▶ sub get_parser {
Subroutine get_parser: called CC=14 SCAR=26.7
Pod documented
236 190 my $self = shift;
237 190 my $role = 'Attean::API::Parser';
238
239 190 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 174 16 190
240 174 my $name = shift;
241 174 my $p = $self->_get_plugin('parsers', $name, $role);
242 174 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 163 11 174
243
244 11 foreach my $type (qw'filename media_type') {
245 11 my $p = $self->get_parser($type => $name);
246 11 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 11 0 11
247 }
248 0 return;
249 }
250
251 16 ▶ while (my $type = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = shift()) 16 0 16
252 16 my %method = (filename => 'file_extensions', media_type => 'media_types');
253 16 ▶ if (my $method = $method{ $type }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $method{$type}) { } 15 1 16
254 15 my $value = shift;
255 15 ▶ $value =~ s/^.*[.]// if ($type eq 'filename');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "filename" 12 3 15
256 15 ▶ $value =~ s/;.*$// if ($type eq 'media_type');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "media_type" 3 12 15
257 15 foreach my $p ($self->parsers()) {
258 82 ▶ if (can_load( modules => { $p => 0 })) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_load("modules", {$p, 0})) 82 0 82
259 82 ▶ next unless ($p->can('does') and $p->does($role));
Branch: 1 branch
BranchTrueFalseTotal
unless $p->can("does") and $p->does($role) 11 71 82
Condition66%2/3
$p->can("does") and $p->does($role)
!ll&&!rl&&r
01171
MC/DC50%1/2
$p->can("does") and $p->does($role)
$p->can("does") $p->does($role)
Truth table66%2/3
A and B
A= $p->can("does")B= $p->does($role)
ABresult
0X0
100
111
260 71 my @exts = @{ $p->$method() };
261 71 ▶ return $p if (any { $value eq $_ } @exts);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $value eq $_; } , @exts) 15 56 71
262 }
263 }
264 } else {
265 1 die "Not a valid constraint in get_parser call: $type";
266 }
267 }
268 0 return;
269 }
270
271 {
272 51 my %roles = (
273 serializers => 'Attean::API::Serializer',
274 parsers => 'Attean::API::Parser',
275 stores => 'Attean::API::Store',
276 );
277 51 for my $method (keys %roles) {
278 153 my $role = $roles{$method};
279 0 ▶ my $code = sub {
Subroutine list_parsers: not called CC=5 SCAR=22.8
Subroutine list_stores: called CC=5 SCAR=22.8
280 0 my $self = shift;
281 0 my @classes;
282 0 foreach my $class ($self->$method()) {
283 0 ▶ next unless (can_load( modules => { $class => 0 }));
Branch: 2 branches
BranchTrueFalseTotal
unless can_load("modules", {$class, 0}) 0 0 0
unless can_load("modules", {$class, 0}) 0 234 234
284 0 ▶ push(@classes, $class) if ($class->can('does') and $class->does($role));
Branch: 2 branches
BranchTrueFalseTotal
if $class->can("does") and $class->does($role) 0 0 0
if $class->can("does") and $class->does($role) 216 18 234
Condition33%2/6
$class->can("does") and $class->does($role)
!ll&&!rl&&r
000
$class->can("does") and $class->does($role)
!ll&&!rl&&r
018216
MC/DC0%0/2
$class->can("does") and $class->does($role)
$class->can("does") $class->does($role)
MC/DC50%1/2
$class->can("does") and $class->does($role)
$class->can("does") $class->does($role)
Truth table0%0/3
A and B
A= $class->can("does")B= $class->does($role)
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $class->can("does")B= $class->does($role)
ABresult
0X0
100
111
285 }
286 0 return @classes;
287 153 };
288 153 Sub::Install::install_sub({
289 code => set_subname("list_${method}", $code),
290 as => "list_${method}"
291 });
292 }
293 }
294
295 298 ▶ sub _get_plugin {
Subroutine _get_plugin: called CC=6 SCAR=19.0
296 298 my $self = shift;
297 298 my $type = shift;
298 298 my $name = shift;
299 298 my @roles = @_;
300 298 foreach my $p ($self->$type()) {
301 1870 ▶ if (lc(substr($p, -(length($name)+2))) eq lc("::$name")) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc substr($p, -(length($name) + 2)) eq lc "::$name") 272 1598 1870
302 272 ▶ unless (can_load( modules => { $p => 0 })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", {$p, 0})) 0 272 272
303 0 warn $Module::Load::Conditional::ERROR;
304 0 return;
305 }
306
307 272 foreach (@roles) {
308 196 ▶ unless ($p->does($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->does($_)) 0 196 196
309 0 die ucfirst($type) . " class $p failed validation for role $_";
310 }
311 }
312 272 return $p;
313 }
314 }
315 }
316
317 =item C<< negotiate_serializer ( request_headers => $request_headers, restrict => \@serializer_names, extend => \%media_types, role => $role ) >>
318  
319 Returns a two-element list containing an appropriate media type and
320 L<Attean::API::Serializer> class as decided by L<HTTP::Negotiate>. If the
321 C<< 'request_headers' >> key-value is supplied, the C<< $request_headers >> is
322 passed to C<< HTTP::Negotiate::choose >>.
323  
324 The option C<< 'restrict' >>, set to a list of serializer names, can be used to
325 limit the serializers to choose from.
326  
327 The option C<< 'role' >> can be used to limit the serializers to only ones that
328 consume the $role. For example, C<< 'Attean::API::ResultSerializer' >> would
329 limit negotiation to result serializers such as SPARQL/XML and SPARQL/JSON,
330 while C<< 'Attean::API::TripleSerializer' >> would limit negotiation to RDF
331 serializers.
332  
333 Finally, an C<<'extend'>> option can be set to a hashref that contains
334 MIME-types as keys and a custom variant as value. This will enable the user to
335 use this negotiator to return a type that isn't supported by any serializers.
336 The subsequent code will have to find out how to return a representation.
337  
338 =cut
339  
340 12 ▶ sub negotiate_serializer {
Subroutine negotiate_serializer: called CC=27 SCAR=33.0
Pod documented
341 12 my $class = shift;
342 12 my %options = @_;
343 12 my $headers = delete $options{ 'request_headers' };
344 12 my $role = delete $options{ 'role' };
345 12 my $restrict = delete $options{ 'restrict' };
346 12 ▶ my $extend = delete $options{ 'extend' } || {};
Condition100%2/2
delete $options{'extend'} || {}
l!l
210
MC/DC100%1/1
delete $options{'extend'} || {}
delete $options{'extend'}
Truth table100%2/2
A
A= delete $options{'extend'}
Aresult
11
00
347 12 my %serializer_names;
348 my %media_types;
349 12 foreach my $sclass ($class->list_serializers) {
350 156 my $name = $sclass =~ s/^.*://r;
351 156 $serializer_names{lc($name)} = $sclass;
352 156 for (@{ $sclass->media_types }) {
353 180 push(@{ $media_types{$_} }, $sclass);
354 }
355 }
356 12 my %sclasses;
357 12 ▶ if (ref($restrict) && ref($restrict) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $restrict and ref $restrict eq 'ARRAY') { } 3 9 12
Condition66%2/3
ref $restrict and ref $restrict eq "ARRAY"
!ll&&!rl&&r
903
MC/DC50%1/2
ref $restrict and ref $restrict eq "ARRAY"
ref $restrict ref $restrict eq "ARRAY"
Truth table66%2/3
A and B
A= ref $restrictB= ref $restrict eq "ARRAY"
ABresult
0X0
100
111
358 3 foreach (@$restrict) {
359 4 ▶ if (my $sclass = $serializer_names{lc($_)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sclass = $serializer_names{lc $_}) 3 1 4
360 3 $sclasses{ $sclass } = 1;
361 }
362 }
363 } else {
364 9 %sclasses = reverse %serializer_names;
365 }
366 12 ▶ if (defined($role)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $role) 3 9 12
367 3 foreach my $sclass (keys %sclasses) {
368 39 ▶ unless ($sclass->does($role)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sclass->does($role)) 25 14 39
369 25 delete $sclasses{ $sclass };
370 }
371 }
372 }
373
374 12 my @default_variants;
375 12 ▶ while (my($type, $sclasses) = each(%media_types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %media_types) 132 12 144
376 132 foreach my $sclass (@$sclasses) {
377 180 ▶ next unless $sclasses{$sclass};
Branch: 1 branch
BranchTrueFalseTotal
unless $sclasses{$sclass} 67 113 180
378 113 my $qv;
379 # slightly prefer turtle as a readable format to others
380 # try hard to avoid using ntriples as 'text/plain' isn't very useful for conneg
381 113 ▶ if ($type eq 'application/n-triples') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'application/n-triples') { } 18 95 113
elsif ($type eq 'text/plain') { } 25 70 95
382 18 $qv = 1.0;
383 } elsif ($type eq 'text/plain') {
384 25 $qv = 0.2;
385 } else {
386 70 $qv = 0.99;
387 70 ▶ $qv -= 0.02 if ($type =~ m#/html#); # prefer data formats to HTML
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[/html]u 7 63 70
388 70 ▶ $qv -= 0.05 if ($type =~ m#/x-#); # prefer non experimental media types
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[/x-]u 0 70 70
389 70 ▶ $qv -= 0.01 if ($type =~ m#^application/(?!rdf[+]xml|sparql)#); # prefer standard rdf/xml to other application/* formats
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^application/(?!rdf[+]xml|sparql)]u 6 64 70
390 70 ▶ $qv -= 0.04 if ($type =~ m#^text/(?!turtle)#);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^text/(?!turtle)]u 21 49 70
391 70 ▶ $qv += 0.01 if ($type =~ m#^text/turtle#);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^text/turtle]u 15 55 70
392 70 ▶ $qv += 0.01 if ($type =~ m#^application/sparql-results[+]json#);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^application/sparql-results[+]json]u 7 63 70
393 }
394 113 push(@default_variants, [$type, $qv, $type]);
395 }
396 }
397
398 12 my %custom_thunks;
399 my @custom_variants;
400 12 ▶ while (my($type,$thunk) = each(%$extend)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$extend) 3 12 15
401 3 push(@custom_variants, [$thunk, 1.0, $type]);
402 3 $custom_thunks{ $thunk } = [$type, $thunk];
403 }
404
405 # remove variants with media types that are in custom_variants from @variants
406 12 my @variants = grep { not exists $extend->{ $_->[2] } } @default_variants;
407 12 push(@variants, @custom_variants);
408
409 12 my $stype = choose( \@variants, $headers );
410 12 ▶ if (defined($stype) and $custom_thunks{ $stype }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stype and $custom_thunks{$stype}) 2 10 12
Condition100%3/3
defined $stype and $custom_thunks{$stype}
!ll&&!rl&&r
282
MC/DC100%2/2
defined $stype and $custom_thunks{$stype}
defined $stype $custom_thunks{$stype}
Truth table100%3/3
A and B
A= defined $stypeB= $custom_thunks{$stype}
ABresult
0X0
100
111
411 2 my $thunk = $stype;
412 2 my $type = $custom_thunks{ $stype }[0];
413 2 return ($type, $thunk);
414 }
415
416 10 ▶ if (defined($stype) and my $sclasses = $media_types{ $stype }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stype and my $sclasses = $media_types{$stype}) { } 8 2 10
Condition66%2/3
defined $stype and my $sclasses = $media_types{$stype}
!ll&&!rl&&r
208
MC/DC50%1/2
defined $stype and my $sclasses = $media_types{$stype}
defined $stype my $sclasses = $media_types{$stype}
Truth table66%2/3
A and B
A= defined $stypeB= my $sclasses = $media_types{$stype}
ABresult
0X0
100
111
417 8 return ($stype, $sclasses->[0]);
418 } else {
419 2 die "No appropriate serializer found for content-negotiation: " . Data::Dumper->Dump([$headers, $restrict, $extend], [qw(headers restrict extend)]);
420 }
421 }
422
423 =item C<< acceptable_parsers ( handles => $item_role, prefer => $parser_role ) >>
424  
425 Returns a string value expressing the media types that are acceptable to the
426 parsers available to the system. This string may be used as an 'Accept' HTTP
427 header value.
428  
429 If a C<< handles >> role is supplied, only parsers that produce objects that
430 conform to C<< $item_role >> will be included.
431  
432 If a C<< prefer >> role is supplied, only parsers that conform to
433 C<< $parser_role >> will be included.
434  
435 Parsers are given a quality-value (expressing a preferred order or use) based
436 on the roles each parser consumes. Parsers consuming L<Attean::API::PullParser>
437 are preferred, while those consuming L<Attean::API::AtOnceParser> are not
438 preferred. An exact ordering between parsers consuming similar roles is
439 currently undefined.
440  
441 =cut
442  
443 6 ▶ sub acceptable_parsers {
Subroutine acceptable_parsers: called CC=15 SCAR=27.1
Pod documented
444 6 my $class = shift;
445 6 my %options = @_;
446 6 my $handles = delete $options{ 'handles' };
447 6 my $prefer = delete $options{ 'prefer' };
448  
449 6 ▶ if (defined($handles) and $handles !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handles and not $handles =~ /::/u) 1 5 6
Condition100%3/3
defined $handles and not $handles =~ /::/u
!ll&&!rl&&r
411
MC/DC100%2/2
defined $handles and not $handles =~ /::/u
defined $handles not $handles =~ /::/u
Truth table100%3/3
A and not(B)
A= defined $handlesB= not $handles =~ /::/u
ABresult
0X0
100
111
450 1 $handles = ucfirst(lc($handles));
451 1 $handles = "Attean::API::$handles";
452 }
453 6 ▶ if (defined($prefer) and $prefer !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefer and not $prefer =~ /::/u) 2 4 6
Condition100%3/3
defined $prefer and not $prefer =~ /::/u
!ll&&!rl&&r
312
MC/DC100%2/2
defined $prefer and not $prefer =~ /::/u
defined $prefer not $prefer =~ /::/u
Truth table100%3/3
A and not(B)
A= defined $preferB= not $prefer =~ /::/u
ABresult
0X0
100
111
454 2 $prefer = "Attean::API::" . ucfirst($prefer);
455 2 ▶ $prefer = "${prefer}Parser" unless ($prefer =~ /Parser$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $prefer =~ /Parser$/u 2 0 2
456 }
457
458 6 my %media_types;
459 6 foreach my $pclass ($class->list_parsers) {
460 60 ▶ if (defined($handles)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handles) 20 40 60
461 20 my $type = $pclass->handled_type;
462 20 ▶ next unless ($type->can('role'));
Branch: 1 branch
BranchTrueFalseTotal
unless $type->can("role") 0 20 20
463 20 my $role = $type->role;
464 20 ▶ next unless Role::Tiny::does_role($handles, $role);
Branch: 1 branch
BranchTrueFalseTotal
unless Role::Tiny::does_role($handles, $role) 12 8 20
465 }
466
467 48 ▶ if (defined($prefer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefer) 30 18 48
468 30 ▶ next unless ($pclass->does($prefer));
Branch: 1 branch
BranchTrueFalseTotal
unless $pclass->does($prefer) 20 10 30
469 }
470
471 28 my $q = 0.5;
472 28 ▶ if ($pclass->does('Attean::API::PullParser')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pclass->does('Attean::API::PullParser')) { } 11 17 28
elsif ($pclass->does('Attean::API::AtOnceParser')) { } 5 12 17
473 11 $q += 0.25;
474 } elsif ($pclass->does('Attean::API::AtOnceParser')) {
475 5 $q -= 0.25;
476 }
477
478 28 for (@{ $pclass->media_types }) {
479 42 my $mt = "$_;q=$q";
480 42 $media_types{$mt} = $q;
481 }
482 }
483
484 6 my @sorted = sort { $media_types{$b} <=> $media_types{$a} } keys %media_types;
485 6 return join(',', @sorted);
486 }
487
488
489 # Global registry for extension functions that can be invoked via BIND, FILTER, etc.
490 {
491 51 our %global_functions;
492  
493 =item C<< register_global_function( %uri_to_func ) >>
494  
495 =cut
496 46 ▶ sub register_global_function {
Subroutine register_global_function: called CC=2 SCAR=6.9
Pod documented
497 46 my $class = shift;
498 46 my %args = @_;
499 46 foreach my $uri (keys %args) {
500 483 my $func = $args{ $uri };
501 483 $global_functions{ $uri } = $func;
502 }
503 }
504  
505 =item C<< get_functions () >>
506  
507 Returns a list of IRI strings for all functions having been registered
508 with L<register_global_function>.
509  
510 =cut
511  
512 0 ▶ sub get_functions {
Subroutine get_functions: not called CC=1 SCAR=6.9
Pod documented
513 0 my $class = shift;
514 0 return keys %global_functions;
515 }
516  
517 =item C<< get_global_function( $uri ) >>
518  
519 =cut
520 0 ▶ sub get_global_function {
Subroutine get_global_function: not called CC=1 SCAR=6.9
Pod documented
521 0 my $class = shift;
522 0 my $uri = shift;
523 0 return $global_functions{ $uri };
524 }
525 }
526
527 # Global registry for extension "functional forms" that can be invoked via BIND, FILTER, etc.
528 # These differ from extension "functions" in that they can be passed undef values as arguments
529 # for expressions whose evaluation resulted in an error.
530 {
531 51 our %global_functional_forms;
532  
533 =item C<< register_global_functional_form( %uri_to_func ) >>
534  
535 =cut
536 46 ▶ sub register_global_functional_form {
Subroutine register_global_functional_form: called CC=2 SCAR=6.9
Pod documented
537 46 my $class = shift;
538 46 my %args = @_;
539 46 foreach my $uri (keys %args) {
540 115 my $func = $args{ $uri };
541 115 $global_functional_forms{ $uri } = $func;
542 }
543 }
544  
545 =item C<< get_functional_forms () >>
546  
547 Returns a list of IRI strings for all functional forms having been registered
548 with L<register_global_functional_form>.
549  
550 =cut
551  
552 0 ▶ sub get_functional_forms {
Subroutine get_functional_forms: not called CC=1 SCAR=6.9
Pod documented
553 0 my $class = shift;
554 0 return keys %global_functional_forms;
555 }
556  
557 =item C<< get_global_functional_form( $uri ) >>
558  
559 =cut
560 0 ▶ sub get_global_functional_form {
Subroutine get_global_functional_form: not called CC=1 SCAR=6.9
Pod documented
561 0 my $class = shift;
562 0 my $uri = shift;
563 0 return $global_functional_forms{ $uri };
564 }
565 }
566
567 # Global registry for extension aggregates
568 {
569 51 our %global_aggregates;
570  
571 =item C<< register_global_aggregate( %uri_to_hash ) >>
572  
573 =cut
574 46 ▶ sub register_global_aggregate {
Subroutine register_global_aggregate: called CC=2 SCAR=6.9
Pod documented
575 46 my $class = shift;
576 46 my %args = @_;
577 46 foreach my $uri (keys %args) {
578 46 my $funcs = $args{ $uri };
579 46 $global_aggregates{ $uri } = $funcs;
580 }
581 }
582  
583  
584 =item C<< get_aggregates () >>
585  
586 Returns a list of IRI strings for all aggregate functions having been registered
587 with L<register_global_aggregate>.
588  
589 =cut
590  
591 0 ▶ sub get_aggregates {
Subroutine get_aggregates: not called CC=1 SCAR=6.9
Pod documented
592 0 my $class = shift;
593 0 return keys %global_aggregates;
594 }
595  
596 =item C<< get_global_aggregate( $uri ) >>
597  
598 =cut
599 2 ▶ sub get_global_aggregate {
Subroutine get_global_aggregate: called CC=1 SCAR=0.0
Pod documented
600 2 my $class = shift;
601 2 my $uri = shift;
602 2 return $global_aggregates{ $uri };
603 }
604 }
605
606 # Global registry for extension datatypes.
607 # When literals of this datatype are constructed, they will have the registered Moo role applied to them.
608 {
609 51 our %datatype_roles;
610  
611 =item C<< register_datatype_role( %uri_to_role ) >>
612  
613 =cut
614 46 ▶ sub register_datatype_role {
Subroutine register_datatype_role: called CC=2 SCAR=6.9
Pod documented
615 46 my $class = shift;
616 46 my %args = @_;
617 46 foreach my $uri (keys %args) {
618 46 my $func = $args{ $uri };
619 46 $datatype_roles{ $uri } = $func;
620 }
621 }
622  
623 =item C<< get_datatype_role( $uri ) >>
624  
625 =cut
626 323 ▶ sub get_datatype_role {
Subroutine get_datatype_role: called CC=1 SCAR=0.0
Pod documented
627 323 my $class = shift;
628 323 my $uri = shift;
629 323 return $datatype_roles{ $uri };
630 }
631 }
632 51 }
633  
634 51 ▶use AtteanX::Functions::CompositeLists;
Subroutine BEGIN: called CC=1 SCAR=0.0
635  
636 51 1;
637  
638 __END__
« Attean/TreeRewriter.pm ↑ Summary Attean/API/Model.pm »