Query.pm

filter: Statementstmt 75.0% 476 / 634 Branchbran 60.3% 146 / 242 Conditioncond 66.3% 79 / 119 MC/DCmcdc 32.2% 20 / 62 Subroutinesub 81.9% 59 / 72 Podpod 100% 43 / 43 totaltotal 70.2% 823 / 1172 CC 172 SCAR 26.3
CC
172
Cov
70%
new
58.6
as_sparql
21.6
prepare_with_named_graphs
20.0
CRAP 13.9
« Query/Algebra/NamedGraph.pm ↑ Summary Query/Federate.pm »
1 # RDF::Query
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query - A complete SPARQL 1.1 Query and Update implementation for use with RDF::Trine.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query version 2.919.
11  
12 =head1 SYNOPSIS
13  
14 # SPARQL SELECT Query
15 my $query = RDF::Query->new( 'SELECT * WHERE ...' );
16 my $iterator = $query->execute( $model );
17 while (my $row = $iterator->next) {
18 # $row is a HASHref containing variable name -> RDF Term bindings
19 print $row->{ 'var' }->as_string;
20 }
21
22 # SPARQL CONSTRUCT/DESCRIBE Query
23 my $query = RDF::Query->new( 'CONSTRUCT { ... } WHERE ...' );
24 my $iterator = $query->execute( $model );
25 while (my $st = $iterator->next) {
26 # $st is a RDF::Trine::Statement object representing an RDF triple
27 print $st->as_string;
28 }
29
30 # SPARQL ASK Query
31 my $query = RDF::Query->new( 'ASK WHERE ...' );
32 my $iterator = $query->execute( $model );
33 my $bool = $iterator->get_boolean;
34 if ($bool) {
35 print "Yes!\n";
36 }
37
38 # RDQL Query
39 my $query = new RDF::Query ( $rdql, { lang => 'rdql' } );
40 my @rows = $query->execute( $model ); # in list context, returns all results
41  
42 =head1 DESCRIPTION
43  
44 RDF::Query allows SPARQL and RDQL queries to be run against an RDF model,
45 returning rows of matching results.
46  
47 See L<http://www.w3.org/TR/rdf-sparql-query/> for more information on SPARQL.
48  
49 See L<http://www.w3.org/Submission/2004/SUBM-RDQL-20040109/> for more
50 information on RDQL.
51  
52 =head1 CHANGES IN VERSION 2.900
53  
54 The 2.9xx versions of RDF::Query introduce some significant changes that will
55 lead to a stable 3.000 release supporting SPARQL 1.1. Version 2.902 introduces
56 the SPARQL 1.1 features up to date with the SPARQL 1.1 working drafts as of its
57 release date. Version 2.902 also is the first version to require use of
58 RDF::Trine for the underlying RDF store. This change means that RDF::Core is
59 no longer supported, and while Redland is still supported, its handling of
60 "contexts" (named graphs) means that existing RDF triples stored in Redland
61 without associated contexts will not be accessible from RDF::Query.
62 See L<RDF::Trine::Store> for more information on supported backend stores.
63  
64 =head1 CHANGES IN VERSION 2.000
65  
66 There are many changes in the code between the 1.x and 2.x releases. Most of
67 these changes will only affect queries that should have raised errors in the
68 first place (SPARQL parsing, queries that use undefined namespaces, etc.).
69 Beyond these changes, however, there are some significant API changes that will
70 affect all users:
71  
72 =over 4
73  
74 =item Use of RDF::Trine objects
75  
76 All nodes and statements returned by RDF::Query are now RDF::Trine objects
77 (more specifically, RDF::Trine::Node and RDF::Trine::Statement objects). This
78 differes from RDF::Query 1.x where nodes and statements were of the same type
79 as the underlying model (Redland nodes from a Redland model and RDF::Core nodes
80 from an RDF::Core model).
81  
82 In the past, it was possible to execute a query and not know what type of nodes
83 were going to be returned, leading to overly verbose code that required
84 examining all nodes and statements with the bridge object. This new API brings
85 consistency to both the execution model and client code, greatly simplifying
86 interaction with query results.
87  
88 =item Binding Result Values
89  
90 Binding result values returned by calling C<< $iterator->next >> are now HASH
91 references (instead of ARRAY references), keyed by variable name. Where prior
92 code might use this code (modulo model definition and namespace declarations):
93  
94 my $sparql = 'SELECT ?name ?homepage WHERE { [ foaf:name ?name ; foaf:homepage ?homepage ] }';
95 my $query = RDF::Query->new( $sparql );
96 my $iterator = $query->execute( $model );
97 while (my $row = $iterator->()) {
98 my ($name, $homepage) = @$row;
99 # ...
100 }
101  
102 New code using RDF::Query 2.000 and later should instead use:
103  
104 my $sparql = 'SELECT ?name ?homepage WHERE { [ foaf:name ?name ; foaf:homepage ?homepage ] }';
105 my $query = RDF::Query->new( $sparql );
106 my $iterator = $query->execute( $model );
107 while (my $row = $iterator->next) {
108 my $name = $row->{ name };
109 my $homepage = $row->{ homepage };
110 # ...
111 }
112  
113 (Also notice the new method calling syntax for retrieving rows.)
114  
115 =back
116  
117 =cut
118  
119 package RDF::Query;
120  
121 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
122 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
123 35 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 35 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 35 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 35 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 35 ▶use I18N::LangTags;
Subroutine BEGIN: called CC=1 SCAR=0.0
129 35 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
130 35 ▶use Scalar::Util qw(blessed reftype looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
131 35 ▶use DateTime::Format::W3CDTF;
Subroutine BEGIN: called CC=1 SCAR=0.0
132  
133 35 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
134 35 ▶if (! Log::Log4perl::initialized()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Log::Log4perl::initialized()) 0 35 35
135 0 Log::Log4perl->easy_init($ERROR);
136 }
137  
138 35 ▶no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
139 35 ▶use RDF::Trine 1.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
140 35 require RDF::Query::Functions; # (needs to happen at runtime because some of the functions rely on RDF::Query being fully loaded (to call add_hook(), for example))
141 # all the built-in functions including:
142 # datatype casting, language ops, logical ops,
143 # numeric ops, datetime ops, and node type testing
144 # also, custom functions including:
145 # jena:sha1sum, jena:now, jena:langeq, jena:listMember
146 # ldodds:Distance, kasei:warn
147 35 ▶use RDF::Query::Expression;
Subroutine BEGIN: called CC=1 SCAR=0.0
148 35 ▶use RDF::Query::Algebra;
Subroutine BEGIN: called CC=1 SCAR=0.0
149 35 ▶use RDF::Query::Node qw(iri);
Subroutine BEGIN: called CC=1 SCAR=0.0
150 35 ▶use RDF::Query::Parser::RDQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
151 35 ▶use RDF::Query::Parser::SPARQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
152 35 ▶use RDF::Query::Parser::SPARQL11;
Subroutine BEGIN: called CC=1 SCAR=0.0
153 35 ▶use RDF::Query::Compiler::SQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 35 ▶use RDF::Query::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
155 35 ▶use RDF::Query::Plan;
Subroutine BEGIN: called CC=1 SCAR=0.0
156  
157 ######################################################################
158  
159 35 our ($VERSION, $DEFAULT_PARSER);
160 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
161 35 $VERSION = '2.919';
162 35 $DEFAULT_PARSER = 'sparql11';
163 }
164  
165  
166 ######################################################################
167  
168 =head1 METHODS
169  
170 =over 4
171  
172 =item C<< new ( $query, \%options ) >>
173  
174 Returns a new RDF::Query object for the specified C<$query>.
175 The query language defaults to SPARQL 1.1, but may be set specifically
176 with the appropriate C<< %options >> value. Valid C<< %options >> are:
177  
178 * lang
179  
180 Specifies the query language. Acceptable values are 'sparql11', 'sparql', or 'rdql'.
181  
182 * base_uri
183  
184 Specifies the base URI used in parsing the query.
185  
186 * update
187  
188 A boolean value indicating whether update operations are allowed during query execution.
189  
190 * load_data
191  
192 A boolean value indicating whether URIs used in SPARQL FROM and FROM NAMED clauses
193 should be dereferenced and the resulting RDF content used to construct the dataset
194 against which the query is run.
195  
196 =cut
197  
198 209 ▶sub new {
Subroutine new: called CC=24 SCAR=40.7
Pod documented
199 209 my $class = shift;
200 209 my $query = shift;
201  
202 209 my ($base_uri, $languri, $lang, %options);
203 209 ▶ if (@_ and ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and ref $_[0]) { } 43 166 209
Condition100%3/3
@_ and ref $_[0]
!ll&&!rl&&r
4512143
MC/DC100%2/2
@_ and ref $_[0]
@_ ref $_[0]
Truth table100%3/3
A and B
A= @_B= ref $_[0]
ABresult
0X0
100
111
204 43 %options = %{ shift() };
205 43 $lang = delete $options{ lang };
206 43 ▶ $base_uri = $options{ base_uri } || $options{ base } ;
Condition33%1/3
$options{'base_uri'} || $options{'base'}
l!l&&r!l&&!r
0043
MC/DC0%0/2
$options{'base_uri'} || $options{'base'}
$options{'base_uri'} $options{'base'}
Truth table33%1/3
A || B
A= $options{'base_uri'}B= $options{'base'}
ABresult
1X1
011
000
207 43 delete $options{ base_uri };
208 43 delete $options{ base };
209 } else {
210 166 ($base_uri, $languri, $lang, %options) = @_;
211 }
212 209 $class->clear_error;
213
214 209 my $l = Log::Log4perl->get_logger("rdf.query");
215 35 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
216
217 209 my %names = (
218 rdql => 'RDF::Query::Parser::RDQL',
219 sparql => 'RDF::Query::Parser::SPARQL',
220 sparql11 => 'RDF::Query::Parser::SPARQL11',
221 );
222 209 my %uris = (
223 'http://jena.hpl.hp.com/2003/07/query/RDQL' => 'RDF::Query::Parser::RDQL',
224 'http://www.w3.org/TR/rdf-sparql-query/' => 'RDF::Query::Parser::SPARQL',
225 'http://www.w3.org/ns/sparql-service-description#SPARQL10Query' => 'RDF::Query::Parser::SPARQL',
226 'http://www.w3.org/ns/sparql-service-description#SPARQL11Query' => 'RDF::Query::Parser::SPARQL11',
227 'http://www.w3.org/ns/sparql-service-description#SPARQL11Update' => 'RDF::Query::Parser::SPARQL11',
228 );
229
230 209 ▶ if ($base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_uri) 0 209 209
231 0 $base_uri = RDF::Query::Node::Resource->new( $base_uri );
232 }
233
234 209 my %pargs;
235 209 ▶ if ($options{canonicalize}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"canonicalize"}) 0 209 209
236 0 $pargs{canonicalize} = 1;
237 }
238 209 ▶ my $update = ((delete $options{update}) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
delete $options{'update'} ? : 15 194 209
239 209 ▶ my $pclass = $names{ $lang } || $uris{ $languri } || $names{ $DEFAULT_PARSER };
Condition83%5/6
$names{$lang} || $uris{$languri} || $names{$RDF::Query::DEFAULT_PARSER}
l!l&&r!l&&!r
156530
$names{$lang} or $uris{$languri}
l!l&&r!l&&!r
149753
MC/DC0%0/3
$names{$lang} || $uris{$languri} || $names{$RDF::Query::DEFAULT_PARSER}
$names{$lang} $uris{$languri} $names{$RDF::Query::DEFAULT_PARSER}
Truth table75%3/4
A or B || C
A= $names{$lang}B= $uris{$languri}C= $names{$RDF::Query::DEFAULT_PARSER}
ABCresult
1XX1
01X1
0011
0000
240 209 my $parser = $pclass->new( %pargs );
241 209 my $parsed;
242
243 209 ▶ if (ref($query) and $query->isa('RDF::Query::Algebra')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $query and $query->isa('RDF::Query::Algebra')) { } 0 209 209
Condition33%1/3
ref $query and $query->isa("RDF::Query::Algebra")
!ll&&!rl&&r
20900
MC/DC0%0/2
ref $query and $query->isa("RDF::Query::Algebra")
ref $query $query->isa("RDF::Query::Algebra")
Truth table33%1/3
A and B
A= ref $queryB= $query->isa("RDF::Query::Algebra")
ABresult
0X0
100
111
244 0 my $method = 'SELECT';
245 0 ▶ $method = 'ASK' if ($query->isa('RDF::Query::Algebra::Ask'));
Branch: 1 branch
BranchTrueFalseTotal
if $query->isa("RDF::Query::Algebra::Ask") 0 0 0
246 0 ▶ $method = 'CONSTRUCT' if ($query->isa('RDF::Query::Algebra::Construct'));
Branch: 1 branch
BranchTrueFalseTotal
if $query->isa("RDF::Query::Algebra::Construct") 0 0 0
247 0 my @vars = map { RDF::Query::Node::Variable->new($_) } _uniq($query->potentially_bound);
248 0 ▶ if ($method eq 'SELECT') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "SELECT") 0 0 0
249 0 ▶ unless ($query->isa('RDF::Query::Algebra::Project')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query->isa("RDF::Query::Algebra::Project")) 0 0 0
250 0 $query = RDF::Query::Algebra::Project->new($query, \@vars);
251 }
252 }
253 $parsed = {
254 0 method => $method,
255 triples => [$query],
256 sources => [],
257 base => $base_uri,
258 options => {},
259 star => 0,
260 variables => \@vars,
261 };
262 0 $query = $query->as_sparql;
263 } else {
264 209 $parsed = $parser->parse( $query, $base_uri, $update );
265 }
266
267 209 my $self = $class->_new(
268 base_uri => $base_uri,
269 parser => $parser,
270 parsed => $parsed,
271 query_string => $query,
272 update => $update,
273 options => { %options },
274 );
275 209 ▶ if (exists $options{load_data}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $options{'load_data'}) { } 0 209 209
elsif ($pclass =~ /^RDF::Query::Parser::(RDQL|SPARQL)$/) { } 106 103 209
276 0 $self->{load_data} = delete $options{load_data};
277 } elsif ($pclass =~ /^RDF::Query::Parser::(RDQL|SPARQL)$/) {
278 106 $self->{load_data} = 1;
279 } else {
280 103 $self->{load_data} = 0;
281 }
282 209 ▶ unless ($parsed->{'triples'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed->{"triples"}) 2 207 209
283 2 $class->set_error( $parser->error );
284 2 $l->debug($parser->error);
285 2 return;
286 }
287
288 207 ▶ if (defined $options{defines}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options{"defines"}) 0 207 207
289 0 @{ $self->{options} }{ keys %{ $options{defines} } } = values %{ delete $options{defines} };
290 }
291
292 207 ▶ if ($options{logger}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"logger"}) 0 207 207
293 0 $l->debug("got external logger");
294 0 $self->{logger} = delete $options{logger};
295 }
296
297 207 ▶ if (my $opt = delete $options{optimize}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt = delete $options{'optimize'}) { } 0 207 207
298 0 $l->debug("got optimization flag: $opt");
299 0 $self->{optimize} = $opt;
300 } else {
301 207 $self->{optimize} = 0;
302 }
303
304 207 ▶ if (my $opt = delete $options{force_no_optimization}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $opt = delete $options{"force_no_optimization"}) 0 207 207
305 0 $l->debug("got force_no_optimization flag");
306 0 $self->{force_no_optimization} = 1;
307 }
308
309 207 ▶ if (my $time = delete $options{optimistic_threshold_time}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $time = delete $options{"optimistic_threshold_time"}) 0 207 207
310 0 $l->debug("got optimistic_threshold_time flag");
311 0 $self->{optimistic_threshold_time} = $time;
312 }
313
314 # add rdf as a default namespace to RDQL queries
315 207 ▶ if ($pclass eq 'RDF::Query::Parser::RDQL') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pclass eq "RDF::Query::Parser::RDQL") 17 190 207
316 17 $self->{parsed}{namespaces}{rdf} = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
317 }
318 207 return $self;
319 }
320  
321 210 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
322 210 my $class = shift;
323 210 my $self = bless( { @_ }, $class );
324 210 return $self;
325 }
326  
327 =item C<< get ( $model ) >>
328  
329 Executes the query using the specified model, and returns the first matching row as a LIST of values.
330  
331 =cut
332  
333 18 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
334 18 my $self = shift;
335 18 my $stream = $self->execute( @_ );
336 18 my $row = $stream->next;
337 18 ▶ if (ref($row)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $row) { } 17 1 18
338 17 return @{ $row }{ $self->variables };
339 } else {
340 1 return undef;
341 }
342 }
343  
344 =item C<< prepare ( $model ) >>
345  
346 Prepares the query, constructing a query execution plan, and returns a list
347 containing ($plan, $context). To execute the plan, call
348 C<< execute_plan( $plan, $context ) >>.
349  
350 =cut
351  
352 158 ▶sub prepare {
Subroutine prepare: called CC=13 SCAR=27.7
Pod documented
353 158 my $self = shift;
354 158 my $_model = shift;
355 158 my %args = @_;
356 158 my $l = Log::Log4perl->get_logger("rdf.query");
357
358 158 $self->{_query_cache} = {}; # a new scratch hash for each execution.
359 158 my %bound;
360 158 ▶ if ($args{ 'bind' }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"bind"}) 1 157 158
361 1 %bound = %{ $args{ 'bind' } };
362 }
363
364 158 my $delegate;
365 158 ▶ if (defined $args{ 'delegate' }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"delegate"}) 0 158 158
366 0 $delegate = delete $args{ 'delegate' };
367 0 ▶ if ($delegate and not blessed($delegate)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delegate and not builtin::blessed($delegate)) 0 0 0
Condition0%0/3
$delegate and not builtin::blessed($delegate)
!ll&&!rl&&r
000
MC/DC0%0/2
$delegate and not builtin::blessed($delegate)
$delegate not builtin::blessed($delegate)
Truth table0%0/3
A and not(B)
A= $delegateB= not builtin::blessed($delegate)
ABresult
0X0
100
111
368 0 $delegate = $delegate->new();
369 }
370 }
371 158 ▶ my $errors = ($args{ 'strict_errors' }) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'strict_errors'} ? : 0 158 158
372 158 my $parsed = $self->{parsed};
373 158 my @vars = $self->variables( $parsed );
374
375 158 local($self->{model}) = $self->{model};
376 158 ▶ my $model = $self->{model} || $self->get_model( $_model, %args );
Condition33%1/3
$self->{'model'} || $self->get_model($_model, %args)
l!l&&r!l&&!r
01580
MC/DC0%0/2
$self->{'model'} || $self->get_model($_model, %args)
$self->{'model'} $self->get_model($_model, %args)
Truth table33%1/3
A || B
A= $self->{'model'}B= $self->get_model($_model, %args)
ABresult
1X1
011
000
377 158 ▶ if ($model) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model) { } 158 0 158
378 158 $self->model( $model );
379 158 $l->debug("got model $model");
380 } else {
381 0 throw RDF::Query::Error::ModelError ( -text => "Could not create a model object." );
382 }
383
384 158 ▶ if ($self->{load_data}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"load_data"}) 100 58 158
385 100 $l->trace("loading data");
386 100 $self->load_data();
387 }
388
389 158 $model = $self->model(); # reload the model object, because load_data might have changed it.
390
391 158 ▶ my $dataset = ($model->isa('RDF::Trine::Model::Dataset')) ? $model : RDF::Trine::Model::Dataset->new($model);
Branch: 1 branch
BranchTrueFalseTotal
$model->isa('RDF::Trine::Model::Dataset') ? : 1 157 158
392
393 158 $l->trace("constructing ExecutionContext");
394 my $context = RDF::Query::ExecutionContext->new(
395 bound => \%bound,
396 model => $dataset,
397 query => $self,
398 base_uri => $parsed->{base_uri},
399 ns => $parsed->{namespaces},
400 logger => $self->logger,
401 optimize => $self->{optimize},
402 force_no_optimization => $self->{force_no_optimization},
403 optimistic_threshold_time => $self->{optimistic_threshold_time} || 0,
404 requested_variables => \@vars,
405 strict_errors => $errors,
406 options => $self->{options},
407 158 ▶ delegate => $delegate,
Condition50%1/2
$self->{'optimistic_threshold_time'} || 0
l!l
0158
MC/DC0%0/1
$self->{'optimistic_threshold_time'} || 0
$self->{'optimistic_threshold_time'}
Truth table50%1/2
A
A= $self->{'optimistic_threshold_time'}
Aresult
11
00
408 );
409 158 $self->{model} = $model;
410
411 158 $l->trace("getting QEP...");
412 158 ▶ my %plan_args = %{ $args{ planner_args } || {} };
Condition100%2/2
$args{'planner_args'} || {}
l!l
1157
MC/DC100%1/1
$args{'planner_args'} || {}
$args{'planner_args'}
Truth table100%2/2
A
A= $args{'planner_args'}
Aresult
11
00
413 158 my $plan = $self->query_plan( $context, %plan_args );
414 158 $l->trace("-> done.");
415
416 158 ▶ unless ($plan) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($plan) 0 158 158
417 0 throw RDF::Query::Error::CompilationError -text => "Query didn't produce a valid execution plan";
418 }
419
420 158 return ($plan, $context);
421 }
422  
423 =item C<execute ( $model, %args )>
424  
425 Executes the query using the specified RDF C<< $model >>. If called in a list
426 context, returns an array of rows, otherwise returns an L<RDF::Trine::Iterator>
427 object. The iterator returned may be an instance of several subclasses of
428 L<RDF::Trine::Iterator>:
429  
430 * A L<RDF::Trine::Iterator::Bindings> object is returned for query forms producing variable binding results (SELECT queries).
431  
432 * A L<RDF::Trine::Iterator::Graph> object is returned for query forms producing in an RDF graph result (DESCRIBE and CONSTRUCT queries).
433  
434 * A L<RDF::Trine::Iterator::Boolean> object is returned for query forms producing a true/false result (ASK queries).
435  
436 =cut
437  
438 138 ▶sub execute {
Subroutine execute: called CC=8 SCAR=21.6
Pod documented
439 138 my $self = shift;
440 138 my $model = shift;
441 138 my %args = @_;
442 138 my $l = Log::Log4perl->get_logger("rdf.query");
443 138 ▶ $l->debug("executing query with model " . ($model or ''));
Condition100%2/2
$model || ''
l!l
1362
MC/DC100%1/1
$model || ''
$model
Truth table100%2/2
A
A= $model
Aresult
11
00
444
445 138 my $lang_iri = '';
446 138 my $parser = $self->{parser};
447 138 my $name;
448 138 ▶ if ($parser->isa('RDF::Query::Parser::SPARQL11')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($parser->isa('RDF::Query::Parser::SPARQL11')) { } 42 96 138
elsif ($parser->isa('RDF::Query::Parser::SPARQL')) { } 83 13 96
449 42 ▶ if ($self->is_update) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_update) { } 5 37 42
450 5 $name = 'SPARQL 1.1 Update';
451 5 $lang_iri = 'http://www.w3.org/ns/sparql-service-description#SPARQL11Update';
452 } else {
453 37 $name = 'SPARQL 1.1 Query';
454 37 $lang_iri = 'http://www.w3.org/ns/sparql-service-description#SPARQL11Query';
455 }
456 } elsif ($parser->isa('RDF::Query::Parser::SPARQL')) {
457 83 $name = 'SPARQL 1.0 Query';
458 83 $lang_iri = 'http://www.w3.org/ns/sparql-service-description#SPARQL10Query';
459 }
460
461 138 local($self->{model}) = $self->{model};
462 # warn "model: $self->{model}";
463 # warn "passthrough checking if model supports $lang_iri\n";
464 138 ▶ if ($self->{options}{allow_passthrough} and $model->supports($lang_iri)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'options'}{'allow_passthrough'} and $model->supports($lang_iri)) { } 0 138 138
Condition33%1/3
$self->{"options"}{"allow_passthrough"} and $model->supports($lang_iri)
!ll&&!rl&&r
13800
MC/DC0%0/2
$self->{"options"}{"allow_passthrough"} and $model->supports($lang_iri)
$self->{"options"}{"allow_passthrough"} $model->supports($lang_iri)
Truth table33%1/3
A and B
A= $self->{"options"}{"allow_passthrough"}B= $model->supports($lang_iri)
ABresult
0X0
100
111
465 0 $l->info("delegating $name execution to the underlying model");
466 0 return $model->get_sparql( $self->{query_string} );
467 } else {
468 138 my ($plan, $context) = $self->prepare( $model, %args );
469 138 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 138 138
470 0 $l->trace(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
471 0 $l->trace($self->as_sparql);
472 0 $l->trace(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
473 }
474 138 return $self->execute_plan( $plan, $context );
475 }
476 }
477  
478 =item C<< execute_plan ( $plan, $context ) >>
479  
480 Executes the query plan generated by the C<<prepare>> method using the supplied
481 L<RDF::Query::ExecutionContext> object. Return value(s) are the same as for the
482 C<<execute>> method.
483  
484 =cut
485  
486 157 ▶sub execute_plan {
Subroutine execute_plan: called CC=7 SCAR=19.5
Pod documented
487 157 my $self = shift;
488 157 my $plan = shift;
489 157 my $context = shift;
490 157 my $model = $context->model;
491 157 my $parsed = $self->{parsed};
492 157 my @vars = $self->variables( $parsed );
493
494 157 my $l = Log::Log4perl->get_logger("rdf.query");
495
496 157 my $pattern = $self->pattern;
497 # $l->trace("calling fixup()");
498 # my $cpattern = $self->fixup();
499
500 157 my @funcs = $pattern->referenced_functions;
501 157 foreach my $f (@funcs) {
502 32 $self->run_hook( 'http://kasei.us/code/rdf-query/hooks/function_init', $f );
503 }
504
505 # RUN THE QUERY!
506
507 157 $l->debug("executing the graph pattern");
508
509 157 ▶ my $options = $parsed->{options} || {};
Condition50%1/2
$parsed->{'options'} || {}
l!l
0157
MC/DC0%0/1
$parsed->{'options'} || {}
$parsed->{'options'}
Truth table50%1/2
A
A= $parsed->{'options'}
Aresult
11
00
510 157 ▶ if ($self->{options}{plan}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"options"}{"plan"}) 0 157 157
511 0 warn $plan->sse({}, '');
512 }
513
514 157 $plan->execute( $context );
515 157 my $stream = $plan->as_iterator( $context );
516
517 157 ▶ if ($parsed->{'method'} eq 'DESCRIBE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($parsed->{'method'} eq 'DESCRIBE') { } 4 153 157
elsif ($parsed->{'method'} eq 'ASK') { } 8 145 153
518 4 $stream = $self->describe( $stream, $context );
519 } elsif ($parsed->{'method'} eq 'ASK') {
520 8 $stream = $self->ask( $stream, $context );
521 }
522
523 157 $l->debug("going to call post-execute hook");
524 157 $self->run_hook( 'http://kasei.us/code/rdf-query/hooks/post-execute', $model, $stream );
525
526 157 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 19 138 157
527 19 return $stream->get_all();
528 } else {
529 138 return $stream;
530 }
531 }
532  
533 =item C<< prepare_with_named_graphs ( $model, @uris ) >>
534  
535 =cut
536  
537 0 ▶sub prepare_with_named_graphs {
Subroutine prepare_with_named_graphs: not called CC=4 SCAR=30.0
Pod documented
538 0 my $self = shift;
539 0 my $_model = shift;
540 0 my @graphs = @_;
541 0 my $l = Log::Log4perl->get_logger("rdf.query");
542 # $self->{model} = $model;
543 0 my $model = $self->get_model( $_model );
544 0 ▶ if ($model) {
Branch: 1 branch
BranchTrueFalseTotal
if ($model) { } 0 0 0
545 0 $self->model( $model );
546 } else {
547 0 throw RDF::Query::Error::ModelError ( -text => "Could not create a model object." );
548 }
549
550 0 foreach my $gdata (@graphs) {
551 0 ▶ my $url = (blessed($gdata)) ? $gdata->uri_value : $gdata;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($gdata) ? : 0 0 0
552 0 $l->debug("-> adding graph data $url");
553 0 $self->parse_url( $url, 1 );
554 }
555
556 0 return $self->prepare( $model );
557 }
558  
559 =item C<< execute_with_named_graphs ( $model, @uris ) >>
560  
561 Executes the query using the specified RDF C<< $model >>, loading the contents
562 of the specified C<@uris> into named graphs immediately prior to matching the
563 query. Otherwise, acts just like C<< execute >>.
564  
565 =cut
566  
567 0 ▶sub execute_with_named_graphs {
Subroutine execute_with_named_graphs: not called CC=4 SCAR=30.0
Pod documented
568 0 my $self = shift;
569 0 my $_model = shift;
570 0 my @graphs;
571 my @options;
572 0 ▶ if (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 0 0 0
573 0 ▶ if (not(blessed($_[0])) and reftype($_[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($_[0]) and builtin::reftype($_[0]) eq 'ARRAY') { } 0 0 0
Condition0%0/3
not builtin::blessed($_[0]) and builtin::reftype($_[0]) eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
not builtin::blessed($_[0]) and builtin::reftype($_[0]) eq "ARRAY"
not builtin::blessed($_[0]) builtin::reftype($_[0]) eq "ARRAY"
Truth table0%0/3
not(A) and B
A= not builtin::blessed($_[0])B= builtin::reftype($_[0]) eq "ARRAY"
ABresult
0X0
100
111
574 0 @graphs = @{ shift(@_) };
575 0 @options = @_;
576 } else {
577 0 @graphs = @_;
578 }
579 }
580
581 0 my ($plan, $ctx) = $self->prepare_with_named_graphs( $_model, @graphs );
582 0 return $self->execute_plan( $plan, $ctx );
583 }
584  
585 =begin private
586  
587 =item C<< query_plan ( $execution_context ) >>
588  
589 Returns a RDF::Query::Plan object that is (hopefully) the optimal QEP for the
590 current query.
591  
592 =end private
593  
594 =cut
595  
596 158 ▶sub query_plan {
Subroutine query_plan: called CC=13 SCAR=27.0
Pod documented
597 158 my $self = shift;
598 158 my $context = shift;
599 158 my %args = @_;
600 158 my $parsed = $self->{parsed};
601
602 158 my $bound = $context->bound;
603 158 my @bkeys = keys %{ $bound };
604 158 my $model = $context->model;
605
606 158 ▶ unless ($self->{update}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"update"}) 148 10 158
607 148 ▶ if (not exists $self->{options}{'rdf.query.plan.delegate'} or $self->{options}{'rdf.query.plan.delegate'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"options"}{"rdf.query.plan.delegate"} or $self->{"options"}{"rdf.query.plan.delegate"}) 148 0 148
Condition33%1/3
not exists $self->{"options"}{"rdf.query.plan.delegate"} or $self->{"options"}{"rdf.query.plan.delegate"}
l!l&&r!l&&!r
14800
MC/DC0%0/2
not exists $self->{"options"}{"rdf.query.plan.delegate"} or $self->{"options"}{"rdf.query.plan.delegate"}
not exists $self->{"options"}{"rdf.query.plan.delegate"} $self->{"options"}{"rdf.query.plan.delegate"}
Truth table33%1/3
not(A) or B
A= not exists $self->{"options"}{"rdf.query.plan.delegate"}B= $self->{"options"}{"rdf.query.plan.delegate"}
ABresult
1X1
011
000
608 my $delegate_key = $self->{update}
609 148 ▶ ? 'http://www.w3.org/ns/sparql-service-description#SPARQL11Update'
Branch: 1 branch
BranchTrueFalseTotal
$self->{'update'} ? : 0 148 148
610 : "http://www.w3.org/ns/sparql-service-description#SPARQL10Query"; # TODO: need to determine if the query is only 1.0, and if so, check for 1.0 support. otherwise check for 1.1 support
611 148 ▶ if (scalar(@bkeys) == 0 and $model->supports($delegate_key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @bkeys == 0 and $model->supports($delegate_key)) 0 148 148
Condition66%2/3
scalar @bkeys == 0 and $model->supports($delegate_key)
!ll&&!rl&&r
11470
MC/DC0%0/2
scalar @bkeys == 0 and $model->supports($delegate_key)
scalar @bkeys == 0 $model->supports($delegate_key)
Truth table66%2/3
A and B
A= scalar @bkeys == 0B= $model->supports($delegate_key)
ABresult
0X0
100
111
612 0 ▶ my $plan = RDF::Query::Plan::Iterator->new( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
613 0 my $context = shift;
614 0 my $model = $context->model;
615 0 my $iter = $model->get_sparql( $self->{query_string} );
616 0 return $iter;
617 0 } );
618 0 return $plan;
619 }
620 }
621 }
622
623 158 my %constant_plan;
624 158 ▶ if (my $b = $self->{parsed}{bindings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $b = $self->{"parsed"}{"bindings"}) 3 155 158
625 3 my $vars = $b->{vars};
626 3 my $values = $b->{terms};
627 3 my @names = map { $_->name } @{ $vars };
628 3 my @constants;
629 3 ▶ while (my $values = shift(@{ $b->{terms} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = shift @{$$b{"terms"};}) 4 3 7
630 4 my %bound;
631 # @bound{ @names } = @{ $values };
632 4 foreach my $i (0 .. $#names) {
633 6 my $k = $names[$i];
634 6 my $v = $values->[$i];
635 6 ▶ next unless defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 5 6
636 5 $bound{ $k } = $v;
637 }
638 4 my $bound = RDF::Query::VariableBindings->new( \%bound );
639 4 push(@constants, $bound);
640 }
641 3 my $constant_plan = RDF::Query::Plan::Constant->new( @constants );
642 3 %constant_plan = ( constants => [ $constant_plan ] );
643 }
644
645 158 my $algebra = $self->pattern;
646 158 my $pclass = $self->plan_class;
647 158 my @plans = $pclass->generate_plans( $algebra, $context, %args, %constant_plan );
648
649 158 my $l = Log::Log4perl->get_logger("rdf.query.plan");
650 158 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 158 158
651 0 return @plans;
652 } else {
653 158 my ($plan) = @plans; # XXX need to figure out what's the 'best' plan here
654 158 ▶ if ($l->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_debug) 0 158 158
655 0 $l->debug("using query plan: " . $plan->sse({}, ''));
656 }
657 158 return $plan;
658 }
659 }
660  
661 =begin private
662  
663 =item C<< plan_class >>
664  
665 Returns the class name for Plan generation. This method should be overloaded by
666 RDF::Query subclasses if the implementation also provides a subclass of
667 RDF::Query::Plan.
668  
669 =end private
670  
671 =cut
672  
673 158 ▶sub plan_class {
Subroutine plan_class: called CC=1 SCAR=0.0
Pod documented
674 158 return 'RDF::Query::Plan';
675 }
676  
677 =begin private
678  
679 =item C<< describe ( $iter, $context ) >>
680  
681 Takes a stream of matching statements and constructs a DESCRIBE graph.
682  
683 =end private
684  
685 =cut
686  
687 4 ▶sub describe {
Subroutine describe: called CC=10 SCAR=23.2
Pod documented
688 4 my $self = shift;
689 4 my $stream = shift;
690 4 my $context = shift;
691 4 my $model = $context->model;
692 4 my @nodes;
693 my %seen;
694 4 ▶ while (my $row = $stream->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $stream->next) 7 4 11
695 7 foreach my $v (@{ $self->{parsed}{variables} }) {
696 7 ▶ if ($v->isa('RDF::Query::Node::Variable')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v->isa('RDF::Query::Node::Variable')) { } 6 1 7
elsif ($v->isa('RDF::Query::Node::Resource')) { } 1 0 1
697 6 my $node = $row->{ $v->name };
698 6 ▶ my $string = blessed($node) ? $node->as_string : '';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($node) ? : 6 0 6
699 6 ▶ push(@nodes, $node) unless ($seen{ $string }++);
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$string}++ 3 3 6
700 } elsif ($v->isa('RDF::Query::Node::Resource')) {
701 1 ▶ my $string = blessed($v) ? $v->as_string : '';
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($v) ? : 1 0 1
702 1 ▶ push(@nodes, $v) unless ($seen{ $string }++);
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$string}++ 1 0 1
703 }
704 }
705 }
706
707 4 my @streams;
708 4 $self->{'describe_nodes'} = [];
709 4 foreach my $node (@nodes) {
710 4 push(@{ $self->{'describe_nodes'} }, $node);
711 4 push(@streams, $model->bounded_description( $node ));
712 }
713
714 148 ▶ my $ret = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
715 148 ▶ while (@streams) {
Branch: 1 branch
BranchTrueFalseTotal
if (@streams) 148 0 148
716 148 my $val = $streams[0]->next;
717 148 ▶ if (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 144 4 148
718 144 return $val;
719 } else {
720 4 shift(@streams);
721 4 ▶ return undef if (not @streams);
Branch: 1 branch
BranchTrueFalseTotal
unless @streams 4 0 4
722 }
723 }
724 4 };
725 4 return RDF::Trine::Iterator::Graph->new( $ret );
726 }
727  
728  
729 =begin private
730  
731 =item C<ask ( $iter, $context )>
732  
733 Takes a stream of matching statements and returns a boolean query result stream.
734  
735 =end private
736  
737 =cut
738  
739 8 ▶sub ask {
Subroutine ask: called CC=2 SCAR=6.9
Pod documented
740 8 my $self = shift;
741 8 my $stream = shift;
742 8 my $context = shift;
743 8 my $value = $stream->next;
744 8 ▶ my $bool = ($value) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 5 3 8
745 8 return RDF::Trine::Iterator::Boolean->new( [ $bool ] );
746 }
747  
748 ######################################################################
749  
750 =item C<< pattern >>
751  
752 Returns the RDF::Query::Algebra::GroupGraphPattern algebra pattern for this query.
753  
754 =cut
755  
756 432 ▶sub pattern {
Subroutine pattern: called CC=14 SCAR=26.4
Pod documented
757 432 my $self = shift;
758 432 my $parsed = $self->parsed;
759 432 my @triples = @{ $parsed->{triples} };
760 432 ▶ if (scalar(@triples) == 1 and ($triples[0]->isa('RDF::Query::Algebra::GroupGraphPattern')
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @triples == 1 and $triples[0]->isa('RDF::Query::Algebra::GroupGraphPattern') || $triples[0]->isa('RDF::Query::Algebra::Filter') || $triples[0]->isa('RDF::Query::Algebra::Sort') || $triples[0]->isa('RDF::Query::Algebra::Limit') || $triples[0]->isa('RDF::Query::Algebra::Offset') || $triples[0]->isa('RDF::Query::Algebra::Distinct') || $triples[0]->isa('RDF::Query::Algebra::Project') || $triples[0]->isa('RDF::Query::Algebra::Construct') || $triples[0]->isa('RDF::Query::Algebra::Load') || $triples[0]->isa('RDF::Query::Algebra::Clear') || $triples[0]->isa('RDF::Query::Algebra::Create') || $triples[0]->isa('RDF::Query::Algebra::Update')) { } 429 3 432
Condition91%33/36
scalar @triples == 1 and $triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") || $triples[0]->isa("RDF::Query::Algebra::Filter") || $triples[0]->isa("RDF::Query::Algebra::Sort") || $triples[0]->isa("RDF::Query::Algebra::Limit") || $triples[0]->isa("RDF::Query::Algebra::Offset") || $triples[0]->isa("RDF::Query::Algebra::Distinct") || $triples[0]->isa("RDF::Query::Algebra::Project") || $triples[0]->isa("RDF::Query::Algebra::Construct") || $triples[0]->isa("RDF::Query::Algebra::Load") || $triples[0]->isa("RDF::Query::Algebra::Clear") || $triples[0]->isa("RDF::Query::Algebra::Create") || $triples[0]->isa("RDF::Query::Algebra::Update")
!ll&&!rl&&r
21429
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset") or $triples[0]->isa("RDF::Query::Algebra::Distinct") or $triples[0]->isa("RDF::Query::Algebra::Project") or $triples[0]->isa("RDF::Query::Algebra::Construct") or $triples[0]->isa("RDF::Query::Algebra::Load") or $triples[0]->isa("RDF::Query::Algebra::Clear") or $triples[0]->isa("RDF::Query::Algebra::Create") or $triples[0]->isa("RDF::Query::Algebra::Update")
l!l&&r!l&&!r
407221
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset") or $triples[0]->isa("RDF::Query::Algebra::Distinct") or $triples[0]->isa("RDF::Query::Algebra::Project") or $triples[0]->isa("RDF::Query::Algebra::Construct") or $triples[0]->isa("RDF::Query::Algebra::Load") or $triples[0]->isa("RDF::Query::Algebra::Clear") or $triples[0]->isa("RDF::Query::Algebra::Create")
l!l&&r!l&&!r
407023
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset") or $triples[0]->isa("RDF::Query::Algebra::Distinct") or $triples[0]->isa("RDF::Query::Algebra::Project") or $triples[0]->isa("RDF::Query::Algebra::Construct") or $triples[0]->isa("RDF::Query::Algebra::Load") or $triples[0]->isa("RDF::Query::Algebra::Clear")
l!l&&r!l&&!r
407023
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset") or $triples[0]->isa("RDF::Query::Algebra::Distinct") or $triples[0]->isa("RDF::Query::Algebra::Project") or $triples[0]->isa("RDF::Query::Algebra::Construct") or $triples[0]->isa("RDF::Query::Algebra::Load")
l!l&&r!l&&!r
406123
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset") or $triples[0]->isa("RDF::Query::Algebra::Distinct") or $triples[0]->isa("RDF::Query::Algebra::Project") or $triples[0]->isa("RDF::Query::Algebra::Construct")
l!l&&r!l&&!r
3961024
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset") or $triples[0]->isa("RDF::Query::Algebra::Distinct") or $triples[0]->isa("RDF::Query::Algebra::Project")
l!l&&r!l&&!r
7232434
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset") or $triples[0]->isa("RDF::Query::Algebra::Distinct")
l!l&&r!l&&!r
5814358
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit") or $triples[0]->isa("RDF::Query::Algebra::Offset")
l!l&&r!l&&!r
544372
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort") or $triples[0]->isa("RDF::Query::Algebra::Limit")
l!l&&r!l&&!r
2628376
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter") or $triples[0]->isa("RDF::Query::Algebra::Sort")
l!l&&r!l&&!r
260404
$triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") or $triples[0]->isa("RDF::Query::Algebra::Filter")
l!l&&r!l&&!r
188404
MC/DC76%10/13
scalar @triples == 1 and $triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") || $triples[0]->isa("RDF::Query::Algebra::Filter") || $triples[0]->isa("RDF::Query::Algebra::Sort") || $triples[0]->isa("RDF::Query::Algebra::Limit") || $triples[0]->isa("RDF::Query::Algebra::Offset") || $triples[0]->isa("RDF::Query::Algebra::Distinct") || $triples[0]->isa("RDF::Query::Algebra::Project") || $triples[0]->isa("RDF::Query::Algebra::Construct") || $triples[0]->isa("RDF::Query::Algebra::Load") || $triples[0]->isa("RDF::Query::Algebra::Clear") || $triples[0]->isa("RDF::Query::Algebra::Create") || $triples[0]->isa("RDF::Query::Algebra::Update")
scalar @triples == 1 $triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern") $triples[0]->isa("RDF::Query::Algebra::Filter") $triples[0]->isa("RDF::Query::Algebra::Sort") $triples[0]->isa("RDF::Query::Algebra::Limit") $triples[0]->isa("RDF::Query::Algebra::Offset") $triples[0]->isa("RDF::Query::Algebra::Distinct") $triples[0]->isa("RDF::Query::Algebra::Project") $triples[0]->isa("RDF::Query::Algebra::Construct") $triples[0]->isa("RDF::Query::Algebra::Load") $triples[0]->isa("RDF::Query::Algebra::Clear") $triples[0]->isa("RDF::Query::Algebra::Create") $triples[0]->isa("RDF::Query::Algebra::Update")
Truth table78%11/14
A and B or C or D or E or F or G or H or I or J or K or L or M
A= scalar @triples == 1B= $triples[0]->isa("RDF::Query::Algebra::GroupGraphPattern")C= $triples[0]->isa("RDF::Query::Algebra::Filter")D= $triples[0]->isa("RDF::Query::Algebra::Sort")E= $triples[0]->isa("RDF::Query::Algebra::Limit")F= $triples[0]->isa("RDF::Query::Algebra::Offset")G= $triples[0]->isa("RDF::Query::Algebra::Distinct")H= $triples[0]->isa("RDF::Query::Algebra::Project")I= $triples[0]->isa("RDF::Query::Algebra::Construct")J= $triples[0]->isa("RDF::Query::Algebra::Load")K= $triples[0]->isa("RDF::Query::Algebra::Clear")L= $triples[0]->isa("RDF::Query::Algebra::Create")M= $triples[0]->isa("RDF::Query::Algebra::Update")
ABCDEFGHIJKLMresult
0XXXXXXXXXXXX0
10000000000000
11XXXXXXXXXXX1
101XXXXXXXXXX1
1001XXXXXXXXX1
10001XXXXXXXX1
100001XXXXXXX1
1000001XXXXXX1
10000001XXXXX1
100000001XXXX1
1000000001XXX1
10000000001XX1
100000000001X1
10000000000011
761 or $triples[0]->isa('RDF::Query::Algebra::Filter')
762 or $triples[0]->isa('RDF::Query::Algebra::Sort')
763 or $triples[0]->isa('RDF::Query::Algebra::Limit')
764 or $triples[0]->isa('RDF::Query::Algebra::Offset')
765 or $triples[0]->isa('RDF::Query::Algebra::Distinct')
766 or $triples[0]->isa('RDF::Query::Algebra::Project')
767 or $triples[0]->isa('RDF::Query::Algebra::Construct')
768 or $triples[0]->isa('RDF::Query::Algebra::Load')
769 or $triples[0]->isa('RDF::Query::Algebra::Clear')
770 or $triples[0]->isa('RDF::Query::Algebra::Create')
771 or $triples[0]->isa('RDF::Query::Algebra::Update')
772 )) {
773 429 my $ggp = $triples[0];
774 429 return $ggp;
775 } else {
776 3 return RDF::Query::Algebra::GroupGraphPattern->new( @triples );
777 }
778 }
779  
780 =item C<< is_update >>
781  
782 =cut
783  
784 48 ▶sub is_update {
Subroutine is_update: called CC=6 SCAR=18.6
Pod documented
785 48 my $self = shift;
786 48 my $pat = $self->pattern;
787 48 ▶ return 1 if ($pat->subpatterns_of_type('RDF::Query::Algebra::Clear'));
Branch: 1 branch
BranchTrueFalseTotal
if $pat->subpatterns_of_type("RDF::Query::Algebra::Clear") 0 48 48
788 48 ▶ return 1 if ($pat->subpatterns_of_type('RDF::Query::Algebra::Copy'));
Branch: 1 branch
BranchTrueFalseTotal
if $pat->subpatterns_of_type("RDF::Query::Algebra::Copy") 0 48 48
789 48 ▶ return 1 if ($pat->subpatterns_of_type('RDF::Query::Algebra::Create'));
Branch: 1 branch
BranchTrueFalseTotal
if $pat->subpatterns_of_type("RDF::Query::Algebra::Create") 0 48 48
790 48 ▶ return 1 if ($pat->subpatterns_of_type('RDF::Query::Algebra::Move'));
Branch: 1 branch
BranchTrueFalseTotal
if $pat->subpatterns_of_type("RDF::Query::Algebra::Move") 0 48 48
791 48 ▶ return 1 if ($pat->subpatterns_of_type('RDF::Query::Algebra::Update'));
Branch: 1 branch
BranchTrueFalseTotal
if $pat->subpatterns_of_type("RDF::Query::Algebra::Update") 6 42 48
792 42 return 0;
793 }
794  
795 =item C<< as_sparql >>
796  
797 Returns the query as a string in the SPARQL syntax.
798  
799 =cut
800  
801 35 ▶sub as_sparql {
Subroutine as_sparql: called CC=18 SCAR=30.7
Pod documented
802 35 my $self = shift;
803 35 ▶ my $parsed = $self->parsed || {};
Condition50%1/2
$self->parsed || {}
l!l
350
MC/DC0%0/1
$self->parsed || {}
$self->parsed
Truth table50%1/2
A
A= $self->parsed
Aresult
11
00
804
805 35 ▶ my $context = { namespaces => { %{ $parsed->{namespaces} || {} } } };
Condition100%2/2
$parsed->{'namespaces'} || {}
l!l
341
MC/DC100%1/1
$parsed->{'namespaces'} || {}
$parsed->{'namespaces'}
Truth table100%2/2
A
A= $parsed->{'namespaces'}
Aresult
11
00
806 35 my $method = $parsed->{method};
807
808 35 ▶ if ($method =~ /^(DESCRIBE|ASK)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(DESCRIBE|ASK)$/i) 4 31 35
809 4 $context->{force_ggp_braces} = 1;
810 }
811
812 35 my @vars = map { $_->as_sparql( $context, '' ) } @{ $parsed->{ variables } };
813 35 my $vars = join(' ', @vars);
814 35 my $ggp = $self->pattern;
815
816 35 ▶ if ($method =~ /^(LOAD|CLEAR|CREATE|UPDATE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^(LOAD|CLEAR|CREATE|UPDATE)$/) { } 0 35 35
817 0 return $ggp->as_sparql;
818 } else {
819 {
820 35 my $pvars = join(' ', sort $ggp->referenced_variables);
821 35 ▶ my $svars = join(' ', sort map { $_->isa('RDF::Query::Node::Resource') ? $_->as_string : $_->name } @{ $parsed->{ variables } });
Branch: 1 branch
BranchTrueFalseTotal
$_->isa('RDF::Query::Node::Resource') ? : 0 40 40
822 35 ▶ if ($pvars eq $svars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pvars eq $svars) 17 18 35
823 17 $vars = '*';
824 }
825 }
826
827 35 ▶ my @ns = map { "PREFIX " . ($_ eq '__DEFAULT__' ? '' : $_) . ": <$parsed->{namespaces}{$_}>" } (sort keys %{ $parsed->{namespaces} });
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '__DEFAULT__' ? : 0 39 39
828 35 my @mod;
829 35 ▶ if (my $ob = $parsed->{options}{orderby}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ob = $parsed->{"options"}{"orderby"}) 0 35 35
830 push(@mod, 'ORDER BY ' . join(' ', map {
831 0 my ($dir,$v) = @$_;
832 0 ▶ ($dir eq 'ASC')
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'ASC' ? : 0 0 0
833 ? $v->as_sparql( $context, '' )
834 : "${dir}" . $v->as_sparql( $context, '' );
835 } @$ob));
836 }
837 35 ▶ if (my $l = $parsed->{options}{limit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $parsed->{"options"}{"limit"}) 0 35 35
838 0 push(@mod, "LIMIT $l");
839 }
840 35 ▶ if (my $o = $parsed->{options}{offset}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $parsed->{"options"}{"offset"}) 0 35 35
841 0 push(@mod, "OFFSET $o");
842 }
843 35 my $mod = join("\n", @mod);
844
845 35 my $methoddata = '';
846 35 ▶ if ($method eq 'SELECT') {
Branch: 3 branches
BranchTrueFalseTotal
if ($method eq 'SELECT') { } 29 6 35
elsif ($method eq 'ASK') { } 2 4 6
elsif ($method eq 'DESCRIBE') { } 2 2 4
847 29 $methoddata = $method;
848 } elsif ($method eq 'ASK') {
849 2 $methoddata = $method;
850 } elsif ($method eq 'DESCRIBE') {
851 2 $methoddata = sprintf("%s %s\nWHERE", $method, $vars);
852 }
853
854 35 ▶ my $ns = scalar(@ns) ? join("\n", @ns, '') : '';
Branch: 1 branch
BranchTrueFalseTotal
scalar @ns ? : 31 4 35
855 35 my $sparql;
856 35 ▶ if ($methoddata or $ns) {
Branch: 1 branch
BranchTrueFalseTotal
if ($methoddata or $ns) { } 35 0 35
Condition66%2/3
$methoddata or $ns
l!l&&r!l&&!r
3320
MC/DC0%0/2
$methoddata or $ns
$methoddata $ns
Truth table66%2/3
A or B
A= $methoddataB= $ns
ABresult
1X1
011
000
857 35 $sparql = sprintf(
858 "$ns%s %s\n%s",
859 $methoddata,
860 $ggp->as_sparql( $context, '' ),
861 $mod,
862 );
863 } else {
864 0 $sparql = sprintf(
865 "%s\n%s",
866 $ggp->as_sparql( $context, '' ),
867 $mod,
868 );
869 }
870
871 35 chomp($sparql);
872 35 return $sparql;
873 }
874 }
875  
876 =item C<< as_hash >>
877  
878 Returns the query as a nested set of plain data structures (no objects).
879  
880 =cut
881  
882 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
883 0 my $self = shift;
884 0 my $pattern = $self->pattern;
885 0 return $pattern->as_hash;
886 }
887  
888 =item C<< sse >>
889  
890 Returns the query as a string in the SSE syntax.
891  
892 =cut
893  
894 13 ▶sub sse {
Subroutine sse: called CC=5 SCAR=16.1
Pod documented
895 13 my $self = shift;
896 13 my $parsed = $self->parsed;
897
898 13 my $ggp = $self->pattern;
899 13 my $ns = $parsed->{namespaces};
900 13 my $nscount = scalar(@{ [ keys %$ns ] });
901 13 my $base_uri = $parsed->{base};
902
903 13 my $indent = ' ';
904 13 my $context = { namespaces => $ns, indent => $indent };
905 13 my $indentcount = 0;
906 13 ▶ $indentcount++ if ($base_uri);
Branch: 1 branch
BranchTrueFalseTotal
if $base_uri 1 12 13
907 13 ▶ $indentcount++ if ($nscount);
Branch: 1 branch
BranchTrueFalseTotal
if $nscount 8 5 13
908 13 my $prefix = $indent x $indentcount;
909
910 13 my $sse = $ggp->sse( $context, $prefix );
911
912 13 ▶ if ($nscount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nscount) 8 5 13
913 8 $sse = sprintf("(prefix (%s)\n${prefix}%s)", join("\n${indent}" . ' 'x9, map { "(${_}: <$ns->{$_}>)" } (sort keys %$ns)), $sse);
914 }
915
916 13 ▶ if ($base_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base_uri) 1 12 13
917 1 $sse = sprintf("(base <%s>\n${indent}%s)", $base_uri->uri_value, $sse);
918 }
919
920 13 chomp($sse);
921 13 return $sse;
922 }
923  
924 =item C<< dateparser >>
925  
926 Returns the DateTime::Format::W3CDTF object associated with this query object.
927  
928 =cut
929  
930 2 ▶sub dateparser {
Subroutine dateparser: called CC=2 SCAR=7.6
Pod documented
931 2 my $self = shift;
932 2 ▶ my $parser = ($self->{dateparser} ||= DateTime::Format::W3CDTF->new);
Condition33%1/3
$self->{"dateparser"} ||= "DateTime::Format::W3CDTF"->new
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"dateparser"} ||= "DateTime::Format::W3CDTF"->new
$self->{"dateparser"} "DateTime::Format::W3CDTF"->new
Truth table33%1/3
A ||= B
A= $self->{"dateparser"}B= "DateTime::Format::W3CDTF"->new
ABresult
1X1
011
000
933 2 return $parser;
934 }
935  
936 =begin private
937  
938 =item C<< supports ( $model, $feature ) >>
939  
940 Returns a boolean value representing the support of $feature for the given model.
941  
942 =end private
943  
944 =cut
945  
946 0 ▶sub supports {
Subroutine supports: not called CC=1 SCAR=6.9
Pod documented
947 0 my $self = shift;
948 0 my $obj = shift;
949 0 my $model = $self->get_model( $obj );
950 0 return $model->supports( @_ );
951 }
952  
953 =item C<< specifies_update_dataset >>
954  
955 Returns true if the query specifies a custom update dataset via the WITH or
956 USING keywords, false otherwise.
957  
958 =cut
959  
960 3 ▶sub specifies_update_dataset {
Subroutine specifies_update_dataset: called CC=2 SCAR=6.9
Pod documented
961 3 my $self = shift;
962 35 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
963 3 ▶ return $self->{parsed}{custom_update_dataset} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'parsed'}{'custom_update_dataset'} ? : 1 2 3
964 }
965  
966 =begin private
967  
968 =item C<< get_model ( $model ) >>
969  
970 Returns a model object for use during execution.
971 If C<< $model >> is a usable model, it is simply returned.
972 Otherwise, a temporary model is constructed and returned.
973  
974 =end private
975  
976 =cut
977  
978 158 ▶sub get_model {
Subroutine get_model: called CC=7 SCAR=26.8
Pod documented
979 158 my $self = shift;
980 158 my $store = shift;
981 158 my %args = @_;
982
983 158 ▶ my $parsed = ref($self) ? $self->{parsed} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 158 0 158
984
985 158 my $model;
986 158 ▶ if (not $store) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $store) { } 2 156 158
elsif ($store->isa('RDF::Trine::Model')) { } 156 0 156
elsif ($store->isa('RDF::Redland::Model')) { } 0 0 0
elsif ($store->isa('RDF::Core::Model')) { } 0 0 0
987 2 $model = RDF::Trine::Model->temporary_model;
988 } elsif (($store->isa('RDF::Trine::Model'))) {
989 156 $model = $store;
990 } elsif ($store->isa('RDF::Redland::Model')) {
991 0 my $s = RDF::Trine::Store->new_with_object( $store );
992 0 $model = RDF::Trine::Model->new( $s );
993 0 ▶ unless (blessed($model)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($model)) 0 0 0
994 0 Carp::cluck "Failed to construct an RDF::Trine model from $store";
995 0 return;
996 }
997 } elsif ($store->isa('RDF::Core::Model')) {
998 0 Carp::croak "RDF::Core is no longer supported";
999 } else {
1000 0 Carp::confess "unknown store type: $store";
1001 }
1002
1003 158 return $model;
1004 }
1005  
1006 =begin private
1007  
1008 =item C<< load_data >>
1009  
1010 Loads any external data required by this query (FROM and FROM NAMED clauses).
1011  
1012 =end private
1013  
1014 =cut
1015  
1016 100 ▶sub load_data {
Subroutine load_data: called CC=6 SCAR=18.0
Pod documented
1017 100 my $self = shift;
1018 100 my $parsed = $self->{parsed};
1019
1020 ## LOAD ANY EXTERNAL RDF FILES
1021 100 my $sources = $parsed->{'sources'};
1022 100 ▶ if (ref($sources) and reftype($sources) eq 'ARRAY' and scalar(@$sources)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sources and builtin::reftype($sources) eq "ARRAY" and scalar @$sources) 13 87 100
Condition80%4/5
ref $sources and builtin::reftype($sources) eq "ARRAY" and scalar @$sources
!ll
1387
ref $sources and builtin::reftype($sources) eq "ARRAY"
!ll&&!rl&&r
13087
MC/DC50%1/2
ref $sources and builtin::reftype($sources) eq "ARRAY" and scalar @$sources
ref $sources builtin::reftype($sources) eq "ARRAY"
Truth table66%2/3
A and B
A= ref $sourcesB= builtin::reftype($sources) eq "ARRAY"
ABresult
0X0
100
111
1023 13 my $model = RDF::Trine::Model->temporary_model;
1024 13 $self->model( $model );
1025 13 foreach my $source (@$sources) {
1026 23 ▶ my $named_source = (2 == @{$source} and $source->[1] eq 'NAMED');
Condition66%2/3
2 == @{$source;} && $source->[1] eq 'NAMED'
!ll&&!rl&&r
7016
MC/DC50%1/2
2 == @{$source;} && $source->[1] eq 'NAMED'
2 == @{$source;} $source->[1] eq 'NAMED'
Truth table66%2/3
A && B
A= 2 == @{$source;}B= $source->[1] eq 'NAMED'
ABresult
0X0
100
111
1027 23 my $uri = $source->[0]->uri_value;
1028 23 $self->parse_url( $uri, $named_source );
1029 }
1030 13 $self->run_hook( 'http://kasei.us/code/rdf-query/hooks/post-create-model', $model );
1031 }
1032 }
1033  
1034  
1035 =begin private
1036  
1037 =item C<< var_or_expr_value ( \%bound, $value, $context ) >>
1038  
1039 Returns an (non-variable) RDF::Query::Node value based on C<< $value >>.
1040 If C<< $value >> is a node object, it is simply returned. If it is an
1041 RDF::Query::Node::Variable object, the corresponding value in C<< \%bound >>
1042 is returned. If it is an RDF::Query::Expression object, the expression
1043 is evaluated using C<< \%bound >>, and the resulting value is returned.
1044  
1045 =end private
1046  
1047 =cut
1048  
1049 334 ▶sub var_or_expr_value {
Subroutine var_or_expr_value: called CC=5 SCAR=16.6
Pod documented
1050 334 my $self = shift;
1051 334 my $bound = shift;
1052 334 my $v = shift;
1053 334 my $ctx = shift;
1054 334 ▶ Carp::confess 'not an object value in var_or_expr_value: ' . Dumper($v) unless (blessed($v));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($v) 0 334 334
1055 334 ▶ if ($v->isa('RDF::Query::Expression')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($v->isa('RDF::Query::Expression')) { } 44 290 334
elsif ($v->isa('RDF::Trine::Node::Variable')) { } 263 27 290
elsif ($v->isa('RDF::Query::Node')) { } 27 0 27
1056 44 return $v->evaluate( $self, $bound, $ctx );
1057 } elsif ($v->isa('RDF::Trine::Node::Variable')) {
1058 263 return $bound->{ $v->name };
1059 } elsif ($v->isa('RDF::Query::Node')) {
1060 27 return $v;
1061 } else {
1062 0 Carp::cluck "not an expression or node value in var_or_expr_value: " . Dumper($v, $bound);
1063 0 throw RDF::Query::Error -text => 'Not an expression or node value';
1064 }
1065 }
1066  
1067  
1068 =item C<add_function ( $uri, $function )>
1069  
1070 Associates the custom function C<$function> (a CODE reference) with the
1071 specified URI, allowing the function to be called by query FILTERs.
1072  
1073 =cut
1074  
1075 2 ▶sub add_function {
Subroutine add_function: called CC=2 SCAR=6.9
Pod documented
1076 2 my $self = shift;
1077 2 my $uri = shift;
1078 2 my $code = shift;
1079 2 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 1 1 2
1080 1 $self->{'functions'}{$uri} = $code;
1081 } else {
1082 1 our %functions;
1083 1 $RDF::Query::functions{ $uri } = $code;
1084 }
1085 }
1086  
1087 =item C<< supported_extensions >>
1088  
1089 Returns a list of URLs representing extensions to SPARQL that are supported
1090 by the query engine.
1091  
1092 =cut
1093  
1094 0 ▶sub supported_extensions {
Subroutine supported_extensions: not called CC=1 SCAR=6.9
Pod documented
1095 0 my $self = shift;
1096 0 return qw(
1097 http://kasei.us/2008/04/sparql-extension/service
1098 http://kasei.us/2008/04/sparql-extension/service/bloom_filters
1099 http://kasei.us/2008/04/sparql-extension/unsaid
1100 http://kasei.us/2008/04/sparql-extension/federate_bindings
1101 http://kasei.us/2008/04/sparql-extension/select_expression
1102 http://kasei.us/2008/04/sparql-extension/aggregate
1103 http://kasei.us/2008/04/sparql-extension/aggregate/count
1104 http://kasei.us/2008/04/sparql-extension/aggregate/count-distinct
1105 http://kasei.us/2008/04/sparql-extension/aggregate/min
1106 http://kasei.us/2008/04/sparql-extension/aggregate/max
1107 );
1108 }
1109  
1110 =item C<< supported_functions >>
1111  
1112 Returns a list URLs that may be used as functions in FILTER clauses
1113 (and the SELECT clause if the SPARQL 1.1 parser is used).
1114  
1115 =cut
1116  
1117 0 ▶sub supported_functions {
Subroutine supported_functions: not called CC=2 SCAR=17.9
Pod documented
1118 0 my $self = shift;
1119 0 my @funcs;
1120
1121 0 ▶ if (blessed($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) 0 0 0
1122 0 push(@funcs, keys %{ $self->{'functions'} });
1123 }
1124
1125 0 push(@funcs, keys %RDF::Query::functions);
1126 0 return grep { not(/^sparql:/) } @funcs;
1127 }
1128  
1129 =begin private
1130  
1131 =item C<get_function ( $uri, %args )>
1132  
1133 If C<$uri> is associated with a query function, returns a CODE reference
1134 to the function. Otherwise returns C<undef>.
1135  
1136 =end private
1137  
1138 =cut
1139  
1140 354 ▶sub get_function {
Subroutine get_function: called CC=8 SCAR=22.8
Pod documented
1141 354 my $self = shift;
1142 354 my $uri = shift;
1143 354 my %args = @_;
1144 354 my $l = Log::Log4perl->get_logger("rdf.query");
1145 354 ▶ if (blessed($uri) and $uri->isa('RDF::Query::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri) and $uri->isa("RDF::Query::Node::Resource")) 354 0 354
Condition33%1/3
builtin::blessed($uri) and $uri->isa("RDF::Query::Node::Resource")
!ll&&!rl&&r
00354
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("RDF::Query::Node::Resource")
builtin::blessed($uri) $uri->isa("RDF::Query::Node::Resource")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("RDF::Query::Node::Resource")
ABresult
0X0
100
111
1146 354 $uri = $uri->uri_value;
1147 }
1148 354 $l->debug("trying to get function from $uri");
1149
1150 354 ▶ if (blessed($uri) and $uri->isa('RDF::Query::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($uri) and $uri->isa("RDF::Query::Node::Resource")) 0 354 354
Condition33%1/3
builtin::blessed($uri) and $uri->isa("RDF::Query::Node::Resource")
!ll&&!rl&&r
35400
MC/DC0%0/2
builtin::blessed($uri) and $uri->isa("RDF::Query::Node::Resource")
builtin::blessed($uri) $uri->isa("RDF::Query::Node::Resource")
Truth table33%1/3
A and B
A= builtin::blessed($uri)B= $uri->isa("RDF::Query::Node::Resource")
ABresult
0X0
100
111
1151 0 $uri = $uri->uri_value;
1152 }
1153
1154 354 my $func;
1155 354 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 255 99 354
1156 255 ▶ $func = $self->{'functions'}{$uri} || $RDF::Query::functions{ $uri };
Condition66%2/3
$self->{'functions'}{$uri} || $RDF::Query::functions{$uri}
l!l&&r!l&&!r
42510
MC/DC0%0/2
$self->{'functions'}{$uri} || $RDF::Query::functions{$uri}
$self->{'functions'}{$uri} $RDF::Query::functions{$uri}
Truth table66%2/3
A || B
A= $self->{'functions'}{$uri}B= $RDF::Query::functions{$uri}
ABresult
1X1
011
000
1157 } else {
1158 99 $func = $RDF::Query::functions{ $uri };
1159 }
1160
1161 354 ▶ if ($func) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func) 354 0 354
1162 354 return $func;
1163 }
1164 0 return;
1165 }
1166  
1167  
1168 =begin private
1169  
1170 =item C<< call_function ( $model, $bound, $uri, @args ) >>
1171  
1172 If C<$uri> is associated with a query function, calls the function with the supplied arguments.
1173  
1174 =end private
1175  
1176 =cut
1177  
1178 0 ▶sub call_function {
Subroutine call_function: not called CC=1 SCAR=6.9
Pod documented
1179 0 my $self = shift;
1180 0 my $model = shift;
1181 0 my $bound = shift;
1182 0 my $uri = shift;
1183 0 my $l = Log::Log4perl->get_logger("rdf.query");
1184 0 $l->debug("trying to get function from $uri");
1185
1186 0 my $filter = RDF::Query::Expression::Function->new( $uri, @_ );
1187 0 return $filter->evaluate( $self, $bound );
1188 }
1189  
1190 =item C<< add_computed_statement_generator ( $predicate => \&generator ) >>
1191  
1192 Adds a statement generator for the given C<< $predicate >> to the query object.
1193 This statement generator will be called as
1194 C<< $generator->( $query, $model, \%bound, $s, $p, $o, $c ) >>
1195 and is expected to return an RDF::Trine::Iterator::Graph object containing
1196 statements with C<< $predicate >>.
1197  
1198 =cut
1199  
1200 1 ▶sub add_computed_statement_generator {
Subroutine add_computed_statement_generator: called CC=4 SCAR=17.3
Pod documented
1201 1 my $self = shift;
1202 1 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 0 1 1
1203 0 throw RDF::Query::Error::MethodInvocationError -text => 'RDF::Query::add_computed_statement_generator must now take two arguments: ( $predicate, \&generator ).';
1204 }
1205 1 my $pred = shift;
1206 1 my $gen = shift;
1207 1 ▶ if (blessed($pred)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($pred)) 0 1 1
1208 0 ▶ if ($pred->can('uri_value')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred->can('uri_value')) { } 0 0 0
1209 0 $pred = $pred->uri_value;
1210 } else {
1211 0 $pred = "$pred";
1212 }
1213 }
1214 1 push( @{ $self->{'computed_statement_generators'}{ $pred } }, $gen );
1215 }
1216  
1217 =item C<< get_computed_statement_generators ( [ $predicate ] ) >>
1218  
1219 Returns an ARRAY reference of computed statement generator closures.
1220  
1221 =cut
1222  
1223 409 ▶sub get_computed_statement_generators {
Subroutine get_computed_statement_generators: called CC=6 SCAR=24.2
Pod documented
1224 409 my $self = shift;
1225 409 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 409 0 409
1226 409 my $pred = shift;
1227 409 ▶ if (blessed($pred)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($pred)) 0 409 409
1228 0 ▶ if ($pred->can('uri_value')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pred->can('uri_value')) { } 0 0 0
1229 0 $pred = $pred->uri_value;
1230 } else {
1231 0 $pred = "$pred";
1232 }
1233 }
1234 409 ▶ return $self->{'computed_statement_generators'}{ $pred } || [];
Condition100%2/2
$self->{'computed_statement_generators'}{$pred} || []
l!l
5404
MC/DC100%1/1
$self->{'computed_statement_generators'}{$pred} || []
$self->{'computed_statement_generators'}{$pred}
Truth table100%2/2
A
A= $self->{'computed_statement_generators'}{$pred}
Aresult
11
00
1235 } else {
1236 0 ▶ return $self->{'computed_statement_generators'} || {};
Condition0%0/2
$self->{'computed_statement_generators'} || {}
l!l
00
MC/DC0%0/1
$self->{'computed_statement_generators'} || {}
$self->{'computed_statement_generators'}
Truth table0%0/2
A
A= $self->{'computed_statement_generators'}
Aresult
11
00
1237 }
1238 }
1239  
1240 =item C<< add_hook_once ( $hook_uri, $function, $token ) >>
1241  
1242 Calls C<< add_hook >> adding the supplied C<< $function >> only once based on
1243 the C<< $token >> identifier. This may be useful if the only code that is able
1244 to add a hook is called many times (in an extension function, for example).
1245  
1246 =cut
1247  
1248 0 ▶sub add_hook_once {
Subroutine add_hook_once: not called CC=2 SCAR=17.9
Pod documented
1249 0 my $self = shift;
1250 0 my $uri = shift;
1251 0 my $code = shift;
1252 0 my $token = shift;
1253 0 ▶ unless ($self->{'hooks_once'}{ $token }++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"hooks_once"}{$token}++) 0 0 0
1254 0 $self->add_hook( $uri, $code );
1255 }
1256 }
1257  
1258 =item C<< add_hook ( $hook_uri, $function ) >>
1259  
1260 Associates the custom function C<$function> (a CODE reference) with the
1261 RDF::Query code hook specified by C<$uri>. Each function that has been
1262 associated with a particular hook will be called (in the order they were
1263 registered as hooks) when the hook event occurs. See L</"Defined Hooks">
1264 for more information.
1265  
1266 =cut
1267  
1268 0 ▶sub add_hook {
Subroutine add_hook: not called CC=2 SCAR=17.9
Pod documented
1269 0 my $self = shift;
1270 0 my $uri = shift;
1271 0 my $code = shift;
1272 0 ▶ if (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 0 0 0
1273 0 push(@{ $self->{'hooks'}{$uri} }, $code);
1274 } else {
1275 0 our %hooks;
1276 0 push(@{ $RDF::Query::hooks{ $uri } }, $code);
1277 }
1278 }
1279  
1280 =begin private
1281  
1282 =item C<get_hooks ( $uri )>
1283  
1284 If C<$uri> is associated with any query callback functions ("hooks"),
1285 returns an ARRAY reference to the functions. If no hooks are associated
1286 with C<$uri>, returns a reference to an empty array.
1287  
1288 =end private
1289  
1290 =cut
1291  
1292 202 ▶sub get_hooks {
Subroutine get_hooks: called CC=3 SCAR=12.0
Pod documented
1293 202 my $self = shift;
1294 202 my $uri = shift;
1295 my $func = $self->{'hooks'}{ $uri }
1296 202 ▶ || $RDF::Query::hooks{ $uri }
Condition40%2/5
$self->{'hooks'}{$uri} || $RDF::Query::hooks{$uri} || []
l!l
0202
$self->{"hooks"}{$uri} or $RDF::Query::hooks{$uri}
l!l&&r!l&&!r
00202
MC/DC0%0/2
$self->{'hooks'}{$uri} || $RDF::Query::hooks{$uri} || []
$self->{"hooks"}{$uri} $RDF::Query::hooks{$uri}
Truth table33%1/3
A or B
A= $self->{"hooks"}{$uri}B= $RDF::Query::hooks{$uri}
ABresult
1X1
011
000
1297 || [];
1298 202 return $func;
1299 }
1300  
1301 =begin private
1302  
1303 =item C<run_hook ( $uri, @args )>
1304  
1305 Calls any query callback functions associated with C<$uri>. Each callback
1306 is called with the query object as the first argument, followed by any
1307 caller-supplied arguments from C<@args>.
1308  
1309 =end private
1310  
1311 =cut
1312  
1313 202 ▶sub run_hook {
Subroutine run_hook: called CC=2 SCAR=7.0
Pod documented
1314 202 my $self = shift;
1315 202 my $uri = shift;
1316 202 my @args = @_;
1317 202 my $hooks = $self->get_hooks( $uri );
1318 202 foreach my $hook (@$hooks) {
1319 0 $hook->( $self, @args );
1320 }
1321 }
1322  
1323 =begin private
1324  
1325 =item C<< parse_url ( $url, $named ) >>
1326  
1327 Retrieve a remote file by URL, and parse RDF into the RDF store.
1328 If $named is TRUE, associate all parsed triples with a named graph.
1329  
1330 =end private
1331  
1332 =cut
1333 23 ▶sub parse_url {
Subroutine parse_url: called CC=2 SCAR=6.9
Pod documented
1334 23 my $self = shift;
1335 23 my $url = shift;
1336 23 my $named = shift;
1337 23 my $model = $self->model;
1338
1339 23 ▶ if ($named) {
Branch: 1 branch
BranchTrueFalseTotal
if ($named) { } 16 7 23
1340 16 RDF::Trine::Parser->parse_url_into_model( $url, $model, context => iri($url) );
1341 } else {
1342 7 RDF::Trine::Parser->parse_url_into_model( $url, $model );
1343 }
1344 }
1345  
1346 =begin private
1347  
1348 =item C<variables ()>
1349  
1350 Returns a list of the ordered variables the query is selecting.
1351
1352 =end private
1353  
1354 =cut
1355  
1356 333 ▶sub variables {
Subroutine variables: called CC=3 SCAR=11.0
Pod documented
1357 333 my $self = shift;
1358 333 ▶ my $parsed = shift || $self->parsed;
Condition66%2/3
shift() || $self->parsed
l!l&&r!l&&!r
315180
MC/DC0%0/2
shift() || $self->parsed
shift() $self->parsed
Truth table66%2/3
A || B
A= shift()B= $self->parsed
ABresult
1X1
011
000
1359 480 my @vars = map { $_->name }
1360 grep {
1361 482 ▶ $_->isa('RDF::Query::Node::Variable') or $_->isa('RDF::Query::Expression::Alias')
Condition100%3/3
$_->isa('RDF::Query::Node::Variable') || $_->isa('RDF::Query::Expression::Alias')
l!l&&r!l&&!r
424562
MC/DC100%2/2
$_->isa('RDF::Query::Node::Variable') || $_->isa('RDF::Query::Expression::Alias')
$_->isa('RDF::Query::Node::Variable') $_->isa('RDF::Query::Expression::Alias')
Truth table100%3/3
A || B
A= $_->isa('RDF::Query::Node::Variable')B= $_->isa('RDF::Query::Expression::Alias')
ABresult
1X1
011
000
1362 333 } @{ $parsed->{'variables'} };
1363 333 return @vars;
1364 }
1365  
1366 =item C<parsed ()>
1367  
1368 Returns the parse tree.
1369  
1370 =cut
1371  
1372 498 ▶sub parsed {
Subroutine parsed: called CC=2 SCAR=7.6
Pod documented
1373 498 my $self = shift;
1374 498 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 498 498
1375 0 $self->{parsed} = shift;
1376 }
1377 498 return $self->{parsed};
1378 }
1379  
1380 =item C<< model >>
1381  
1382 Returns the RDF::Trine::Model object for this query.
1383  
1384 =cut
1385  
1386 365 ▶sub model {
Subroutine model: called CC=3 SCAR=11.4
Pod documented
1387 365 my $self = shift;
1388 365 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 171 194 365
1389 171 $self->{model} = shift;
1390 }
1391 365 my $model = $self->{model};
1392 365 ▶ unless (defined $model) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $model) 0 365 365
1393 0 Carp::confess "query->model shouldn't be calling get_model";
1394 0 $model = $self->get_model();
1395 }
1396
1397 365 return $model;
1398 }
1399  
1400  
1401 =item C<< useragent >>
1402  
1403 Returns the LWP::UserAgent object used for retrieving web content.
1404  
1405 =cut
1406  
1407 0 ▶sub useragent {
Subroutine useragent: not called CC=2 SCAR=17.9
Pod documented
1408 0 my $self = shift;
1409 0 ▶ if (my $ua = $self->{useragent}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ua = $self->{'useragent'}) { } 0 0 0
1410 0 return $ua;
1411 } else {
1412 0 my $ua = LWP::UserAgent->new( agent => "RDF::Query/${VERSION}" );
1413 0 $ua->default_headers->push_header( 'Accept' => "application/sparql-results+xml;q=0.9,application/rdf+xml;q=0.5,text/turtle;q=0.7,text/xml" );
1414 0 $self->{useragent} = $ua;
1415 0 return $ua;
1416 }
1417 }
1418  
1419  
1420 =item C<< log ( $key [, $value ] ) >>
1421  
1422 If no logger object is associated with this query object, does nothing.
1423 Otherwise, return or set the corresponding value depending on whether a
1424 C<< $value >> is specified.
1425  
1426 =cut
1427  
1428 0 ▶sub log {
Subroutine log: not called CC=2 SCAR=17.9
Pod documented
1429 0 my $self = shift;
1430 0 ▶ if (blessed(my $l = $self->{ logger })) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed(my $l = $self->{"logger"})) 0 0 0
1431 0 $l->log( @_ );
1432 }
1433 }
1434  
1435  
1436 =item C<< logger >>
1437  
1438 Returns the logger object associated with this query object (if present).
1439  
1440 =cut
1441  
1442 158 ▶sub logger {
Subroutine logger: called CC=1 SCAR=0.0
Pod documented
1443 158 my $self = shift;
1444 158 return $self->{ logger };
1445 }
1446  
1447 =item C<error ()>
1448  
1449 Returns the last error the parser experienced.
1450  
1451 =cut
1452  
1453 0 ▶sub error {
Subroutine error: not called CC=2 SCAR=17.9
Pod documented
1454 0 my $self = shift;
1455 0 ▶ if (blessed($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 0 0 0
1456 0 return $self->{error};
1457 } else {
1458 0 our $_ERROR;
1459 0 return $_ERROR;
1460 }
1461 }
1462  
1463 1440 ▶sub _uniq {
Subroutine _uniq: called CC=3 SCAR=11.0
1464 1440 my %seen;
1465 my @data;
1466 1440 foreach (@_) {
1467 1004 ▶ push(@data, $_) unless ($seen{ $_ }++);
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{$_}++ 883 121 1004
1468 }
1469 1440 return @data;
1470 }
1471  
1472 =begin private
1473  
1474 =item C<set_error ( $error )>
1475  
1476 Sets the object's error variable.
1477  
1478 =end private
1479  
1480 =cut
1481  
1482 2 ▶sub set_error {
Subroutine set_error: called CC=2 SCAR=7.5
Pod documented
1483 2 my $self = shift;
1484 2 my $error = shift;
1485 2 my $e = shift;
1486 2 ▶ if (blessed($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) 0 2 2
1487 0 $self->{error} = $error;
1488 0 $self->{exception} = $e;
1489 }
1490 2 our $_ERROR = $error;
1491 2 our $_EXCEPTION = $e;
1492 }
1493  
1494 =begin private
1495  
1496 =item C<clear_error ()>
1497  
1498 Clears the object's error variable.
1499  
1500 =end private
1501  
1502 =cut
1503  
1504 209 ▶sub clear_error {
Subroutine clear_error: called CC=2 SCAR=7.6
Pod documented
1505 209 my $self = shift;
1506 209 ▶ if (blessed($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) 0 209 209
1507 0 $self->{error} = undef;
1508 0 $self->{exception} = undef;
1509 }
1510 209 our($_ERROR, $_EXCEPTION);
1511 209 undef $_ERROR;
1512 209 undef $_EXCEPTION;
1513 }
1514  
1515  
1516 # =begin private
1517 #
1518 # =item C<_debug_closure ( $code )>
1519 #
1520 # Debugging function to print out a deparsed (textual) version of a closure.
1521 #
1522 # =end private
1523 #
1524 # =cut
1525 #
1526 # sub _debug_closure {
1527 # my $closure = shift;
1528 # my $l = Log::Log4perl->get_logger("rdf.query");
1529 # if ($l->is_trace) {
1530 # require B::Deparse;
1531 # my $deparse = B::Deparse->new("-p", "-sC");
1532 # my $body = $deparse->coderef2text($closure);
1533 # $l->trace("--- --- CLOSURE --- ---");
1534 # $l->logcluck($body);
1535 # }
1536 # }
1537  
1538  
1539 35 1;
1540  
1541 __END__
« Query/Algebra/NamedGraph.pm ↑ Summary Query/Federate.pm »