Attean/API/Model.pm

filter: Statementstmt 76.6% 246 / 321 Branchbran 58.8% 40 / 68 Conditioncond 48.9% 24 / 49 MC/DCmcdc 14.2% 4 / 28 Subroutinesub 86.3% 38 / 44 Podpod 85.7% 12 / 14 totaltotal 69.4% 364 / 524 CC 60 SCAR 23.4
CC
60
Cov
71%
load_urls_into_graph_with_base
72.0
type_hierarchy
20.0
algebra_holds
9.6
CRAP 10.4
« Attean.pm ↑ Summary Attean/SPARQLClient.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  
4 =head1 NAME
5  
6 Attean::API::Model - RDF Model
7  
8 =head1 VERSION
9  
10 This document describes Attean::API::Model version 0.041
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 51 ▶use Attean::API::Binding;
Subroutine BEGIN: called CC=1 SCAR=0.0
168  
169 51 package Attean::API::Model 0.041 {
170 51 ▶ use Sub::Install;
Subroutine BEGIN: called CC=1 SCAR=0.0
171 51 ▶ use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
172 51 ▶ use URI::Namespace;
Subroutine BEGIN: called CC=1 SCAR=0.0
173 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
174 51 ▶ use List::Util qw(uniq);
Subroutine BEGIN: called CC=1 SCAR=0.0
175 51 ▶ use Math::Cartesian::Product;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 51 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 51 ▶ use Attean::RDF qw(iri);
Subroutine BEGIN: called CC=1 SCAR=0.0
178  
179 51 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
180
181 # get_quads($s, $p, $o, $g)
182 # or:
183 # get_quads([$s1, $s2, ...], \@p, \@o, \@g)
184 51 requires 'get_quads';
185
186 50 ▶ sub get_bindings {
Subroutine get_bindings: called CC=9 SCAR=22.4
Pod documented
187 50 my $self = shift;
188 50 my @nodes = @_;
189 50 my @pos = Attean::API::Quad->variables;
190 # my %vars;
191 50 my %bound;
192 my %projected_vars;
193 50 foreach my $i (0 .. $#nodes) {
194 196 my $n = $nodes[$i];
195 196 $bound{ $pos[ $i ] } = $n;
196 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
197 0 foreach my $v ($n->referenced_variables) {
198 0 $projected_vars{ $v }++;
199 }
200 ▶ } 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
201 84 my $name = $n->value;
202 # $vars{ $pos[ $i ] } = $name;
203 84 $projected_vars{ $name }++;
204 }
205 }
206
207 50 my @patterns;
208 50 ▶ cartesian {
Subroutine __ANON__: called CC=2 SCAR=6.9
209 50 my %bound;
210 50 foreach my $i (0 .. $#_) {
211 196 my $n = $_[$i];
212 196 $bound{ $pos[ $i ] } = $n;
213 }
214 50 push(@patterns, Attean::QuadPattern->new( %bound ));
215 50 ▶ } map { ref($_) eq 'ARRAY' ? $_ : [$_] } @nodes;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 11 185 196
216
217 50 my $quads = $self->get_quads(@nodes);
218 50 ▶ unless (blessed($quads)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($quads)) 0 50 50
219 0 return Attean::ListIterator->new(values => [], item_type => 'Attean::API::Result', variables => []);
220 }
221 85 ▶ return $quads->map(sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
222 85 my $q = shift;
223 # warn 'model got quad: ' . $q->as_string . "\n";
224 85 foreach my $pattern (@patterns) {
225 # warn 'model using pattern: ' . $pattern->as_string . "\n";
226 85 ▶ if (my $b = $pattern->unify($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $b = $pattern->unify($q)) 85 0 85
227 # warn 'unified binding: ' . $b->as_string;
228 85 my $g = $pattern->ground($b);
229 # warn "get_bindings unification: " . $b->as_string;
230 # warn "get_bindings ground: " . $g->as_string;
231 # warn 'project vars: ' . Dumper([keys %projected_vars]);
232 85 my $p = $b->project(keys %projected_vars);
233 # warn "get_bindings result: " . $p->as_string;
234 85 return $p;
235 }
236 }
237 0 return;
238 50 }, 'Attean::API::Result', variables => [keys %projected_vars]);
239 }
240
241 51 requires 'count_quads';
242 51 requires 'count_quads_estimate';
243 51 requires 'get_graphs';
244 51 requires 'holds';
245
246 2 ▶ sub get_list {
Subroutine get_list: called CC=8 SCAR=21.5
Pod documented
247 2 my $self = shift;
248 2 ▶ die "get_list called without a graph name" unless (scalar(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 2 2
249 2 my $graph = shift;
250 2 ▶ die "get_list called without a list head" unless (scalar(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 1 2
251 1 my $head = shift;
252 1 my $rdf_first = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#first');
253 1 my $rdf_rest = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest');
254 1 my $rdf_nil = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
255 1 my @elements;
256 my %seen;
257 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
258 3 ▶ if ($seen{ $head->as_string }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$head->as_string}++) 0 3 3
259 0 die "Loop found during rdf:List traversal";
260 }
261 3 my @n = $self->objects( $head, $rdf_first )->elements;
262 3 ▶ if (scalar(@n) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @n != 1) 0 3 3
263 0 die "Invalid structure found during rdf:List traversal";
264 }
265 3 push(@elements, @n);
266 3 ($head) = $self->objects( $head, $rdf_rest )->elements;
267 }
268 1 return Attean::ListIterator->new(values => \@elements, item_type => 'Attean::API::Term' );
269 }
270  
271 2 ▶ sub get_sequence {
Subroutine get_sequence: called CC=4 SCAR=13.9
Pod documented
272 2 my $self = shift;
273 2 my $graph = shift;
274 2 my $head = shift;
275 2 my $rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
276 2 my @elements;
277 2 my $i = 1;
278 2 while (1) {
279 6 my $term = Attean::IRI->new("${rdf}_$i");
280 6 my @elem = $self->objects( $head, $term, $graph )->elements;
281 6 ▶ last unless (scalar(@elem));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @elem 1 5 6
282 5 ▶ if (scalar(@elem) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elem > 1) 1 4 5
283 1 my $count = scalar(@elem);
284 1 die "Invalid structure found during rdf:Seq access: $count elements found for element $i";
285 }
286 4 my $elem = $elem[0];
287 4 ▶ last unless (blessed($elem));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($elem) 0 4 4
288 4 push(@elements, $elem);
289 4 $i++;
290 }
291 1 return Attean::ListIterator->new(values => \@elements, item_type => 'Attean::API::Term' );
292 }
293  
294 {
295 # auto-generate methods subjects, predicates, objects, and graphs
296 51 my @pos = Attean::API::Quad->variables;
297 51 my %pos = map { $pos[$_] => $_ } (0 .. $#pos);
298 51 for my $method (@pos) {
299 0 ▶ my $code = sub {
Subroutine graphs: not called CC=1 SCAR=1.0
Subroutine predicates: called CC=1 SCAR=1.0
300 0 my $self = shift;
301 0 my @nodes = @_;
302 0 $#nodes = 3;
303 0 splice(@nodes, $pos{$method}, 0, undef);
304 0 $#nodes = 3;
305 0 my $iter = $self->get_quads(@nodes);
306 my $nodes = $iter->map(
307 33 ▶ sub { $_->$method() },
Subroutine __ANON__: called CC=1 SCAR=0.0
308 0 'Attean::API::Term',
309 );
310 0 return $nodes;
311 204 };
312 204 Sub::Install::install_sub({
313 code => set_subname("${method}s", $code),
314 as => "${method}s"
315 });
316 }
317 }
318
319 =item C<< type_hierarchy ( $node ) >>
320  
321 Returns a list of Attean::API::IRI objects for the type hierarchy of $node.
322  
323 This is equivalent to what a query would return for:
324  
325 <$node> rdf:type/rdfs:subClassOf* ?types
326  
327 =cut
328 0 ▶ sub type_hierarchy {
Subroutine type_hierarchy: not called CC=4 SCAR=30.0
Pod documented
329 0 my $self = shift;
330 0 my $n = shift;
331
332 0 my @types = $self->objects( $n, iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type") )->elements;
333 0 my @iris = @types;
334 0 my $current = \@types;
335
336 0 my %seen = map { $_->value => 1 } @types;
337 0 while (1) {
338 0 my @new = $self->objects( $current, iri("http://www.w3.org/2000/01/rdf-schema#subClassOf") )->elements;
339 0 my $new = 0;
340 0 my @next;
341 0 foreach my $t (@new) {
342 0 ▶ unless ($seen{ $t->value }++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$t->value}++) 0 0 0
343 0 $new++;
344 0 push(@iris, $t);
345 0 push(@next, $t);
346 }
347 }
348 0 ▶ last unless ($new);
Branch: 1 branch
BranchTrueFalseTotal
unless $new 0 0 0
349 0 $current = \@next;
350 }
351 0 return @iris;
352 }
353  
354 2 ▶ sub graph_nodes {
Subroutine graph_nodes: called CC=1 SCAR=0.0
Pod documented
355 2 my $self = shift;
356 2 my $graph = shift;
357 2 my $s = $self->subjects(undef, undef, $graph);
358 2 my $o = $self->objects(undef, undef, $graph);
359 2 my $union = Attean::IteratorSequence->new( iterators => [$s, $o], item_type => 'Attean::API::Term' );
360 2 my %seen;
361 2 ▶ return $union->grep(sub {not($seen{shift->as_string}++)});
Subroutine __ANON__: called CC=1 SCAR=0.0
362 }
363  
364 2 ▶ sub evaluate {
Subroutine evaluate: called CC=6 SCAR=19.9
Pod documented
365 2 my $self = shift;
366 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
367 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
368 2 ▶ $default_graphs = [$default_graphs] if (blessed($default_graphs));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($default_graphs) 2 0 2
369
370 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
371 0 die "Unexpected argument to evaluate: " . Dumper($algebra);
372 }
373
374 2 my $planner = Attean::IDPQueryPlanner->new();
375 2 my $plan = $planner->plan_for_algebra($algebra, $self, $default_graphs);
376 2 my $iter = $plan->evaluate($self);
377 2 return $iter;
378 }
379  
380 6 ▶ sub algebra_holds {
Subroutine algebra_holds: called CC=8 SCAR=22.6
Pod documented
381 6 my $self = shift;
382 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
383 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
384 6 ▶ $default_graphs = [$default_graphs] if (blessed($default_graphs));
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($default_graphs) 6 0 6
385
386 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
387 0 die "Unexpected argument to algebra_holds: " . Dumper($algebra);
388 }
389
390 6 ▶ unless ($algebra->isa('Attean::Algebra::Ask')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($algebra->isa("Attean::Algebra::Ask")) 6 0 6
391 6 $algebra = Attean::Algebra::Ask->new(children => [$algebra]);
392 }
393 6 my $planner = Attean::IDPQueryPlanner->new();
394 6 my $plan = $planner->plan_for_algebra($algebra, $self, $default_graphs);
395 6 my $iter = $plan->evaluate($self);
396 6 my $r = $iter->next;
397 6 my $ebv = eval { $r->ebv };
398 6 ▶ return 0 if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
399 6 return $ebv;
400 }
401
402 16 ▶ sub holds {
Subroutine holds: called CC=7 SCAR=21.5
Pod documented
403 16 my $self = shift;
404 16 ▶ return 0 unless scalar(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 16 16
405 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
406 # firt argument is undef or a term/variable, so we assume this is a call with up to 3 term/variable/undef args
407 13 return ($self->count_quads_estimate(@_) > 0);
408 ▶ } 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
409 3 my $t = shift;
410 3 return ($self->count_quads_estimate($t->values) > 0);
411 } else {
412 0 die "Unexpected argument to holds: " . Dumper($_[0]);
413 }
414 }
415 }
416  
417  
418 51 package Attean::API::MutableModel 0.041 {
419 51 ▶ use Attean::RDF;
Subroutine BEGIN: called CC=1 SCAR=0.0
420 51 ▶ use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
421 51 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
422 51 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
423 51 ▶ use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
424  
425 51 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
426
427 51 requires 'add_quad';
428 51 requires 'remove_quad';
429 51 requires 'create_graph';
430 51 requires 'drop_graph';
431 51 requires 'clear_graph';
432 51 requires 'add_iter';
433
434 51 with 'Attean::API::Model';
435
436 0 ▶ sub load_urls_into_graph {
Subroutine load_urls_into_graph: not called CC=1 SCAR=6.9
Pod undocumented
437 0 my $self = shift;
438 0 my $graph = shift;
439 0 my $urls = [@_];
440 0 return $self->load_urls_into_graph_with_base($graph, $urls);
441 }
442
443 0 ▶ sub load_urls_into_graph_with_base {
Subroutine load_urls_into_graph_with_base: not called CC=8 SCAR=42.8
Pod undocumented
444 0 my $self = shift;
445 0 my $graph = shift;
446 0 my $urls = shift;
447 0 my @urls = @$urls;
448 0 my $default_base = shift;
449 0 my $ua = LWP::UserAgent->new();
450 0 my $accept = Attean->acceptable_parsers( handles => 'Attean::API::Triple' );
451 0 $ua->default_headers->push_header( 'Accept' => $accept );
452
453 0 foreach my $u (@urls) {
454 0 my $mapper = Attean::TermMap->uuid_blank_map;
455 0 my $bindings_mapper = $mapper->binding_mapper;
456 0 ▶ my $url = blessed($u) ? $u->value : $u;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($u) ? : 0 0 0
457 0 my $resp = $ua->get($url);
458 0 ▶ if ($resp->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->is_success) { } 0 0 0
459 0 my $ct = $resp->header('Content-Type');
460 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
461 0 ▶ if ($pclass) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass) { } 0 0 0
462 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
463 0 my $p = $pclass->new(base => $base);
464 0 my $str = $resp->decoded_content;
465 0 my $bytes = encode('UTF-8', $str, Encode::FB_CROAK);
466 0 my $iter = eval { $p->parse_iter_from_bytes( $bytes ) };
467 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
468 0 die "Failed to parse URL $url: $@";
469 }
470 0 $self->add_iter($iter->map($bindings_mapper)->as_quads($graph));
471 } else {
472 0 die "No parser found for content type $ct: $url";
473 }
474 } else {
475 0 die "Failed to load URL <$url>: " . $resp->status_line;
476 }
477 }
478 }
479
480 # $model->load_triples( 'turtle', iri('http://example.org/graph1') => "@prefix foaf: ...", iri('http://example.org/graph2') => "@prefix foaf: ..." );
481 6 ▶ sub load_triples {
Subroutine load_triples: called CC=4 SCAR=14.0
Pod documented
482 6 my $self = shift;
483 6 my $format = shift;
484 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
485 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
486 6 ▶ while (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 6 6 12
487 6 my ($graph, $string) = splice(@_, 0, 2);
488 6 my $iter = $parser->parse_iter_from_bytes(encode('UTF-8', $string, Encode::FB_CROAK));
489 6 my $quads = $iter->as_quads($graph);
490 6 $self->add_iter($quads);
491 }
492 }
493
494 1 ▶ sub load_triples_from_io {
Subroutine load_triples_from_io: called CC=4 SCAR=14.0
Pod documented
495 1 my $self = shift;
496 1 my $format = shift;
497 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
498 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
499 1 ▶ while (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 1 1 2
500 1 my ($graph, $fh) = splice(@_, 0, 2);
501 1 my $iter = $parser->parse_iter_from_io($fh);
502 1 my $quads = $iter->as_quads($graph);
503 1 $self->add_iter($quads);
504 }
505 }
506
507 2 ▶ sub add_iter {
Subroutine add_iter: called CC=3 SCAR=11.0
Pod documented
508 2 my $self = shift;
509 2 my $iter = shift;
510 2 my $type = $iter->item_type;
511 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
512 2 ▶ while (my $q = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $q = $iter->next) 10 2 12
513 10 $self->add_quad($q);
514 }
515 }
516  
517 5 ▶ sub add_list {
Subroutine add_list: called CC=3 SCAR=11.0
Pod documented
518 5 my $self = shift;
519 5 ▶ die "add_list called without a graph name" unless (scalar(@_));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 4 5
520 4 my $graph = shift;
521 4 my @elements = @_;
522 4 my $rdf_first = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#first');
523 4 my $rdf_rest = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#rest');
524 4 my $rdf_nil = Attean::IRI->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#nil');
525 4 ▶ if (scalar(@elements) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elements == 0) { } 1 3 4
526 1 return $rdf_nil;
527 } else {
528 3 my $head = Attean::Blank->new();
529 3 my $node = shift(@elements);
530 3 my $rest = $self->add_list($graph, @elements);
531 3 $self->add_quad( Attean::Quad->new($head, $rdf_first, $node, $graph) );
532 3 $self->add_quad( Attean::Quad->new($head, $rdf_rest, $rest, $graph) );
533 3 return $head;
534 }
535 }
536 }
537  
538  
539 51 package Attean::API::ETagCacheableModel 0.041 {
540 51 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
541
542 51 requires 'etag_value_for_quads';
543 }
544  
545  
546 51 package Attean::API::TimeCacheableModel 0.041 {
547 51 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
548
549 51 requires 'mtime_for_quads';
550 }
551  
552  
553 51 package Attean::API::BulkUpdatableModel 0.041 {
554 51 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
555
556 51 with 'Attean::API::MutableModel';
557
558 51 requires 'begin_bulk_updates';
559 51 requires 'end_bulk_updates';
560
561 0 ▶ around [qw(load_triples load_triples_from_io add_iter add_list)] => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
562 0 my $orig = shift;
563 0 my $self = shift;
564 0 $self->begin_bulk_updates();
565 0 $self->$orig(@_);
566 0 $self->end_bulk_updates();
567 51 };
568  
569 # End bulk updates the moment a read operation is performed...
570 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
571 0 my $self = shift;
572 0 $self->end_bulk_updates();
573 51 };
574 }
575  
576 51 package Attean::API::RDFStarModel 0.041 {
577 51 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
578  
579 51 with 'Attean::API::Model';
580 }
581  
582  
583  
584 51 1;
585  
586 __END__
« Attean.pm ↑ Summary Attean/SPARQLClient.pm »