RDF/Trine/Serializer/TriG.pm

filter: Statementstmt 54.7% 98 / 179 Branchbran 40.3% 21 / 52 Conditioncond 6.6% 1 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 61.5% 16 / 26 Podpod 100% 7 / 7 totaltotal 49.4% 143 / 289 CC 34 SCAR 28.8
CC
34
Cov
49%
_node_concise_string
72.0
serialize_model_to_file
30.0
new
14.0
CRAP 17.9
« RDF/Trine/Iterator/Graph.pm ↑ Summary RDF/Trine/Store.pm »
1 # RDF::Trine::Serializer::TriG
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Serializer::TriG - TriG Serializer
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Serializer::TriG version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Serializer::TriG;
15 my $serializer = RDF::Trine::Serializer::TriG->new();
16  
17 =head1 DESCRIPTION
18  
19 The RDF::Trine::Serializer::TriG class provides an API for serializing RDF
20 graphs to the TriG syntax.
21  
22 =head1 METHODS
23  
24 Beyond the methods documented below, this class inherits methods from the
25 L<RDF::Trine::Serializer> class.
26  
27 =over 4
28  
29 =cut
30  
31 package RDF::Trine::Serializer::TriG;
32  
33 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 68 ▶use base qw(RDF::Trine::Serializer);
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 68 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 68 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 68 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 68 ▶use Scalar::Util qw(blessed refaddr reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 68 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 ######################################################################
48  
49 68 our ($VERSION);
50 68 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
51 68 $VERSION = '1.019';
52 68 $RDF::Trine::Serializer::serializer_names{ 'trig' } = __PACKAGE__;
53 # $RDF::Trine::Serializer::format_uris{ 'http://sw.deri.org/2008/07/n-quads/#n-quads' } = __PACKAGE__;
54 # foreach my $type (qw(text/x-nquads)) {
55 # $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
56 # }
57 }
58  
59 ######################################################################
60  
61 =item C<< new >>
62  
63 Returns a new TriG serializer object.
64  
65 =cut
66  
67 5 ▶sub new {
Subroutine new: called CC=10 SCAR=26.4
Pod documented
68 5 my $class = shift;
69 5 my $ns = {};
70 5 my $base_uri;
71  
72 5 my @args = @_;
73 5 my $ttl = RDF::Trine::Serializer::Turtle->new(@args);
74 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 3 5
75 2 ▶ if (scalar(@_) == 1 and reftype($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1 and builtin::reftype($_[0]) eq 'HASH') { } 0 2 2
Condition33%1/3
scalar @_ == 1 and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
200
MC/DC0%0/2
scalar @_ == 1 and builtin::reftype($_[0]) eq "HASH"
scalar @_ == 1 builtin::reftype($_[0]) eq "HASH"
Truth table33%1/3
A and B
A= scalar @_ == 1B= builtin::reftype($_[0]) eq "HASH"
ABresult
0X0
100
111
76 0 $ns = shift;
77 } else {
78 2 my %args = @_;
79 2 ▶ if (exists $args{ base }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"base"}) 0 2 2
80 0 $base_uri = $args{ base };
81 }
82 2 ▶ if (exists $args{ base_uri }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"base_uri"}) 0 2 2
83 0 $base_uri = $args{ base_uri };
84 }
85 2 ▶ if (exists $args{ namespaces }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"namespaces"}) 2 0 2
86 2 $ns = $args{ namespaces };
87 }
88 }
89 }
90
91 5 my %rev;
92 5 ▶ while (my ($ns, $uri) = each(%{ $ns })) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$ns;}) 2 5 7
93 2 ▶ if (blessed($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri)) 0 2 2
94 0 $uri = $uri->uri_value;
95 0 ▶ if (blessed($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri)) 0 0 0
96 0 $uri = $uri->uri_value;
97 }
98 }
99 2 $rev{ $uri } = $ns;
100 }
101
102 5 my $self = bless( {
103 ns => \%rev,
104 base_uri => $base_uri,
105 ttl => $ttl,
106 }, $class );
107
108 5 return $self;
109 }
110  
111 =item C<< serialize_model_to_file ( $fh, $model ) >>
112  
113 Serializes the C<$model> to TriG, printing the results to the supplied
114 filehandle C<<$fh>>.
115  
116 =cut
117  
118 0 ▶sub serialize_model_to_file {
Subroutine serialize_model_to_file: not called CC=5 SCAR=34.0
Pod documented
119 0 my $self = shift;
120 0 my $file = shift;
121 0 my $model = shift;
122
123 0 my %ns = reverse(%{ $self->{ns} });
124 0 my @nskeys = sort keys %ns;
125 0 ▶ if (@nskeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nskeys) 0 0 0
126 0 foreach my $ns (sort @nskeys) {
127 0 my $uri = $ns{ $ns };
128 0 print $file "\@prefix $ns: <$uri> .\n";
129 }
130 0 print $file "\n";
131 }
132
133 0 my $s = $self->{ttl};
134 0 my $count = $model->count_statements(undef, undef, undef, RDF::Trine::Node::Nil->new());
135 0 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 0 0 0
136 0 my $iter = $model->get_statements(undef, undef, undef, RDF::Trine::Node::Nil->new());
137 0 print $file "{\n\t";
138 0 my $ttl = $s->serialize_iterator_to_string($iter);
139 0 $ttl =~ s/\n/\n\t/g;
140 0 print {$file} $ttl;
141 0 print $file "}\n\n";
142 }
143
144 0 my $graphs = $model->get_graphs;
145 0 ▶ while (my $g = $graphs->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $g = $graphs->next) 0 0 0
146 0 my $iter = $model->get_statements(undef, undef, undef, $g);
147 0 print $file sprintf("%s {\n", $self->node_as_concise_string($g));
148 0 my $ttl = $s->serialize_iterator_to_string($iter);
149 0 $ttl =~ s/\n/\n\t/g;
150 0 print $file $ttl;
151 0 print $file "}\n\n";
152 }
153 }
154  
155 =item C<< serialize_model_to_string ( $model ) >>
156  
157 Serializes the C<$model> to TriG, returning the result as a string.
158  
159 =cut
160  
161 0 ▶sub serialize_model_to_string {
Subroutine serialize_model_to_string: not called CC=1 SCAR=6.9
Pod documented
162 0 my $self = shift;
163 0 my $model = shift;
164 0 my $iter = $model->as_stream;
165 0 my $data = '';
166 0 open(my $fh, '>:encoding(UTF-8)', \$data);
167 0 $self->serialize_model_to_file($fh, $model);
168 0 close($fh);
169 0 return decode('UTF-8', $data);
170 }
171  
172 =item C<< serialize_iterator_to_file ( $file, $iter ) >>
173  
174 Serializes the iterator to TriG, printing the results to the supplied
175 filehandle C<<$fh>>.
176  
177 =cut
178  
179 5 ▶sub serialize_iterator_to_file {
Subroutine serialize_iterator_to_file: called CC=9 SCAR=22.0
Pod documented
180 5 my $self = shift;
181 5 my $file = shift;
182 5 my $iter = shift;
183
184 5 my %ns = reverse(%{ $self->{ns} });
185 5 my @nskeys = sort keys %ns;
186 5 ▶ if (@nskeys) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nskeys) 2 3 5
187 2 foreach my $ns (sort @nskeys) {
188 2 my $uri = $ns{ $ns };
189 2 print $file "\@prefix $ns: <$uri> .\n";
190 }
191 2 print $file "\n";
192 }
193
194 5 my $g;
195 5 my $in_graph = 0;
196 5 my $s = $self->{ttl};
197 5 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 7 5 12
198 7 ▶ my $new_graph = $st->isa('RDF::Trine::Statement::Quad') ? $st->graph : RDF::Trine::Node::Nil->new();
Branch: 1 branch
BranchTrueFalseTotal
$st->isa('RDF::Trine::Statement::Quad') ? : 7 0 7
199 7 ▶ if (not($in_graph)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_graph) { } 5 2 7
elsif (not $g->equal($new_graph)) { } 1 1 2
200 5 $g = $new_graph;
201 5 ▶ if ($g->is_nil) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->is_nil) { } 4 1 5
202 4 print $file "{\n"
203 } else {
204 1 print $file sprintf("%s {\n", $s->node_as_concise_string($g));
205 }
206 } elsif (not($g->equal($new_graph))) {
207 1 $g = $new_graph;
208 1 print $file sprintf("}\n\n%s {\n", $s->node_as_concise_string($g));
209 }
210 7 $in_graph = 1;
211
212 7 print {$file} "\t" . $self->_statement_as_string( $st );
213 }
214
215 5 ▶ if ($in_graph) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_graph) 5 0 5
216 5 print $file "}\n";
217 }
218 }
219  
220 =item C<< serialize_iterator_to_string ( $iter ) >>
221  
222 Serializes the iterator to TriG, returning the result as a string.
223  
224 =cut
225  
226 5 ▶sub serialize_iterator_to_string {
Subroutine serialize_iterator_to_string: called CC=1 SCAR=0.0
Pod documented
227 5 my $self = shift;
228 5 my $iter = shift;
229 5 my $data = '';
230 5 open(my $fh, '>:encoding(UTF-8)', \$data);
231 5 $self->serialize_iterator_to_file($fh, $iter);
232 5 close($fh);
233 5 return decode('UTF-8', $data);
234 }
235  
236 7 ▶sub _statement_as_string {
Subroutine _statement_as_string: called CC=1 SCAR=0.0
237 7 my $self = shift;
238 7 my $st = shift;
239 7 my @nodes;
240 7 my $s = $self->{ttl};
241 7 @nodes = ($st->nodes)[0..2];
242 7 return join(' ', map { $s->node_as_concise_string($_) } @nodes) . " .\n";
243 }
244  
245  
246 =item C<< statement_as_string ( $st ) >>
247  
248 Returns a string with the supplied RDF::Trine::Statement::Quad object serialized
249 as TriG, ending in a DOT and newline.
250  
251 =cut
252  
253 0 ▶sub statement_as_string {
Subroutine statement_as_string: not called CC=1 SCAR=6.9
Pod documented
254 0 my $self = shift;
255 0 my $st = shift;
256 0 my @nodes = $st->nodes;
257 0 return join(' ', map { $_->as_ntriples } @nodes[0..3]) . " .\n";
258 }
259  
260  
261 0 ▶sub _node_concise_string {
Subroutine _node_concise_string: not called CC=8 SCAR=42.8
262 0 my $self = shift;
263 0 my $obj = shift;
264 0 ▶ if ($obj->is_literal and $obj->has_datatype) {
Branch: 2 branches
BranchTrueFalseTotal
if ($obj->is_literal and $obj->has_datatype) { } 0 0 0
elsif ($obj->isa('RDF::Trine::Node::Resource')) { } 0 0 0
Condition0%0/3
$obj->is_literal and $obj->has_datatype
!ll&&!rl&&r
000
MC/DC0%0/2
$obj->is_literal and $obj->has_datatype
$obj->is_literal $obj->has_datatype
Truth table0%0/3
A and B
A= $obj->is_literalB= $obj->has_datatype
ABresult
0X0
100
111
265 0 my $dt = $obj->literal_datatype;
266 0 ▶ 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) { } 0 0 0
Condition0%0/3
$dt =~ m[^http://www.w3.org/2001/XMLSchema#(integer|double|decimal)$] and $obj->is_canonical_lexical_form
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
267 0 my $value = $obj->literal_value;
268 0 return $value;
269 } else {
270 0 my $dtr = iri($dt);
271 0 my $literal = $obj->literal_value;
272 0 my $qname;
273 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
274 0 my ($ns,$local) = $dtr->qname;
275 0 ▶ if (blessed($self) and exists $self->{ns}{$ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self) and exists $self->{"ns"}{$ns}) 0 0 0
Condition0%0/3
builtin::blessed($self) and exists $self->{"ns"}{$ns}
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self) and exists $self->{"ns"}{$ns}
builtin::blessed($self) exists $self->{"ns"}{$ns}
Truth table0%0/3
A and B
A= builtin::blessed($self)B= exists $self->{"ns"}{$ns}
ABresult
0X0
100
111
276 0 $qname = join(':', $self->{ns}{$ns}, $local);
277 0 $self->{used_ns}{ $self->{ns}{$ns} }++;
278 }
279 0 ▶ } catch RDF::Trine::Error with {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
280 0 ▶ if ($qname) {
Branch: 1 branch
BranchTrueFalseTotal
if ($qname) 0 0 0
281 0 my $escaped = $obj->_unicode_escape( $literal );
282 0 return qq["$escaped"^^$qname];
283 }
284 }
285 } elsif ($obj->isa('RDF::Trine::Node::Resource')) {
286 0 my $value;
287 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
288 0 my ($ns,$local) = $obj->qname;
289 0 ▶ if (blessed($self) and exists $self->{ns}{$ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self) and exists $self->{"ns"}{$ns}) 0 0 0
Condition0%0/3
builtin::blessed($self) and exists $self->{"ns"}{$ns}
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($self) and exists $self->{"ns"}{$ns}
builtin::blessed($self) exists $self->{"ns"}{$ns}
Truth table0%0/3
A and B
A= builtin::blessed($self)B= exists $self->{"ns"}{$ns}
ABresult
0X0
100
111
290 0 $value = join(':', $self->{ns}{$ns}, $local);
291 0 $self->{used_ns}{ $self->{ns}{$ns} }++;
292 }
293 0 ▶ } catch RDF::Trine::Error with {} otherwise {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
294 0 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 0 0 0
295 0 return $value;
296 }
297 }
298 0 return;
299 }
300  
301 =item C<< node_as_concise_string >>
302  
303 Returns a string representation using common Turtle syntax shortcuts (e.g. for numeric literals).
304  
305 =cut
306  
307 0 ▶sub node_as_concise_string {
Subroutine node_as_concise_string: not called CC=2 SCAR=17.9
Pod documented
308 0 my $self = shift;
309 0 my $obj = shift;
310 0 my $str = $self->_node_concise_string( $obj );
311 0 ▶ if (defined($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) { } 0 0 0
312 0 return $str;
313 } else {
314 0 return $obj->as_ntriples;
315 }
316 }
317  
318 68 1;
319  
320 __END__
« RDF/Trine/Iterator/Graph.pm ↑ Summary RDF/Trine/Store.pm »