RDF/Trine/Serializer/NTriples.pm

filter: Statementstmt 71.4% 70 / 98 Branchbran 42.8% 6 / 14 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 84.2% 16 / 19 Podpod 100% 7 / 7 totaltotal 70.2% 99 / 141 CC 10 SCAR 19.3
CC
10
Cov
67%
_serialize_bounded_description
30.0
serialize_model_to_string
6.0
BEGIN
2.0
CRAP 6.9
« RDF/Trine/Store/DBI/SQLite.pm ↑ Summary RDF/Trine/Serializer/NQuads.pm »
1 # RDF::Trine::Serializer::NTriples
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Serializer::NTriples - N-Triples Serializer
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Serializer::NTriples version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Serializer::NTriples;
15 my $serializer = RDF::Trine::Serializer::NTriples->new();
16  
17 =head1 DESCRIPTION
18  
19 The RDF::Trine::Serializer::NTriples class provides an API for serializing RDF
20 graphs to the N-Triples 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::NTriples;
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 Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 68 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 68 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 68 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 68 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 ######################################################################
47  
48 68 our ($VERSION);
49 68 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
50 68 $VERSION = '1.019';
51 68 $RDF::Trine::Serializer::serializer_names{ 'ntriples' } = __PACKAGE__;
52 68 $RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/ns/formats/N-Triples' } = __PACKAGE__;
53 68 foreach my $type (qw(text/plain)) {
54 68 $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
55 }
56 }
57  
58 ######################################################################
59  
60 =item C<< new >>
61  
62 Returns a new N-Triples serializer object.
63  
64 =cut
65  
66 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
67 7 my $class = shift;
68 7 my %args = @_;
69 7 my $self = bless( {}, $class);
70 7 return $self;
71 }
72  
73 =item C<< serialize_model_to_file ( $fh, $model ) >>
74  
75 Serializes the C<$model> to N-Triples, printing the results to the supplied
76 filehandle C<<$fh>>.
77  
78 =cut
79  
80 1 ▶sub serialize_model_to_file {
Subroutine serialize_model_to_file: called CC=2 SCAR=6.9
Pod documented
81 1 my $self = shift;
82 1 my $file = shift;
83 1 my $model = shift;
84 1 my $st = RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) );
85 1 my $pat = RDF::Trine::Pattern->new( $st );
86 1 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
87 1 my $iter = $stream->as_statements( qw(s p o) );
88 1 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 4 1 5
89 4 print {$file} $self->statement_as_string( $st );
90 }
91 }
92  
93 =item C<< serialize_model_to_string ( $model ) >>
94  
95 Serializes the C<$model> to N-Triples, returning the result as a string.
96  
97 =cut
98  
99 0 ▶sub serialize_model_to_string {
Subroutine serialize_model_to_string: not called CC=2 SCAR=17.9
Pod documented
100 0 my $self = shift;
101 0 my $model = shift;
102 0 my $st = RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) );
103 0 my $pat = RDF::Trine::Pattern->new( $st );
104 0 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
105 0 my $iter = $stream->as_statements( qw(s p o) );
106
107 0 my $string = '';
108 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
109 0 my @nodes = $st->nodes;
110 0 $string .= $self->statement_as_string( $st );
111 }
112 0 return $string;
113 }
114  
115 =item C<< serialize_iterator_to_file ( $file, $iter ) >>
116  
117 Serializes the iterator to N-Triples, printing the results to the supplied
118 filehandle C<<$fh>>.
119  
120 =cut
121  
122 1 ▶sub serialize_iterator_to_file {
Subroutine serialize_iterator_to_file: called CC=2 SCAR=6.9
Pod documented
123 1 my $self = shift;
124 1 my $file = shift;
125 1 my $iter = shift;
126 1 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 2 1 3
127 2 print {$file} $self->statement_as_string( $st );
128 }
129 }
130  
131 =item C<< serialize_iterator_to_string ( $iter ) >>
132  
133 Serializes the iterator to N-Triples, returning the result as a string.
134  
135 =cut
136  
137 1 ▶sub serialize_iterator_to_string {
Subroutine serialize_iterator_to_string: called CC=2 SCAR=6.9
Pod documented
138 1 my $self = shift;
139 1 my $iter = shift;
140 1 my $string = '';
141 1 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 2 1 3
142 2 my @nodes = $st->nodes;
143 2 $string .= $self->statement_as_string( $st );
144 }
145 1 return $string;
146 }
147  
148 0 ▶sub _serialize_bounded_description {
Subroutine _serialize_bounded_description: not called CC=5 SCAR=34.0
149 0 my $self = shift;
150 0 my $model = shift;
151 0 my $node = shift;
152 0 ▶ my $seen = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
153 0 ▶ return '' if ($seen->{ $node->sse }++);
Branch: 1 branch
BranchTrueFalseTotal
if $$seen{$node->sse}++ 0 0 0
154 0 my $iter = $model->get_statements( $node, undef, undef );
155 0 my $string = '';
156 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
157 0 my @nodes = $st->nodes;
158 0 $string .= $self->statement_as_string( $st );
159 0 ▶ if ($nodes[2]->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[2]->isa("RDF::Trine::Node::Blank")) 0 0 0
160 0 $string .= $self->_serialize_bounded_description( $model, $nodes[2], $seen );
161 }
162 }
163 0 return $string;
164 }
165  
166 =item C<< statement_as_string ( $st ) >>
167  
168 Returns a string with the supplied RDF::Trine::Statement object serialized as
169 N-Triples, ending in a DOT and newline.
170  
171 =cut
172  
173 8 ▶sub statement_as_string {
Subroutine statement_as_string: called CC=1 SCAR=0.0
Pod documented
174 8 my $self = shift;
175 8 my $st = shift;
176 8 my @nodes = $st->nodes;
177 8 return join(' ', map { $_->as_ntriples } @nodes[0..2]) . " .\n";
178 }
179  
180 =item C<< serialize_node ( $node ) >>
181  
182 Returns a string containing the N-Triples serialization of C<< $node >>.
183  
184 =cut
185  
186 0 ▶sub serialize_node {
Subroutine serialize_node: not called CC=1 SCAR=6.9
Pod documented
187 0 my $self = shift;
188 0 my $node = shift;
189 0 return $node->as_ntriples;
190 }
191  
192 68 1;
193  
194 __END__
« RDF/Trine/Store/DBI/SQLite.pm ↑ Summary RDF/Trine/Serializer/NQuads.pm »