blib/lib/RDF/Trine/Serializer/SparqlUpdate.pm

filter: Statementstmt 63.5% 110 / 173 Branchbran 37.0% 20 / 54 Conditioncond 40.0% 4 / 10 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 72.7% 24 / 33 Podpod 100% 4 / 4 totaltotal 58.2% 163 / 280 CC 36 SCAR 22.9
CC
36
Cov
57%
_serialize_atomic
30.0
__ANON__
30.0
_to_iter
20.0
CRAP 9.9
↑ Summary
1 # RDF::Trine::Serializer::SparqlUpdate
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Trine::Serializer::SparqlUpdate - SPARQL/U serialization of triples
7  
8 =head1 SYNOPSIS
9  
10 my $sparqlu = RDF::Trine::Serializer::SparqlUpdate->new;
11 $query = $sparqlu->serialize_to_string( $stmt, delete => $model );
12 $query = $sparqlu->serialize_to_string( undef, delete => $iter );
13  
14 my $sparqlu_quad = RDF::Trine::Serializer->new('sparqlu', quad_semantics => 1 );
15 $fh = $sparqlu_quad->serialize_to_io( $model );
16 while (<$fh>) {
17 print $_;
18 }
19  
20 =head1 DESCRIPTION
21  
22 TODO
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  
30 =cut
31  
32 package RDF::Trine::Serializer::SparqlUpdate;
33  
34 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 3 ▶no warnings 'redefine', 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 3 ▶use base qw(RDF::Trine::Serializer);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 3 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 3 ▶use IO::Handle::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use SUPER;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 3 ▶use RDF::Trine::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use RDF::Trine::Statement;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use RDF::Trine::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 ▶use RDF::Trine::Serializer::NTriples;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 ######################################################################
52  
53 3 our ($VERSION);
54  
55 3 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
56 3 $VERSION = '0.002';
57 3 $RDF::Trine::Serializer::serializer_names{'sparqlu'} = __PACKAGE__;
58 3 $RDF::Trine::Serializer::serializer_names{'sparqlupdate'} = __PACKAGE__;
59 3 $RDF::Trine::Serializer::format_uris{'http://www.w3.org/Submission/SPARQL-Update/'}
60 = __PACKAGE__;
61 3 foreach my $type (qw(application/sparql-update)) {
62 3 $RDF::Trine::Serializer::media_types{$type} = __PACKAGE__;
63 }
64 }
65  
66 ######################################################################
67  
68 =head2 C<< new( [quad_semantics => (0|1)], [atomic => (0|1)] ) >>
69  
70 Returns a new SPARQL/Update serializer object.
71  
72 If C< quad_semantics > is set, contexts/graphs will be considered for INSERT
73 and DELETE clauses. Otherwise, the serializer works on the union graph of the
74 model.
75  
76 If C< atomic > is set, every statement is wrapped in it's own INSERT/DELETE
77 clause.
78  
79 =cut
80  
81 7 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
82 7 my $class = shift;
83 7 my %args = @_;
84  
85 7 ▶ $args{quad_semantics} //= undef;
Condition100%2/2
$args{"quad_semantics"} //= undef
l!l
25
MC/DC100%1/1
$args{"quad_semantics"} //= undef
$args{"quad_semantics"}
Truth table100%2/2
A
A= $args{"quad_semantics"}
Aresult
11
00
86 7 $args{serializer} = RDF::Trine::Serializer::NTriples->new(%args);
87  
88 7 my $self = bless( {%args}, $class );
89 7 return $self;
90 }
91  
92 =head2 C<< serialize_to_string ( $data, %opts ) >>
93  
94 Coerces both $data and $opts{delete} to a model and calls C<<
95 serialize_model_to_string >> with those.
96  
97 =cut
98  
99 3 ▶sub serialize_to_string {
Subroutine serialize_to_string: called CC=2 SCAR=9.9
Pod documented
100 3 my $self = shift;
101 3 ▶ if ($self->{atomic}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'atomic'}) { } 0 3 3
102 0 return join "", do {
103 0 my $fh = $self->_serialize_atomic( @_ );
104 0 local $/;
105 <$fh>
106 0 };
107 }
108 else {
109 3 return $self->_serialize_non_atomic( @_ );
110 }
111 }
112  
113 =head2 C<< serialize_to_file ( $fh, $data, %opts ) >>
114  
115 Coerces both $data and $opts{delete} to a model and calls C<<
116 serialize_model_to_string >> with those and writes the resulting
117 string to filehandle $fh.
118  
119 =cut
120  
121 2 ▶sub serialize_to_file {
Subroutine serialize_to_file: called CC=1 SCAR=0.0
Pod documented
122 2 my $self = shift;
123 2 my $file = shift;
124 2 print {$file} $self->serialize_to_string(@_);
125 }
126  
127 =head2 C<< serialize_to_io( $data, %opts ) >>
128  
129 Returns an IO::Handle with the C<$data> and $opts{delete} serialized to atomic SPARQL/U clauses
130  
131 =cut
132 0 ▶sub serialize_to_io {
Subroutine serialize_to_io: not called CC=1 SCAR=6.9
Pod documented
133 0 my $self =shift;
134 0 return $self->_serialize_atomic( @_ );
135 }
136  
137 =head2 C<< serialize_model_to_string ( $model ) >>
138  
139 Serializes the C<$model> to SPARQL/Update, returning the result as a string.
140  
141 =cut
142  
143 3 *serialize_model_to_string = *serialize_to_string;
144  
145 =head2 C<< serialize_model_to_file ( $fh, $model ) >>
146  
147 Serializes the C<$model> to SPARQL/Update, printing the results to the supplied
148 filehandle C<<$fh>>.
149  
150 Alias for L</serialize_to_file>.
151  
152 =cut
153  
154 3 *serialize_model_to_file = *serialize_to_file;
155  
156  
157 =head2 C<< serialize_iterator_to_file ( $file, $iter ) >>
158  
159 Serializes the iterator to SPARQL/Update, printing the results to the supplied
160 filehandle C<<$fh>>.
161  
162 Alias for L</serialize_to_file>.
163  
164 =cut
165  
166 3 *serialize_iterator_to_file = *serialize_to_file;
167  
168 =head2 C<< serialize_iterator_to_string ( $iter ) >>
169  
170 Serializes the iterator to SPARQL/Update, returning the result as a string.
171  
172 Alias for L</serialize_to_string>.
173  
174 =cut
175  
176 3 *serialize_iterator_to_string = *serialize_to_string;
177  
178 =head2 C<< statement_as_string ( $st ) >>
179  
180 Serializes a statement to a SPARQL/Update INSERT DATA clause.
181  
182 Alias for L</serialize_to_string>.
183  
184 =cut
185  
186 3 *statement_as_string = *serialize_to_string;
187  
188 =head2 C<< serialize_model_to_io( $model ) >>
189  
190 TODO Returns an IO::Handle with the C<$model> serialized to atomic SPARQL/U clauses
191  
192 =cut
193  
194 # sub serialize_model_to_io {
195 # my $self = shift;
196 # my $model = shift;
197  
198  
199  
200 =head2 C<< _serialize_data_to_ntriples( $data ) >>
201  
202 Turns $data into a string of N-Triples.
203  
204 =cut
205  
206  
207 3 ▶sub _serialize_data_to_ntriples {
Subroutine _serialize_data_to_ntriples: called CC=4 SCAR=14.0
208 3 my $self = shift;
209 3 my $data = shift;
210 3 ▶ return "" unless blessed $data;
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($data) 0 3 3
211 3 my %class_to_serializer_function = (
212 'RDF::Trine::Statement' => 'statement_as_string',
213 'RDF::Trine::Model' => 'serialize_model_to_string',
214 'RDF::Trine::Iterator' => 'serialize_iterator_to_string',
215 );
216 3 ▶ while ( my ( $isa, $sub ) = each %class_to_serializer_function ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %class_to_serializer_function) 6 0 6
217 6 ▶ return $self->{serializer}->$sub($data) if $data->isa($isa);
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa($isa) 3 3 6
218 }
219 }
220  
221 =head2 C<< _to_model( $data ) >>
222  
223 Turns $data into a L<RDF::Trine::Model>.
224  
225 =cut
226  
227 6 ▶sub _to_model {
Subroutine _to_model: called CC=4 SCAR=14.1
228 6 my $self = shift;
229 6 my $data = shift;
230 6 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 3 3 6
231 my %class_to_model = (
232 0 ▶ 'RDF::Trine::Statement' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
233 0 my $model = RDF::Trine::Model->temporary_model;
234 0 $model->add_statement( shift() );
235 0 return $model;
236 },
237 2 ▶ 'RDF::Trine::Iterator' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
238 2 my $iter = shift;
239 2 my $model = RDF::Trine::Model->temporary_model;
240 2 ▶ while ( my $stmt = $iter->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stmt = $iter->next) 4 2 6
241 4 $model->add_statement($stmt);
242 }
243 2 return $model;
244 },
245 1 ▶ 'RDF::Trine::Model' => sub { shift; },
Subroutine __ANON__: called CC=1 SCAR=0.0
246 3 );
247 3 ▶ while ( my ( $isa, $sub ) = each %class_to_model ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %class_to_model) 7 0 7
248 7 ▶ return $sub->($data) if $data->isa($isa);
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa($isa) 3 4 7
249 }
250 }
251  
252 0 ▶sub _to_iter {
Subroutine _to_iter: not called CC=4 SCAR=30.0
253 0 my $self =shift;
254 0 my $data = shift;
255 0 ▶ return undef unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 0 0
256 my %class_to_iter = (
257 0 ▶ 'RDF::Trine::Statement' => sub { [RDF::Trine::Iterator->new( [ shift() ], 'graph' )] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
258 0 ▶ 'RDF::Trine::Iterator' => sub { [shift] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 0 ▶ 'RDF::Trine::Model' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
260 0 my $model = shift;
261 0 my @iters;
262 0 ▶ if ( $self->{quad_semantics} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'quad_semantics'}) { } 0 0 0
263 0 my @contexts = $model->get_contexts->get_all ;
264 0 push @contexts, RDF::Trine::Node::Nil->new;
265 0 push @iters, $model->get_statements( undef, undef, undef, $_ ) for @contexts;
266 0 return \@iters;
267 }
268 else {
269 0 return [ $model->as_stream ];
270 }
271  
272 }
273 0 );
274 0 ▶ while ( my ( $isa, $sub ) = each %class_to_iter ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %class_to_iter) 0 0 0
275 0 ▶ return $sub->($data) if $data->isa($isa);
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa($isa) 0 0 0
276 }
277 }
278  
279 =head2 C<< _create_clause( $type, $data, [$graph] ) >>
280  
281 Creates a SPARQL/U 'INSERT' or 'DELETE' clause, depending on L<$type>.
282  
283 C<$data> can be anything that L</_serialize_data_to_ntriples> can coerce to N-Triples.
284  
285 If C<$graph> is given, clauses take the form
286
287 INSERT DATA { GRAPH <$graph> { ... } };
288  
289 otherwise they act on the default graph, like so:
290  
291 INSERT DATA { ... };
292  
293 =cut
294  
295 3 ▶sub _create_clause {
Subroutine _create_clause: called CC=9 SCAR=28.2
296 3 my $self = shift;
297 3 my $type = uc shift;
298 3 my $data = shift;
299 3 my %opts = @_;
300 3 my $graph = $opts{graph};
301 3 my $data_ser;
302  
303 3 ▶ return unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 3 3
304  
305 3 ▶ if ( ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) { } 3 0 3
306 3 $data_ser = $self->_serialize_data_to_ntriples($data);
307 3 ▶ return unless $data_ser;
Branch: 1 branch
BranchTrueFalseTotal
unless $data_ser 0 3 3
308 3 ▶ if (! $graph && $self->{quad_semantics}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $graph and $self->{"quad_semantics"}) 0 3 3
Condition33%1/3
not $graph and $self->{"quad_semantics"}
!ll&&!rl&&r
030
MC/DC0%0/2
not $graph and $self->{"quad_semantics"}
not $graph $self->{"quad_semantics"}
Truth table33%1/3
not(A) and B
A= not $graphB= $self->{"quad_semantics"}
ABresult
0X0
100
111
309 0 ▶ if ($data->isa('RDF::Trine::Statement::Quad')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa("RDF::Trine::Statement::Quad")) 0 0 0
310 0 $graph = [$data->nodes]->[3];
311 }
312 }
313 }
314 else {
315 0 $data_ser = $data;
316 }
317 3 ▶ if ($graph && $graph->is_resource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($graph and $graph->is_resource) { } 0 3 3
Condition33%1/3
$graph and $graph->is_resource
!ll&&!rl&&r
300
MC/DC0%0/2
$graph and $graph->is_resource
$graph $graph->is_resource
Truth table33%1/3
A and B
A= $graphB= $graph->is_resource
ABresult
0X0
100
111
318 0 my $graph_uri;
319 # if ($graph->is_resource) {
320 0 $graph_uri = $graph->uri_value;
321 # }
322 # elsif ($graph->is_blank) {
323 # $graph_uri = $graph->sse;
324 # }
325 0 return sprintf( "%s DATA { GRAPH <%s> {\n%s}};\n", $type, $graph_uri, $data_ser );
326 }
327 else {
328 3 return sprintf( "%s DATA { \n%s};\n", $type, $data_ser );
329 }
330 }
331  
332 3 ▶sub _serialize_non_atomic {
Subroutine _serialize_non_atomic: called CC=5 SCAR=18.2
333 3 my $self = shift;
334 3 my $insert_model = shift;
335 3 my %opts = @_;
336 my %model = (
337 INSERT => $self->_to_model($insert_model),
338 3 DELETE => $self->_to_model( $opts{delete} ),
339 );
340 3 my @clauses;
341 3 for ( keys %model ) {
342 6 ▶ next unless $model{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless $model{$_} 3 3 6
343 3 ▶ if ( $self->{quad_semantics} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'quad_semantics'}) { } 0 3 3
344 0 my $iter_context = $model{$_}->get_contexts;
345  
346 # create clauses for every context'd statement
347 0 ▶ while ( my $cur_context = $iter_context->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cur_context = $iter_context->next) 0 0 0
348 push @clauses,
349 $self->_create_clause( $_,
350 0 $model{$_}->get_statements( undef, undef, undef, $cur_context ),
351 graph => $cur_context );
352 }
353  
354 # also those statements not in any context
355 push @clauses,
356 $self->_create_clause( $_,
357 0 $model{$_}->get_statements( undef, undef, undef, RDF::Trine::Node::Nil->new ),
358 graph => RDF::Trine::Node::Nil->new
359 );
360 }
361 else {
362 3 push @clauses, $self->_create_clause( $_, $model{$_} );
363 }
364 }
365 0 return join "", sort { $a cmp $b } map {$_} @clauses;
366 }
367  
368 0 ▶sub _serialize_atomic {
Subroutine _serialize_atomic: not called CC=5 SCAR=34.0
369 0 my $self =shift;
370 0 my $insert_data = shift;
371 0 my %opts = @_;
372 my %iters = (
373 0 DELETE => $self->_to_iter($opts{delete}),
374 INSERT => $self->_to_iter($insert_data),
375 );
376 0 my @iters_in_order;
377 0 for (qw(DELETE INSERT)) {
378 0 ▶ if ( $iters{$_} && scalar @{ $iters{$_} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iters{$_} and scalar @{$iters{$_};}) 0 0 0
Condition0%0/2
$iters{$_} and scalar @{$iters{$_};}
!ll
00
MC/DC0%0/1
$iters{$_} and scalar @{$iters{$_};}
$iters{$_}
Truth table0%0/2
A
A= $iters{$_}
Aresult
00
11
379 0 push @iters_in_order, $_ => @{$iters{$_}};
380 }
381 }
382 0 ▶ return sub {1} unless @iters_in_order;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless @iters_in_order 0 0 0
383  
384 # warn Dumper \@iters_in_order;
385  
386  
387 0 my $current_type = shift @iters_in_order;
388 0 my $current_iter = shift @iters_in_order;
389 0 ▶ my $sub = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
390 0 my $st = $current_iter->next;
391 0 ▶ if ($current_iter->finished) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_iter->finished) 0 0 0
392 0 my $iter_or_type = shift @iters_in_order;
393 0 ▶ return unless $iter_or_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $iter_or_type 0 0 0
394 0 ▶ if ( ! ref $iter_or_type ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $iter_or_type) { } 0 0 0
395 0 $current_type = $iter_or_type;
396 0 $current_iter = shift @iters_in_order;
397 }
398 else {
399 0 $current_iter = $iter_or_type;
400 }
401 0 $st = $current_iter->next;
402 }
403 0 ▶ return unless blessed($st);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($st) 0 0 0
404 0 return $self->_create_clause( $current_type, $st, @_ );
405 0 };
406 0 return IO::Handle::Iterator->new( $sub );
407 }
408  
409  
410 3 1;
411  
412 __END__
↑ Summary