| line | stmt | bran | cond | sub | pod | time | code | 
| 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.017 | 
| 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 | 1 |  |  | 1 |  | 374 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 34 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 35 | 1 |  |  | 1 |  | 5 | use base qw(RDF::Trine::Serializer); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 55 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 |  |  | 1 |  | 5 | use URI; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 20 |  | 
| 38 | 1 |  |  | 1 |  | 4 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 49 |  | 
| 39 | 1 |  |  | 1 |  | 6 | use Data::Dumper; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 40 | 1 |  |  | 1 |  | 5 | use Scalar::Util qw(blessed); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 38 |  | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 1 |  |  | 1 |  | 6 | use RDF::Trine::Node; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 43 | 1 |  |  | 1 |  | 5 | use RDF::Trine::Statement; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 44 | 1 |  |  | 1 |  | 5 | use RDF::Trine::Error qw(:try); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | ###################################################################### | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | our ($VERSION); | 
| 49 |  |  |  |  |  |  | BEGIN { | 
| 50 | 1 |  |  | 1 |  | 190 | $VERSION	= '1.017'; | 
| 51 | 1 |  |  |  |  | 4 | $RDF::Trine::Serializer::serializer_names{ 'tsv' }	= __PACKAGE__; | 
| 52 | 1 |  |  |  |  | 2 | $RDF::Trine::Serializer::format_uris{ 'http://www.w3.org/ns/formats/TSV' }	= __PACKAGE__; | 
| 53 | 1 |  |  |  |  | 3 | foreach my $type (qw(text/tsv)) { | 
| 54 | 1 |  |  |  |  | 829 | $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 |  |  |  |  |  |  | sub new { | 
| 67 | 1 |  |  | 1 | 1 | 10 | my $class	= shift; | 
| 68 | 1 |  |  |  |  | 4 | my %args	= @_; | 
| 69 | 1 |  |  |  |  | 4 | my $self = bless( {}, $class); | 
| 70 | 1 |  |  |  |  | 3 | 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 |  |  |  |  |  |  | sub serialize_model_to_file { | 
| 81 | 0 |  |  | 0 | 1 | 0 | my $self	= shift; | 
| 82 | 0 |  |  |  |  | 0 | my $file	= shift; | 
| 83 | 0 |  |  |  |  | 0 | my $model	= shift; | 
| 84 | 0 |  |  |  |  | 0 | my $st		= RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 85 | 0 |  |  |  |  | 0 | my $pat		= RDF::Trine::Pattern->new( $st ); | 
| 86 | 0 |  |  |  |  | 0 | my $stream	= $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] ); | 
| 87 | 0 |  |  |  |  | 0 | my $iter	= $stream->as_statements( qw(s p o) ); | 
| 88 | 0 |  |  |  |  | 0 | print {$file} join("\t", qw(s p o)); | 
|  | 0 |  |  |  |  | 0 |  | 
| 89 | 0 |  |  |  |  | 0 | while (my $st = $iter->next) { | 
| 90 | 0 |  |  |  |  | 0 | print {$file} $self->statement_as_string( $st ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 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 |  |  |  |  |  |  | sub serialize_model_to_string { | 
| 101 | 0 |  |  | 0 | 1 | 0 | my $self	= shift; | 
| 102 | 0 |  |  |  |  | 0 | my $model	= shift; | 
| 103 | 0 |  |  |  |  | 0 | my $st		= RDF::Trine::Statement->new( map { RDF::Trine::Node::Variable->new($_) } qw(s p o) ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 104 | 0 |  |  |  |  | 0 | my $pat		= RDF::Trine::Pattern->new( $st ); | 
| 105 | 0 |  |  |  |  | 0 | my $stream	= $model->get_pattern( $pat, undef, orderby => [ qw(s ASC p ASC o ASC) ] ); | 
| 106 | 0 |  |  |  |  | 0 | my $iter	= $stream->as_statements( qw(s p o) ); | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 0 |  |  |  |  | 0 | my $string	= join("\t", qw(s p o)) . "\n"; | 
| 109 | 0 |  |  |  |  | 0 | while (my $st = $iter->next) { | 
| 110 | 0 |  |  |  |  | 0 | my @nodes	= $st->nodes; | 
| 111 | 0 |  |  |  |  | 0 | $string		.= $self->statement_as_string( $st ); | 
| 112 |  |  |  |  |  |  | } | 
| 113 | 0 |  |  |  |  | 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 |  |  |  |  |  |  | sub serialize_iterator_to_file { | 
| 124 | 0 |  |  | 0 | 1 | 0 | my $self	= shift; | 
| 125 | 0 |  |  |  |  | 0 | my $file	= shift; | 
| 126 | 0 |  |  |  |  | 0 | my $iter	= shift; | 
| 127 | 0 |  |  |  |  | 0 | my $e		= $iter->peek; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 0 | 0 | 0 |  |  | 0 | if (defined($e) and blessed($e) and $e->isa('RDF::Trine::Statement')) { | 
|  |  | 0 | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
|  |  |  | 0 |  |  |  |  | 
| 130 | 0 |  |  |  |  | 0 | print {$file} join("\t", qw(?s ?p ?o)) . "\n"; | 
|  | 0 |  |  |  |  | 0 |  | 
| 131 | 0 |  |  |  |  | 0 | while (my $st = $iter->next) { | 
| 132 | 0 |  |  |  |  | 0 | print {$file} $self->statement_as_string( $st ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  | } elsif (defined($e) and blessed($e) and $e->isa('RDF::Trine::VariableBindings')) { | 
| 135 | 0 |  |  |  |  | 0 | my @names	= $iter->binding_names; | 
| 136 | 0 |  |  |  |  | 0 | print {$file} join("\t", map { "?$_" } @names) . "\n"; | 
|  | 0 |  |  |  |  | 0 |  | 
|  | 0 |  |  |  |  | 0 |  | 
| 137 | 0 |  |  |  |  | 0 | while (my $r = $iter->next) { | 
| 138 | 0 |  |  |  |  | 0 | print {$file} $self->result_as_string( $r, \@names ); | 
|  | 0 |  |  |  |  | 0 |  | 
| 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 |  |  |  |  |  |  | sub serialize_iterator_to_string { | 
| 150 | 3 |  |  | 3 | 1 | 15 | my $self	= shift; | 
| 151 | 3 |  |  |  |  | 6 | my $iter	= shift; | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | # TODO: must print the header line corresponding to the bindings in the entire iterator... | 
| 154 | 3 |  |  |  |  | 6 | my $string	= ''; | 
| 155 | 3 |  |  |  |  | 12 | my $e		= $iter->peek; | 
| 156 | 3 | 100 | 33 |  |  | 43 | if (defined($e) and blessed($e) and $e->isa('RDF::Trine::Statement')) { | 
|  |  | 50 | 66 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
|  |  |  | 33 |  |  |  |  | 
| 157 | 2 |  |  |  |  | 5 | $string	.= join("\t", qw(?s ?p ?o)) . "\n"; | 
| 158 | 2 |  |  |  |  | 6 | while (my $st = $iter->next) { | 
| 159 | 7 |  |  |  |  | 17 | $string		.= $self->statement_as_string( $st ); | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  | } elsif (defined($e) and blessed($e) and $e->isa('RDF::Trine::VariableBindings')) { | 
| 162 | 1 |  |  |  |  | 6 | my @names	= $iter->binding_names; | 
| 163 | 1 |  |  |  |  | 3 | $string	.= join("\t", map { "?$_" } @names) . "\n"; | 
|  | 2 |  |  |  |  | 7 |  | 
| 164 | 1 |  |  |  |  | 4 | while (my $r = $iter->next) { | 
| 165 | 3 |  |  |  |  | 9 | $string		.= $self->result_as_string( $r, \@names ); | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  | } | 
| 168 | 3 |  |  |  |  | 8 | return $string; | 
| 169 |  |  |  |  |  |  | } | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | sub _serialize_bounded_description { | 
| 172 | 0 |  |  | 0 |  | 0 | my $self	= shift; | 
| 173 | 0 |  |  |  |  | 0 | my $model	= shift; | 
| 174 | 0 |  |  |  |  | 0 | my $node	= shift; | 
| 175 | 0 |  | 0 |  |  | 0 | my $seen	= shift || {}; | 
| 176 |  |  |  |  |  |  | # TODO: must print the header line, but only on the first (non-recursive) call to _serialize_bounded_description | 
| 177 | 0 | 0 |  |  |  | 0 | return '' if ($seen->{ $node->sse }++); | 
| 178 | 0 |  |  |  |  | 0 | my $iter	= $model->get_statements( $node, undef, undef ); | 
| 179 | 0 |  |  |  |  | 0 | my $string	= ''; | 
| 180 | 0 |  |  |  |  | 0 | while (my $st = $iter->next) { | 
| 181 | 0 |  |  |  |  | 0 | my @nodes	= $st->nodes; | 
| 182 | 0 |  |  |  |  | 0 | $string		.= $self->statement_as_string( $st ); | 
| 183 | 0 | 0 |  |  |  | 0 | if ($nodes[2]->isa('RDF::Trine::Node::Blank')) { | 
| 184 | 0 |  |  |  |  | 0 | $string	.= $self->_serialize_bounded_description( $model, $nodes[2], $seen ); | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  | } | 
| 187 | 0 |  |  |  |  | 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 |  |  |  |  |  |  | sub result_as_string { | 
| 199 | 3 |  |  | 3 | 1 | 6 | my $self	= shift; | 
| 200 | 3 |  |  |  |  | 4 | my $r		= shift; | 
| 201 | 3 |  |  |  |  | 4 | my $names	= shift; | 
| 202 | 3 |  |  |  |  | 6 | my @terms	= map { $r->{ $_ } } @$names; | 
|  | 6 |  |  |  |  | 15 |  | 
| 203 | 3 | 100 |  |  |  | 6 | return join("\t", map { blessed($_) ? $_->as_ntriples : '' } @terms) . "\n"; | 
|  | 6 |  |  |  |  | 29 |  | 
| 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 |  |  |  |  |  |  | sub statement_as_string { | 
| 214 | 7 |  |  | 7 | 1 | 11 | my $self	= shift; | 
| 215 | 7 |  |  |  |  | 11 | my $st		= shift; | 
| 216 | 7 |  |  |  |  | 22 | my @nodes	= $st->nodes; | 
| 217 | 7 |  |  |  |  | 19 | return join("\t", map { $_->as_ntriples } @nodes[0..2]) . "\n"; | 
|  | 21 |  |  |  |  | 58 |  | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | 1; | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | __END__ | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | =back | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | =head1 BUGS | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | Please report any bugs or feature requests to through the GitHub web interface | 
| 229 |  |  |  |  |  |  | at L<https://github.com/kasei/perlrdf/issues>. | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | L<http://www.w3.org/TR/rdf-testcases/#ntriples> | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | =head1 AUTHOR | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | Gregory Todd Williams  C<< <gwilliams@cpan.org> >> | 
| 238 |  |  |  |  |  |  |  | 
| 239 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | Copyright (c) 2006-2012 Gregory Todd Williams. This | 
| 242 |  |  |  |  |  |  | program is free software; you can redistribute it and/or modify it under | 
| 243 |  |  |  |  |  |  | the same terms as Perl itself. | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | =cut |