RDF/Trine/Model.pm

filter: Statementstmt 77.4% 402 / 519 Branchbran 74.0% 154 / 208 Conditioncond 70.0% 75 / 107 MC/DCmcdc 40.9% 25 / 61 Subroutinesub 79.2% 42 / 53 Podpod 100% 32 / 32 totaltotal 74.4% 730 / 980 CC 157 SCAR 24.8
CC
157
Cov
76%
as_graphviz
30.0
_debug
30.0
add_iterator
20.0
CRAP 11.9
« RDF/Trine/Store/Hexastore.pm ↑ Summary RDF/Trine/Serializer/RDFXML.pm »
1 # RDF::Trine::Model
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Model - Model class
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Model version 1.019
11  
12 =head1 METHODS
13  
14 =over 4
15  
16 =cut
17  
18 package RDF::Trine::Model;
19  
20 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 68 our ($VERSION);
25 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 68 $VERSION = '1.019';
27 }
28  
29 68 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
33 68 ▶use RDF::Trine qw(variable);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use RDF::Trine::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 68 ▶use RDF::Trine::Store;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 68 ▶use RDF::Trine::Model::Dataset;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 =item C<< new ( $store ) >>
40  
41 Returns a new model over the supplied L<rdf store|RDF::Trine::Store> or a new temporary model.
42 If you provide an unblessed value, it will be used to create a new rdf store.
43  
44 =cut
45  
46 947 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
47 947 my $class = shift;
48 947 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 941 6 947
49 941 my $store = shift;
50 941 ▶ $store = RDF::Trine::Store->new( $store ) unless (blessed($store));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($store) 1 940 941
51 941 my %args = @_;
52 941 my $self = bless({
53 store => $store,
54 temporary => 0,
55 added => 0,
56 threshold => -1,
57 %args
58 }, $class);
59 } else {
60 6 return $class->temporary_model;
61 }
62 }
63  
64 =item C<< temporary_model >>
65
66 Returns a new temporary (non-persistent) model.
67
68 =cut
69
70 588 ▶sub temporary_model {
Subroutine temporary_model: called CC=1 SCAR=0.0
Pod documented
71 588 my $class = shift;
72 588 my $store = RDF::Trine::Store::Memory->new();
73 # my $store = RDF::Trine::Store::DBI->temporary_store();
74 588 my $self = $class->new( $store );
75 588 $self->{temporary} = 1;
76 588 $self->{threshold} = 25_000;
77 588 return $self;
78 }
79  
80 =item C<< dataset_model ( default => \@dgraphs, named => \@ngraphs ) >>
81  
82 Returns a new model object with the default graph mapped to the union of the
83 graphs named in C<< @dgraphs >>, and with available named graphs named in
84 C<< @ngraphs >>.
85  
86 =cut
87  
88 0 ▶sub dataset_model {
Subroutine dataset_model: not called CC=1 SCAR=6.9
Pod documented
89 0 my $self = shift;
90 0 my $ds = RDF::Trine::Model::Dataset->new( $self );
91 0 $ds->push_dataset( @_ );
92 0 return $ds;
93 }
94  
95 =item C<< begin_bulk_ops >>
96  
97 Provides a hint to the backend that many update operations are about to occur.
98 The backend may use this hint to, for example, aggregate many operations into a
99 single operation, or delay index maintenence. After the update operations have
100 been executed, C<< end_bulk_ops >> should be called to ensure the updates are
101 committed to the backend.
102  
103 =cut
104  
105 773 ▶sub begin_bulk_ops {
Subroutine begin_bulk_ops: called CC=3 SCAR=12.0
Pod documented
106 773 my $self = shift;
107 773 my $store = $self->_store;
108 773 ▶ if (blessed($store) and $store->can('_begin_bulk_ops')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($store) and $store->can("_begin_bulk_ops")) 773 0 773
Condition33%1/3
builtin::blessed($store) and $store->can("_begin_bulk_ops")
!ll&&!rl&&r
00773
MC/DC0%0/2
builtin::blessed($store) and $store->can("_begin_bulk_ops")
builtin::blessed($store) $store->can("_begin_bulk_ops")
Truth table33%1/3
A and B
A= builtin::blessed($store)B= $store->can("_begin_bulk_ops")
ABresult
0X0
100
111
109 773 $store->_begin_bulk_ops();
110 }
111 }
112  
113 =item C<< end_bulk_ops >>
114  
115 Provides a hint to the backend that a set of bulk operations have been completed
116 and may be committed to the backend.
117  
118 =cut
119  
120 7230 ▶sub end_bulk_ops {
Subroutine end_bulk_ops: called CC=3 SCAR=11.0
Pod documented
121 7230 my $self = shift;
122 7230 my $store = $self->_store;
123 7230 ▶ if (blessed($store) and $store->can('_end_bulk_ops')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($store) and $store->can("_end_bulk_ops")) 7226 4 7230
Condition66%2/3
builtin::blessed($store) and $store->can("_end_bulk_ops")
!ll&&!rl&&r
407226
MC/DC50%1/2
builtin::blessed($store) and $store->can("_end_bulk_ops")
builtin::blessed($store) $store->can("_end_bulk_ops")
Truth table66%2/3
A and B
A= builtin::blessed($store)B= $store->can("_end_bulk_ops")
ABresult
0X0
100
111
124 7226 $store->_end_bulk_ops();
125 }
126 }
127  
128 =item C<< logger ( [ $logger ] ) >>
129  
130 Returns the logging object responsible for recording data inserts and deletes.
131  
132 If C<< $logger >> is passed as an argument, sets the logger to this object.
133  
134 =cut
135  
136 4107 ▶sub logger {
Subroutine logger: called CC=2 SCAR=6.9
Pod documented
137 4107 my $self = shift;
138 4107 ▶ if (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 1 4106 4107
139 1 $self->{'logger'} = shift;
140 }
141 4107 return $self->{'logger'};
142 }
143  
144 =item C<< add_statement ( $statement [, $context] ) >>
145  
146 Adds the specified C<< $statement >> to the rdf store.
147  
148 =cut
149
150 4086 ▶sub add_statement {
Subroutine add_statement: called CC=15 SCAR=27.8
Pod documented
151 4086 my ($self, @args) = @_;
152 4086 ▶ if ($args[0]->isa('RDF::Trine::Statement')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0]->isa('RDF::Trine::Statement')) { } 4074 12 4086
153 4074 foreach my $n ($args[0]->nodes) {
154 12267 ▶ unless (blessed($n) and ($n->isa('RDF::Trine::Node::Resource') or $n->isa('RDF::Trine::Node::Literal') or $n->isa('RDF::Trine::Node::Blank') or $n->isa('RDF::Trine::Node::Nil'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($n) and $n->isa("RDF::Trine::Node::Resource") || $n->isa("RDF::Trine::Node::Literal") || $n->isa("RDF::Trine::Node::Blank") || $n->isa("RDF::Trine::Node::Nil")) 0 12267 12267
Condition75%9/12
builtin::blessed($n) and $n->isa("RDF::Trine::Node::Resource") || $n->isa("RDF::Trine::Node::Literal") || $n->isa("RDF::Trine::Node::Blank") || $n->isa("RDF::Trine::Node::Nil")
!ll&&!rl&&r
0012267
$n->isa("RDF::Trine::Node::Resource") or $n->isa("RDF::Trine::Node::Literal") or $n->isa("RDF::Trine::Node::Blank") or $n->isa("RDF::Trine::Node::Nil")
l!l&&r!l&&!r
1226340
$n->isa("RDF::Trine::Node::Resource") or $n->isa("RDF::Trine::Node::Literal") or $n->isa("RDF::Trine::Node::Blank")
l!l&&r!l&&!r
1049917644
$n->isa("RDF::Trine::Node::Resource") or $n->isa("RDF::Trine::Node::Literal")
l!l&&r!l&&!r
909314061768
MC/DC0%0/5
builtin::blessed($n) and $n->isa("RDF::Trine::Node::Resource") || $n->isa("RDF::Trine::Node::Literal") || $n->isa("RDF::Trine::Node::Blank") || $n->isa("RDF::Trine::Node::Nil")
builtin::blessed($n) $n->isa("RDF::Trine::Node::Resource") $n->isa("RDF::Trine::Node::Literal") $n->isa("RDF::Trine::Node::Blank") $n->isa("RDF::Trine::Node::Nil")
Truth table66%4/6
A and B or C or D or E
A= builtin::blessed($n)B= $n->isa("RDF::Trine::Node::Resource")C= $n->isa("RDF::Trine::Node::Literal")D= $n->isa("RDF::Trine::Node::Blank")E= $n->isa("RDF::Trine::Node::Nil")
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
155 0 throw RDF::Trine::Error::MethodInvocationError -text => 'Cannot add a pattern (non-ground statement) to a model';
156 }
157 }
158 } else {
159 12 throw RDF::Trine::Error::MethodInvocationError -text => 'Argument is not an RDF::Trine::Statement';
160 }
161 4074 ▶ if ($self->{temporary}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"temporary"}) 3339 735 4074
162 3339 ▶ if ($self->{added}++ >= $self->{threshold}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"added"}++ >= $self->{"threshold"}) 1 3338 3339
163 # warn "*** should upgrade to a DBI store here";
164 1 my $store = RDF::Trine::Store::DBI->temporary_store;
165 1 my $iter = $self->get_statements(undef, undef, undef, undef);
166 1 ▶ if ($store->can('_begin_bulk_ops')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->can("_begin_bulk_ops")) 1 0 1
167 1 $store->_begin_bulk_ops();
168 }
169 1 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 10 1 11
170 10 $store->add_statement( $st );
171 }
172 1 ▶ if ($store->can('_begin_bulk_ops')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store->can("_begin_bulk_ops")) 1 0 1
173 1 $store->_end_bulk_ops();
174 }
175 1 $self->{store} = $store;
176 1 $self->{temporary} = 0;
177 # warn "*** upgraded to a DBI store";
178 }
179 }
180
181 4074 ▶ if (my $log = $self->logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $self->logger) 3 4071 4074
182 3 my ($st, $context) = @args;
183 3 ▶ if (defined($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context) 0 3 3
184 0 $st = RDF::Trine::Statement::Quad->new(($st->nodes)[0..2], $context);
185 }
186 3 $log->add($st);
187 }
188
189 4074 return $self->_store->add_statement( @args );
190 }
191  
192 =item C<< add_hashref ( $hashref [, $context] ) >>
193  
194 Add triples represented in an RDF/JSON-like manner to the model.
195  
196 See C<< as_hashref >> for full documentation of the hashref format.
197  
198 =cut
199  
200 49 ▶sub add_hashref {
Subroutine add_hashref: called CC=14 SCAR=27.1
Pod documented
201 49 my $self = shift;
202 49 my $index = shift;
203 49 my $context = shift;
204
205 49 $self->begin_bulk_ops();
206 49 foreach my $s (keys %$index) {
207 78 ▶ my $ts = ( $s =~ /^_:(.*)$/ ) ?
Branch: 1 branch
BranchTrueFalseTotal
$s =~ /^_:(.*)$/ ? : 55 23 78
208 RDF::Trine::Node::Blank->new($1) :
209 RDF::Trine::Node::Resource->new($s);
210
211 78 foreach my $p (keys %{ $index->{$s} }) {
212 107 my $tp = RDF::Trine::Node::Resource->new($p);
213
214 107 foreach my $O (@{ $index->{$s}->{$p} }) {
215 128 my $to;
216
217 # $O should be a hashref, but we can do a little error-correcting.
218 128 ▶ unless (ref $O) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $O) 34 94 128
219 34 ▶ if ($O =~ /^_:/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($O =~ /^_:/) { } 0 34 34
elsif ($O =~ /^[a-z0-9._\+-]{1,12}:\S+$/i) { } 0 34 34
elsif ($O =~ /^(.*)\@([a-z]{2})$/) { } 7 27 34
220 0 $O = { 'value'=>$O, 'type'=>'bnode' };
221 } elsif ($O =~ /^[a-z0-9._\+-]{1,12}:\S+$/i) {
222 0 $O = { 'value'=>$O, 'type'=>'uri' };
223 } elsif ($O =~ /^(.*)\@([a-z]{2})$/) {
224 7 $O = { 'value'=>$1, 'type'=>'literal', 'lang'=>$2 };
225 } else {
226 27 $O = { 'value'=>$O, 'type'=>'literal' };
227 }
228 }
229
230 128 ▶ if (lc $O->{'type'} eq 'literal') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $O->{'type'} eq 'literal') { } 74 54 128
231 $to = RDF::Trine::Node::Literal->new(
232 74 $O->{'value'}, $O->{'lang'}, $O->{'datatype'});
233 } else {
234 $to = ( $O->{'value'} =~ /^_:(.*)$/ ) ?
235 RDF::Trine::Node::Blank->new($1) :
236 54 ▶ RDF::Trine::Node::Resource->new($O->{'value'});
Branch: 1 branch
BranchTrueFalseTotal
$O->{'value'} =~ /^_:(.*)$/ ? : 28 26 54
237 }
238
239 128 ▶ if ($ts and $tp and $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ts and $tp and $to) 128 0 128
Condition50%3/6
$ts and $tp and $to
!ll&&!rl&&r
08120
$ts and $tp
!ll&&!rl&&r
01280
MC/DC0%0/3
$ts and $tp and $to
$ts $tp $to
Truth table0%0/4
A and B and C
A= $tsB= $tpC= $to
ABCresult
0XX0
10X0
1100
1111
240 128 my $st = RDF::Trine::Statement->new($ts, $tp, $to);
241 128 $self->add_statement($st, $context);
242 }
243 }
244 }
245 }
246 49 $self->end_bulk_ops();
247 }
248  
249 =item C<< add_iterator ( $iter ) >>
250  
251 Add triples from the statement iterator to the model.
252  
253 =cut
254  
255 0 ▶sub add_iterator {
Subroutine add_iterator: not called CC=4 SCAR=30.0
Pod documented
256 0 my $self = shift;
257 0 my $iter = shift;
258 0 ▶ unless (blessed($iter) and ($iter->is_graph)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($iter) and $iter->is_graph) 0 0 0
Condition0%0/3
builtin::blessed($iter) and $iter->is_graph
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($iter) and $iter->is_graph
builtin::blessed($iter) $iter->is_graph
Truth table0%0/3
A and B
A= builtin::blessed($iter)B= $iter->is_graph
ABresult
0X0
100
111
259 0 throw RDF::Trine::Error::MethodInvocationError -text => 'Cannot add a '. ref($iter) . ' iterator to a model, only graphs.';
260 }
261 0 $self->begin_bulk_ops();
262 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
263 0 $self->add_statement( $st );
264 }
265 0 $self->end_bulk_ops();
266 }
267  
268 =item C<< add_list ( @elements ) >>
269  
270 Adds an rdf:List to the model with the given elements. Returns the node object
271 that is the head of the list.
272  
273 =cut
274  
275 0 ▶sub add_list {
Subroutine add_list: not called CC=2 SCAR=17.9
Pod documented
276 0 my $self = shift;
277 0 my @elements = @_;
278 0 my $rdf = RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
279 0 ▶ if (scalar(@elements) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elements == 0) { } 0 0 0
280 0 return $rdf->nil;
281 } else {
282 0 my $head = RDF::Query::Node::Blank->new();
283 0 my $node = shift(@elements);
284 0 my $rest = $self->add_list( @elements );
285 0 $self->add_statement( RDF::Trine::Statement->new($head, $rdf->first, $node) );
286 0 $self->add_statement( RDF::Trine::Statement->new($head, $rdf->rest, $rest) );
287 0 return $head;
288 }
289 }
290  
291 =item C<< get_list ( $head ) >>
292  
293 Returns a list of nodes that are elements of the rdf:List represented by the
294 supplied head node.
295  
296 =cut
297  
298 1 ▶sub get_list {
Subroutine get_list: called CC=6 SCAR=18.7
Pod documented
299 1 my $self = shift;
300 1 my $head = shift;
301 1 my $rdf = RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
302 1 my @elements;
303 my %seen;
304 1 ▶ while (blessed($head) and not($head->isa('RDF::Trine::Node::Resource') and $head->uri_value eq $rdf->nil->uri_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($head) and not $head->isa("RDF::Trine::Node::Resource") && $head->uri_value eq $rdf->nil->uri_value) 291 1 292
Condition66%4/6
builtin::blessed($head) and not $head->isa("RDF::Trine::Node::Resource") && $head->uri_value eq $rdf->nil->uri_value
!ll&&!rl&&r
01291
$head->isa('RDF::Trine::Node::Resource') && $head->uri_value eq $rdf->nil->uri_value
!ll&&!rl&&r
29101
MC/DC33%1/3
builtin::blessed($head) and not $head->isa("RDF::Trine::Node::Resource") && $head->uri_value eq $rdf->nil->uri_value
builtin::blessed($head) $head->isa('RDF::Trine::Node::Resource') $head->uri_value eq $rdf->nil->uri_value
Truth table50%2/4
A and not(B && C)
A= builtin::blessed($head)B= $head->isa('RDF::Trine::Node::Resource')C= $head->uri_value eq $rdf->nil->uri_value
ABCresult
0XX0
1110
10X1
1101
305 291 ▶ if ($seen{ $head->as_string }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$head->as_string}++) 0 291 291
306 0 throw RDF::Trine::Error -text => "Loop found during rdf:List traversal";
307 }
308 291 my @n = $self->objects( $head, $rdf->first );
309 291 ▶ if (scalar(@n) != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @n != 1) 0 291 291
310 0 throw RDF::Trine::Error -text => "Invalid structure found during rdf:List traversal";
311 }
312 291 push(@elements, @n);
313 291 ($head) = $self->objects( $head, $rdf->rest );
314 }
315 1 return @elements;
316 }
317  
318 =item C<< remove_list ( $head [, orphan_check => 1] ) >>
319  
320 Removes the nodes of type rdf:List that make up the list. Optionally checks each node
321 before removal to make sure that it is not used in any other statements. Returns false
322 if the list was removed completely; returns the first remaining node if the removal
323 was abandoned because of an orphan check.
324  
325 =cut
326  
327 5 ▶sub remove_list {
Subroutine remove_list: called CC=18 SCAR=29.6
Pod documented
328 5 my $self = shift;
329 5 my $head = shift;
330 5 my $rdf = RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
331 5 my %args = @_;
332 5 my %seen;
333
334 5 ▶ while (blessed($head) and not($head->isa('RDF::Trine::Node::Resource') and $head->uri_value eq $rdf->nil->uri_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($head) and not $head->isa("RDF::Trine::Node::Resource") && $head->uri_value eq $rdf->nil->uri_value) 11 2 13
Condition66%4/6
builtin::blessed($head) and not $head->isa("RDF::Trine::Node::Resource") && $head->uri_value eq $rdf->nil->uri_value
!ll&&!rl&&r
0211
$head->isa('RDF::Trine::Node::Resource') && $head->uri_value eq $rdf->nil->uri_value
!ll&&!rl&&r
1102
MC/DC33%1/3
builtin::blessed($head) and not $head->isa("RDF::Trine::Node::Resource") && $head->uri_value eq $rdf->nil->uri_value
builtin::blessed($head) $head->isa('RDF::Trine::Node::Resource') $head->uri_value eq $rdf->nil->uri_value
Truth table50%2/4
A and not(B && C)
A= builtin::blessed($head)B= $head->isa('RDF::Trine::Node::Resource')C= $head->uri_value eq $rdf->nil->uri_value
ABCresult
0XX0
1110
10X1
1101
335 11 ▶ if ($seen{ $head->as_string }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$head->as_string}++) 0 11 11
336 0 throw RDF::Trine::Error -text => "Loop found during rdf:List traversal";
337 }
338 11 my $stream = $self->get_statements($head, undef, undef);
339 11 my %statements;
340 11 ▶ while (my $st = $stream->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $stream->next) 29 11 40
341 my $statement_type = {
342 $rdf->first->uri => 'rdf:first',
343 $rdf->rest->uri => 'rdf:rest',
344 $rdf->type->uri => 'rdf:type',
345 29 ▶ }->{$st->predicate->uri} || 'other';
Condition100%2/2
{$rdf->first->uri, 'rdf:first', $rdf->rest->uri, 'rdf:rest', $rdf->type->uri, 'rdf:type'}->{$st->predicate->uri} || 'other'
l!l
272
MC/DC100%1/1
{$rdf->first->uri, 'rdf:first', $rdf->rest->uri, 'rdf:rest', $rdf->type->uri, 'rdf:type'}->{$st->predicate->uri} || 'other'
{$rdf->first->uri, 'rdf:first', $rdf->rest->uri, 'rdf:rest', $rdf->type->uri, 'rdf:type'}->{$st->predicate->uri}
Truth table100%2/2
A
A= {$rdf->first->uri, 'rdf:first', $rdf->rest->uri, 'rdf:rest', $rdf->type->uri, 'rdf:type'}->{$st->predicate->uri}
Aresult
11
00
346 29 ▶ $statement_type = 'other'
Branch: 1 branch
BranchTrueFalseTotal
if $statement_type eq "rdf:type" and not $st->object->equal($rdf->List) 0 29 29
Condition66%2/3
$statement_type eq "rdf:type" and not $st->object->equal($rdf->List)
!ll&&!rl&&r
2540
MC/DC0%0/2
$statement_type eq "rdf:type" and not $st->object->equal($rdf->List)
$statement_type eq "rdf:type" not $st->object->equal($rdf->List)
Truth table66%2/3
A and not(B)
A= $statement_type eq "rdf:type"B= not $st->object->equal($rdf->List)
ABresult
0X0
100
111
347 if $statement_type eq 'rdf:type' && !$st->object->equal($rdf->List);
348 29 push @{$statements{$statement_type}}, $st;
349 }
350 11 ▶ if ($args{orphan_check}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"orphan_check"}) 3 8 11
351 3 ▶ return $head if defined $statements{other} && scalar(@{ $statements{other} }) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $statements{"other"} and scalar @{$statements{"other"};} > 0 1 2 3
Condition66%2/3
defined $statements{"other"} and scalar @{$statements{"other"};} > 0
!ll&&!rl&&r
201
MC/DC50%1/2
defined $statements{"other"} and scalar @{$statements{"other"};} > 0
defined $statements{"other"} scalar @{$statements{"other"};} > 0
Truth table66%2/3
A and B
A= defined $statements{"other"}B= scalar @{$statements{"other"};} > 0
ABresult
0X0
100
111
352 2 ▶ return $head if $self->count_statements(undef, undef, $head) > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->count_statements(undef, undef, $head) > 0 1 1 2
353 }
354 9 ▶ unless (defined $statements{'rdf:first'} and defined $statements{'rdf:rest'} and scalar(@{$statements{'rdf:first'} })==1 and scalar(@{ $statements{'rdf:rest'} })==1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $statements{"rdf:first"} and defined $statements{"rdf:rest"} and scalar @{$statements{"rdf:first"};} == 1 and scalar @{$statements{"rdf:rest"};} == 1) 1 8 9
Condition55%5/9
defined $statements{"rdf:first"} and defined $statements{"rdf:rest"} and scalar @{$statements{"rdf:first"};} == 1 and scalar @{$statements{"rdf:rest"};} == 1
!ll&&!rl&&r
108
defined $statements{"rdf:first"} and defined $statements{"rdf:rest"} and scalar @{$statements{"rdf:first"};} == 1
!ll&&!rl&&r
018
defined $statements{"rdf:first"} and defined $statements{"rdf:rest"}
!ll&&!rl&&r
009
MC/DC25%1/4
defined $statements{"rdf:first"} and defined $statements{"rdf:rest"} and scalar @{$statements{"rdf:first"};} == 1 and scalar @{$statements{"rdf:rest"};} == 1
defined $statements{"rdf:first"} defined $statements{"rdf:rest"} scalar @{$statements{"rdf:first"};} == 1 scalar @{$statements{"rdf:rest"};} == 1
Truth table40%2/5
A and B and C and D
A= defined $statements{"rdf:first"}B= defined $statements{"rdf:rest"}C= scalar @{$statements{"rdf:first"};} == 1D= scalar @{$statements{"rdf:rest"};} == 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
355 1 throw RDF::Trine::Error -text => "Invalid structure found during rdf:List traversal";
356 }
357 $self->remove_statement($_)
358 8 foreach (@{$statements{'rdf:first'}}, @{$statements{'rdf:rest'}}, @{$statements{'rdf:type'}});
359
360 8 $head = $statements{'rdf:rest'}->[0]->object;
361 }
362
363 2 return;
364 }
365  
366 =item C<< get_sequence ( $seq ) >>
367  
368 Returns a list of nodes that are elements of the rdf:Seq sequence.
369  
370 =cut
371  
372 0 ▶sub get_sequence {
Subroutine get_sequence: not called CC=4 SCAR=30.0
Pod documented
373 0 my $self = shift;
374 0 my $head = shift;
375 0 my $rdf = RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
376 0 my @elements;
377 0 my $i = 1;
378 0 while (1) {
379 0 my $method = '_' . $i;
380 0 my (@elem) = $self->objects( $head, $rdf->$method() );
381 0 ▶ unless (scalar(@elem)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @elem) 0 0 0
382 0 last;
383 }
384 0 ▶ if (scalar(@elem) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @elem > 1) 0 0 0
385 0 my $count = scalar(@elem);
386 0 throw RDF::Trine::Error -text => "Invalid structure found during rdf:Seq access: $count elements found for element $i";
387 }
388 0 my $elem = $elem[0];
389 0 ▶ last unless (blessed($elem));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($elem) 0 0 0
390 0 push(@elements, $elem);
391 0 $i++;
392 }
393 0 return @elements;
394 }
395  
396 =item C<< remove_statement ( $statement [, $context]) >>
397  
398 Removes the specified C<< $statement >> from the rdf store.
399  
400 =cut
401  
402 26 ▶sub remove_statement {
Subroutine remove_statement: called CC=3 SCAR=11.1
Pod documented
403 26 my $self = shift;
404 26 my @args = @_;
405 26 ▶ if (my $log = $self->logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $self->logger) 1 25 26
406 1 my ($st, $context) = @args;
407 1 ▶ if (defined($context)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context) 0 1 1
408 0 $st = RDF::Trine::Statement::Quad->new(($st->nodes)[0..2], $context);
409 }
410 1 $log->delete($st);
411 }
412 26 return $self->_store->remove_statement( @args );
413 }
414  
415 =item C<< remove_statements ( $subject, $predicate, $object [, $context] ) >>
416  
417 Removes all statements matching the supplied C<< $statement >> pattern from the rdf store.
418  
419 =cut
420  
421 6 ▶sub remove_statements {
Subroutine remove_statements: called CC=3 SCAR=12.0
Pod documented
422 6 my $self = shift;
423 6 ▶ if (my $log = $self->logger) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $log = $self->logger) 0 6 6
424 0 $log->delete($_) foreach (@_);
425 }
426 6 return $self->_store->remove_statements( @_ );
427 }
428  
429 =item C<< size >>
430  
431 Returns the number of statements in the model.
432  
433 =cut
434  
435 26 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
436 26 my $self = shift;
437 26 $self->end_bulk_ops();
438 26 return $self->count_statements(undef, undef, undef, undef);
439 }
440  
441 =item C<< etag >>
442  
443 If the model is based on a store that has the capability and knowledge to
444 support caching, this method returns a persistent token that will remain
445 consistent as long as the store's data doesn't change. This token is acceptable
446 for use as an HTTP ETag.
447  
448 =cut
449  
450 3 ▶sub etag {
Subroutine etag: called CC=2 SCAR=7.4
Pod documented
451 3 my $self = shift;
452 3 my $store = $self->_store;
453 3 ▶ if ($store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) 3 0 3
454 3 return $store->etag;
455 }
456 0 return;
457 }
458  
459 =item C<< supports ( [ $feature ] ) >>
460  
461 If C<< $feature >> is specified, returns true if the feature is supported by the
462 underlying store, false otherwise. If C<< $feature >> is not specified, returns
463 a list of supported features.
464  
465 =cut
466  
467 0 ▶sub supports {
Subroutine supports: not called CC=2 SCAR=17.9
Pod documented
468 0 my $self = shift;
469 0 my $store = $self->_store;
470 0 ▶ if ($store) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) 0 0 0
471 0 return $store->supports( @_ );
472 }
473 0 return;
474 }
475  
476 =item C<< count_statements ( $subject, $predicate, $object ) >>
477  
478 Returns a count of all the statements matching the specified subject,
479 predicate and objects. Any of the arguments may be undef to match any value.
480  
481 =cut
482  
483 710 ▶sub count_statements {
Subroutine count_statements: called CC=5 SCAR=16.1
Pod documented
484 710 my $self = shift;
485 710 $self->end_bulk_ops();
486  
487 710 ▶ if (scalar(@_) >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 4) 58 652 710
488 58 my $graph = $_[3];
489 58 ▶ if (blessed($graph) and $graph->isa('RDF::Trine::Node::Resource') and $graph->uri_value eq 'tag:gwilliams@cpan.org,2010-01-01:RT:ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource") and $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL") 1 57 58
Condition100%6/6
builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource") and $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
!ll&&!rl&&r
38191
builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
36220
MC/DC100%3/3
builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource") and $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
builtin::blessed($graph) $graph->isa("RDF::Trine::Node::Resource") $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
Truth table100%4/4
A and B and C
A= builtin::blessed($graph)B= $graph->isa("RDF::Trine::Node::Resource")C= $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
ABCresult
0XX0
10X0
1100
1111
490 1 $_[3] = undef;
491 }
492 }
493 710 return $self->_store->count_statements( @_ );
494 }
495  
496 =item C<< get_statements ($subject, $predicate, $object [, $context] ) >>
497  
498 Returns an L<iterator|RDF::Trine::Iterator> of all statements matching the specified
499 subject, predicate and objects from the rdf store. Any of the arguments may be undef
500 to match any value.
501  
502 If three or fewer arguments are given, the statements returned will be matched
503 based on triple semantics (the graph union of triples from all the named
504 graphs). If four arguments are given (even if C<< $context >> is undef),
505 statements will be matched based on quad semantics (the union of all quads in
506 the underlying store).
507  
508 =cut
509  
510 2527 ▶sub get_statements {
Subroutine get_statements: called CC=9 SCAR=22.1
Pod documented
511 2527 my $self = shift;
512 2527 $self->end_bulk_ops();
513
514 2527 my @pos = qw(subject predicate object graph);
515 2527 foreach my $i (0 .. $#_) {
516 9470 my $n = $_[$i];
517 9470 ▶ next unless defined($n); # undef is OK
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 2882 6588 9470
518 6588 ▶ next if (blessed($n) and $n->isa('RDF::Trine::Node')); # node objects are OK
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($n) and $n->isa("RDF::Trine::Node") 6584 4 6588
Condition66%2/3
builtin::blessed($n) and $n->isa("RDF::Trine::Node")
!ll&&!rl&&r
406584
MC/DC50%1/2
builtin::blessed($n) and $n->isa("RDF::Trine::Node")
builtin::blessed($n) $n->isa("RDF::Trine::Node")
Truth table66%2/3
A and B
A= builtin::blessed($n)B= $n->isa("RDF::Trine::Node")
ABresult
0X0
100
111
519 4 my $pos = $pos[$i];
520 4 local($Data::Dumper::Indent) = 0;
521 4 my $ser = Data::Dumper->Dump([$n], [$pos]);
522 4 throw RDF::Trine::Error::MethodInvocationError -text => "get_statements called with a value that isn't undef or a node object: $ser";
523 }
524
525 2523 ▶ if (scalar(@_) >= 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ >= 4) 2169 354 2523
526 2169 my $graph = $_[3];
527 2169 ▶ if (blessed($graph) and $graph->isa('RDF::Trine::Node::Resource') and $graph->uri_value eq 'tag:gwilliams@cpan.org,2010-01-01:RT:ALL') {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource") and $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL") 0 2169 2169
Condition83%5/6
builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource") and $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
!ll&&!rl&&r
2154150
builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource")
!ll&&!rl&&r
136678815
MC/DC0%0/3
builtin::blessed($graph) and $graph->isa("RDF::Trine::Node::Resource") and $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
builtin::blessed($graph) $graph->isa("RDF::Trine::Node::Resource") $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
Truth table75%3/4
A and B and C
A= builtin::blessed($graph)B= $graph->isa("RDF::Trine::Node::Resource")C= $graph->uri_value eq "tag:gwilliams\@cpan.org,2010-01-01:RT:ALL"
ABCresult
0XX0
10X0
1100
1111
528 0 $_[3] = undef;
529 }
530 }
531 2523 return $self->_store->get_statements( @_ );
532 }
533  
534 =item C<< get_pattern ( $bgp [, $context] [, %args ] ) >>
535  
536 Returns a stream object of all bindings matching the specified graph pattern.
537  
538 If C<< $context >> is given, restricts BGP matching to only quads with the
539 C<< $context >> value.
540  
541 C<< %args >> may contain an 'orderby' key-value pair to request a specific
542 ordering based on variable name. The value for the 'orderby' key should be an
543 ARRAY reference containing variable name and direction ('ASC' or 'DESC') tuples.
544 A valid C<< %args >> hash, therefore, might look like
545 C<< orderby => [qw(name ASC)] >> (corresponding to a SPARQL-like request to
546 'ORDER BY ASC(?name)').
547  
548 =cut
549  
550 956 ▶sub get_pattern {
Subroutine get_pattern: called CC=16 SCAR=27.7
Pod documented
551 956 my $self = shift;
552 956 my $bgp = shift;
553 956 my $context = shift;
554 956 my @args = @_;
555 956 my %args = @args;
556
557 956 $self->end_bulk_ops();
558 956 ▶ my (@triples) = ($bgp->isa('RDF::Trine::Statement') or $bgp->isa('RDF::Query::Algebra::Filter'))
Branch: 1 branch
BranchTrueFalseTotal
$bgp->isa('RDF::Trine::Statement') || $bgp->isa('RDF::Query::Algebra::Filter') ? : 18 938 956
Condition66%2/3
$bgp->isa("RDF::Trine::Statement") or $bgp->isa("RDF::Query::Algebra::Filter")
l!l&&r!l&&!r
180938
MC/DC50%1/2
$bgp->isa("RDF::Trine::Statement") or $bgp->isa("RDF::Query::Algebra::Filter")
$bgp->isa("RDF::Trine::Statement") $bgp->isa("RDF::Query::Algebra::Filter")
Truth table66%2/3
A or B
A= $bgp->isa("RDF::Trine::Statement")B= $bgp->isa("RDF::Query::Algebra::Filter")
ABresult
1X1
011
000
559 ? $bgp
560 : $bgp->triples;
561 956 ▶ unless (@triples) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@triples) 5 951 956
562 5 throw RDF::Trine::Error::CompilationError -text => 'Cannot call get_pattern() with empty pattern';
563 }
564
565 951 my $store = $self->_store;
566 # while almost all models will delegate get_pattern() to the underlying
567 # store object, in some cases this isn't possible (union models don't have
568 # a single store, so have to fall back to the model-specific get_pattern()
569 # implementation).
570 951 ▶ if (blessed($store) and $store->can('get_pattern')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($store) and $store->can('get_pattern')) { } 41 910 951
Condition100%3/3
builtin::blessed($store) and $store->can("get_pattern")
!ll&&!rl&&r
390741
MC/DC100%2/2
builtin::blessed($store) and $store->can("get_pattern")
builtin::blessed($store) $store->can("get_pattern")
Truth table100%3/3
A and B
A= builtin::blessed($store)B= $store->can("get_pattern")
ABresult
0X0
100
111
571 41 return $self->_store->get_pattern( $bgp, $context, @args );
572 } else {
573 910 ▶ if ($bgp->isa('RDF::Trine::Pattern')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bgp->isa("RDF::Trine::Pattern")) 904 6 910
574 904 $bgp = $bgp->sort_for_join_variables();
575 }
576 910 my $iter = $self->_get_pattern( $bgp, $context );
577 910 ▶ if (my $ob = $args{orderby}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ob = $args{"orderby"}) 901 9 910
578 901 my @order = @$ob;
579 901 ▶ if (scalar(@order) % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@order) % 2) 1 900 901
580 1 throw RDF::Trine::Error::MethodInvocationError -text => "Invalid arguments to orderby argument in get_pattern";
581 }
582
583 900 my @results = $iter->get_all();
584 900 my $order_vars = scalar(@order) / 2;
585 900 my %seen;
586 900 foreach my $r (@results) {
587 1688 foreach my $var (keys %$r) {
588 6385 $seen{$var}++;
589 }
590 }
591
592 @results = sort {
593 900 my $r = 0;
594 1445 foreach my $i (0 .. ($order_vars-1)) {
595 2359 my $var = $order[$i*2];
596 2359 my $rev = ($order[$i*2+1] =~ /DESC/i);
597 2359 $r = RDF::Trine::Node::compare( $a->{$var}, $b->{$var} );
598 2359 ▶ $r *= -1 if ($rev);
Branch: 1 branch
BranchTrueFalseTotal
if $rev 1 2358 2359
599 2359 ▶ last if ($r);
Branch: 1 branch
BranchTrueFalseTotal
if $r 1445 914 2359
600 }
601 $r;
602 } @results;
603
604 900 my @sortedby;
605 900 foreach my $i (0 .. ($order_vars-1)) {
606 2634 my $var = $order[$i*2];
607 2634 my $dir = $order[$i*2+1];
608 2634 ▶ push(@sortedby, $var, $dir) if ($seen{$var});
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$var} 2600 34 2634
609 }
610 900 $iter = RDF::Trine::Iterator::Bindings->new(\@results, undef, sorted_by => \@sortedby);
611 }
612 909 return $iter;
613 }
614 }
615  
616 =item C<< get_sparql ( $sparql ) >>
617  
618 Returns a stream object of all bindings matching the specified graph pattern.
619  
620 =cut
621  
622 0 ▶sub get_sparql {
Subroutine get_sparql: not called CC=1 SCAR=6.9
Pod documented
623 0 my $self = shift;
624 0 return $self->_store->get_sparql( @_ );
625 }
626  
627 928 ▶sub _get_pattern {
Subroutine _get_pattern: called CC=14 SCAR=26.5
628 928 my $self = shift;
629 928 my $bgp = shift;
630 928 my $context = shift;
631 928 my @args = @_;
632
633 928 ▶ my (@triples) = ($bgp->isa('RDF::Trine::Statement') or $bgp->isa('RDF::Query::Algebra::Filter'))
Branch: 1 branch
BranchTrueFalseTotal
$bgp->isa('RDF::Trine::Statement') || $bgp->isa('RDF::Query::Algebra::Filter') ? : 6 922 928
Condition66%2/3
$bgp->isa("RDF::Trine::Statement") or $bgp->isa("RDF::Query::Algebra::Filter")
l!l&&r!l&&!r
60922
MC/DC50%1/2
$bgp->isa("RDF::Trine::Statement") or $bgp->isa("RDF::Query::Algebra::Filter")
$bgp->isa("RDF::Trine::Statement") $bgp->isa("RDF::Query::Algebra::Filter")
Truth table66%2/3
A or B
A= $bgp->isa("RDF::Trine::Statement")B= $bgp->isa("RDF::Query::Algebra::Filter")
ABresult
1X1
011
000
634 ? $bgp
635 : $bgp->triples;
636 928 ▶ if (1 == scalar(@triples)) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 == scalar @triples) { } 919 9 928
637 919 my $t = shift(@triples);
638 919 my @nodes = $t->nodes;
639 919 my %vars;
640 919 my @names = qw(subject predicate object context);
641 919 foreach my $n (0 .. $#nodes) {
642 3545 ▶ if ($nodes[$n]->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[$n]->isa("RDF::Trine::Node::Variable")) 3441 104 3545
643 3441 $vars{ $names[ $n ] } = $nodes[$n]->name;
644 }
645 }
646 919 ▶ if ($context) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context) 0 919 919
647 0 $nodes[3] = $context;
648 }
649 919 my $iter = $self->get_statements( @nodes );
650 919 my @vars = values %vars;
651 2643 ▶ my $sub = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
652 2643 my $row = $iter->next;
653 2643 ▶ return unless ($row);
Branch: 1 branch
BranchTrueFalseTotal
unless $row 919 1724 2643
654 1724 my %data = map { $vars{ $_ } => $row->$_() } (keys %vars);
655 1724 return RDF::Trine::VariableBindings->new( \%data );
656 919 };
657 919 return RDF::Trine::Iterator::Bindings->new( $sub, \@vars );
658 } else {
659 9 my $t = pop(@triples);
660 9 my $rhs = $self->_get_pattern( RDF::Trine::Pattern->new( $t ), $context, @args );
661 9 my $lhs = $self->_get_pattern( RDF::Trine::Pattern->new( @triples ), $context, @args );
662 9 my @inner;
663 9 ▶ while (my $row = $rhs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $rhs->next) 21 9 30
664 21 push(@inner, $row);
665 }
666 9 my @results;
667 9 ▶ while (my $row = $lhs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $lhs->next) 15 9 24
668 15 RESULT: foreach my $irow (@inner) {
669 34 my %keysa;
670 34 my @keysa = keys %$irow;
671 34 @keysa{ @keysa } = (1) x scalar(@keysa);
672 34 my @shared = grep { exists $keysa{ $_ } } (keys %$row);
673 34 foreach my $key (@shared) {
674 25 my $val_a = $irow->{ $key };
675 25 my $val_b = $row->{ $key };
676 25 ▶ next unless (defined($val_a) and defined($val_b));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val_a and defined $val_b 0 25 25
Condition33%1/3
defined $val_a and defined $val_b
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $val_a and defined $val_b
defined $val_a defined $val_b
Truth table33%1/3
A and B
A= defined $val_aB= defined $val_b
ABresult
0X0
100
111
677 25 my $equal = $val_a->equal( $val_b );
678 25 ▶ unless ($equal) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($equal) 12 13 25
679 12 next RESULT;
680 }
681 }
682
683 22 my $jrow = { (map { $_ => $irow->{$_} } grep { defined($irow->{$_}) } keys %$irow), (map { $_ => $row->{$_} } grep { defined($row->{$_}) } keys %$row) };
684 22 push(@results, RDF::Trine::VariableBindings->new($jrow));
685 }
686 }
687 9 my $result = RDF::Trine::Iterator::Bindings->new( \@results, [ $bgp->referenced_variables ] );
688 9 return $result;
689 }
690 }
691  
692 =item C<< get_graphs >>
693  
694 =item C<< get_contexts >>
695  
696 Returns an L<iterator|RDF::Trine::Iterator> containing the nodes representing
697 the named graphs in the model.
698  
699 =cut
700  
701 2 ▶sub get_contexts {
Subroutine get_contexts: called CC=2 SCAR=7.1
Pod documented
702 2 my $self = shift;
703 2 my $store = $self->_store;
704 2 $self->end_bulk_ops();
705 2 my $iter = $store->get_contexts( @_ );
706 2 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2 2
707 0 return $iter->get_all;
708 } else {
709 2 return $iter;
710 }
711 }
712 68 *get_graphs = \&get_contexts;
713  
714 =item C<< as_stream >>
715  
716 Returns an L<iterator|RDF::Trine::Iterator> containing every statement in the model.
717  
718 =cut
719  
720 788 ▶sub as_stream {
Subroutine as_stream: called CC=1 SCAR=0.0
Pod documented
721 788 my $self = shift;
722 788 $self->end_bulk_ops();
723 788 my $st = RDF::Trine::Statement::Quad->new( map { variable($_) } qw(s p o g) );
724 788 my $pat = RDF::Trine::Pattern->new( $st );
725 788 my $stream = $self->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
726 788 return $stream->as_statements( qw(s p o g) );
727 }
728  
729 =item C<< as_hashref >>
730  
731 Returns a hashref representing the model in an RDF/JSON-like manner.
732  
733 A graph like this (in Turtle):
734  
735 @prefix ex: <http://example.com/> .
736
737 ex:subject1
738 ex:predicate1
739 "Foo"@en ,
740 "Bar"^^ex:datatype1 .
741
742 _:bnode1
743 ex:predicate2
744 ex:object2 ;
745 ex:predicate3 ;
746 _:bnode3 .
747  
748 Is represented like this as a hashref:
749  
750 {
751 "http://example.com/subject1" => {
752 "http://example.com/predicate1" => [
753 { 'type'=>'literal', 'value'=>"Foo", 'lang'=>"en" },
754 { 'type'=>'literal', 'value'=>"Bar", 'datatype'=>"http://example.com/datatype1" },
755 ],
756 },
757 "_:bnode1" => {
758 "http://example.com/predicate2" => [
759 { 'type'=>'uri', 'value'=>"http://example.com/object2" },
760 ],
761 "http://example.com/predicate2" => [
762 { 'type'=>'bnode', 'value'=>"_:bnode3" },
763 ],
764 },
765 }
766  
767 Note that the type of subjects (resource or blank node) is indicated
768 entirely by the convention of starting blank nodes with "_:".
769  
770 This hashref structure is compatible with RDF/JSON and with the ARC2
771 library for PHP.
772  
773 =cut
774  
775 2 ▶sub as_hashref {
Subroutine as_hashref: called CC=1 SCAR=0.0
Pod documented
776 2 my $self = shift;
777 2 $self->end_bulk_ops();
778 2 return $self->as_stream->as_hashref;
779 }
780  
781 =item C<< as_graphviz >>
782  
783 Returns a L<GraphViz> object of the RDF graph of this model, ignoring graph
784 names/contexts.
785  
786 This method will attempt to load the L<GraphViz> module at runtime and will fail
787 if the module is unavailable.
788  
789 =cut
790  
791 0 ▶sub as_graphviz {
Subroutine as_graphviz: not called CC=5 SCAR=34.0
Pod documented
792 0 my $self = shift;
793 0 require GraphViz;
794 0 my $g = GraphViz->new();
795 0 my %seen;
796 0 my $iter = $self->as_stream;
797 0 ▶ while (my $t = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $t = $iter->next) 0 0 0
798 0 my @nodes;
799 0 foreach my $pos (qw(subject object)) {
800 0 my $n = $t->$pos();
801 0 ▶ my $label = ($n->isa('RDF::Trine::Node::Literal')) ? $n->literal_value : $n->as_string;
Branch: 1 branch
BranchTrueFalseTotal
$n->isa('RDF::Trine::Node::Literal') ? : 0 0 0
802 0 push(@nodes, $label);
803 0 ▶ unless ($seen{ $label }++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$label}++) 0 0 0
804 0 $g->add_node( $label );
805 }
806 }
807 0 $g->add_edge( @nodes, label => $t->predicate->as_string );
808 }
809 0 return $g;
810 }
811  
812 =back
813  
814 =head2 Node-Centric Graph API
815  
816 =over 4
817  
818 =item C<< subjects ( $predicate, $object ) >>
819  
820 Returns a list of the nodes that appear as the subject of statements with the
821 specified C<< $predicate >> and C<< $object >>. Either of the two arguments may
822 be undef to signify a wildcard.
823  
824 =cut
825  
826 14 ▶sub subjects {
Subroutine subjects: called CC=3 SCAR=11.0
Pod documented
827 14 my $self = shift;
828 14 my $pred = shift;
829 14 my $obj = shift;
830 14 my $graph = shift;
831 14 $self->end_bulk_ops();
832 14 my $iter = $self->get_statements( undef, $pred, $obj, $graph );
833 14 my %nodes;
834 14 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 22 14 36
835 22 my $subj = $st->subject;
836 22 $nodes{ $subj->as_string } = $subj;
837 }
838 14 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 10 4 14
839 10 return values(%nodes);
840 } else {
841 4 return RDF::Trine::Iterator->new( [values(%nodes)] );
842 }
843 }
844  
845 =item C<< predicates ( $subject, $object ) >>
846  
847 Returns a list of the nodes that appear as the predicate of statements with the
848 specified C<< $subject >> and C<< $object >>. Either of the two arguments may
849 be undef to signify a wildcard.
850  
851 =cut
852  
853 12 ▶sub predicates {
Subroutine predicates: called CC=3 SCAR=11.0
Pod documented
854 12 my $self = shift;
855 12 my $subj = shift;
856 12 my $obj = shift;
857 12 my $graph = shift;
858 12 $self->end_bulk_ops();
859 12 my $iter = $self->get_statements( $subj, undef, $obj, $graph );
860 12 my %nodes;
861 12 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 20 12 32
862 20 my $pred = $st->predicate;
863 20 $nodes{ $pred->as_string } = $pred;
864 }
865 12 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 8 4 12
866 8 return values(%nodes);
867 } else {
868 4 return RDF::Trine::Iterator->new( [values(%nodes)] );
869 }
870 }
871  
872 =item C<< objects ( $subject, $predicate [, $graph ] [, %options ] ) >>
873  
874 Returns a list of the nodes that appear as the object of statements with the
875 specified C<< $subject >> and C<< $predicate >>. Either of the two arguments
876 may be undef to signify a wildcard. You can further filter objects using the
877 C<< %options >> argument. Keys in C<< %options >> indicate the restriction type
878 and may be 'type', 'language', or 'datatype'. The value of the 'type' key may be
879 one of 'node', 'nil', 'blank', 'resource', 'literal', or 'variable'. The use of
880 either 'language' or 'datatype' restrict objects to literal nodes with a
881 specific language or datatype value, respectively.
882  
883 =cut
884  
885 1319 ▶sub objects {
Subroutine objects: called CC=18 SCAR=29.0
Pod documented
886 1319 my $self = shift;
887 1319 my $subj = shift;
888 1319 my $pred = shift;
889 1319 ▶ my ($graph, %options) = (@_ % 2 == 0) ? (undef, @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 == 0 ? : 1319 0 1319
890 1319 my $type = $options{type};
891 1319 ▶ $type = 'literal' if ($options{language} or $options{datatype});
Branch: 1 branch
BranchTrueFalseTotal
if $options{"language"} or $options{"datatype"} 3 1316 1319
Condition100%3/3
$options{"language"} or $options{"datatype"}
l!l&&r!l&&!r
121316
MC/DC100%2/2
$options{"language"} or $options{"datatype"}
$options{"language"} $options{"datatype"}
Truth table100%3/3
A or B
A= $options{"language"}B= $options{"datatype"}
ABresult
1X1
011
000
892 1319 ▶ if ($options{datatype} and not blessed($options{datatype})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"datatype"} and not builtin::blessed($options{"datatype"})) 1 1318 1319
Condition100%3/3
$options{"datatype"} and not builtin::blessed($options{"datatype"})
!ll&&!rl&&r
131711
MC/DC100%2/2
$options{"datatype"} and not builtin::blessed($options{"datatype"})
$options{"datatype"} not builtin::blessed($options{"datatype"})
Truth table100%3/3
A and not(B)
A= $options{"datatype"}B= not builtin::blessed($options{"datatype"})
ABresult
0X0
100
111
893 1 $options{datatype} = RDF::Trine::Node::Resource->new($options{datatype});
894 }
895
896 1319 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 6 1313 1319
897 6 ▶ if ($type =~ /^(node|nil|blank|resource|literal|variable)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^(node|nil|blank|resource|literal|variable)$/) { } 6 0 6
898 6 $type = "is_$type";
899 } else {
900 0 throw RDF::Trine::Error::CompilationError -text => "unknown type"
901 }
902 }
903 1319 $self->end_bulk_ops();
904 1319 my $iter = $self->get_statements( $subj, $pred, undef, $graph );
905 1319 my %nodes;
906 1319 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 1351 1319 2670
907 1351 my $obj = $st->object;
908 1351 ▶ if (defined $type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type) 30 1321 1351
909 30 ▶ next unless $obj->$type;
Branch: 1 branch
BranchTrueFalseTotal
unless $obj->$type 16 14 30
910 14 ▶ if ($options{language}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options{'language'}) { } 3 11 14
elsif ($options{'datatype'}) { } 6 5 11
911 3 my $lang = $obj->literal_value_language;
912 3 ▶ next unless ($lang and $lang eq $options{language});
Branch: 1 branch
BranchTrueFalseTotal
unless $lang and $lang eq $options{"language"} 2 1 3
Condition66%2/3
$lang and $lang eq $options{"language"}
!ll&&!rl&&r
201
MC/DC50%1/2
$lang and $lang eq $options{"language"}
$lang $lang eq $options{"language"}
Truth table66%2/3
A and B
A= $langB= $lang eq $options{"language"}
ABresult
0X0
100
111
913 } elsif ($options{datatype}) {
914 6 my $dt = $obj->literal_datatype;
915 6 ▶ next unless ($dt and $dt eq $options{datatype}->uri_value);
Branch: 1 branch
BranchTrueFalseTotal
unless $dt and $dt eq $options{"datatype"}->uri_value 4 2 6
Condition66%2/3
$dt and $dt eq $options{"datatype"}->uri_value
!ll&&!rl&&r
402
MC/DC50%1/2
$dt and $dt eq $options{"datatype"}->uri_value
$dt $dt eq $options{"datatype"}->uri_value
Truth table66%2/3
A and B
A= $dtB= $dt eq $options{"datatype"}->uri_value
ABresult
0X0
100
111
916 }
917 }
918 1329 $nodes{ $obj->as_string } = $obj;
919 }
920 1319 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 1315 4 1319
921 1315 return values(%nodes);
922 } else {
923 4 return RDF::Trine::Iterator->new( [values(%nodes)] );
924 }
925 }
926  
927 =item C<< objects_for_predicate_list ( $subject, @predicates ) >>
928  
929 Given the RDF::Trine::Node objects C<< $subject >> and C<< @predicates >>,
930 finds all matching triples in the model with the specified subject and any
931 of the given predicates, and returns a list of object values (in the partial
932 order given by the ordering of C<< @predicates >>).
933  
934 =cut
935  
936 94 ▶sub objects_for_predicate_list {
Subroutine objects_for_predicate_list: called CC=4 SCAR=13.9
Pod documented
937 94 my $self = shift;
938 94 my $node = shift;
939 94 my @preds = @_;
940 94 $self->end_bulk_ops();
941 94 my @objects;
942 94 foreach my $p (@preds) {
943 137 my $iter = $self->get_statements( $node, $p );
944 137 ▶ while (my $s = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $iter->next) 137 137 274
945 137 ▶ if (not(wantarray)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray) { } 0 137 137
946 0 return $s->object;
947 } else {
948 137 push( @objects, $s->object );
949 }
950 }
951 }
952 94 return @objects;
953 }
954  
955 =item C<< bounded_description ( $node ) >>
956  
957 Returns an RDF::Trine::Iterator::Graph object over the bounded description
958 triples for C<< $node >> (all triples resulting from a graph traversal starting
959 with C<< node >> and stopping at non-blank nodes).
960  
961 =cut
962  
963 8 ▶sub bounded_description {
Subroutine bounded_description: called CC=1 SCAR=0.0
Pod documented
964 8 my $self = shift;
965 8 my $node = shift;
966 8 $self->end_bulk_ops();
967 8 my %seen;
968 8 my @nodes = $node;
969 8 my @statements;
970 35 ▶ my $sub = sub {
Subroutine __ANON__: called CC=12 SCAR=25.0
971 35 ▶ return if (not(@statements) and not(@nodes));
Branch: 1 branch
BranchTrueFalseTotal
unless @statements or @nodes 5 30 35
Condition100%3/3
@statements or @nodes
l!l&&r!l&&!r
13175
MC/DC100%2/2
@statements or @nodes
@statements @nodes
Truth table100%3/3
A or B
A= @statementsB= @nodes
ABresult
1X1
011
000
972 30 while (1) {
973 33 ▶ if (not(@statements)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@statements) 20 13 33
974 20 my $l = Log::Log4perl->get_logger("rdf.trine.model");
975 20 ▶ return unless (scalar(@nodes));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @nodes 3 17 20
976 17 my $n = shift(@nodes);
977 # warn "CBD handling node " . $n->sse . "\n";
978 17 ▶ next if ($seen{ $n->sse });
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$n->sse} 1 16 17
979 16 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
980 16 my $st = RDF::Trine::Statement->new( $n, map { variable($_) } qw(p o) );
981 16 my $pat = RDF::Trine::Pattern->new( $st );
982 16 my $sts = $self->get_pattern( $pat, undef, orderby => [ qw(p ASC o ASC) ] );
983 # my $sts = $stream->as_statements( qw(s p o) );
984 # my $sts = $self->get_statements( $n );
985 16 my @s = grep { not($seen{$_->{'o'}->sse}) } $sts->get_all;
986 # warn "+ " . $_->as_string . "\n" for (@s);
987 16 push(@statements, map { RDF::Trine::Statement->new($n, @{ $_ }{qw(p o)}) } @s);
988 0 ▶ } catch RDF::Trine::Error::UnimplementedError with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
989 0 $l->debug('[model] Ignored UnimplementedError in bounded_description: ' . $_[0]->{'-text'});
990 16 };
991 16 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=6.9
992 16 my $st = RDF::Trine::Statement->new( (map { variable($_) } qw(s p)), $n );
993 16 my $pat = RDF::Trine::Pattern->new( $st );
994 16 my $sts = $self->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC) ] );
995 # my $sts = $stream->as_statements( qw(s p o) );
996 # my $sts = $self->get_statements( undef, undef, $n );
997 16 ▶ my @s = grep { not($seen{$_->{'s'}->sse}) and not($_->{'s'}->equal($n)) } $sts->get_all;
Condition100%3/3
$seen{$_->{'s'}->sse} || $_->{'s'}->equal($n)
l!l&&r!l&&!r
931
MC/DC100%2/2
$seen{$_->{'s'}->sse} || $_->{'s'}->equal($n)
$seen{$_->{'s'}->sse} $_->{'s'}->equal($n)
Truth table100%3/3
A || B
A= $seen{$_->{'s'}->sse}B= $_->{'s'}->equal($n)
ABresult
1X1
011
000
998 # warn "- " . $_->as_string . "\n" for (@s);
999 16 push(@statements, map { RDF::Trine::Statement->new(@{ $_ }{qw(s p)}, $n) } @s);
1000 0 ▶ } catch RDF::Trine::Error::UnimplementedError with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1001 0 $l->debug('[model] Ignored UnimplementedError in bounded_description: ' . $_[0]->{'-text'});
1002 16 };
1003 16 $seen{ $n->sse }++
1004 }
1005 29 ▶ last if (scalar(@statements));
Branch: 1 branch
BranchTrueFalseTotal
if scalar @statements 27 2 29
1006 }
1007 27 ▶ return unless (scalar(@statements));
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @statements 0 27 27
1008 27 my $st = shift(@statements);
1009 27 ▶ if ($st->object->isa('RDF::Trine::Node::Blank') and not($seen{ $st->object->sse })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->object->isa("RDF::Trine::Node::Blank") and not $seen{$st->object->sse}) 9 18 27
Condition66%2/3
$st->object->isa("RDF::Trine::Node::Blank") and not $seen{$st->object->sse}
!ll&&!rl&&r
1809
MC/DC50%1/2
$st->object->isa("RDF::Trine::Node::Blank") and not $seen{$st->object->sse}
$st->object->isa("RDF::Trine::Node::Blank") not $seen{$st->object->sse}
Truth table66%2/3
A and not(B)
A= $st->object->isa("RDF::Trine::Node::Blank")B= not $seen{$st->object->sse}
ABresult
0X0
100
111
1010 # warn "+ CBD pushing " . $st->object->sse . "\n";
1011 9 push(@nodes, $st->object);
1012 }
1013 27 ▶ if ($st->subject->isa('RDF::Trine::Node::Blank') and not($seen{ $st->subject->sse })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st->subject->isa("RDF::Trine::Node::Blank") and not $seen{$st->subject->sse}) 0 27 27
Condition66%2/3
$st->subject->isa("RDF::Trine::Node::Blank") and not $seen{$st->subject->sse}
!ll&&!rl&&r
16110
MC/DC0%0/2
$st->subject->isa("RDF::Trine::Node::Blank") and not $seen{$st->subject->sse}
$st->subject->isa("RDF::Trine::Node::Blank") not $seen{$st->subject->sse}
Truth table66%2/3
A and not(B)
A= $st->subject->isa("RDF::Trine::Node::Blank")B= not $seen{$st->subject->sse}
ABresult
0X0
100
111
1014 # warn "- CBD pushing " . $st->subject->sse . "\n";
1015 0 push(@nodes, $st->subject);
1016 }
1017 27 return $st;
1018 8 };
1019 8 return RDF::Trine::Iterator::Graph->new( $sub );
1020 }
1021  
1022 =item C<< as_string >>
1023  
1024 =cut
1025  
1026 0 ▶sub as_string {
Subroutine as_string: not called CC=2 SCAR=17.9
Pod documented
1027 0 my $self = shift;
1028 0 $self->end_bulk_ops();
1029 0 my $iter = $self->get_statements( undef, undef, undef, undef );
1030 0 my @rows;
1031 0 my @names = qw[subject predicate object context];
1032 0 ▶ while (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 0 0 0
1033 0 push(@rows, [map {$row->$_()->as_string} @names]);
1034 }
1035 0 my @rule = qw(- +);
1036 0 my @headers = (\q"| ");
1037 0 push(@headers, map { $_ => \q" | " } @names);
1038 0 pop @headers;
1039 0 push @headers => (\q" |");
1040 0 my $table = Text::Table->new(@names);
1041 0 $table->rule(@rule);
1042 0 $table->body_rule(@rule);
1043 0 $table->load(@rows);
1044 0 my $size = scalar(@rows);
1045 return join('',
1046 $table->rule(@rule),
1047 $table->title,
1048 $table->rule(@rule),
1049 0 map({ $table->body($_) } 0 .. @rows),
1050 $table->rule(@rule)
1051 ) . "$size statements\n";
1052 }
1053  
1054 16336 ▶sub _store {
Subroutine _store: called CC=1 SCAR=0.0
1055 16336 my $self = shift;
1056 16336 return $self->{store};
1057 }
1058  
1059 0 ▶sub _debug {
Subroutine _debug: not called CC=5 SCAR=34.0
1060 0 my $self = shift;
1061 0 my $warn = shift;
1062 0 my $stream = $self->get_statements( undef, undef, undef, undef );
1063 0 my $l = Log::Log4perl->get_logger("rdf.trine.model");
1064 0 $l->debug( 'model statements:' );
1065 0 ▶ if ($warn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn) 0 0 0
1066 0 warn "Model $self:\n";
1067 }
1068 0 my $count = 0;
1069 0 ▶ while (my $s = $stream->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = $stream->next) 0 0 0
1070 0 $count++;
1071 0 ▶ if ($warn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn) 0 0 0
1072 0 warn $s->as_string . "\n";
1073 }
1074 0 $l->debug('[model]' . $s->as_string);
1075 }
1076 0 ▶ if ($warn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($warn) 0 0 0
1077 0 warn "$count statements\n";
1078 }
1079 }
1080  
1081 68 1;
1082  
1083 __END__
« RDF/Trine/Store/Hexastore.pm ↑ Summary RDF/Trine/Serializer/RDFXML.pm »