Query/Algebra/NamedGraph.pm

filter: Statementstmt 59.0% 75 / 127 Branchbran 8.3% 1 / 12 Conditioncond 33.3% 4 / 12 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 74.0% 20 / 27 Podpod 100% 16 / 16 totaltotal 58.2% 117 / 201 CC 17 SCAR 26.3
CC
17
Cov
53%
check_duplicate_blanks
56.0
qualify_uris
30.0
sse
3.0
CRAP 13.8
« Query/Plan/Iterator.pm ↑ Summary Query.pm »
1 # RDF::Query::Algebra::NamedGraph
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Algebra::NamedGraph - Algebra class for NamedGraph patterns
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Algebra::NamedGraph version 2.919.
11  
12 =cut
13  
14 package RDF::Query::Algebra::NamedGraph;
15  
16 36 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 36 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 36 ▶no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 36 ▶use base qw(RDF::Query::Algebra);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 36 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 36 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 36 ▶use RDF::Query::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 36 ▶use Carp qw(carp croak confess);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 36 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 36 ▶use RDF::Trine::Iterator qw(sgrep smap swatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 ######################################################################
29  
30 36 our ($VERSION);
31 36 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
32 36 $VERSION = '2.919';
33 }
34  
35 ######################################################################
36  
37 =head1 METHODS
38  
39 Beyond the methods documented below, this class inherits methods from the
40 L<RDF::Query::Algebra> class.
41  
42 =over 4
43  
44 =cut
45  
46 =item C<new ( $graph, $pattern )>
47  
48 Returns a new NamedGraph structure.
49  
50 =cut
51  
52 28 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
53 28 my $class = shift;
54 28 my $graph = shift;
55 28 my $pattern = shift;
56 28 return bless( [ 'GRAPH', $graph, $pattern ], $class );
57 }
58  
59 =item C<< construct_args >>
60  
61 Returns a list of arguments that, passed to this class' constructor,
62 will produce a clone of this algebra pattern.
63  
64 =cut
65  
66 69 ▶sub construct_args {
Subroutine construct_args: called CC=1 SCAR=0.0
Pod documented
67 69 my $self = shift;
68 69 return ($self->graph, $self->pattern);
69 }
70  
71 =item C<< graph >>
72  
73 Returns the graph node of the named graph expression.
74  
75 =cut
76  
77 180 ▶sub graph {
Subroutine graph: called CC=2 SCAR=7.9
Pod documented
78 180 my $self = shift;
79 180 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 180 180
80 0 my $graph = shift;
81 0 $self->[1] = $graph;
82 }
83 180 my $graph = $self->[1];
84 180 return $graph;
85 }
86  
87 =item C<< pattern >>
88  
89 Returns the graph pattern of the named graph expression.
90  
91 =cut
92  
93 144 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
94 144 my $self = shift;
95 144 return $self->[2];
96 }
97  
98 =item C<< quads >>
99  
100 Returns a list of the quads belonging to this NamedGraph.
101  
102 =cut
103  
104 4 ▶sub quads {
Subroutine quads: called CC=3 SCAR=11.0
Pod documented
105 4 my $self = shift;
106 4 my @quads;
107 4 foreach my $p ($self->subpatterns_of_type('RDF::Query::Algebra::BasicGraphPattern')) {
108 4 push(@quads, $p->quads);
109 }
110 4 my @graphquads;
111 4 foreach my $q (@quads) {
112 12 my $st = RDF::Trine::Statement::Quad->new(
113 $q->subject,
114 $q->predicate,
115 $q->object,
116 $self->graph,
117 );
118 12 push(@graphquads, $st);
119 }
120 4 return @graphquads;
121 }
122  
123 =item C<< sse >>
124  
125 Returns the SSE string for this algebra expression.
126  
127 =cut
128  
129 48 ▶sub sse {
Subroutine sse: called CC=3 SCAR=11.0
Pod documented
130 48 my $self = shift;
131 48 my $context = shift;
132 48 ▶ my $prefix = shift || '';
Condition100%2/2
shift() || ''
l!l
2226
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
133 48 ▶ my $indent = $context->{indent} || '';
Condition50%1/2
$context->{'indent'} || ''
l!l
480
MC/DC0%0/1
$context->{'indent'} || ''
$context->{'indent'}
Truth table50%1/2
A
A= $context->{'indent'}
Aresult
11
00
134
135 48 return sprintf(
136 "(namedgraph\n${prefix}${indent}%s\n${prefix}${indent}%s)",
137 $self->graph->sse( $context, "${prefix}${indent}" ),
138 $self->pattern->sse( $context, "${prefix}${indent}" )
139 );
140 }
141  
142 =item C<< explain >>
143  
144 Returns a string serialization of the algebra appropriate for display on the
145 command line.
146  
147 =cut
148  
149 0 ▶sub explain {
Subroutine explain: not called CC=1 SCAR=6.9
Pod documented
150 0 my $self = shift;
151 0 my $s = shift;
152 0 my $count = shift;
153 0 my $indent = $s x $count;
154 0 my $string = "${indent}named graph pattern\n"
155 . "${indent}${s}graph: " . $self->graph->as_string . "\n"
156 . $self->pattern->explain( $s, $count+1 );
157 0 return $string;
158 }
159  
160 =item C<< as_sparql >>
161  
162 Returns the SPARQL string for this algebra expression.
163  
164 =cut
165  
166 6 ▶sub as_sparql {
Subroutine as_sparql: called CC=2 SCAR=7.0
Pod documented
167 6 my $self = shift;
168 6 ▶ my $context = shift || {};
Condition50%1/2
shift() || {}
l!l
60
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
169 6 my $indent = shift;
170 6 my $pcontext = { %$context, force_ggp_braces => 1 };
171
172 6 my $string = sprintf(
173 "GRAPH %s %s",
174 $self->graph->as_sparql( $context, $indent ),
175 $self->pattern->as_sparql( $pcontext, $indent ),
176 );
177 6 return $string;
178 }
179  
180 =item C<< as_hash >>
181  
182 Returns the query as a nested set of plain data structures (no objects).
183  
184 =cut
185  
186 0 ▶sub as_hash {
Subroutine as_hash: not called CC=1 SCAR=6.9
Pod documented
187 0 my $self = shift;
188 0 my $context = shift;
189 return {
190 0 type => lc($self->type),
191 graph => $self->graph,
192 pattern => $self->pattern->as_hash,
193 };
194 }
195  
196 =item C<< as_spin ( $model ) >>
197  
198 Adds statements to the given model to represent this algebra object in the
199 SPARQL Inferencing Notation (L<http://www.spinrdf.org/>).
200  
201 =cut
202  
203 0 ▶sub as_spin {
Subroutine as_spin: not called CC=1 SCAR=6.9
Pod documented
204 0 my $self = shift;
205 0 my $model = shift;
206 0 my $spin = RDF::Trine::Namespace->new('http://spinrdf.org/spin#');
207 0 my $rdf = RDF::Trine::Namespace->new('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
208 0 my @t = $self->pattern->as_spin( $model );
209
210 0 my $ng = RDF::Query::Node::Blank->new();
211 0 my $list = $model->add_list( @t );
212 0 $model->add_statement( RDF::Trine::Statement->new($ng, $rdf->type, $spin->NamedGraph) );
213 0 $model->add_statement( RDF::Trine::Statement->new($ng, $spin->elements, $list) );
214
215 0 return $ng;
216 }
217  
218 =item C<< type >>
219  
220 Returns the type of this algebra expression.
221  
222 =cut
223  
224 0 ▶sub type {
Subroutine type: not called CC=1 SCAR=6.9
Pod documented
225 0 return 'GRAPH';
226 }
227  
228 =item C<< referenced_variables >>
229  
230 Returns a list of the variable names used in this algebra expression.
231  
232 =cut
233  
234 4 ▶sub referenced_variables {
Subroutine referenced_variables: called CC=1 SCAR=0.0
Pod documented
235 4 my $self = shift;
236 my @list = RDF::Query::_uniq(
237 $self->pattern->referenced_variables,
238 4 (map { $_->name } grep { $_->isa('RDF::Query::Node::Variable') } ($self->graph)),
239 );
240 4 return @list;
241 }
242  
243 =item C<< potentially_bound >>
244  
245 Returns a list of the variable names used in this algebra expression that will
246 bind values during execution.
247  
248 =cut
249  
250 3 ▶sub potentially_bound {
Subroutine potentially_bound: called CC=1 SCAR=0.0
Pod documented
251 3 my $self = shift;
252 my @list = RDF::Query::_uniq(
253 $self->pattern->potentially_bound,
254 3 (map { $_->name } grep { $_->isa('RDF::Query::Node::Variable') } ($self->graph)),
255 );
256 3 return @list;
257 }
258  
259 =item C<< definite_variables >>
260  
261 Returns a list of the variable names that will be bound after evaluating this algebra expression.
262  
263 =cut
264  
265 0 ▶sub definite_variables {
Subroutine definite_variables: not called CC=1 SCAR=6.9
Pod documented
266 0 my $self = shift;
267 return RDF::Query::_uniq(
268 $self->pattern->definite_variables,
269 0 map { $_->name } grep { $_->isa('RDF::Query::Node::Variable') } ($self->graph),
270 );
271 }
272  
273  
274 =item C<< qualify_uris ( \%namespaces, $base_uri ) >>
275  
276 Returns a new algebra pattern where all referenced Resource nodes representing
277 QNames (ns:local) are qualified using the supplied %namespaces.
278  
279 =cut
280  
281 0 ▶sub qualify_uris {
Subroutine qualify_uris: not called CC=5 SCAR=34.0
Pod documented
282 0 my $self = shift;
283 0 my $class = ref($self);
284 0 my $ns = shift;
285 0 my $base_uri = shift;
286
287 0 my $pattern = $self->pattern->qualify_uris( $ns, $base_uri );
288 0 my $graph = $self->graph;
289 0 ▶ if (blessed($graph) and $graph->isa('RDF::Query::Node::Resource')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($graph) and $graph->isa("RDF::Query::Node::Resource")) 0 0 0
Condition0%0/3
builtin::blessed($graph) and $graph->isa("RDF::Query::Node::Resource")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($graph) and $graph->isa("RDF::Query::Node::Resource")
builtin::blessed($graph) $graph->isa("RDF::Query::Node::Resource")
Truth table0%0/3
A and B
A= builtin::blessed($graph)B= $graph->isa("RDF::Query::Node::Resource")
ABresult
0X0
100
111
290 0 my $uri = $graph->uri;
291 0 ▶ if (ref($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri) 0 0 0
292 0 my ($n,$l) = @$uri;
293 0 ▶ unless (exists($ns->{ $n })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ns->{$n}) 0 0 0
294 0 throw RDF::Query::Error::QuerySyntaxError -text => "Namespace $n is not defined";
295 }
296 0 my $resolved = join('', $ns->{ $n }, $l);
297 0 $graph = RDF::Query::Node::Resource->new( $resolved, $base_uri );
298 }
299 }
300 0 return $class->new( $graph, $pattern );
301 }
302  
303 =item C<< check_duplicate_blanks >>
304  
305 Returns true if blank nodes respect the SPARQL rule of no blank-label re-use
306 across BGPs, otherwise throws a RDF::Query::Error::QueryPatternError exception.
307  
308 =cut
309  
310 0 ▶sub check_duplicate_blanks {
Subroutine check_duplicate_blanks: not called CC=7 SCAR=40.3
Pod documented
311 0 my $self = shift;
312 0 my @data;
313 0 foreach my $arg ($self->construct_args) {
314 0 ▶ if (blessed($arg) and $arg->isa('RDF::Query::Algebra')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($arg) and $arg->isa("RDF::Query::Algebra")) 0 0 0
Condition0%0/3
builtin::blessed($arg) and $arg->isa("RDF::Query::Algebra")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($arg) and $arg->isa("RDF::Query::Algebra")
builtin::blessed($arg) $arg->isa("RDF::Query::Algebra")
Truth table0%0/3
A and B
A= builtin::blessed($arg)B= $arg->isa("RDF::Query::Algebra")
ABresult
0X0
100
111
315 0 push(@data, $arg->_referenced_blanks());
316 }
317 }
318
319 0 my %seen;
320 0 foreach my $d (@data) {
321 0 foreach my $b (@$d) {
322 0 ▶ if ($seen{ $b }++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$b}++) 0 0 0
323 0 throw RDF::Query::Error::QueryPatternError -text => "Same blank node identifier ($b) used in more than one BasicGraphPattern.";
324 }
325 }
326 }
327
328 0 return 1;
329 }
330  
331 36 1;
332  
333 __END__
« Query/Plan/Iterator.pm ↑ Summary Query.pm »