RDF/Trine/Serializer/TSV.pm

filter: Statementstmt 65.5% 80 / 122 Branchbran 39.2% 11 / 28 Conditioncond 19.2% 5 / 26 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 84.2% 16 / 19 Podpod 100% 7 / 7 totaltotal 55.8% 120 / 215 CC 25 SCAR 32.3
CC
25
Cov
55%
serialize_iterator_to_file
90.0
_serialize_bounded_description
30.0
serialize_iterator_to_string
10.1
CRAP 25.3
« RDF/Trine/Store/Redland.pm ↑ Summary RDF/Trine/Store/Dydra.pm »
1 # RDF::Trine::Serializer::TSV
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Serializer::TSV - TSV Serializer
7  
8 =head1 VERSION
9  
10 This document describes RDF::Trine::Store version 1.019
11  
12 =head1 SYNOPSIS
13  
14 use RDF::Trine::Serializer::TSV;
15 my $serializer = RDF::Trine::Serializer::TSV->new();
16  
17 =head1 DESCRIPTION
18  
19 The RDF::Trine::Serializer::TSV class provides an API for serializing RDF
20 graphs to the TSV 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::TSV;
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{ 'tsv' } = __PACKAGE__;
52 68 $RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/ns/formats/TSV' } = __PACKAGE__;
53 68 foreach my $type (qw(text/tsv)) {
54 68 $RDF::Trine::Serializer::media_types{ $type } = __PACKAGE__;
55 }
56 }
57  
58 ######################################################################
59  
60 =item C<< new >>
61  
62 Returns a new TSV serializer object.
63  
64 =cut
65  
66 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
67 1 my $class = shift;
68 1 my %args = @_;
69 1 my $self = bless( {}, $class);
70 1 return $self;
71 }
72  
73 =item C<< serialize_model_to_file ( $fh, $model ) >>
74  
75 Serializes the C<$model> to TSV, 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 print {$file} join("\t", qw(s p o)) . "\n";
89 1 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 1 1 2
90 1 print {$file} $self->statement_as_string( $st );
91 }
92 }
93  
94 =item C<< serialize_model_to_string ( $model ) >>
95  
96 Serializes the C<$model> to TSV, returning the result as a string.
97  
98 =cut
99  
100 0 ▶sub serialize_model_to_string {
Subroutine serialize_model_to_string: not called CC=2 SCAR=17.9
Pod documented
101 0 my $self = shift;
102 0 my $model = shift;
103 0 my $st = RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) );
104 0 my $pat = RDF::Trine::Pattern->new( $st );
105 0 my $stream = $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] );
106 0 my $iter = $stream->as_statements( qw(s p o) );
107
108 0 my $string = join("\t", qw(s p o)) . "\n";
109 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
110 0 my @nodes = $st->nodes;
111 0 $string .= $self->statement_as_string( $st );
112 }
113 0 return $string;
114 }
115  
116 =item C<< serialize_iterator_to_file ( $file, $iter ) >>
117  
118 Serializes the iterator to TSV, printing the results to the supplied
119 filehandle C<<$fh>>.
120  
121 =cut
122  
123 0 ▶sub serialize_iterator_to_file {
Subroutine serialize_iterator_to_file: not called CC=9 SCAR=45.0
Pod documented
124 0 my $self = shift;
125 0 my $file = shift;
126 0 my $iter = shift;
127 0 my $e = $iter->peek;
128
129 0 ▶ if (defined($e) and blessed($e) and $e->isa('RDF::Trine::Statement')) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $e and builtin::blessed($e) and $e->isa('RDF::Trine::Statement')) { } 0 0 0
elsif (defined $e and builtin::blessed($e) and $e->isa('RDF::Trine::VariableBindings')) { } 0 0 0
Condition0%0/6
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::Statement")
!ll&&!rl&&r
000
defined $e and builtin::blessed($e)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::Statement")
defined $e builtin::blessed($e) $e->isa("RDF::Trine::Statement")
Truth table0%0/4
A and B and C
A= defined $eB= builtin::blessed($e)C= $e->isa("RDF::Trine::Statement")
ABCresult
0XX0
10X0
1100
1111
130 0 print {$file} join("\t", qw(?s ?p ?o)) . "\n";
131 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
132 0 print {$file} $self->statement_as_string( $st );
133 }
134 ▶ } elsif (defined($e) and blessed($e) and $e->isa('RDF::Trine::VariableBindings')) {
Condition0%0/6
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::VariableBindings")
!ll&&!rl&&r
000
defined $e and builtin::blessed($e)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::VariableBindings")
defined $e builtin::blessed($e) $e->isa("RDF::Trine::VariableBindings")
Truth table0%0/4
A and B and C
A= defined $eB= builtin::blessed($e)C= $e->isa("RDF::Trine::VariableBindings")
ABCresult
0XX0
10X0
1100
1111
135 0 my @names = $iter->binding_names;
136 0 print {$file} join("\t", map { "?$_" } @names) . "\n";
137 0 ▶ while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 0 0 0
138 0 print {$file} $self->result_as_string( $r, \@names );
139 }
140 }
141 }
142  
143 =item C<< serialize_iterator_to_string ( $iter ) >>
144  
145 Serializes the iterator to TSV, returning the result as a string.
146  
147 =cut
148  
149 3 ▶sub serialize_iterator_to_string {
Subroutine serialize_iterator_to_string: called CC=9 SCAR=23.1
Pod documented
150 3 my $self = shift;
151 3 my $iter = shift;
152
153 # TODO: must print the header line corresponding to the bindings in the entire iterator...
154 3 my $string = '';
155 3 my $e = $iter->peek;
156 3 ▶ if (defined($e) and blessed($e) and $e->isa('RDF::Trine::Statement')) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $e and builtin::blessed($e) and $e->isa('RDF::Trine::Statement')) { } 2 1 3
elsif (defined $e and builtin::blessed($e) and $e->isa('RDF::Trine::VariableBindings')) { } 1 0 1
Condition50%3/6
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::Statement")
!ll&&!rl&&r
012
defined $e and builtin::blessed($e)
!ll&&!rl&&r
003
MC/DC33%1/3
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::Statement")
defined $e builtin::blessed($e) $e->isa("RDF::Trine::Statement")
Truth table50%2/4
A and B and C
A= defined $eB= builtin::blessed($e)C= $e->isa("RDF::Trine::Statement")
ABCresult
0XX0
10X0
1100
1111
157 2 $string .= join("\t", qw(?s ?p ?o)) . "\n";
158 2 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 7 2 9
159 7 $string .= $self->statement_as_string( $st );
160 }
161 ▶ } elsif (defined($e) and blessed($e) and $e->isa('RDF::Trine::VariableBindings')) {
Condition33%2/6
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::VariableBindings")
!ll&&!rl&&r
001
defined $e and builtin::blessed($e)
!ll&&!rl&&r
001
MC/DC0%0/3
defined $e and builtin::blessed($e) and $e->isa("RDF::Trine::VariableBindings")
defined $e builtin::blessed($e) $e->isa("RDF::Trine::VariableBindings")
Truth table25%1/4
A and B and C
A= defined $eB= builtin::blessed($e)C= $e->isa("RDF::Trine::VariableBindings")
ABCresult
0XX0
10X0
1100
1111
162 1 my @names = $iter->binding_names;
163 1 $string .= join("\t", map { "?$_" } @names) . "\n";
164 1 ▶ while (my $r = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $iter->next) 3 1 4
165 3 $string .= $self->result_as_string( $r, \@names );
166 }
167 }
168 3 return $string;
169 }
170  
171 0 ▶sub _serialize_bounded_description {
Subroutine _serialize_bounded_description: not called CC=5 SCAR=34.0
172 0 my $self = shift;
173 0 my $model = shift;
174 0 my $node = shift;
175 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
176 # TODO: must print the header line, but only on the first (non-recursive) call to _serialize_bounded_description
177 0 ▶ return '' if ($seen->{ $node->sse }++);
Branch: 1 branch
BranchTrueFalseTotal
if $$seen{$node->sse}++ 0 0 0
178 0 my $iter = $model->get_statements( $node, undef, undef );
179 0 my $string = '';
180 0 ▶ while (my $st = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $st = $iter->next) 0 0 0
181 0 my @nodes = $st->nodes;
182 0 $string .= $self->statement_as_string( $st );
183 0 ▶ if ($nodes[2]->isa('RDF::Trine::Node::Blank')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nodes[2]->isa("RDF::Trine::Node::Blank")) 0 0 0
184 0 $string .= $self->_serialize_bounded_description( $model, $nodes[2], $seen );
185 }
186 }
187 0 return $string;
188 }
189  
190 =item C<< result_as_string ( $result, \@names ) >>
191  
192 Returns a string with the bound terms of the given RDF::Trine::VariableBindings
193 corresponding to the given C<@names> serialized in N-Triples format, separated
194 by tab characters.
195  
196 =cut
197  
198 3 ▶sub result_as_string {
Subroutine result_as_string: called CC=2 SCAR=6.9
Pod documented
199 3 my $self = shift;
200 3 my $r = shift;
201 3 my $names = shift;
202 3 my @terms = map { $r->{ $_ } } @$names;
203 3 ▶ return join("\t", map { blessed($_) ? $_->as_ntriples : '' } @terms) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 5 1 6
204 }
205  
206 =item C<< statement_as_string ( $st ) >>
207  
208 Returns a string with the nodes of the given RDF::Trine::Statement serialized
209 in N-Triples format, separated by tab characters.
210  
211 =cut
212  
213 8 ▶sub statement_as_string {
Subroutine statement_as_string: called CC=1 SCAR=0.0
Pod documented
214 8 my $self = shift;
215 8 my $st = shift;
216 8 my @nodes = $st->nodes;
217 8 return join("\t", map { $_->as_ntriples } @nodes[0..2]) . "\n";
218 }
219  
220 68 1;
221  
222 __END__
« RDF/Trine/Store/Redland.pm ↑ Summary RDF/Trine/Store/Dydra.pm »