RDF/Trine/Model/Dataset.pm

filter: Statementstmt 74.4% 134 / 180 Branchbran 58.5% 41 / 70 Conditioncond 50.0% 14 / 28 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 62.9% 17 / 27 Podpod 100% 16 / 16 totaltotal 67.8% 228 / 336 CC 50 SCAR 25.8
CC
50
Cov
68%
get_pattern
42.0
count_statements
28.6
get_statements
13.1
CRAP 13.2
« RDF/Trine/Iterator/Bindings.pm ↑ Summary RDF/Trine/Parser/RDFXML.pm »
1 # RDF::Trine::Model::Dataset
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Model::Dataset - Model for SPARQL datasets
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Model::Dataset version 1.019
11  
12 =head1 STATUS
13  
14 This module's API and functionality should be considered unstable.
15 In the future, this module may change in backwards-incompatible ways,
16 or be removed entirely. If you need functionality that this module provides,
17 please L<get in touch|http://www.perlrdf.org/>.
18  
19 =head1 METHODS
20  
21 Beyond the methods documented below, this class inherits methods from the
22 L<RDF::Trine::Model> class.
23  
24 =over 4
25  
26 =cut
27  
28 package RDF::Trine::Model::Dataset;
29  
30 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 68 ▶use base qw(RDF::Trine::Model);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 68 ▶use RDF::Trine::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 68 our ($VERSION);
39 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 $VERSION = '1.019';
41 }
42  
43 ################################################################################
44  
45 =item C<< new ( $model ) >>
46  
47 Returns a new dataset-model over the supplied model.
48  
49 =cut
50  
51 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
52 2 my $class = shift;
53 2 my $model = shift;
54 2 my $self = bless({ model => $model, stack => [] }, $class);
55 }
56  
57 =item C<< push_dataset ( default => \@graphs, named => \@graphs ) >>
58  
59 Creates a new dataset view over the underlying model.
60  
61 =cut
62  
63 6 ▶sub push_dataset {
Subroutine push_dataset: called CC=9 SCAR=22.2
Pod documented
64 6 my $self = shift;
65 6 my %dataset = @_;
66
67 6 ▶ my @dgraphs = @{ $dataset{ default } || [] };
Condition100%2/2
$dataset{'default'} || []
l!l
51
MC/DC100%1/1
$dataset{'default'} || []
$dataset{'default'}
Truth table100%2/2
A
A= $dataset{'default'}
Aresult
11
00
68 6 unshift(@{ $self->{ stack } }, { default => {}, named => {} });
69 6 foreach my $graph (@dgraphs) {
70 8 ▶ my $name = blessed($graph) ? $graph->uri_value : $graph;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($graph) ? : 8 0 8
71 8 ▶ $graph = blessed($graph) ? $graph : RDF::Trine::Node::Resource->new( $graph );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($graph) ? : 8 0 8
72 8 $self->{stack}[0]{default}{$name} = $graph;
73 }
74
75 6 ▶ my @ngraphs = @{ $dataset{ named } || [] };
Condition100%2/2
$dataset{'named'} || []
l!l
15
MC/DC100%1/1
$dataset{'named'} || []
$dataset{'named'}
Truth table100%2/2
A
A= $dataset{'named'}
Aresult
11
00
76 6 foreach my $graph (@ngraphs) {
77 1 ▶ my $name = blessed($graph) ? $graph->uri_value : $graph;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($graph) ? : 1 0 1
78 1 ▶ $graph = blessed($graph) ? $graph : RDF::Trine::Node::Resource->new( $graph );
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($graph) ? : 1 0 1
79 1 $self->{stack}[0]{named}{$name} = $graph;
80 }
81
82 6 return 1;
83 }
84  
85 =item C<< pop_dataset >>
86  
87 Removes the last pushed dataset view.
88  
89 =cut
90  
91 1 ▶sub pop_dataset {
Subroutine pop_dataset: called CC=1 SCAR=0.0
Pod documented
92 1 my $self = shift;
93 1 shift(@{ $self->{ stack } });
94 1 return 1;
95 }
96  
97 =item C<< temporary_model >>
98
99 Returns a new temporary (non-persistent) model.
100
101 =cut
102
103 0 ▶sub temporary_model {
Subroutine temporary_model: not called CC=1 SCAR=6.9
Pod documented
104 0 my $class = shift;
105 0 my $model = RDF::Trine::Model->temporary_model;
106 0 return $class->new( $model );
107 }
108  
109 =item C<< add_hashref ( $hashref [, $context] ) >>
110  
111 Add triples represented in an RDF/JSON-like manner to the model.
112  
113 =cut
114  
115 0 ▶sub add_hashref {
Subroutine add_hashref: not called CC=1 SCAR=6.9
Pod documented
116 0 my $self = shift;
117 0 return $self->model->add_hashref( @_ );
118 }
119  
120 =item C<< size >>
121  
122 Returns the number of statements in the model.
123  
124 =cut
125  
126 0 ▶sub size {
Subroutine size: not called CC=1 SCAR=6.9
Pod documented
127 0 my $self = shift;
128 0 return $self->count_statements( undef, undef, undef, undef );
129 }
130  
131 =item C<< supports ( [ $feature ] ) >>
132  
133 If C<< $feature >> is specified, returns true if the feature is supported by the
134 underlying store, false otherwise. If C<< $feature >> is not specified, returns
135 a list of supported features.
136  
137 =cut
138  
139 0 ▶sub supports {
Subroutine supports: not called CC=2 SCAR=17.9
Pod documented
140 0 my $self = shift;
141 0 my $store = $self->_store;
142 0 ▶ if ($store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) 0 0 0
143 0 return $store->supports( @_ );
144 }
145 0 return;
146 }
147  
148 =item C<< count_statements ( $subject, $predicate, $object ) >>
149  
150 Returns a count of all the statements matching the specified subject,
151 predicate and objects. Any of the arguments may be undef to match any value.
152  
153 =cut
154  
155 22 ▶sub count_statements {
Subroutine count_statements: called CC=14 SCAR=33.5
Pod documented
156 22 my $self = shift;
157 22 ▶ return $self->model->count_statements( @_ ) unless (scalar(@{ $self->{stack} }));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"stack"};} 5 17 22
158 17 my $use_quad = (scalar(@_) >= 4);
159 17 ▶ if ($use_quad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_quad) { } 15 2 17
160 # warn "counting quads with dataset";
161 15 my $quad = $_[3];
162 15 ▶ if (blessed($quad) and $quad->isa('RDF::Trine::Node::Nil')) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($quad) and $quad->isa('RDF::Trine::Node::Nil')) { } 14 1 15
elsif (not defined $quad or builtin::blessed($quad) and $quad->isa('RDF::Trine::Node::Variable')) { } 1 0 1
Condition66%2/3
builtin::blessed($quad) and $quad->isa("RDF::Trine::Node::Nil")
!ll&&!rl&&r
1014
MC/DC50%1/2
builtin::blessed($quad) and $quad->isa("RDF::Trine::Node::Nil")
builtin::blessed($quad) $quad->isa("RDF::Trine::Node::Nil")
Truth table66%2/3
A and B
A= builtin::blessed($quad)B= $quad->isa("RDF::Trine::Node::Nil")
ABresult
0X0
100
111
163 # warn "- default graph query";
164 # warn "- " . join(', ', keys %{ $self->{stack}[0] });
165 14 my $count = 0;
166 14 foreach my $g (values %{ $self->{stack}[0]{default} }) {
167 16 $count += $self->model->count_statements( @_[0..2], $g );
168 # warn "$count statments in graph " . $g->uri_value;
169 }
170 14 return $count;
171 ▶ } elsif (not(defined($quad)) or (blessed($quad) and $quad->isa('RDF::Trine::Node::Variable'))) {
Condition16%1/6
not defined $quad or builtin::blessed($quad) and $quad->isa("RDF::Trine::Node::Variable")
l!l&&r!l&&!r
100
builtin::blessed($quad) and $quad->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $quad or builtin::blessed($quad) and $quad->isa("RDF::Trine::Node::Variable")
not defined $quad builtin::blessed($quad) $quad->isa("RDF::Trine::Node::Variable")
Truth table25%1/4
not(A) or B and C
A= not defined $quadB= builtin::blessed($quad)C= $quad->isa("RDF::Trine::Node::Variable")
ABCresult
1XX1
0111
00X0
0100
172 1 my $iter = $self->get_contexts;
173 1 my $count = 0;
174 1 ▶ while (my $g = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $iter->next) 0 1 1
175 0 $count += $self->model->count_statements( @_[0..2], $g );
176 }
177 1 return $count;
178 } else {
179 0 ▶ my $name = blessed($quad) ? $quad->uri_value : $quad;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($quad) ? : 0 0 0
180 0 ▶ if ($self->{stack}[0]{named}{ $name }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'stack'}[0]{'named'}{$name}) { } 0 0 0
181 0 return $self->model->count_statements( @_[0..2], $quad );
182 } else {
183 0 return 0;
184 }
185 }
186 } else {
187 2 my %seen;
188 2 my $count = 0;
189 2 my $iter = $self->get_statements( @_[0..2], undef );
190 2 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 2 2
191 0 warn 'counting triples in dataset: ' . $st->as_string;
192 0 ▶ $count++ unless ($seen{ join(' ', map { $_->as_string } (map { $st->$_() } qw(subject predicate object)) ) }++);
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{join " ", map({$_->as_string;} map({$st->$_;} "subject", "predicate", "object"))}++ 0 0 0
193 }
194 2 return $count;
195 }
196 }
197  
198 =item C<< add_statement ( $statement [, $context] ) >>
199
200 Adds the specified C<< $statement >> to the rdf store.
201
202 =cut
203
204 0 ▶sub add_statement {
Subroutine add_statement: not called CC=1 SCAR=6.9
Pod documented
205 0 my $self = shift;
206 0 return $self->model->add_statement( @_ );
207 }
208  
209 =item C<< remove_statement ( $statement [, $context]) >>
210  
211 Removes the specified C<< $statement >> from the rdf store.
212  
213 =cut
214  
215 0 ▶sub remove_statement {
Subroutine remove_statement: not called CC=1 SCAR=6.9
Pod documented
216 0 my $self = shift;
217 0 return $self->model->remove_statement( @_ );
218 }
219  
220 =item C<< remove_statements ( $subject, $predicate, $object [, $context] ) >>
221  
222 Removes all statements matching the supplied C<< $statement >> pattern from the rdf store.
223  
224 =cut
225  
226 0 ▶sub remove_statements {
Subroutine remove_statements: not called CC=1 SCAR=6.9
Pod documented
227 0 my $self = shift;
228 0 return $self->model->remove_statements( @_ );
229 }
230  
231 =item C<< get_statements ($subject, $predicate, $object [, $context] ) >>
232  
233 Returns an iterator of all statements matching the specified subject,
234 predicate and objects from the rdf store. Any of the arguments may be undef to
235 match any value.
236  
237 If three or fewer arguments are given, the statements returned will be matched
238 based on triple semantics (the graph union of triples from all the named
239 graphs). If four arguments are given (even if C<< $context >> is undef),
240 statements will be matched based on quad semantics (the union of all quads in
241 the underlying store).
242  
243 =cut
244  
245 16 ▶sub get_statements {
Subroutine get_statements: called CC=13 SCAR=25.7
Pod documented
246 16 my $self = shift;
247 16 ▶ return $self->model->get_statements( @_ ) unless (scalar(@{ $self->{stack} }));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"stack"};} 2 14 16
248 14 my $bound = 0;
249 14 my $use_quad = (scalar(@_) >= 4);
250 14 my $nil = RDF::Trine::Node::Nil->new();
251 14 ▶ if ($use_quad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_quad) { } 11 3 14
252 11 my $quad = $_[3];
253 11 ▶ if (blessed($quad) and not($quad->isa('RDF::Trine::Node::Variable')) and not($quad->isa('RDF::Trine::Node::Nil'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($quad) and not $quad->isa('RDF::Trine::Node::Variable') and not $quad->isa('RDF::Trine::Node::Nil')) { } 1 10 11
Condition83%5/6
builtin::blessed($quad) and not $quad->isa("RDF::Trine::Node::Variable") and not $quad->isa("RDF::Trine::Node::Nil")
!ll&&!rl&&r
551
builtin::blessed($quad) and not $quad->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
506
MC/DC66%2/3
builtin::blessed($quad) and not $quad->isa("RDF::Trine::Node::Variable") and not $quad->isa("RDF::Trine::Node::Nil")
builtin::blessed($quad) not $quad->isa("RDF::Trine::Node::Variable") not $quad->isa("RDF::Trine::Node::Nil")
Truth table75%3/4
A and not(B) and not(C)
A= builtin::blessed($quad)B= not $quad->isa("RDF::Trine::Node::Variable")C= not $quad->isa("RDF::Trine::Node::Nil")
ABCresult
0XX0
10X0
1100
1111
254 1 ▶ if (exists($self->{stack}[0]{named}{$quad->uri_value})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'stack'}[0]{'named'}{$quad->uri_value}) { } 1 0 1
255 1 return $self->model->get_statements( @_ );
256 } else {
257 0 return RDF::Trine::Iterator::Graph->new([]);
258 }
259 } else {
260 10 my @iters;
261 10 foreach my $g (values %{ $self->{stack}[0]{default} }) {
262 13 my $iter = $self->model->get_statements( @_[0..2], $g );
263 31 ▶ my $code = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
264 31 my $st = $iter->next;
265 31 ▶ return unless $st;
Branch: 1 branch
BranchTrueFalseTotal
unless $st 13 18 31
266 18 my @nodes = $st->nodes;
267 18 $nodes[3] = $nil;
268 18 my $quad = RDF::Trine::Statement::Quad->new( @nodes );
269 18 return $quad;
270 13 };
271 13 push(@iters, RDF::Trine::Iterator::Graph->new( $code ));
272 }
273 10 ▶ if (not(defined($quad)) or $quad->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $quad or $quad->isa("RDF::Trine::Node::Variable")) 5 5 10
Condition66%2/3
not defined $quad or $quad->isa("RDF::Trine::Node::Variable")
l!l&&r!l&&!r
505
MC/DC50%1/2
not defined $quad or $quad->isa("RDF::Trine::Node::Variable")
not defined $quad $quad->isa("RDF::Trine::Node::Variable")
Truth table66%2/3
not(A) or B
A= not defined $quadB= $quad->isa("RDF::Trine::Node::Variable")
ABresult
1X1
011
000
274 5 my $graphs = $self->get_contexts;
275 5 ▶ while (my $g = $graphs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $graphs->next) 1 5 6
276 1 ▶ next if ($g->isa('RDF::Trine::Node::Nil'));
Branch: 1 branch
BranchTrueFalseTotal
if $g->isa("RDF::Trine::Node::Nil") 0 1 1
277 1 push(@iters, $self->model->get_statements( @_[0..2], $g ));
278 }
279 }
280 10 my %seen;
281 29 ▶ my $code = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
282 29 while (1) {
283 43 ▶ return unless scalar(@iters);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @iters 10 33 43
284 33 my $st = $iters[0]->next;
285 33 ▶ if ($st) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st) { } 19 14 33
286 19 ▶ if ($seen{ $st->as_string }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$st->as_string}++) 0 19 19
287 0 next;
288 }
289 19 return $st;
290 } else {
291 14 shift(@iters);
292 }
293 }
294 10 };
295 10 my $iter = RDF::Trine::Iterator::Graph->new( $code );
296 10 return $iter;
297 }
298 } else {
299 3 my %seen;
300 my @iters;
301 3 my $iter = $self->get_statements( @_[0..2], $nil );
302 3 push(@iters, $iter);
303 3 my $giter = $self->get_contexts;
304 3 ▶ while (my $g = $giter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $giter->next) 1 3 4
305 1 my $iter = $self->get_statements( @_[0..2], $g );
306 1 push(@iters, $iter);
307 }
308
309 11 ▶ my $code = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
310 11 while (1) {
311 15 ▶ return unless scalar(@iters);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @iters 3 12 15
312 12 my $st = $iters[0]->next;
313 12 ▶ if ($st) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st) { } 8 4 12
314 8 my @nodes = (map { $st->$_() } qw(subject predicate object));
315 8 ▶ next if ($seen{ join(' ', map { $_->as_string } @nodes ) }++);
Branch: 1 branch
BranchTrueFalseTotal
if $seen{join " ", map({$_->as_string;} @nodes)}++ 0 8 8
316 8 return RDF::Trine::Statement->new( @nodes );
317 } else {
318 4 shift(@iters);
319 }
320 }
321 3 };
322 3 return RDF::Trine::Iterator::Graph->new( $code );
323 }
324 }
325  
326 =item C<< get_pattern ( $bgp [, $context] [, %args ] ) >>
327  
328 Returns a stream object of all bindings matching the specified graph pattern.
329  
330 =cut
331  
332 0 ▶sub get_pattern {
Subroutine get_pattern: not called CC=6 SCAR=37.4
Pod documented
333 0 my $self = shift;
334 0 ▶ return $self->model->get_pattern( @_ ) unless (scalar(@{ $self->{stack} }));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"stack"};} 0 0 0
335 0 my $use_quad = (scalar(@_) >= 4);
336 0 ▶ if ($use_quad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_quad) { } 0 0 0
337 0 my $quad = $_[3];
338 0 ▶ if (blessed($quad) and not($quad->isa('RDF::Trine::Node::Variable')) and not($quad->isa('RDF::Trine::Node::Nil'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($quad) and not $quad->isa('RDF::Trine::Node::Variable') and not $quad->isa('RDF::Trine::Node::Nil')) { } 0 0 0
Condition0%0/6
builtin::blessed($quad) and not $quad->isa("RDF::Trine::Node::Variable") and not $quad->isa("RDF::Trine::Node::Nil")
!ll&&!rl&&r
000
builtin::blessed($quad) and not $quad->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
000
MC/DC0%0/3
builtin::blessed($quad) and not $quad->isa("RDF::Trine::Node::Variable") and not $quad->isa("RDF::Trine::Node::Nil")
builtin::blessed($quad) not $quad->isa("RDF::Trine::Node::Variable") not $quad->isa("RDF::Trine::Node::Nil")
Truth table0%0/4
A and not(B) and not(C)
A= builtin::blessed($quad)B= not $quad->isa("RDF::Trine::Node::Variable")C= not $quad->isa("RDF::Trine::Node::Nil")
ABCresult
0XX0
10X0
1100
1111
339 0 return $self->model->get_pattern( @_ );
340 } else {
341 0 return $self->SUPER::get_pattern( @_ );
342 }
343 } else {
344 0 return $self->model->get_pattern( @_ );
345 }
346 }
347  
348 =item C<< get_sparql ( $sparql ) >>
349  
350 Returns a stream object of all bindings matching the specified graph pattern.
351  
352 =cut
353  
354 0 ▶sub get_sparql {
Subroutine get_sparql: not called CC=2 SCAR=17.9
Pod documented
355 0 my $self = shift;
356 0 ▶ return $self->model->get_sparql( @_ ) unless (scalar(@{ $self->{stack} }));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"stack"};} 0 0 0
357 0 throw RDF::Trine::Error::UnimplementedError -text => "Cannot execute SPARQL queries against a complex dataset model";
358 }
359  
360 =item C<< get_graphs >>
361  
362 =item C<< get_contexts >>
363  
364 Returns an iterator containing the nodes representing the named graphs in the
365 model.
366  
367 =cut
368  
369 9 ▶sub get_contexts {
Subroutine get_contexts: called CC=3 SCAR=11.4
Pod documented
370 9 my $self = shift;
371 9 ▶ return $self->model->get_contexts unless (scalar(@{ $self->{stack} }));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"stack"};} 0 9 9
372 9 my @nodes = values %{ $self->{stack}[0]{named} };
373 9 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 9 9
374 0 return @nodes;
375 } else {
376 9 return RDF::Trine::Iterator->new( \@nodes );
377 }
378 }
379 68 *get_graphs = \&get_contexts;
380  
381 =item C<< model >>
382  
383 Returns the underlying model object.
384  
385 =cut
386  
387 38 ▶sub model {
Subroutine model: called CC=1 SCAR=0.0
Pod documented
388 38 my $self = shift;
389 38 return $self->{model};
390 }
391  
392 0 ▶sub _store {
Subroutine _store: not called CC=1 SCAR=6.9
393 0 my $self = shift;
394 0 return $self->model->_store;
395 }
396  
397 68 1;
398  
399 __END__
« RDF/Trine/Iterator/Bindings.pm ↑ Summary RDF/Trine/Parser/RDFXML.pm »