Attean/TripleModel.pm

filter: Statementstmt 93.3% 197 / 211 Branchbran 54.1% 26 / 48 Conditioncond 44.4% 8 / 18 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 92.6% 38 / 41 Podpod 81.2% 13 / 16 totaltotal 82.0% 284 / 346 CC 38 SCAR 15.0
CC
38
Cov
83%
_get_quads
11.6
get_quads
10.0
count_quads_estimate
7.0
CRAP 4.5
« Attean/API/Expression.pm ↑ Summary Attean/API/Plan.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::TripleModel - RDF model backed by a set of triple-stores
7  
8 =head1 VERSION
9  
10 This document describes Attean::TripleModel version 0.040
11  
12 =head1 SYNOPSIS
13  
14 use v5.14;
15 use Attean;
16 my $model = Attean::TripleModel->new( stores => {
17 'http://example.org/graph1' => $store1,
18 'http://example.org/graph2' => $store2,
19 } );
20  
21 =head1 DESCRIPTION
22  
23 The Attean::TripleModel class represents a model that is backed by a set of
24 L<Attean::API::TripleStore|Attean::API::Store> objects, identified by an IRI
25 string. It conforms to the L<Attean::API::Model> role.
26  
27 The Attean::TripleModel constructor requires one named argument:
28  
29 =over 4
30  
31 =item stores
32  
33 A hash mapping graph IRI values to L<Attean::API::TripleStore|Attean::API::Store>
34 objects representing the backing triple-store for that graph.
35  
36 =back
37  
38 =head1 METHODS
39  
40 =over 4
41  
42 =cut
43  
44 50 package Attean::TripleModel 0.040 {
45 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
47 50 ▶ use Scalar::Util qw(reftype blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
48 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 50 with 'MooX::Log::Any';
51 50 with 'Attean::API::Model';
52 50 with 'Attean::API::CostPlanner';
53  
54 has 'stores' => (
55 is => 'ro',
56 isa => HashRef[ConsumerOf['Attean::API::TripleStore']],
57 required => 1,
58 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 50 );
60
61 =item C<< size >>
62  
63 =cut
64  
65 5 ▶ sub size {
Subroutine size: called CC=2 SCAR=6.9
Pod documented
66 5 my $self = shift;
67 5 my $count = 0;
68 5 foreach my $store (values %{ $self->stores }) {
69 5 $count += $store->size;
70 }
71 5 return $count;
72 }
73
74  
75 =item C<< count_quads >>
76  
77 =cut
78  
79 12 ▶ sub count_quads {
Subroutine count_quads: called CC=2 SCAR=6.9
Pod documented
80 12 my $self = shift;
81 # TODO: don't materialize results here just to count them
82 12 my $iter = $self->get_quads( @_ );
83 12 my $count = 0;
84 12 ▶ while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 22 12 34
85 22 $count++;
86 }
87 12 return $count;
88 }
89  
90 =item C<< count_quads_estimate >>
91  
92 =cut
93  
94 7 ▶ sub count_quads_estimate {
Subroutine count_quads_estimate: called CC=4 SCAR=19.4
Pod documented
95 7 my $self = shift;
96 7 my ($s, $p, $o, $g) = @_;
97 7 ▶ if (blessed($g) and $g->does('Attean::API::IRI')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($g) and $g->does('Attean::API::IRI')) { } 0 7 7
Condition33%1/3
builtin::blessed($g) and $g->does("Attean::API::IRI")
!ll&&!rl&&r
700
MC/DC0%0/2
builtin::blessed($g) and $g->does("Attean::API::IRI")
builtin::blessed($g) $g->does("Attean::API::IRI")
Truth table33%1/3
A and B
A= builtin::blessed($g)B= $g->does("Attean::API::IRI")
ABresult
0X0
100
111
98 0 ▶ if (my $store = $self->stores->{ $g->value }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $store = $self->stores->{$g->value}) { } 0 0 0
99 0 return $store->count_quads_estimate(@_);
100 } else {
101 0 return 0;
102 }
103 } else {
104 7 return $self->count_quads(@_);
105 }
106 }
107
108 =item C<< holds >>
109  
110 =cut
111  
112 0 ▶ sub holds {
Subroutine holds: not called CC=1 SCAR=6.9
Pod documented
113 0 my $self = shift;
114 0 return ($self->count_quads_estimate(@_) > 0)
115 }
116
117 =item C<< get_graphs >>
118  
119 =cut
120  
121 6 ▶ sub get_graphs {
Subroutine get_graphs: called CC=1 SCAR=0.0
Pod documented
122 6 my $self = shift;
123 6 my @graphs = map { Attean::IRI->new($_) } keys %{ $self->stores };
124 6 return Attean::ListIterator->new( values => \@graphs, item_type => 'Attean::API::Term' );
125 }
126
127 =item C<< get_quads ( $subject, $predicate, $object, $graph ) >>
128  
129 Returns an L<Attean::API::Iterator> for quads in the model that match the
130 supplied C<< $subject >>, C<< $predicate >>, C<< $object >>, and C<< $graph >>.
131 Any of these terms may be undefined or a L<Attean::API::Variable> object, in
132 which case that term will be considered as a wildcard for the purposes of
133 matching.
134  
135 The returned iterator conforms to both L<Attean::API::Iterator> and
136 L<Attean::API::QuadIterator>.
137  
138 =cut
139  
140 22 ▶ sub get_quads {
Subroutine get_quads: called CC=10 SCAR=23.1
Pod documented
141 22 my $self = shift;
142 22 my @nodes = @_[0..3];
143 22 foreach my $i (0..3) {
144 88 ▶ my $t = $nodes[$i] // Attean::Variable->new();
Condition66%2/3
$nodes[$i] // "Attean::Variable"->new
l!l&&r!l&&!r
42460
MC/DC0%0/2
$nodes[$i] // "Attean::Variable"->new
$nodes[$i] "Attean::Variable"->new
Truth table66%2/3
A // B
A= $nodes[$i]B= "Attean::Variable"->new
ABresult
1X1
011
000
145 88 ▶ if (not(ref($t)) or reftype($t) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $t or builtin::reftype($t) ne "ARRAY") 83 5 88
Condition66%2/3
not ref $t or builtin::reftype($t) ne "ARRAY"
l!l&&r!l&&!r
0835
MC/DC50%1/2
not ref $t or builtin::reftype($t) ne "ARRAY"
not ref $t builtin::reftype($t) ne "ARRAY"
Truth table66%2/3
not(A) or B
A= not ref $tB= builtin::reftype($t) ne "ARRAY"
ABresult
1X1
011
000
146 83 $nodes[$i] = [$t];
147 }
148 }
149
150 22 my @iters;
151 22 foreach my $s (@{ $nodes[0] }) {
152 22 foreach my $p (@{ $nodes[1] }) {
153 23 foreach my $o (@{ $nodes[2] }) {
154 23 foreach my $g (@{ $nodes[3] }) {
155 23 my $iter = $self->_get_quads($s, $p, $o, $g);
156 23 push(@iters, $iter);
157 }
158 }
159 }
160 }
161 22 ▶ if (scalar(@iters) <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @iters <= 1) { } 21 1 22
162 21 return shift(@iters);
163 } else {
164 1 return Attean::IteratorSequence->new( iterators => \@iters, item_type => $iters[0]->item_type );
165 }
166 }
167
168 23 ▶ sub _get_quads {
Subroutine _get_quads: called CC=9 SCAR=24.5
169 23 my $self = shift;
170 23 my $s = shift;
171 23 my $p = shift;
172 23 my $o = shift;
173 23 my $g = shift;
174 23 ▶ if (blessed($g) and $g->does('Attean::API::IRI')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($g) and $g->does('Attean::API::IRI')) { } 5 18 23
elsif (builtin::blessed($g) and $g->does('Attean::API::Variable')) { } 18 0 18
Condition66%2/3
builtin::blessed($g) and $g->does("Attean::API::IRI")
!ll&&!rl&&r
0185
MC/DC50%1/2
builtin::blessed($g) and $g->does("Attean::API::IRI")
builtin::blessed($g) $g->does("Attean::API::IRI")
Truth table66%2/3
A and B
A= builtin::blessed($g)B= $g->does("Attean::API::IRI")
ABresult
0X0
100
111
175 5 ▶ if (my $store = $self->stores->{ $g->value }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $store = $self->stores->{$g->value}) 5 0 5
176 5 my $iter = $store->get_triples($s, $p, $o);
177 5 return $iter->as_quads($g);
178 }
179 ▶ } elsif (blessed($g) and $g->does('Attean::API::Variable')) {
Condition33%1/3
builtin::blessed($g) and $g->does("Attean::API::Variable")
!ll&&!rl&&r
0018
MC/DC0%0/2
builtin::blessed($g) and $g->does("Attean::API::Variable")
builtin::blessed($g) $g->does("Attean::API::Variable")
Truth table33%1/3
A and B
A= builtin::blessed($g)B= $g->does("Attean::API::Variable")
ABresult
0X0
100
111
180 18 my @iters;
181 18 ▶ while (my ($g, $store) = each %{ $self->stores }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->stores;}) 18 18 36
182 18 my $iter = $store->get_triples($s, $p, $o);
183 18 my $graph = Attean::IRI->new($g);
184 18 ▶ my $quads = $iter->map(sub { $_->as_quad($graph) }, 'Attean::API::Quad');
Subroutine __ANON__: called CC=1 SCAR=0.0
185 18 push(@iters, $quads);
186 }
187 18 my $iter = Attean::IteratorSequence->new( iterators => \@iters, item_type => $iters[0]->item_type );
188 18 return $iter;
189 } else {
190 0 ▶ my $name = (blessed($g) and $g->can('as_string')) ? $g->as_string : "$g";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($g) && $g->can('as_string') ? : 0 0 0
Condition0%0/3
builtin::blessed($g) and $g->can("as_string")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($g) and $g->can("as_string")
builtin::blessed($g) $g->can("as_string")
Truth table0%0/3
A and B
A= builtin::blessed($g)B= $g->can("as_string")
ABresult
0X0
100
111
191 0 $self->log->warn("TripleModel cannot produce quads for non-IRI graph: $name");
192 }
193 0 return Attean::ListIterator->new( values => [], item_type => 'Attean::API::Quad' );
194 }
195
196 =item C<< plans_for_algebra( $algebra, $planner, $active_graphs, $default_graphs ) >>
197  
198 Delegates to an underlying store if the active graph is bound to the store,
199 and the store consumes Attean::API::CostPlanner.
200  
201 =cut
202  
203 5 ▶ sub plans_for_algebra {
Subroutine plans_for_algebra: called CC=4 SCAR=14.3
Pod documented
204 5 my $self = shift;
205 5 my $algebra = shift;
206 5 my $planner = shift;
207 5 my $active_graphs = shift;
208 5 my $default_graphs = shift;
209 5 my @plans;
210 5 ▶ if (scalar(@$active_graphs) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$active_graphs == 1) 5 0 5
211 5 my $graph = $active_graphs->[0];
212 5 ▶ if (my $store = $self->stores->{ $graph->value }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $store = $self->stores->{$graph->value}) 5 0 5
213 5 ▶ if ($store->does('Attean::API::CostPlanner')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->does("Attean::API::CostPlanner")) 0 5 5
214 0 push(@plans, $store->plans_for_algebra($algebra, $planner, $active_graphs, $default_graphs));
215 }
216 }
217 }
218 5 return @plans;
219 }
220
221 =item C<< cost_for_plan( $plan ) >>
222  
223 Attempts to delegate to all the underlying stores if that store consumes Attean::API::CostPlanner.
224  
225 =cut
226  
227 12 ▶ sub cost_for_plan {
Subroutine cost_for_plan: called CC=4 SCAR=16.4
Pod documented
228 12 my $self = shift;
229 12 my $plan = shift;
230 12 foreach my $store (values %{ $self->stores }) {
231 12 ▶ if ($store->does('Attean::API::CostPlanner')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->does("Attean::API::CostPlanner")) 0 12 12
232 0 ▶ if (defined(my $cost = $store->cost_for_plan($plan, @_))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cost = $store->cost_for_plan($plan, @_))) 0 0 0
233 0 return $cost;
234 }
235 }
236 }
237 12 return;
238 }
239 }
240  
241 50 package Attean::AddativeTripleModelRole 0.040 {
242 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
243 50 ▶ use Types::Standard qw(CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
244  
245 50 ▶ use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
246
247 50 with 'Attean::API::Model';
248 50 has 'store_constructor' => (is => 'ro', isa => CodeRef, required => 1);
249
250 =item C<< add_store( $graph => $store ) >>
251  
252 Add the L<Attean::TripleStore> C<< $store >> object to the model using the
253 IRI string value C<< $graph >> as the graph name.
254  
255 =cut
256  
257 1 ▶ sub add_store {
Subroutine add_store: called CC=3 SCAR=11.2
Pod undocumented
258 1 my $self = shift;
259 1 my $graph = shift;
260 1 ▶ my $iri = blessed($graph) ? $graph->value : $graph;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($graph) ? : 0 1 1
261 1 my $store = shift;
262
263 1 ▶ die if exists $self->stores->{ $iri };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->stores->{$iri} 0 1 1
264 1 $self->stores->{ $iri } = $store;
265 }
266  
267 =item C<< create_graph( $graph ) >>
268  
269 Create a new L<Attean::TripleStore> and add it to the model using the
270 L<Attean::API::IRI> C<< $graph >> as the graph name.
271  
272 The store is constructed by using this object's C<< store_constructor >>
273 attribute:
274  
275 my $store = $self->store_constructor->($graph);
276  
277 =cut
278  
279 1 ▶ sub create_graph {
Subroutine create_graph: called CC=2 SCAR=7.0
Pod undocumented
280 1 my $self = shift;
281 1 my $graph = shift;
282 1 my $iri = $graph->value;
283 1 ▶ return if exists $self->stores->{ $iri };
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->stores->{$iri} 0 1 1
284  
285 1 my $store = $self->store_constructor->($graph);
286 1 $self->stores->{ $iri } = $store;
287 };
288  
289 =item C<< drop_graph( $graph ) >>
290  
291 Removes the store associated with the given C<< $graph >>.
292  
293 =cut
294  
295 1 ▶ sub drop_graph {
Subroutine drop_graph: called CC=2 SCAR=7.0
Pod undocumented
296 1 my $self = shift;
297 1 my $g = shift;
298 1 ▶ if ($g->does('Attean::API::IRI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->does("Attean::API::IRI")) 1 0 1
299 1 delete $self->stores->{ $g->value };
300 }
301 }
302 }
303  
304  
305 50 package Attean::MutableTripleModel 0.040 {
306 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
307 50 ▶ use Types::Standard qw(ArrayRef ConsumerOf HashRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
308 50 ▶ use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
309 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
310  
311 50 extends 'Attean::TripleModel';
312 50 with 'Attean::API::MutableModel';
313
314 has 'stores' => (
315 is => 'ro',
316 isa => HashRef[ConsumerOf['Attean::API::MutableTripleStore']],
317 required => 1,
318 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
319 50 );
320  
321 =item C<< add_quad ( $quad ) >>
322  
323 Adds the specified C<$quad> to the underlying model.
324  
325 =cut
326  
327 14 ▶ sub add_quad {
Subroutine add_quad: called CC=3 SCAR=11.4
Pod documented
328 14 my $self = shift;
329 14 my $q = shift;
330 14 my $g = $q->graph;
331 14 ▶ die "Cannot add a quad whose graph is not an IRI" unless ($g->does('Attean::API::IRI'));
Branch: 1 branch
BranchTrueFalseTotal
unless $g->does("Attean::API::IRI") 0 14 14
332 14 my $v = $g->value;
333 14 ▶ if (my $store = $self->stores->{ $v }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $store = $self->stores->{$v}) { } 14 0 14
334 14 $store->add_triple( $q->as_triple );
335 } else {
336 0 Carp::confess "No such graph: $v";
337 }
338 }
339  
340 =item C<< remove_quad ( $quad ) >>
341  
342 Removes the specified C<< $quad >> from the underlying store.
343  
344 =cut
345  
346 1 ▶ sub remove_quad {
Subroutine remove_quad: called CC=3 SCAR=11.2
Pod documented
347 1 my $self = shift;
348 1 my $q = shift;
349 1 my $g = $q->graph;
350 1 ▶ if ($g->does('Attean::API::IRI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->does("Attean::API::IRI")) 1 0 1
351 1 my $v = $g->value;
352 1 ▶ if (my $store = $self->stores->{ $v }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $store = $self->stores->{$v}) 1 0 1
353 1 $store->remove_triple( $q->as_triple );
354 }
355 }
356 }
357
358 2 ▶ sub create_graph { die; }
Subroutine create_graph: called CC=1 SCAR=0.0
Pod documented
359  
360 =item C<< drop_graph( $graph ) >>
361  
362 Removes the store associated with the given C<< $graph >>.
363  
364 =cut
365  
366 2 ▶ sub drop_graph {
Subroutine drop_graph: called CC=2 SCAR=7.0
Pod documented
367 2 my $self = shift;
368 2 my $g = shift;
369 2 ▶ if ($g->does('Attean::API::IRI')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->does("Attean::API::IRI")) 2 0 2
370 2 delete $self->stores->{ $g->value };
371 }
372 }
373
374 =item C<< clear_graph( $graph ) >>
375  
376 Removes all quads with the given C<< $graph >>.
377  
378 =cut
379  
380 1 ▶ sub clear_graph {
Subroutine clear_graph: called CC=1 SCAR=0.0
Pod documented
381 1 my $self = shift;
382 1 my $g = shift;
383 1 $self->drop_graph($g);
384 1 $self->create_graph($g);
385 }
386 }
387  
388 50 package Attean::AddativeTripleModel 0.040 {
389 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
390 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
391 50 ▶ use Types::Standard qw(CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
392 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
393
394 50 extends 'Attean::TripleModel';
395 50 with 'Attean::AddativeTripleModelRole';
396 }
397  
398 50 package Attean::AddativeMutableTripleModel 0.040 {
399 50 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
400 50 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
401 50 ▶ use Types::Standard qw(CodeRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
402 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
403
404 50 extends 'Attean::MutableTripleModel';
405 50 with 'Attean::AddativeTripleModelRole';
406 }
407  
408  
409 50 1;
410  
411 __END__
« Attean/API/Expression.pm ↑ Summary Attean/API/Plan.pm »