RDF/Trine/Statement.pm

filter: Statementstmt 80.0% 120 / 150 Branchbran 61.3% 27 / 44 Conditioncond 44.4% 4 / 9 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 93.5% 29 / 31 Podpod 100% 19 / 19 totaltotal 77.5% 200 / 258 CC 31 SCAR 20.0
CC
31
Cov
74%
__ANON__
42.0
rdf_compatible
9.1
subsumes
6.7
CRAP 7.4
« RDF/Trine/Error.pm ↑ Summary RDF/Trine/Parser/RDFPatch.pm »
1 # RDF::Trine::Statement
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Statement - Class for triples and triple patterns
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Statement version 1.019
11  
12 =cut
13  
14 package RDF::Trine::Statement;
15  
16 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 68 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 68 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 68 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 68 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 68 ▶use RDF::Trine::Iterator qw(smap sgrep swatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 68 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 68 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 ######################################################################
29  
30 68 our ($VERSION);
31 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 68 $VERSION = '1.019';
33 }
34  
35 ######################################################################
36  
37 =head1 METHODS
38  
39 =over 4
40  
41 =cut
42  
43 =item C<new ( $s, $p, $o )>
44  
45 Returns a new Triple structure.
46  
47 =cut
48  
49 6835 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
50 6835 my $class = shift;
51 6835 my @nodes = @_;
52 6835 ▶ unless (scalar(@nodes) == 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @nodes == 3) 1 6834 6835
53 1 throw RDF::Trine::Error::MethodInvocationError -text => "Triple constructor must have three node arguments";
54 }
55 6834 my @names = qw(subject predicate object);
56 6834 foreach my $i (0 .. 2) {
57 20502 ▶ unless (defined($nodes[ $i ])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $nodes[$i]) 50 20452 20502
58 50 $nodes[ $i ] = RDF::Trine::Node::Variable->new($names[ $i ]);
59 }
60 }
61
62 6834 return bless( [ @nodes ], $class );
63 }
64  
65 =item C<< construct_args >>
66  
67 Returns a list of arguments that, passed to this class' constructor,
68 will produce a clone of this algebra pattern.
69  
70 =cut
71  
72 1 ▶sub construct_args {
Subroutine construct_args: called CC=1 SCAR=0.0
Pod documented
73 1 my $self = shift;
74 1 return ($self->nodes);
75 }
76  
77 =item C<< nodes >>
78  
79 Returns the subject, predicate and object of the triple pattern.
80  
81 =cut
82  
83 9092 ▶sub nodes {
Subroutine nodes: called CC=1 SCAR=0.0
Pod documented
84 9092 my $self = shift;
85 9092 return @$self;
86 }
87  
88 =item C<< node_names >>
89  
90 Returns the method names for accessing the nodes of this statement.
91  
92 =cut
93  
94 40 ▶sub node_names {
Subroutine node_names: called CC=1 SCAR=0.0
Pod documented
95 40 return qw(subject predicate object);
96 }
97  
98 =item C<< subject >>
99  
100 Returns the subject node of the triple pattern.
101  
102 =cut
103  
104 16380 ▶sub subject {
Subroutine subject: called CC=2 SCAR=6.9
Pod documented
105 16380 my $self = shift;
106 16380 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 16379 16380
107 1 $self->[0] = shift;
108 }
109 16380 return $self->[0];
110 }
111  
112 =item C<< predicate >>
113  
114 Returns the predicate node of the triple pattern.
115  
116 =cut
117  
118 10867 ▶sub predicate {
Subroutine predicate: called CC=2 SCAR=6.9
Pod documented
119 10867 my $self = shift;
120 10867 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 10866 10867
121 1 $self->[1] = shift;
122 }
123 10867 return $self->[1];
124 }
125  
126 =item C<< object >>
127  
128 Returns the object node of the triple pattern.
129  
130 =cut
131  
132 17661 ▶sub object {
Subroutine object: called CC=2 SCAR=6.9
Pod documented
133 17661 my $self = shift;
134 17661 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 17660 17661
135 1 $self->[2] = shift;
136 }
137 17661 return $self->[2];
138 }
139  
140 =item C<< as_string >>
141  
142 Returns the statement in a string form.
143  
144 =cut
145  
146 6597 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
147 6597 my $self = shift;
148 6597 return $self->sse;
149 }
150  
151 =item C<< has_blanks >>
152  
153 Returns true if any of the nodes in this statement are blank nodes.
154  
155 =cut
156  
157 135 ▶sub has_blanks {
Subroutine has_blanks: called CC=3 SCAR=11.0
Pod documented
158 135 my $self = shift;
159 135 foreach my $node ($self->nodes) {
160 255 ▶ return 1 if $node->isa('RDF::Trine::Node::Blank');
Branch: 1 branch
BranchTrueFalseTotal
if $node->isa("RDF::Trine::Node::Blank") 75 180 255
161 }
162 60 return 0;
163 }
164  
165 =item C<< sse >>
166  
167 Returns the SSE string for this algebra expression.
168  
169 =cut
170  
171 2272 ▶sub sse {
Subroutine sse: called CC=1 SCAR=0.0
Pod documented
172 2272 my $self = shift;
173 2272 my $context = shift;
174 2272 return sprintf(
175 '(triple %s %s %s)',
176 $self->subject->sse( $context ),
177 $self->predicate->sse( $context ),
178 $self->object->sse( $context ),
179 );
180 }
181  
182 =item C<< from_sse ( $string, $context ) >>
183  
184 Parses the supplied SSE-encoded string and returns a RDF::Trine::Statement object.
185  
186 =cut
187  
188 1 ▶sub from_sse {
Subroutine from_sse: called CC=3 SCAR=11.3
Pod documented
189 1 my $class = shift;
190 1 my $context = $_[1];
191 1 $_ = $_[0];
192 1 ▶ if (m/^[(]triple/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[(]triple/) { } 1 0 1
193 1 s/^[(]triple\s+//;
194 1 my @nodes;
195 1 push(@nodes, RDF::Trine::Node->from_sse( $_, $context ));
196 1 push(@nodes, RDF::Trine::Node->from_sse( $_, $context ));
197 1 push(@nodes, RDF::Trine::Node->from_sse( $_, $context ));
198 1 ▶ if (m/^\s*[)]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*[)]/) { } 1 0 1
199 1 s/^\s*[)]//;
200 1 return RDF::Trine::Statement->new( @nodes );
201 } else {
202 0 throw RDF::Trine::Error -text => "Cannot parse end-of-triple from SSE string: >>$_<<";
203 }
204 } else {
205 0 throw RDF::Trine::Error -text => "Cannot parse triple from SSE string: >>$_<<";
206 }
207 }
208  
209 =item C<< type >>
210  
211 Returns the type of this algebra expression.
212  
213 =cut
214  
215 76 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
216 76 return 'TRIPLE';
217 }
218  
219 =item C<< referenced_variables >>
220  
221 Returns a list of the variable names used in this algebra expression.
222  
223 =cut
224  
225 693 ▶sub referenced_variables {
Subroutine referenced_variables: called CC=1 SCAR=0.0
Pod documented
226 693 my $self = shift;
227 693 return RDF::Trine::_uniq(map { $_->name } grep { $_->isa('RDF::Trine::Node::Variable') } $self->nodes);
228 }
229  
230 =item C<< definite_variables >>
231  
232 Returns a list of the variable names that will be bound after evaluating this algebra expression.
233  
234 =cut
235  
236 2 ▶sub definite_variables {
Subroutine definite_variables: called CC=1 SCAR=0.0
Pod documented
237 2 my $self = shift;
238 2 return $self->referenced_variables;
239 }
240  
241 =item C<< clone >>
242  
243 =cut
244  
245 3 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
246 3 my $self = shift;
247 3 my $class = ref($self);
248 3 return $class->new( $self->nodes );
249 }
250  
251 =item C<< bind_variables ( \%bound ) >>
252  
253 Returns a new algebra pattern with variables named in %bound replaced by their corresponding bound values.
254  
255 =cut
256  
257 6 ▶sub bind_variables {
Subroutine bind_variables: called CC=4 SCAR=13.9
Pod documented
258 6 my $self = shift;
259 6 my $class = ref($self);
260 6 my $bound = shift;
261 6 my @nodes = $self->nodes;
262 6 foreach my $i (0 .. 2) {
263 18 my $n = $nodes[ $i ];
264 18 ▶ if ($n->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa("RDF::Trine::Node::Variable")) 9 9 18
265 9 my $name = $n->name;
266 9 ▶ if (my $value = $bound->{ $name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $bound->{$name}) 8 1 9
267 8 $nodes[ $i ] = $value;
268 }
269 }
270 }
271 6 return $class->new( @nodes );
272 }
273  
274 =item C<< subsumes ( $statement ) >>
275  
276 Returns true if this statement will subsume the $statement when matched against
277 a triple store.
278  
279 =cut
280  
281 8 ▶sub subsumes {
Subroutine subsumes: called CC=6 SCAR=19.0
Pod documented
282 8 my $self = shift;
283 8 my $st = shift;
284 8 my @nodes = $self->nodes;
285 8 my @match = $st->nodes;
286
287 8 my %bind;
288 8 my $l = Log::Log4perl->get_logger("rdf.trine.statement");
289 8 foreach my $i (0..2) {
290 23 my $m = $match[ $i ];
291 23 ▶ if ($nodes[$i]->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[$i]->isa('RDF::Trine::Node::Variable')) { } 5 18 23
292 5 my $name = $nodes[$i]->name;
293 5 ▶ if (exists( $bind{ $name } )) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $bind{$name}) { } 0 5 5
294 0 $l->debug("variable $name has already been bound");
295 0 ▶ if (not $bind{ $name }->equal( $m )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bind{$name}->equal($m)) 0 0 0
296 0 $l->debug("-> and " . $bind{$name}->sse . " does not equal " . $m->sse);
297 0 return 0;
298 }
299 } else {
300 5 $bind{ $name } = $m;
301 }
302 } else {
303 18 ▶ return 0 unless ($nodes[$i]->equal( $m ));
Branch: 1 branch
BranchTrueFalseTotal
unless $nodes[$i]->equal($m) 1 17 18
304 }
305 }
306 7 return 1;
307 }
308  
309  
310 =item C<< from_redland ( $statement ) >>
311  
312 Given a RDF::Redland::Statement object, returns a perl-native
313 RDF::Trine::Statement object.
314  
315 =cut
316  
317 0 ▶sub from_redland {
Subroutine from_redland: not called CC=2 SCAR=17.9
Pod documented
318 0 my $self = shift;
319 0 my $rstmt = shift;
320 0 my $rs = $rstmt->subject;
321 0 my $rp = $rstmt->predicate;
322 0 my $ro = $rstmt->object;
323
324 0 ▶ my $cast = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
325 0 my $node = shift;
326 0 my $type = $node->type;
327 0 ▶ if ($type == $RDF::Redland::Node::Type_Resource) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type == $RDF::Redland::Node::Type_Resource) { } 0 0 0
elsif ($type == $RDF::Redland::Node::Type_Blank) { } 0 0 0
elsif ($type == $RDF::Redland::Node::Type_Literal) { } 0 0 0
328 0 my $uri = $node->uri->as_string;
329 0 ▶ if ($uri =~ /%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /%/) 0 0 0
330 # Redland's parser doesn't properly unescape percent-encoded RDF URI References
331 0 $uri = decode_utf8(uri_unescape(encode_utf8($uri)));
332 }
333 0 return RDF::Trine::Node::Resource->new( $uri );
334 } elsif ($type == $RDF::Redland::Node::Type_Blank) {
335 0 return RDF::Trine::Node::Blank->new( $node->blank_identifier );
336 } elsif ($type == $RDF::Redland::Node::Type_Literal) {
337 0 my $lang = $node->literal_value_language;
338 0 my $dturi = $node->literal_datatype;
339 0 ▶ my $dt = ($dturi)
Branch: 1 branch
BranchTrueFalseTotal
$dturi ? : 0 0 0
340 ? $dturi->as_string
341 : undef;
342 0 return RDF::Trine::Node::Literal->new( $node->literal_value, $lang, $dt );
343 } else {
344 0 croak 'Unknown node type in statement conversion';
345 }
346 0 };
347
348 0 my @nodes;
349 0 foreach my $n ($rs, $rp, $ro) {
350 0 push(@nodes, $cast->( $n ));
351 }
352 0 my $st = $self->new( @nodes );
353 0 return $st;
354 }
355  
356 =item C<< rdf_compatible >>
357  
358 Returns true if and only if the statement can be expressed in RDF. That is,
359 the subject of the statement must be a resource or blank node; the predicate
360 must be a resource; and the object must be a resource, blank node or literal.
361  
362 RDF::Trine::Statement does allow statements to be created which cannot be
363 expressed in RDF - for instance, statements including variables.
364  
365 =cut
366  
367 4 ▶sub rdf_compatible {
Subroutine rdf_compatible: called CC=7 SCAR=22.1
Pod documented
368 4 my $self = shift;
369  
370 return
371 4 ▶ unless $self->subject->is_resource
Branch: 1 branch
BranchTrueFalseTotal
unless $self->subject->is_resource or $self->subject->is_blank 0 4 4
Condition33%1/3
$self->subject->is_resource or $self->subject->is_blank
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->subject->is_resource or $self->subject->is_blank
$self->subject->is_resource $self->subject->is_blank
Truth table33%1/3
A or B
A= $self->subject->is_resourceB= $self->subject->is_blank
ABresult
1X1
011
000
372 || $self->subject->is_blank;
373
374 return
375 4 ▶ unless $self->predicate->is_resource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->predicate->is_resource 0 4 4
376
377 return
378 4 ▶ unless $self->object->is_resource
Branch: 1 branch
BranchTrueFalseTotal
unless $self->object->is_resource or $self->object->is_blank or $self->object->is_literal 1 3 4
Condition50%3/6
$self->object->is_resource or $self->object->is_blank or $self->object->is_literal
l!l&&r!l&&!r
031
$self->object->is_resource or $self->object->is_blank
l!l&&r!l&&!r
004
MC/DC33%1/3
$self->object->is_resource or $self->object->is_blank or $self->object->is_literal
$self->object->is_resource $self->object->is_blank $self->object->is_literal
Truth table50%2/4
A or B or C
A= $self->object->is_resourceB= $self->object->is_blankC= $self->object->is_literal
ABCresult
1XX1
01X1
0011
0000
379 || $self->object->is_blank
380 || $self->object->is_literal;
381
382 3 return $self;
383 }
384  
385 68 1;
386  
387 __END__
« RDF/Trine/Error.pm ↑ Summary RDF/Trine/Parser/RDFPatch.pm »