RDF/Trine/Serializer/Turtle.pm

filter: Statementstmt 94.3% 335 / 355 Branchbran 86.6% 123 / 142 Conditioncond 76.4% 39 / 51 MC/DCmcdc 62.5% 20 / 32 Subroutinesub 88.2% 30 / 34 Podpod 100% 8 / 8 totaltotal 89.2% 555 / 622 CC 95 SCAR 25.6
CC
95
Cov
91%
serialize_iterator
26.3
_check_valid_rdf_list
23.9
new
13.0
CRAP 13.0
« RDF/Trine/Parser/RDFXML.pm ↑ Summary RDF/Trine/Model/StatementFilter.pm »
1 # RDF::Trine::Serializer::Turtle
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Serializer::Turtle - Turtle Serializer
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Serializer::Turtle version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Serializer::Turtle;
15 my $serializer = RDF::Trine::Serializer::Turtle->new( namespaces => { ex => 'http://example/' } );
16 print $serializer->serialize_model_to_string($model);
17  
18 =head1 DESCRIPTION
19  
20 The RDF::Trine::Serializer::Turtle class provides an API for serializing RDF
21 graphs to the Turtle syntax. XSD numeric types are serialized as bare literals,
22 and where possible the more concise syntax is used for rdf:Lists.
23  
24 =head1 METHODS
25  
26 Beyond the methods documented below, this class inherits methods from the
27 L<RDF::Trine::Serializer> class.
28  
29 =over 4
30  
31 =cut
32  
33 package RDF::Trine::Serializer::Turtle;
34  
35 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 68 ▶use base qw(RDF::Trine::Serializer);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 68 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use Scalar::Util qw(blessed refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 68 ▶use RDF::Trine qw(variable iri);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 68 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 68 ▶use RDF::Trine::Namespace qw(rdf);
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 ######################################################################
52  
53 68 our ($VERSION, $debug);
54 68 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
55 68 $debug = 0;
56 68 $VERSION = '1.019';
57 68 $RDF::Trine::Serializer::serializer_names{ 'turtle' } = __PACKAGE__;
58 68 $RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/ns/formats/Turtle' } = __PACKAGE__;
59 68 foreach my $type (qw(application/x-turtle application/turtle text/turtle text/rdf+n3)) {
60 272 $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
61 }
62 }
63  
64 ######################################################################
65  
66 =item C<< new ( namespaces => \%namespaces, base_uri => $base_uri ) >>
67  
68 Returns a new Turtle serializer object.
69  
70 =cut
71  
72 51 ▶sub new {
Subroutine new: called CC=13 SCAR=25.7
Pod documented
73 51 my $class = shift;
74 51 my $ns = {};
75 51 my $base_uri;
76  
77 51 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 36 51
78 15 ▶ if (scalar(@_) == 1 and reftype($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and builtin::reftype($_[0]) eq 'HASH') { } 9 6 15
Condition66%2/3
scalar @_ == 1 and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
609
MC/DC50%1/2
scalar @_ == 1 and builtin::reftype($_[0]) eq "HASH"
scalar @_ == 1 builtin::reftype($_[0]) eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= builtin::reftype($_[0]) eq "HASH"
ABresult
0X0
100
111
79 9 $ns = shift;
80 } else {
81 6 my %args = @_;
82 6 ▶ if (exists $args{ base }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"base"}) 1 5 6
83 1 $base_uri = $args{ base };
84 }
85 6 ▶ if (exists $args{ base_uri }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"base_uri"}) 1 5 6
86 1 $base_uri = $args{ base_uri };
87 }
88 6 ▶ if (exists $args{ namespaces }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"namespaces"}) 4 2 6
89 4 $ns = $args{ namespaces };
90 }
91 }
92 }
93  
94 51 my %rev;
95 51 ▶ if (blessed($ns) and $ns->isa('RDF::Trine::NamespaceMap')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($ns) and $ns->isa('RDF::Trine::NamespaceMap')) { } 1 50 51
Condition66%2/3
builtin::blessed($ns) and $ns->isa("RDF::Trine::NamespaceMap")
!ll&&!rl&&r
5001
MC/DC50%1/2
builtin::blessed($ns) and $ns->isa("RDF::Trine::NamespaceMap")
builtin::blessed($ns) $ns->isa("RDF::Trine::NamespaceMap")
Truth table66%2/3
A and B
A= builtin::blessed($ns)B= $ns->isa("RDF::Trine::NamespaceMap")
ABresult
0X0
100
111
96 1 for my $prefix ($ns->list_prefixes) {
97 # way convoluted
98 2 my $nsuri = $ns->namespace_uri($prefix)->uri->value;
99 2 $rev{$nsuri} = $prefix;
100 }
101 }
102 else {
103 50 ▶ while (my ($ns, $uri) = each(%{ $ns })) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ns;}) 34 50 84
104 34 ▶ if (blessed($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri)) 1 33 34
105 1 $uri = $uri->uri_value;
106 1 ▶ if (blessed($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri)) 1 0 1
107 1 $uri = $uri->uri_value;
108 }
109 }
110 34 $rev{ $uri } = $ns;
111 }
112 }
113  
114 51 my $self = bless( {
115 ns => \%rev,
116 base_uri => $base_uri,
117 }, $class );
118 51 return $self;
119 }
120  
121 =item C<< serialize_model_to_file ( $fh, $model ) >>
122  
123 Serializes the C<$model> to Turtle, printing the results to the supplied
124 filehandle C<<$fh>>.
125  
126 =cut
127  
128 1 ▶sub serialize_model_to_file {
Subroutine serialize_model_to_file: called CC=1 SCAR=0.0
Pod documented
129 1 my $self = shift;
130 1 my $fh = shift;
131 1 my $model = shift;
132 1 my $sink = RDF::Trine::Serializer::FileSink->new($fh);
133
134 1 my $st = RDF::Trine::Statement->new( map { variable($_) } qw(s p o) );
135 1 my $pat = RDF::Trine::Pattern->new( $st );
136 1 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
137 1 my $iter = $stream->as_statements( qw(s p o) );
138
139 1 $self->serialize_iterator( $sink, $iter, seen => {}, level => 0, tab => "\t", @_, model => $model );
140 1 return 1;
141 }
142  
143 =item C<< serialize_model_to_string ( $model ) >>
144  
145 Serializes the C<$model> to Turtle, returning the result as a string.
146  
147 =cut
148  
149 36 ▶sub serialize_model_to_string {
Subroutine serialize_model_to_string: called CC=1 SCAR=0.0
Pod documented
150 36 my $self = shift;
151 36 my $model = shift;
152 36 my $sink = RDF::Trine::Serializer::StringSink->new();
153  
154 36 my $st = RDF::Trine::Statement->new( map { variable($_) } qw(s p o) );
155 36 my $pat = RDF::Trine::Pattern->new( $st );
156 36 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
157 36 my $iter = $stream->as_statements( qw(s p o) );
158
159 36 $self->serialize_iterator( $sink, $iter, seen => {}, level => 0, tab => "\t", @_, model => $model, string => 1 );
160 36 return $sink->string;
161 }
162  
163 =item C<< serialize_iterator_to_file ( $file, $iter ) >>
164  
165 Serializes the iterator to Turtle, printing the results to the supplied
166 filehandle C<<$fh>>.
167  
168 =cut
169  
170 0 ▶sub serialize_iterator_to_file {
Subroutine serialize_iterator_to_file: not called CC=1 SCAR=6.9
Pod documented
171 0 my $self = shift;
172 0 my $fh = shift;
173 0 my $iter = shift;
174 0 my %args = @_;
175  
176 0 my $sink = RDF::Trine::Serializer::FileSink->new($fh);
177 0 $self->serialize_iterator( $sink, $iter, %args );
178 0 return 1;
179 }
180  
181 =item C<< serialize_iterator ( $sink, $iter ) >>
182  
183 Serializes the iterator to Turtle, printing the results to the supplied
184 sink object.
185  
186 =cut
187  
188 40 ▶sub serialize_iterator {
Subroutine serialize_iterator: called CC=26 SCAR=32.7
Pod documented
189 40 my $self = shift;
190 40 my $sink = shift;
191 40 my $iter = shift;
192 40 my %args = @_;
193
194 40 ▶ my $seen = $args{ seen } || {};
Condition50%1/2
$args{'seen'} || {}
l!l
400
MC/DC0%0/1
$args{'seen'} || {}
$args{'seen'}
Truth table50%1/2
A
A= $args{'seen'}
Aresult
11
00
195 40 ▶ my $level = $args{ level } || 0;
Condition50%1/2
$args{'level'} || 0
l!l
040
MC/DC0%0/1
$args{'level'} || 0
$args{'level'}
Truth table50%1/2
A
A= $args{'level'}
Aresult
11
00
196 40 ▶ my $tab = $args{ tab } || "\t";
Condition50%1/2
$args{'tab'} || "\t"
l!l
400
MC/DC0%0/1
$args{'tab'} || "\t"
$args{'tab'}
Truth table50%1/2
A
A= $args{'tab'}
Aresult
11
00
197 40 my $indent = $tab x $level;
198
199 40 my %ns = reverse(%{ $self->{ns} });
200 40 my @nskeys = sort keys %ns;
201
202 40 ▶ unless ($sink->can('prepend')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sink->can("prepend")) 1 39 40
203 1 ▶ if (@nskeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nskeys) 1 0 1
204 1 foreach my $ns (sort @nskeys) {
205 3 my $uri = $ns{ $ns };
206 3 $sink->emit("\@prefix $ns: <$uri> .\n");
207 }
208 1 $sink->emit("\n");
209 }
210 }
211 40 ▶ if ($self->{base_uri}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"base_uri"}) 2 38 40
212 2 $sink->emit("\@base <$self->{base_uri}> .\n\n");
213 }
214
215 40 my $last_subj;
216 my $last_pred;
217
218 40 my $open_triple = 0;
219 40 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 141 40 181
220 # warn "------------------\n";
221 # warn $st->as_string . "\n";
222 141 my $subj = $st->subject;
223 141 my $pred = $st->predicate;
224 141 my $obj = $st->object;
225
226 # we're abusing the seen hash here as the key isn't really a node value,
227 # but since it isn't a valid node string being used it shouldn't collide
228 # with real data. we set this here so that later on when we check for
229 # single-owner bnodes (when attempting to use the [...] concise syntax),
230 # bnodes that have already been serialized as the 'head' of a statement
231 # aren't considered as single-owner. This is because the output string
232 # is acting as a second ownder of the node -- it's already been emitted
233 # as something like '_:foobar', so it can't also be output as '[...]'.
234 141 $seen->{ ' heads' }{ $subj->as_string }++;
235
236 141 ▶ if (my $model = $args{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $model = $args{"model"}) 132 9 141
237 132 ▶ if (my $head = $self->_statement_describes_list($model, $st)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $head = $self->_statement_describes_list($model, $st)) 17 115 132
238 17 ▶ warn "found a rdf:List head " . $head->as_string . " for the subject in statement " . $st->as_string if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Serializer::Turtle::debug 0 17 17
239 17 ▶ if ($model->count_statements(undef, undef, $head)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->count_statements(undef, undef, $head)) 8 9 17
240 # the rdf:List appears as the object of a statement, and so
241 # will be serialized whenever we get to serializing that
242 # statement
243 8 ▶ warn "next" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Serializer::Turtle::debug 0 8 8
244 8 next;
245 }
246 }
247 }
248
249 133 ▶ if ($seen->{ $subj->as_string }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$seen{$subj->as_string}) 48 85 133
250 48 ▶ warn "next on seen subject " . $st->as_string if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Serializer::Turtle::debug 0 48 48
251 48 next;
252 }
253
254 85 ▶ if ($subj->equal( $last_subj )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subj->equal($last_subj)) { } 32 53 85
255 # continue an existing subject
256 32 ▶ if ($pred->equal( $last_pred )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred->equal($last_pred)) { } 7 25 32
257 # continue an existing predicate
258 7 $sink->emit(qq[, ]);
259 7 $self->_serialize_object_to_file( $sink, $obj, $seen, $level, $tab, %args );
260 } else {
261 # start a new predicate
262 25 $sink->emit(qq[ ;\n${indent}$tab]);
263 25 $self->_turtle( $sink, $pred, 1, $seen, $level, $tab, %args );
264 25 $sink->emit(' ');
265 25 $self->_serialize_object_to_file( $sink, $obj, $seen, $level, $tab, %args );
266 }
267 } else {
268 # start a new subject
269 53 ▶ if ($open_triple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open_triple) 13 40 53
270 13 $sink->emit(qq[ .\n${indent}]);
271 }
272 53 $open_triple = 1;
273 53 $self->_turtle( $sink, $subj, 0, $seen, $level, $tab, %args );
274
275 53 ▶ warn '-> ' . $pred->as_string if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Serializer::Turtle::debug 0 53 53
276 53 $sink->emit(' ');
277 53 $self->_turtle( $sink, $pred, 1, $seen, $level, $tab, %args );
278 53 $sink->emit(' ');
279 53 $self->_serialize_object_to_file( $sink, $obj, $seen, $level, $tab, %args );
280 }
281 } continue {
282 141 ▶ if (blessed($last_subj) and not($last_subj->equal($st->subject))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($last_subj) and not $last_subj->equal($st->subject)) 41 100 141
Condition100%3/3
builtin::blessed($last_subj) and not $last_subj->equal($st->subject)
!ll&&!rl&&r
406041
MC/DC100%2/2
builtin::blessed($last_subj) and not $last_subj->equal($st->subject)
builtin::blessed($last_subj) not $last_subj->equal($st->subject)
Truth table100%3/3
A and not(B)
A= builtin::blessed($last_subj)B= not $last_subj->equal($st->subject)
ABresult
0X0
100
111
283 # warn "marking " . $st->subject->as_string . " as seen";
284 41 $seen->{ $last_subj->as_string }++;
285 }
286 # warn "setting last subject to " . $st->subject->as_string;
287 141 $last_subj = $st->subject;
288 141 $last_pred = $st->predicate;
289 }
290
291 40 ▶ if ($open_triple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($open_triple) 40 0 40
292 40 $sink->emit(qq[ .\n]);
293 }
294
295 40 ▶ if ($sink->can('prepend')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sink->can("prepend")) 39 1 40
296 39 my @used_nskeys = keys %{ $self->{used_ns} };
297 39 ▶ if (@used_nskeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@used_nskeys) 10 29 39
298 10 my $string = '';
299 10 foreach my $ns (sort @used_nskeys) {
300 18 my $uri = $ns{ $ns };
301 18 $string .= "\@prefix $ns: <$uri> .\n";
302 }
303 10 $string .= "\n";
304 10 $sink->prepend($string);
305 }
306 }
307 }
308  
309 =item C<< serialize_iterator_to_string ( $iter ) >>
310  
311 Serializes the iterator to Turtle, returning the result as a string.
312  
313 =cut
314  
315 3 ▶sub serialize_iterator_to_string {
Subroutine serialize_iterator_to_string: called CC=1 SCAR=0.0
Pod documented
316 3 my $self = shift;
317 3 my $iter = shift;
318 3 my $sink = RDF::Trine::Serializer::StringSink->new();
319 3 $self->serialize_iterator( $sink, $iter, seen => {}, level => 0, tab => "\t", @_, string => 1 );
320 3 return $sink->string;
321 }
322  
323 =item C<< serialize_node ( $node ) >>
324  
325 Returns a string containing the Turtle serialization of C<< $node >>.
326  
327 =cut
328  
329 0 ▶sub serialize_node {
Subroutine serialize_node: not called CC=1 SCAR=6.9
Pod documented
330 0 my $self = shift;
331 0 my $node = shift;
332 0 return $self->node_as_concise_string( $node );
333 }
334  
335 137 ▶sub _serialize_object_to_file {
Subroutine _serialize_object_to_file: called CC=13 SCAR=25.7
336 137 my $self = shift;
337 137 my $sink = shift;
338 137 my $subj = shift;
339 137 my $seen = shift;
340 137 my $level = shift;
341 137 my $tab = shift;
342 137 my %args = @_;
343 137 my $indent = $tab x $level;
344
345 137 ▶ if (my $model = $args{model}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $model = $args{"model"}) 128 9 137
346 128 ▶ if ($subj->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($subj->isa("RDF::Trine::Node::Blank")) 34 94 128
347 34 ▶ if ($self->_check_valid_rdf_list( $subj, $model )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_valid_rdf_list($subj, $model)) { } 2 32 34
348 # warn "node is a valid rdf:List: " . $subj->as_string . "\n";
349 2 return $self->_turtle_rdf_list( $sink, $subj, $model, $seen, $level, $tab, %args );
350 } else {
351 32 my $count = $model->count_statements( undef, undef, $subj );
352 32 my $rec = $model->count_statements( $subj, undef, $subj );
353 32 ▶ warn "count=$count, rec=$rec for node " . $subj->as_string if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Serializer::Turtle::debug 0 32 32
354 32 ▶ if ($count == 1 and $rec == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1 and $rec == 0) 30 2 32
Condition66%2/3
$count == 1 and $rec == 0
!ll&&!rl&&r
2030
MC/DC50%1/2
$count == 1 and $rec == 0
$count == 1 $rec == 0
Truth table66%2/3
A and B
A= $count == 1B= $rec == 0
ABresult
0X0
100
111
355 30 ▶ unless ($seen->{ $subj->as_string }++ or $seen->{ ' heads' }{ $subj->as_string }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$seen{$subj->as_string}++ or $seen->{" heads"}{$subj->as_string}) 26 4 30
Condition100%3/3
$$seen{$subj->as_string}++ or $seen->{" heads"}{$subj->as_string}
l!l&&r!l&&!r
3126
MC/DC100%2/2
$$seen{$subj->as_string}++ or $seen->{" heads"}{$subj->as_string}
$$seen{$subj->as_string}++ $seen->{" heads"}{$subj->as_string}
Truth table100%3/3
A or B
A= $$seen{$subj->as_string}++B= $seen->{" heads"}{$subj->as_string}
ABresult
1X1
011
000
356 26 my $pat = RDF::Trine::Pattern->new( RDF::Trine::Statement->new($subj, variable('p'), variable('o')) );
357 26 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(p ASC o ASC) ] );
358 26 my $iter = $stream->as_statements( qw(s p o) );
359 26 my $last_pred;
360 26 my $triple_count = 0;
361 26 $sink->emit("[");
362 26 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 48 26 74
363 48 my $pred = $st->predicate;
364 48 my $obj = $st->object;
365
366 # continue an existing subject
367 48 ▶ if ($pred->equal( $last_pred )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred->equal($last_pred)) { } 3 45 48
368 # continue an existing predicate
369 3 $sink->emit(qq[, ]);
370 3 $self->_serialize_object_to_file( $sink, $obj, $seen, $level, $tab, %args );
371 # $self->_turtle( $fh, $obj, 2, $seen, $level, $tab, %args );
372 } else {
373 # start a new predicate
374 45 ▶ if ($triple_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($triple_count == 0) { } 24 21 45
375 24 $sink->emit(qq[\n${indent}${tab}${tab}]);
376 } else {
377 21 $sink->emit(qq[ ;\n${indent}$tab${tab}]);
378 }
379 45 $self->_turtle( $sink, $pred, 1, $seen, $level, $tab, %args );
380 45 $sink->emit(' ');
381 45 $self->_serialize_object_to_file( $sink, $obj, $seen, $level+1, $tab, %args );
382 }
383
384 48 $last_pred = $pred;
385 48 $triple_count++;
386 }
387 26 ▶ if ($triple_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($triple_count) 24 2 26
388 24 $sink->emit("\n${indent}${tab}");
389 }
390 26 $sink->emit("]");
391 26 return;
392 }
393 }
394 }
395 }
396 }
397
398 109 $self->_turtle( $sink, $subj, 2, $seen, $level, $tab, %args );
399 }
400  
401 132 ▶sub _statement_describes_list {
Subroutine _statement_describes_list: called CC=4 SCAR=13.9
402 132 my $self = shift;
403 132 my $model = shift;
404 132 my $st = shift;
405 132 my $subj = $st->subject;
406 132 my $pred = $st->predicate;
407 132 my $obj = $st->object;
408 132 ▶ if ($model->count_statements($subj, $rdf->first) and $model->count_statements($subj, $rdf->rest)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->first) and $model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->rest)) 26 106 132
Condition66%2/3
$model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->first) and $model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->rest)
!ll&&!rl&&r
106026
MC/DC50%1/2
$model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->first) and $model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->rest)
$model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->first) $model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->rest)
Truth table66%2/3
A and B
A= $model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->first)B= $model->count_statements($subj, $RDF::Trine::Serializer::Turtle::rdf->rest)
ABresult
0X0
100
111
409 # warn $subj->as_string . " looks like a rdf:List element";
410 26 ▶ if (my $head = $self->_node_belongs_to_valid_list( $model, $subj )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $head = $self->_node_belongs_to_valid_list($model, $subj)) 17 9 26
411 17 return $head;
412 }
413 }
414
415 115 return;
416 }
417  
418 26 ▶sub _node_belongs_to_valid_list {
Subroutine _node_belongs_to_valid_list: called CC=4 SCAR=13.9
419 26 my $self = shift;
420 26 my $model = shift;
421 26 my $node = shift;
422 26 ▶ while ($model->count_statements( undef, $rdf->rest, $node )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model->count_statements(undef, $RDF::Trine::Serializer::Turtle::rdf->rest, $node)) 12 26 38
423 12 my $iter = $model->get_statements( undef, $rdf->rest, $node );
424 12 my $s = $iter->next;
425 12 my $ancestor = $s->subject;
426 12 ▶ unless (blessed($ancestor)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($ancestor)) 0 12 12
427 # warn "failed to get an expected rdf:List element ancestor";
428 0 return 0;
429 }
430 12 ($node) = $ancestor;
431 # warn "stepping back to rdf:List element ancestor " . $node->as_string;
432 }
433 26 ▶ if ($self->_check_valid_rdf_list( $node, $model )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_check_valid_rdf_list($node, $model)) { } 17 9 26
434 17 return $node;
435 } else {
436 9 return;
437 }
438 }
439  
440 60 ▶sub _check_valid_rdf_list {
Subroutine _check_valid_rdf_list: called CC=16 SCAR=31.7
441 60 my $self = shift;
442 60 my $head = shift;
443 60 my $model = shift;
444 # warn '--------------------------';
445 # warn "checking if node " . $head->as_string . " is a valid rdf:List\n";
446
447 60 my $headrest = $model->count_statements( undef, $rdf->rest, $head );
448 60 ▶ if ($headrest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headrest) 3 57 60
449 # warn "\tnode " . $head->as_string . " seems to be the middle of an rdf:List\n";
450 3 return 0;
451 }
452
453 57 my %list_elements;
454 57 my $node = $head;
455 57 ▶ until ($node->equal( $rdf->nil )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->equal($RDF::Trine::Serializer::Turtle::rdf->nil)) 81 19 100
456 81 $list_elements{ $node->as_string }++;
457
458 81 ▶ unless ($node->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->isa("RDF::Trine::Node::Blank")) 5 76 81
459 # warn "\tnode " . $node->as_string . " isn't a blank node\n";
460 5 return 0;
461 }
462
463 76 my $first = $model->count_statements( $node, $rdf->first );
464 76 ▶ unless ($first == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first == 1) 33 43 76
465 # warn "\tnode " . $node->as_string . " has $first rdf:first links when 1 was expected\n";
466 33 return 0;
467 }
468
469 43 my $rest = $model->count_statements( $node, $rdf->rest );
470 43 ▶ unless ($rest == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rest == 1) 0 43 43
471 # warn "\tnode " . $node->as_string . " has $rest rdf:rest links when 1 was expected\n";
472 0 return 0;
473 }
474
475 43 my $in = $model->count_statements( undef, undef, $node );
476 43 ▶ unless ($in < 2) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in < 2) 0 43 43
477 # warn "\tnode " . $node->as_string . " has $in incoming links when 2 were expected\n";
478 0 return 0;
479 }
480
481 43 ▶ if (not($head->equal( $node ))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($head->equal($node)) 19 24 43
482 # It's OK for the head of a list to have any outgoing links (e.g. (1 2) ex:p "o"
483 # but internal list elements should have only the expected links of rdf:first,
484 # rdf:rest, and optionally an rdf:type rdf:List
485 19 my $out = $model->count_statements( $node );
486 19 ▶ unless ($out == 2 or $out == 3) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($out == 2 or $out == 3) 0 19 19
Condition33%1/3
$out == 2 or $out == 3
l!l&&r!l&&!r
1900
MC/DC0%0/2
$out == 2 or $out == 3
$out == 2 $out == 3
Truth table33%1/3
A or B
A= $out == 2B= $out == 3
ABresult
1X1
011
000
487 # warn "\tnode " . $node->as_string . " has $out outgoing links when 2 or 3 were expected\n";
488 0 return 0;
489 }
490
491 19 ▶ if ($out == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out == 3) 0 19 19
492 0 my $type = $model->count_statements( $node, $rdf->type, $rdf->List );
493 0 ▶ unless ($type == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type == 1) 0 0 0
494 # warn "\tnode " . $node->as_string . " has more outgoing links than expected\n";
495 0 return 0;
496 }
497 }
498 }
499
500
501
502 43 my @links = $model->objects_for_predicate_list( $node, $rdf->first, $rdf->rest );
503 43 foreach my $l (@links) {
504 86 ▶ if ($list_elements{ $l->as_string }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list_elements{$l->as_string}) 0 86 86
505 0 ▶ warn $node->as_string . " is repeated in the list" if ($debug);
Branch: 1 branch
BranchTrueFalseTotal
if $RDF::Trine::Serializer::Turtle::debug 0 0 0
506 0 return 0;
507 }
508 }
509
510 43 ($node) = $model->objects_for_predicate_list( $node, $rdf->rest );
511 43 ▶ unless (blessed($node)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($node)) 0 43 43
512 # warn "\tno valid rdf:rest object found";
513 0 return 0;
514 }
515 # warn "\tmoving on to rdf:rest object " . $node->as_string . "\n";
516 }
517
518 # warn "\tlooks like a valid rdf:List\n";
519 19 return 1;
520 }
521  
522 2 ▶sub _turtle_rdf_list {
Subroutine _turtle_rdf_list: called CC=3 SCAR=11.0
523 2 my $self = shift;
524 2 my $sink = shift;
525 2 my $head = shift;
526 2 my $model = shift;
527 2 my $seen = shift;
528 2 my $level = shift;
529 2 my $tab = shift;
530 2 my %args = @_;
531 2 my $node = $head;
532 2 my $count = 0;
533 2 $sink->emit('(');
534 2 ▶ until ($node->equal( $rdf->nil )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($node->equal($RDF::Trine::Serializer::Turtle::rdf->nil)) 4 2 6
535 4 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 2 2 4
536 2 $sink->emit(' ');
537 }
538 4 my ($value) = $model->objects_for_predicate_list( $node, $rdf->first );
539 4 $self->_serialize_object_to_file( $sink, $value, $seen, $level, $tab, %args );
540 4 $seen->{ $node->as_string }++;
541 4 ($node) = $model->objects_for_predicate_list( $node, $rdf->rest );
542 4 $count++;
543 }
544 2 $sink->emit(')');
545 }
546  
547 258 ▶sub _node_concise_string {
Subroutine _node_concise_string: called CC=8 SCAR=20.8
548 258 my $self = shift;
549 258 my $obj = shift;
550 258 ▶ if ($obj->is_literal and $obj->has_datatype) {
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->is_literal and $obj->has_datatype) { } 28 230 258
elsif ($obj->isa('RDF::Trine::Node::Resource')) { } 184 46 230
Condition100%3/3
$obj->is_literal and $obj->has_datatype
!ll&&!rl&&r
1913928
MC/DC100%2/2
$obj->is_literal and $obj->has_datatype
$obj->is_literal $obj->has_datatype
Truth table100%3/3
A and B
A= $obj->is_literalB= $obj->has_datatype
ABresult
0X0
100
111
551 28 my $dt = $obj->literal_datatype;
552 28 ▶ if ($dt =~ m<^http://www.w3.org/2001/XMLSchema#(integer|double|decimal)$> and $obj->is_canonical_lexical_form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt =~ m[^http://www.w3.org/2001/XMLSchema#(integer|double|decimal)$] and $obj->is_canonical_lexical_form) { } 21 7 28
Condition100%3/3
$dt =~ m[^http://www.w3.org/2001/XMLSchema#(integer|double|decimal)$] and $obj->is_canonical_lexical_form
!ll&&!rl&&r
4321
MC/DC100%2/2
$dt =~ m[^http://www.w3.org/2001/XMLSchema#(integer|double|decimal)$] and $obj->is_canonical_lexical_form
$dt =~ m[^http://www.w3.org/2001/XMLSchema#(integer|double|decimal)$] $obj->is_canonical_lexical_form
Truth table100%3/3
A and B
A= $dt =~ m[^http://www.w3.org/2001/XMLSchema#(integer|double|decimal)$]B= $obj->is_canonical_lexical_form
ABresult
0X0
100
111
553 21 my $value = $obj->literal_value;
554 21 return $value;
555 } else {
556 7 my $dtr = iri($dt);
557 7 my $literal = $obj->literal_value;
558 7 my $qname;
559 7 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
560 7 my ($ns,$local) = $dtr->qname;
561 7 ▶ if (blessed($self) and exists $self->{ns}{$ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self) and exists $self->{"ns"}{$ns}) 3 4 7
Condition66%2/3
builtin::blessed($self) and exists $self->{"ns"}{$ns}
!ll&&!rl&&r
043
MC/DC50%1/2
builtin::blessed($self) and exists $self->{"ns"}{$ns}
builtin::blessed($self) exists $self->{"ns"}{$ns}
Truth table66%2/3
A and B
A= builtin::blessed($self)B= exists $self->{"ns"}{$ns}
ABresult
0X0
100
111
562 3 $qname = join(':', $self->{ns}{$ns}, $local);
563 3 $self->{used_ns}{ $self->{ns}{$ns} }++;
564 }
565 7 ▶ } catch RDF::Trine::Error with {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
566 7 ▶ if ($qname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qname) 3 4 7
567 3 my $escaped = $obj->_unicode_escape( $literal );
568 3 return qq["$escaped"^^$qname];
569 }
570 }
571 } elsif ($obj->isa('RDF::Trine::Node::Resource')) {
572 184 my $value;
573 184 ▶ try {
Subroutine __ANON__: called CC=3 SCAR=11.0
574 184 my ($ns,$local) = $obj->qname;
575 180 ▶ if (blessed($self) and exists $self->{ns}{$ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self) and exists $self->{"ns"}{$ns}) 61 119 180
Condition66%2/3
builtin::blessed($self) and exists $self->{"ns"}{$ns}
!ll&&!rl&&r
011961
MC/DC50%1/2
builtin::blessed($self) and exists $self->{"ns"}{$ns}
builtin::blessed($self) exists $self->{"ns"}{$ns}
Truth table66%2/3
A and B
A= builtin::blessed($self)B= exists $self->{"ns"}{$ns}
ABresult
0X0
100
111
576 61 $value = join(':', $self->{ns}{$ns}, $local);
577 61 $self->{used_ns}{ $self->{ns}{$ns} }++;
578 }
579 184 ▶ } catch RDF::Trine::Error with {} otherwise {};
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
580 184 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 61 123 184
581 61 return $value;
582 }
583 }
584 173 return;
585 }
586  
587 =item C<< node_as_concise_string >>
588  
589 Returns a string representation using common Turtle syntax shortcuts (e.g. for numeric literals).
590  
591 =cut
592  
593 23 ▶sub node_as_concise_string {
Subroutine node_as_concise_string: called CC=2 SCAR=6.9
Pod documented
594 23 my $self = shift;
595 23 my $obj = shift;
596 23 my $str = $self->_node_concise_string( $obj );
597 23 ▶ if (defined($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) { } 4 19 23
598 4 return $str;
599 } else {
600 19 return $obj->as_ntriples;
601 }
602 }
603  
604 285 ▶sub _turtle {
Subroutine _turtle: called CC=10 SCAR=23.0
605 285 my $self = shift;
606 285 my $sink = shift;
607 285 my $obj = shift;
608 285 my $pos = shift;
609 285 my $seen = shift;
610 285 my $level = shift;
611 285 my $tab = shift;
612 285 my %args = @_;
613
614 285 ▶ if ($obj->isa('RDF::Trine::Node::Resource') and $pos == 1 and $obj->uri_value eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
Branch: 3 branches
BranchTrueFalseTotal
if ($obj->isa('RDF::Trine::Node::Resource') and $pos == 1 and $obj->uri_value eq 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') { } 14 271 285
elsif ($obj->isa('RDF::Trine::Node::Blank') and $pos == 0) { } 36 235 271
elsif (defined(my $str = $self->_node_concise_string($obj))) { } 81 154 235
Condition100%6/6
$obj->isa("RDF::Trine::Node::Resource") and $pos == 1 and $obj->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
!ll&&!rl&&r
16210914
$obj->isa("RDF::Trine::Node::Resource") and $pos == 1
!ll&&!rl&&r
10656123
MC/DC100%3/3
$obj->isa("RDF::Trine::Node::Resource") and $pos == 1 and $obj->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
$obj->isa("RDF::Trine::Node::Resource") $pos == 1 $obj->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
Truth table100%4/4
A and B and C
A= $obj->isa("RDF::Trine::Node::Resource")B= $pos == 1C= $obj->uri_value eq "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
ABCresult
0XX0
10X0
1100
1111
615 14 $sink->emit('a');
616 14 return;
617 ▶ } elsif ($obj->isa('RDF::Trine::Node::Blank') and $pos == 0) {
Condition100%3/3
$obj->isa("RDF::Trine::Node::Blank") and $pos == 0
!ll&&!rl&&r
228736
MC/DC100%2/2
$obj->isa("RDF::Trine::Node::Blank") and $pos == 0
$obj->isa("RDF::Trine::Node::Blank") $pos == 0
Truth table100%3/3
A and B
A= $obj->isa("RDF::Trine::Node::Blank")B= $pos == 0
ABresult
0X0
100
111
618 36 ▶ if (my $model = $args{ model }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $model = $args{"model"}) 35 1 36
619 35 my $count = $model->count_statements( undef, undef, $obj );
620 35 my $rec = $model->count_statements( $obj, undef, $obj );
621 # XXX if $count == 1, then it would be better to ignore this triple for now, since it's a 'single-owner' bnode, and better serialized as a '[ ... ]' bnode in the object position as part of the 'owning' triple
622 35 ▶ if ($count < 1 and $rec == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < 1 and $rec == 0) 31 4 35
Condition66%2/3
$count < 1 and $rec == 0
!ll&&!rl&&r
4031
MC/DC50%1/2
$count < 1 and $rec == 0
$count < 1 $rec == 0
Truth table66%2/3
A and B
A= $count < 1B= $rec == 0
ABresult
0X0
100
111
623 31 $sink->emit('[]');
624 31 return;
625 }
626 }
627 } elsif (defined(my $str = $self->_node_concise_string( $obj ))) {
628 81 $sink->emit($str);
629 81 return;
630 }
631
632 159 $sink->emit($obj->as_ntriples);
633 159 return;
634 }
635  
636 68 1;
637  
638 __END__
« RDF/Trine/Parser/RDFXML.pm ↑ Summary RDF/Trine/Model/StatementFilter.pm »