Attean/API/Model.pm

filter: Statementstmt 83.9% 251 / 299 Branchbran 62.5% 40 / 64 Conditioncond 48.9% 24 / 49 MC/DCmcdc 14.2% 4 / 28 Subroutinesub 90.4% 38 / 42 Podpod 84.6% 11 / 13 totaltotal 74.3% 368 / 495 CC 57 SCAR 23.2
CC
57
Cov
76%
load_urls_into_graph_with_base
72.0
algebra_holds
9.6
get_bindings
9.4
CRAP 10.1
« Attean.pm ↑ Summary Attean/SPARQLClient.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::Model - RDF Model
7  
8 =head1 VERSION
9  
10 This document describes Attean::API::Model version 0.040
11  
12 =head1 DESCRIPTION
13  
14 The Attean::API::Model role defines a common API for all RDF models to conform
15 to. It is provides a consistent interface for probing, counting, and retrieving
16 L<Attean::API::Quad|Attean::API::Binding>s matching a pattern, as well as
17 related functionality such as enumerating the graph names, and extracting
18 structured data from the models' quads.
19  
20 =head1 REQUIRED METHODS
21  
22 The following methods are required by the L<Attean::API::Model> role:
23  
24 =over 4
25  
26 =item C<< get_quads( $subject, $predicate, $object, $graph ) >>
27  
28 Returns an L<Attean::API::Iterator> for quads in the model that match the
29 supplied C<< $subject >>, C<< $predicate >>, C<< $object >>, and C<< $graph >>.
30  
31 Any of these terms may be:
32  
33 * An L<Attean::API::Term> object, in which case matching is equality-based
34  
35 * A L<Attean::API::Variable> object or C<< undef >>, in which case that term
36 will be considered as a wildcard for the purposes of matching
37  
38 * An ARRAY reference of L<Attean::API::Term> objects, in which case the
39 matching will be equality-based on the disjunction of the supplied terms
40  
41 The returned iterator conforms to both L<Attean::API::Iterator> and
42 L<Attean::API::QuadIterator>.
43  
44 =item C<< count_quads( $subject, $predicate, $object, $graph ) >>
45  
46 Returns the number of quads in the model matching the supplied pattern
47 (using the same matching semantics as C<< get_quads >>).
48  
49 =item C<< count_quads_estimate( $subject, $predicate, $object, $graph ) >>
50  
51 Returns an estimate of the number of quads in the model matching the supplied
52 pattern (using the same matching semantics as C<< get_quads >>). This estimate
53 is guaranteed to non-zero if the count returned from an equivalent call to
54 `count_quads` would return a non-zero result.
55  
56 =item C<< get_graphs >>
57  
58 Returns an L<Attean::API::Iterator> of distinct L<Attean::API::Term> objects
59 that are used in the graph position of quads in the model.
60  
61 =back
62  
63 =head1 METHODS
64  
65 The L<Attean::API::Model> role provides default implementations of the
66 following methods:
67  
68 =over 4
69  
70 =item C<< get_bindings( $subject, $predicate, $object, $graph ) >>
71  
72 Returns an L<Attean::API::Iterator> of L<Attean::API::Result> objects
73 corresponding to quads in the model matching the supplied pattern. For each
74 L<Attean::API::Variable> in the pattern list, a mapping will be present in the
75 corresponding result object. For example,
76 C<< $model->get_bindings( variable('s') ) >> will return an iterator of results
77 containing just a mapping from C<< 's' >> to subjects of all quads in the
78 model.
79  
80 =item C<< get_list( $graph, $head ) >>
81  
82 Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects that are
83 members of the rdf:List with the specified C<< $head >> (and matching
84 restricted to only the specified C<< $graph >>).
85  
86 To check if a certain term is a list, the C<holds> method may be used, for example:
87  
88 $model->holds($head, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#first'), undef, $graph))
89  
90 will return true if a given term C<$head> is a list.
91  
92 =item C<< get_sequence( $graph, $head ) >>
93  
94 Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects that are
95 members of the rdf:Sequence with the specified C<< $head >> (and matching
96 restricted to only the specified C<< $graph >>).
97  
98 =item C<< subjects( $predicate, $object, $graph ) >>
99  
100 Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
101 subjects of quads matching the supplied pattern (using the same matching
102 semantics as C<< get_quads >>).
103  
104 The objects returned will not necessarily be unique. It will instead be
105 equivalent to calling C<< get_quads >> and accessing C<< $quad->subject >>
106 for each C<< $quad >> value returned by the iterator. For an iterator of unique
107 subjects, use C<< $model->subjects->uniq >>.
108  
109 =item C<< predicates( $subject, $object, $graph ) >>
110  
111 Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
112 predicates of quads matching the supplied pattern (using the same matching
113 semantics as C<< get_quads >> with an C<< undef >> predicate).
114  
115 The objects returned will not necessarily be unique
116 (see the note for C<< subjects >> above).
117  
118 =item C<< objects( $subject, $predicate, $graph ) >>
119  
120 Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
121 objects of quads matching the supplied pattern (using the same matching
122 semantics as C<< get_quads >> with an C<< undef >> object).
123  
124 The objects returned will not necessarily be unique
125 (see the note for C<< subjects >> above).
126  
127 =item C<< graphs( $subject, $predicate, $object ) >>
128  
129 Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of all
130 graphs of quads matching the supplied pattern (using the same matching
131 semantics as C<< get_quads >> with an C<< undef >> graph).
132  
133 The objects returned will not necessarily be unique
134 (see the note for C<< subjects >> above).
135  
136 =item C<< graph_nodes( $graph ) >>
137  
138 Returns an L<Attean::API::Iterator> of L<Attean::API::Term> objects of unique
139 subjects and objects present in the specified C<< $graph >>.
140  
141 =item C<< holds($s, $p, $o, $g) >>
142  
143 =item C<< holds($triple_pattern) >>
144  
145 =item C<< holds($quad_pattern) >>
146  
147 Returns true if the triple/quad pattern matches any data in the model, false
148 otherwise.
149  
150 =item C<< algebra_holds($algebra, $graph) >>
151  
152 =item C<< algebra_holds($algebra, \@graphs) >>
153  
154 Returns true if the algebra, evaluated with the supplied default graph(s)
155 matches any data in the model, false otherwise. This is equivalent to the
156 result of an ASK query over the supplied algebra.
157  
158 =item C<< evaluate($algebra, [ $default_graph | \@default_graphs ]) >>
159  
160 Returns an L<Attean::API::Iterator> of L<Attean::Result> objects which result
161 from evaluating the given query algebra (e.g. one obtained from parsing a query
162 with L<AtteanX::Parser::SPARQL>) with the supplied default graph(s) against data
163 in the model.
164  
165 =cut
166  
167 50 ▶use Attean::API::Binding;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 50 package Attean::API::Model 0.040 {
170 50 ▶ use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 50 ▶ use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
172 50 ▶ use URI::Namespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
174 50 ▶ use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
175 50 ▶ use Math::Cartesian::Product;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 50 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
177  
178 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
179
180 # get_quads($s, $p, $o, $g)
181 # or:
182 # get_quads([$s1, $s2, ...], \@p, \@o, \@g)
183 50 requires 'get_quads';
184
185 50 ▶ sub get_bindings {
Subroutine get_bindings: called CC=9 SCAR=22.4
Pod documented
186 50 my $self = shift;
187 50 my @nodes = @_;
188 50 my @pos = Attean::API::Quad->variables;
189 # my %vars;
190 50 my %bound;
191 my %projected_vars;
192 50 foreach my $i (0 .. $#nodes) {
193 196 my $n = $nodes[$i];
194 196 $bound{ $pos[ $i ] } = $n;
195 196 ▶ if (blessed($n) and $n->does('Attean::API::Binding')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($n) and $n->does('Attean::API::Binding')) { } 0 196 196
elsif (builtin::blessed($n) and $n->isa('Attean::Variable')) { } 84 123 207
Condition66%2/3
builtin::blessed($n) and $n->does("Attean::API::Binding")
!ll&&!rl&&r
111850
MC/DC0%0/2
builtin::blessed($n) and $n->does("Attean::API::Binding")
builtin::blessed($n) $n->does("Attean::API::Binding")
Truth table66%2/3
A and B
A= builtin::blessed($n)B= $n->does("Attean::API::Binding")
ABresult
0X0
100
111
196 0 foreach my $v ($n->referenced_variables) {
197 0 $projected_vars{ $v }++;
198 }
199 ▶ } elsif (blessed($n) and $n->isa('Attean::Variable')) {
Condition100%3/3
builtin::blessed($n) and $n->isa("Attean::Variable")
!ll&&!rl&&r
1110184
MC/DC100%2/2
builtin::blessed($n) and $n->isa("Attean::Variable")
builtin::blessed($n) $n->isa("Attean::Variable")
Truth table100%3/3
A and B
A= builtin::blessed($n)B= $n->isa("Attean::Variable")
ABresult
0X0
100
111
200 84 my $name = $n->value;
201 # $vars{ $pos[ $i ] } = $name;
202 84 $projected_vars{ $name }++;
203 }
204 }
205
206 50 my @patterns;
207 50 ▶ cartesian {
Subroutine __ANON__: called CC=2 SCAR=6.9
208 50 my %bound;
209 50 foreach my $i (0 .. $#_) {
210 196 my $n = $_[$i];
211 196 $bound{ $pos[ $i ] } = $n;
212 }
213 50 push(@patterns, Attean::QuadPattern->new( %bound ));
214 50 ▶ } map { ref($_) eq 'ARRAY' ? $_ : [$_] } @nodes;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 11 185 196
215
216 50 my $quads = $self->get_quads(@nodes);
217 50 ▶ unless (blessed($quads)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($quads)) 0 50 50
218 0 return Attean::ListIterator->new(values => [], item_type => 'Attean::API::Result', variables => []);
219 }
220 85 ▶ return $quads->map(sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
221 85 my $q = shift;
222 # warn 'model got quad: ' . $q->as_string . "\n";
223 85 foreach my $pattern (@patterns) {
224 # warn 'model using pattern: ' . $pattern->as_string . "\n";
225 85 ▶ if (my $b = $pattern->unify($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $b = $pattern->unify($q)) 85 0 85
226 # warn 'unified binding: ' . $b->as_string;
227 85 my $g = $pattern->ground($b);
228 # warn "get_bindings unification: " . $b->as_string;
229 # warn "get_bindings ground: " . $g->as_string;
230 # warn 'project vars: ' . Dumper([keys %projected_vars]);
231 85 my $p = $b->project(keys %projected_vars);
232 # warn "get_bindings result: " . $p->as_string;
233 85 return $p;
234 }
235 }
236 0 return;
237 50 }, 'Attean::API::Result', variables => [keys %projected_vars]);
238 }
239
240 50 requires 'count_quads';
241 50 requires 'count_quads_estimate';
242 50 requires 'get_graphs';
243 50 requires 'holds';
244
245 2 ▶ sub get_list {
Subroutine get_list: called CC=8 SCAR=21.5
Pod documented
246 2 my $self = shift;
247 2 ▶ die "get_list called without a graph name" unless (scalar(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 2 2
248 2 my $graph = shift;
249 2 ▶ die "get_list called without a list head" unless (scalar(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 1 2
250 1 my $head = shift;
251 1 my $rdf_first = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#first');
252 1 my $rdf_rest = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest');
253 1 my $rdf_nil = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
254 1 my @elements;
255 my %seen;
256 1 ▶ while (blessed($head) and not($head->does('Attean::API::IRI') and $head->value eq $rdf_nil->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($head) and not $head->does("Attean::API::IRI") && $head->value eq $rdf_nil->value) 3 1 4
Condition66%4/6
builtin::blessed($head) and not $head->does("Attean::API::IRI") && $head->value eq $rdf_nil->value
!ll&&!rl&&r
013
$head->does('Attean::API::IRI') && $head->value eq $rdf_nil->value
!ll&&!rl&&r
301
MC/DC33%1/3
builtin::blessed($head) and not $head->does("Attean::API::IRI") && $head->value eq $rdf_nil->value
builtin::blessed($head) $head->does('Attean::API::IRI') $head->value eq $rdf_nil->value
Truth table50%2/4
A and not(B && C)
A= builtin::blessed($head)B= $head->does('Attean::API::IRI')C= $head->value eq $rdf_nil->value
ABCresult
0XX0
1110
10X1
1101
257 3 ▶ if ($seen{ $head->as_string }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$head->as_string}++) 0 3 3
258 0 die "Loop found during rdf:List traversal";
259 }
260 3 my @n = $self->objects( $head, $rdf_first )->elements;
261 3 ▶ if (scalar(@n) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @n != 1) 0 3 3
262 0 die "Invalid structure found during rdf:List traversal";
263 }
264 3 push(@elements, @n);
265 3 ($head) = $self->objects( $head, $rdf_rest )->elements;
266 }
267 1 return Attean::ListIterator->new(values => \@elements, item_type => 'Attean::API::Term' );
268 }
269  
270 2 ▶ sub get_sequence {
Subroutine get_sequence: called CC=4 SCAR=13.9
Pod documented
271 2 my $self = shift;
272 2 my $graph = shift;
273 2 my $head = shift;
274 2 my $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
275 2 my @elements;
276 2 my $i = 1;
277 2 while (1) {
278 6 my $term = Attean::IRI->new("${rdf}_$i");
279 6 my @elem = $self->objects( $head, $term, $graph )->elements;
280 6 ▶ last unless (scalar(@elem));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @elem 1 5 6
281 5 ▶ if (scalar(@elem) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elem > 1) 1 4 5
282 1 my $count = scalar(@elem);
283 1 die "Invalid structure found during rdf:Seq access: $count elements found for element $i";
284 }
285 4 my $elem = $elem[0];
286 4 ▶ last unless (blessed($elem));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($elem) 0 4 4
287 4 push(@elements, $elem);
288 4 $i++;
289 }
290 1 return Attean::ListIterator->new(values => \@elements, item_type => 'Attean::API::Term' );
291 }
292  
293 {
294 # auto-generate methods subjects, predicates, objects, and graphs
295 50 my @pos = Attean::API::Quad->variables;
296 50 my %pos = map { $pos[$_] => $_ } (0 .. $#pos);
297 50 for my $method (@pos) {
298 5 ▶ my $code = sub {
Subroutine objects: called CC=1 SCAR=0.0
Subroutine subjects: called CC=1 SCAR=0.0
299 5 my $self = shift;
300 5 my @nodes = @_;
301 5 $#nodes = 3;
302 5 splice(@nodes, $pos{$method}, 0, undef);
303 5 $#nodes = 3;
304 5 my $iter = $self->get_quads(@nodes);
305 my $nodes = $iter->map(
306 33 ▶ sub { $_->$method() },
Subroutine __ANON__: called CC=1 SCAR=0.0
307 5 'Attean::API::Term',
308 );
309 5 return $nodes;
310 200 };
311 200 Sub::Install::install_sub({
312 code => set_subname("${method}s", $code),
313 as => "${method}s"
314 });
315 }
316 }
317  
318 2 ▶ sub graph_nodes {
Subroutine graph_nodes: called CC=1 SCAR=0.0
Pod documented
319 2 my $self = shift;
320 2 my $graph = shift;
321 2 my $s = $self->subjects(undef, undef, $graph);
322 2 my $o = $self->objects(undef, undef, $graph);
323 2 my $union = Attean::IteratorSequence->new( iterators => [$s, $o], item_type => 'Attean::API::Term' );
324 2 my %seen;
325 2 ▶ return $union->grep(sub {not($seen{shift->as_string}++)});
Subroutine __ANON__: called CC=1 SCAR=0.0
326 }
327  
328 2 ▶ sub evaluate {
Subroutine evaluate: called CC=6 SCAR=19.9
Pod documented
329 2 my $self = shift;
330 2 ▶ my $algebra = shift || die "No algebra available in evaluate call";
Condition50%1/2
shift() || die('No algebra available in evaluate call')
l!l
20
MC/DC0%0/1
shift() || die('No algebra available in evaluate call')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
331 2 ▶ my $default_graphs = shift || die "No default graphs available in evaluate call";
Condition50%1/2
shift() || die('No default graphs available in evaluate call')
l!l
20
MC/DC0%0/1
shift() || die('No default graphs available in evaluate call')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
332 2 ▶ $default_graphs = [$default_graphs] if (blessed($default_graphs));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($default_graphs) 2 0 2
333
334 2 ▶ unless (blessed($algebra) and $algebra->does('Attean::API::Algebra')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($algebra) and $algebra->does("Attean::API::Algebra")) 0 2 2
Condition33%1/3
builtin::blessed($algebra) and $algebra->does("Attean::API::Algebra")
!ll&&!rl&&r
002
MC/DC0%0/2
builtin::blessed($algebra) and $algebra->does("Attean::API::Algebra")
builtin::blessed($algebra) $algebra->does("Attean::API::Algebra")
Truth table33%1/3
A and B
A= builtin::blessed($algebra)B= $algebra->does("Attean::API::Algebra")
ABresult
0X0
100
111
335 0 die "Unexpected argument to evaluate: " . Dumper($algebra);
336 }
337
338 2 my $planner = Attean::IDPQueryPlanner->new();
339 2 my $plan = $planner->plan_for_algebra($algebra, $self, $default_graphs);
340 2 my $iter = $plan->evaluate($self);
341 2 return $iter;
342 }
343  
344 6 ▶ sub algebra_holds {
Subroutine algebra_holds: called CC=8 SCAR=22.6
Pod documented
345 6 my $self = shift;
346 6 ▶ my $algebra = shift || die "No algebra available in algebra_holds call";
Condition50%1/2
shift() || die('No algebra available in algebra_holds call')
l!l
60
MC/DC0%0/1
shift() || die('No algebra available in algebra_holds call')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
347 6 ▶ my $default_graphs = shift || die "No default graphs available in algebra_holds call";
Condition50%1/2
shift() || die('No default graphs available in algebra_holds call')
l!l
60
MC/DC0%0/1
shift() || die('No default graphs available in algebra_holds call')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
348 6 ▶ $default_graphs = [$default_graphs] if (blessed($default_graphs));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($default_graphs) 6 0 6
349
350 6 ▶ unless (blessed($algebra) and $algebra->does('Attean::API::Algebra')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($algebra) and $algebra->does("Attean::API::Algebra")) 0 6 6
Condition33%1/3
builtin::blessed($algebra) and $algebra->does("Attean::API::Algebra")
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($algebra) and $algebra->does("Attean::API::Algebra")
builtin::blessed($algebra) $algebra->does("Attean::API::Algebra")
Truth table33%1/3
A and B
A= builtin::blessed($algebra)B= $algebra->does("Attean::API::Algebra")
ABresult
0X0
100
111
351 0 die "Unexpected argument to algebra_holds: " . Dumper($algebra);
352 }
353
354 6 ▶ unless ($algebra->isa('Attean::Algebra::Ask')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($algebra->isa("Attean::Algebra::Ask")) 6 0 6
355 6 $algebra = Attean::Algebra::Ask->new(children => [$algebra]);
356 }
357 6 my $planner = Attean::IDPQueryPlanner->new();
358 6 my $plan = $planner->plan_for_algebra($algebra, $self, $default_graphs);
359 6 my $iter = $plan->evaluate($self);
360 6 my $r = $iter->next;
361 6 my $ebv = eval { $r->ebv };
362 6 ▶ return 0 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
363 6 return $ebv;
364 }
365
366 16 ▶ sub holds {
Subroutine holds: called CC=7 SCAR=21.5
Pod documented
367 16 my $self = shift;
368 16 ▶ return 0 unless scalar(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 16 16
369 16 ▶ if (not defined($_[0]) or (blessed($_[0]) and $_[0]->does('Attean::API::TermOrVariable'))) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_[0] or builtin::blessed($_[0]) and $_[0]->does('Attean::API::TermOrVariable')) { } 13 3 16
elsif (builtin::blessed($_[0]) and $_[0]->does('Attean::API::TripleOrQuadPattern')) { } 3 0 3
Condition66%4/6
not defined $_[0] or builtin::blessed($_[0]) and $_[0]->does("Attean::API::TermOrVariable")
l!l&&r!l&&!r
0133
builtin::blessed($_[0]) and $_[0]->does("Attean::API::TermOrVariable")
!ll&&!rl&&r
0313
MC/DC33%1/3
not defined $_[0] or builtin::blessed($_[0]) and $_[0]->does("Attean::API::TermOrVariable")
not defined $_[0] builtin::blessed($_[0]) $_[0]->does("Attean::API::TermOrVariable")
Truth table50%2/4
not(A) or B and C
A= not defined $_[0]B= builtin::blessed($_[0])C= $_[0]->does("Attean::API::TermOrVariable")
ABCresult
1XX1
0111
00X0
0100
370 # firt argument is undef or a term/variable, so we assume this is a call with up to 3 term/variable/undef args
371 13 return ($self->count_quads_estimate(@_) > 0);
372 ▶ } elsif (blessed($_[0]) and $_[0]->does('Attean::API::TripleOrQuadPattern')) {
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->does("Attean::API::TripleOrQuadPattern")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->does("Attean::API::TripleOrQuadPattern")
builtin::blessed($_[0]) $_[0]->does("Attean::API::TripleOrQuadPattern")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->does("Attean::API::TripleOrQuadPattern")
ABresult
0X0
100
111
373 3 my $t = shift;
374 3 return ($self->count_quads_estimate($t->values) > 0);
375 } else {
376 0 die "Unexpected argument to holds: " . Dumper($_[0]);
377 }
378 }
379 }
380  
381  
382 50 package Attean::API::MutableModel 0.040 {
383 50 ▶ use Attean::RDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
384 50 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
385 50 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
386 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
387 50 ▶ use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
388  
389 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
390
391 50 requires 'add_quad';
392 50 requires 'remove_quad';
393 50 requires 'create_graph';
394 50 requires 'drop_graph';
395 50 requires 'clear_graph';
396 50 requires 'add_iter';
397
398 50 with 'Attean::API::Model';
399
400 0 ▶ sub load_urls_into_graph {
Subroutine load_urls_into_graph: not called CC=1 SCAR=6.9
Pod undocumented
401 0 my $self = shift;
402 0 my $graph = shift;
403 0 my $urls = [@_];
404 0 return $self->load_urls_into_graph_with_base($graph, $urls);
405 }
406
407 0 ▶ sub load_urls_into_graph_with_base {
Subroutine load_urls_into_graph_with_base: not called CC=8 SCAR=42.8
Pod undocumented
408 0 my $self = shift;
409 0 my $graph = shift;
410 0 my $urls = shift;
411 0 my @urls = @$urls;
412 0 my $default_base = shift;
413 0 my $ua = LWP::UserAgent->new();
414 0 my $accept = Attean->acceptable_parsers( handles => 'Attean::API::Triple' );
415 0 $ua->default_headers->push_header( 'Accept' => $accept );
416
417 0 foreach my $u (@urls) {
418 0 my $mapper = Attean::TermMap->uuid_blank_map;
419 0 my $bindings_mapper = $mapper->binding_mapper;
420 0 ▶ my $url = blessed($u) ? $u->value : $u;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($u) ? : 0 0 0
421 0 my $resp = $ua->get($url);
422 0 ▶ if ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
423 0 my $ct = $resp->header('Content-Type');
424 0 ▶ my $pclass = Attean->get_parser( media_type => $ct, filename => $url ) // Attean->get_parser('ntriples');
Condition0%0/3
"Attean"->get_parser("media_type", $ct, "filename", $url) // "Attean"->get_parser("ntriples")
l!l&&r!l&&!r
000
MC/DC0%0/2
"Attean"->get_parser("media_type", $ct, "filename", $url) // "Attean"->get_parser("ntriples")
"Attean"->get_parser("media_type", $ct, "filename", $url) "Attean"->get_parser("ntriples")
Truth table0%0/3
A // B
A= "Attean"->get_parser("media_type", $ct, "filename", $url)B= "Attean"->get_parser("ntriples")
ABresult
1X1
011
000
425 0 ▶ if ($pclass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass) { } 0 0 0
426 0 ▶ my $base = $default_base || iri($url);
Condition0%0/3
$default_base || iri($url)
l!l&&r!l&&!r
000
MC/DC0%0/2
$default_base || iri($url)
$default_base iri($url)
Truth table0%0/3
A || B
A= $default_baseB= iri($url)
ABresult
1X1
011
000
427 0 my $p = $pclass->new(base => $base);
428 0 my $str = $resp->decoded_content;
429 0 my $bytes = encode('UTF-8', $str, Encode::FB_CROAK);
430 0 my $iter = eval { $p->parse_iter_from_bytes( $bytes ) };
431 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
432 0 die "Failed to parse URL $url: $@";
433 }
434 0 $self->add_iter($iter->map($bindings_mapper)->as_quads($graph));
435 } else {
436 0 die "No parser found for content type $ct: $url";
437 }
438 } else {
439 0 die "Failed to load URL <$url>: " . $resp->status_line;
440 }
441 }
442 }
443
444 # $model->load_triples( 'turtle', iri('http://example.org/graph1') => "@prefix foaf: ...", iri('http://example.org/graph2') => "@prefix foaf: ..." );
445 6 ▶ sub load_triples {
Subroutine load_triples: called CC=4 SCAR=14.0
Pod documented
446 6 my $self = shift;
447 6 my $format = shift;
448 6 ▶ my $class = Attean->get_parser($format) || die "Failed to load parser for '$format'";
Condition50%1/2
'Attean'->get_parser($format) || die("Failed to load parser for '${format}'")
l!l
60
MC/DC0%0/1
'Attean'->get_parser($format) || die("Failed to load parser for '${format}'")
'Attean'->get_parser($format)
Truth table50%1/2
A
A= 'Attean'->get_parser($format)
Aresult
11
00
449 6 ▶ my $parser = $class->new() || die "Failed to construct parser for '$format'";
Condition50%1/2
$class->new || die("Failed to construct parser for '${format}'")
l!l
60
MC/DC0%0/1
$class->new || die("Failed to construct parser for '${format}'")
$class->new
Truth table50%1/2
A
A= $class->new
Aresult
11
00
450 6 ▶ while (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 6 6 12
451 6 my ($graph, $string) = splice(@_, 0, 2);
452 6 my $iter = $parser->parse_iter_from_bytes(encode('UTF-8', $string, Encode::FB_CROAK));
453 6 my $quads = $iter->as_quads($graph);
454 6 $self->add_iter($quads);
455 }
456 }
457
458 1 ▶ sub load_triples_from_io {
Subroutine load_triples_from_io: called CC=4 SCAR=14.0
Pod documented
459 1 my $self = shift;
460 1 my $format = shift;
461 1 ▶ my $class = Attean->get_parser($format) || die "Failed to load parser for '$format'";
Condition50%1/2
'Attean'->get_parser($format) || die("Failed to load parser for '${format}'")
l!l
10
MC/DC0%0/1
'Attean'->get_parser($format) || die("Failed to load parser for '${format}'")
'Attean'->get_parser($format)
Truth table50%1/2
A
A= 'Attean'->get_parser($format)
Aresult
11
00
462 1 ▶ my $parser = $class->new() || die "Failed to construct parser for '$format'";
Condition50%1/2
$class->new || die("Failed to construct parser for '${format}'")
l!l
10
MC/DC0%0/1
$class->new || die("Failed to construct parser for '${format}'")
$class->new
Truth table50%1/2
A
A= $class->new
Aresult
11
00
463 1 ▶ while (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 1 1 2
464 1 my ($graph, $fh) = splice(@_, 0, 2);
465 1 my $iter = $parser->parse_iter_from_io($fh);
466 1 my $quads = $iter->as_quads($graph);
467 1 $self->add_iter($quads);
468 }
469 }
470
471 2 ▶ sub add_iter {
Subroutine add_iter: called CC=3 SCAR=11.0
Pod documented
472 2 my $self = shift;
473 2 my $iter = shift;
474 2 my $type = $iter->item_type;
475 2 ▶ die "Iterator type $type isn't quads" unless (Role::Tiny::does_role($type, 'Attean::API::Quad'));
Branch: 1 branch
BranchTrueFalseTotal
unless Role::Tiny::does_role($type, "Attean::API::Quad") 0 2 2
476 2 ▶ while (my $q = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $q = $iter->next) 10 2 12
477 10 $self->add_quad($q);
478 }
479 }
480  
481 5 ▶ sub add_list {
Subroutine add_list: called CC=3 SCAR=11.0
Pod documented
482 5 my $self = shift;
483 5 ▶ die "add_list called without a graph name" unless (scalar(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 4 5
484 4 my $graph = shift;
485 4 my @elements = @_;
486 4 my $rdf_first = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#first');
487 4 my $rdf_rest = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest');
488 4 my $rdf_nil = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
489 4 ▶ if (scalar(@elements) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elements == 0) { } 1 3 4
490 1 return $rdf_nil;
491 } else {
492 3 my $head = Attean::Blank->new();
493 3 my $node = shift(@elements);
494 3 my $rest = $self->add_list($graph, @elements);
495 3 $self->add_quad( Attean::Quad->new($head, $rdf_first, $node, $graph) );
496 3 $self->add_quad( Attean::Quad->new($head, $rdf_rest, $rest, $graph) );
497 3 return $head;
498 }
499 }
500 }
501  
502  
503 50 package Attean::API::ETagCacheableModel 0.040 {
504 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
505
506 50 requires 'etag_value_for_quads';
507 }
508  
509  
510 50 package Attean::API::TimeCacheableModel 0.040 {
511 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
512
513 50 requires 'mtime_for_quads';
514 }
515  
516  
517 50 package Attean::API::BulkUpdatableModel 0.040 {
518 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
519
520 50 with 'Attean::API::MutableModel';
521
522 50 requires 'begin_bulk_updates';
523 50 requires 'end_bulk_updates';
524
525 0 ▶ around [qw(load_triples load_triples_from_io add_iter add_list)] => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
526 0 my $orig = shift;
527 0 my $self = shift;
528 0 $self->begin_bulk_updates();
529 0 $self->$orig(@_);
530 0 $self->end_bulk_updates();
531 50 };
532  
533 # End bulk updates the moment a read operation is performed...
534 0 ▶ before [qw(get_quads get_bindings count_quads count_quads_estimate get_graphs subject predicate object graph)] => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
535 0 my $self = shift;
536 0 $self->end_bulk_updates();
537 50 };
538 }
539  
540 50 package Attean::API::RDFStarModel 0.040 {
541 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
542  
543 50 with 'Attean::API::Model';
544 }
545  
546  
547  
548 50 1;
549  
550 __END__
« Attean.pm ↑ Summary Attean/SPARQLClient.pm »