Query/Plan/ComputedStatement.pm

filter: Statementstmt 68.0% 119 / 175 Branchbran 46.1% 24 / 52 Conditioncond 50.0% 4 / 8 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 77.2% 17 / 22 Podpod 100% 13 / 13 totaltotal 65.0% 179 / 275 CC 37 SCAR 24.2
CC
37
Cov
63%
bf
20.0
new
19.9
next
18.1
CRAP 11.2
« Query/Algebra/Table.pm ↑ Summary Query/Algebra/BasicGraphPattern.pm »
1 # RDF::Query::Plan::ComputedStatement
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan::ComputedStatement - Executable query plan for computed triples.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan::ComputedStatement version 2.919.
11  
12 =head1 METHODS
13  
14 Beyond the methods documented below, this class inherits methods from the
15 L<RDF::Query::Plan> class.
16  
17 =over 4
18  
19 =cut
20  
21 package RDF::Query::Plan::ComputedStatement;
22  
23 35 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 35 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 35 ▶use base qw(RDF::Query::Plan);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 35 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
29 35 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 35 ▶use RDF::Query::ExecutionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 35 ▶use RDF::Query::VariableBindings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 ######################################################################
35  
36 35 our ($VERSION);
37 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
38 35 $VERSION = '2.919';
39 }
40  
41 ######################################################################
42  
43 =item C<< new ( @triple ) >>
44  
45 =cut
46  
47 2 ▶sub new {
Subroutine new: called CC=12 SCAR=29.9
Pod documented
48 2 my $class = shift;
49 2 my @nodes = splice(@_, 0, 4);
50 2 my $quad = shift;
51 2 ▶ my $keys = shift || {};
Condition50%1/2
shift() || {}
l!l
02
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
52 2 my $self = $class->SUPER::new( \@nodes, $quad );
53 2 $self->[0]{logging_keys} = $keys;
54
55 2 my %var_to_position;
56 2 my @methodmap = qw(subject predicate object);
57 2 my %counts;
58 my $dup_var;
59 2 foreach my $idx (0 .. 3) {
60 8 my $node = $nodes[ $idx ];
61 8 ▶ if (blessed($node) and $node->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($node) and $node->isa("RDF::Trine::Node::Variable")) 4 4 8
Condition100%3/3
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
224
MC/DC100%2/2
builtin::blessed($node) and $node->isa("RDF::Trine::Node::Variable")
builtin::blessed($node) $node->isa("RDF::Trine::Node::Variable")
Truth table100%3/3
A and B
A= builtin::blessed($node)B= $node->isa("RDF::Trine::Node::Variable")
ABresult
0X0
100
111
62 4 my $name = $node->name;
63 4 $var_to_position{ $name } = $methodmap[ $idx ];
64 4 $counts{ $name }++;
65 4 ▶ if ($counts{ $name } >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($counts{$name} >= 2) 0 4 4
66 0 $dup_var = $name;
67 }
68 }
69 }
70 2 $self->[0]{referenced_variables} = [ keys %counts ];
71
72 2 my @positions;
73 2 ▶ if (defined($dup_var)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dup_var) 0 2 2
74 0 foreach my $idx (0 .. 2) {
75 0 my $var = $nodes[ $idx ];
76 0 ▶ if (blessed($var) and $var->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($var) and $var->isa("RDF::Trine::Node::Variable")) 0 0 0
Condition0%0/3
builtin::blessed($var) and $var->isa("RDF::Trine::Node::Variable")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($var) and $var->isa("RDF::Trine::Node::Variable")
builtin::blessed($var) $var->isa("RDF::Trine::Node::Variable")
Truth table0%0/3
A and B
A= builtin::blessed($var)B= $var->isa("RDF::Trine::Node::Variable")
ABresult
0X0
100
111
77 0 my $name = $var->name;
78 0 ▶ if ($name eq $dup_var) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq $dup_var) 0 0 0
79 0 push(@positions, $methodmap[ $idx ]);
80 }
81 }
82 }
83 }
84
85 2 $self->[0]{mappings} = \%var_to_position;
86
87 2 ▶ if (@positions) {
Branch: 1 branch
BranchTrueFalseTotal
if (@positions) 0 2 2
88 0 $self->[0]{dups} = \@positions;
89 }
90
91 2 return $self;
92 }
93  
94 =item C<< execute ( $execution_context ) >>
95  
96 =cut
97  
98 1 ▶sub execute ($) {
Subroutine execute: called CC=9 SCAR=22.3
Pod documented
99 1 my $self = shift;
100 1 my $context = shift;
101 1 $self->[0]{delegate} = $context->delegate;
102 1 ▶ if ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == $self->OPEN) 0 1 1
103 0 throw RDF::Query::Error::ExecutionError -text => "COMPUTEDSTATEMENT plan can't be executed while already open";
104 }
105
106 1 my $l = Log::Log4perl->get_logger("rdf.query.plan.computedstatement");
107 1 $l->trace( "executing RDF::Query::Plan::ComputedStatement" );
108
109 1 $self->[0]{start_time} = [gettimeofday];
110 1 my @nodes = @{ $self->[1] };
111 1 ▶ unless ($self->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[2]) 1 0 1
112 1 pop(@nodes);
113 }
114
115 1 my $bound = $context->bound;
116 1 ▶ if (%$bound) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$bound) 1 0 1
117 1 foreach my $i (0 .. $#nodes) {
118 3 ▶ next unless ($nodes[$i]->isa('RDF::Trine::Node::Variable'));
Branch: 1 branch
BranchTrueFalseTotal
unless $nodes[$i]->isa("RDF::Trine::Node::Variable") 1 2 3
119 2 ▶ next unless (blessed($bound->{ $nodes[$i]->name }));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($$bound{$nodes[$i]->name}) 1 1 2
120 1 $nodes[ $i ] = $bound->{ $nodes[$i]->name };
121 }
122 }
123  
124 1 $l->trace( "computed statement pattern after pre-binding: " . join(' ', map { $_->as_string } @nodes));
125
126 1 my $query = $context->query;
127 1 my $csg = $query->get_computed_statement_generators( $nodes[1]->uri_value );
128 1 ▶ unless (scalar(@$csg)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @$csg) 0 1 1
129 0 throw RDF::Query::Error::ExecutionError -text => "No computed statement generator found for predicate " . $nodes[1]->uri_value;
130 }
131 1 my $iter;
132 {
133 1 local($query->{model}) = $context->model;
134 1 $iter = $csg->[0]->( $query, $bound, @nodes );
135 }
136 1 ▶ if (blessed($iter)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($iter)) { } 1 0 1
137 1 $self->[0]{iter} = $iter;
138 1 $self->[0]{bound} = $bound;
139 1 $self->[0]{logger} = $context->logger;
140 1 $self->[0]{count} = 0;
141 1 $self->state( $self->OPEN );
142 } else {
143 0 warn "no iterator in execute()";
144 }
145 1 $self;
146 }
147  
148 =item C<< next >>
149  
150 =cut
151  
152 4 ▶sub next {
Subroutine next: called CC=11 SCAR=28.9
Pod documented
153 4 my $self = shift;
154 4 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 4 4
155 0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open COMPUTEDSTATEMENT";
156 }
157
158 4 my $l = Log::Log4perl->get_logger("rdf.query.plan.computedstatement");
159 4 my $iter = $self->[0]{iter};
160 4 ▶ LOOP: while (my $row = $iter->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $iter->next) 3 1 4
161 3 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 3 3
162 0 $l->trace( "- got triple from model: " . $row->as_string );
163 }
164 3 ▶ if (my $pos = $self->[0]{dups}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pos = $self->[0]{"dups"}) 0 3 3
165 0 $l->trace( "- checking for duplicate variables in triple" );
166 0 my @pos = @$pos;
167 0 my $first_method = shift(@pos);
168 0 my $first = $row->$first_method();
169 0 foreach my $p (@pos) {
170 0 ▶ unless ($first->equal( $row->$p() )) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($first->equal($row->$p)) 0 0 0
171 0 next LOOP;
172 }
173 }
174 }
175
176 3 my $binding = {};
177
178 3 foreach my $key (keys %{ $self->[0]{mappings} }) {
179 6 my $method = $self->[0]{mappings}{ $key };
180 6 $binding->{ $key } = $row->$method();
181 }
182 3 my $pre_bound = $self->[0]{bound};
183 3 my $bindings = RDF::Query::VariableBindings->new( $binding );
184 3 ▶ if ($row->can('label')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->can("label")) 3 0 3
185 3 ▶ if (my $o = $row->label('origin')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $o = $row->label("origin")) 0 3 3
186 0 $bindings->label( origin => [ $o ] );
187 }
188 }
189 3 @{ $bindings }{ keys %$pre_bound } = values %$pre_bound;
190 3 $self->[0]{count}++;
191 3 ▶ if (my $d = $self->delegate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delegate) 0 3 3
192 0 $d->log_result( $self, $bindings );
193 }
194 3 return $bindings;
195 }
196 1 return;
197 }
198  
199 =item C<< close >>
200  
201 =cut
202  
203 1 ▶sub close {
Subroutine close: called CC=2 SCAR=7.1
Pod documented
204 1 my $self = shift;
205 1 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 1 1
206 0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open TRIPLE";
207 }
208
209 # my $l = Log::Log4perl->get_logger("rdf.query.plan.computedstatement");
210 1 my $t0 = delete $self->[0]{start_time};
211 1 my $count = delete $self->[0]{count};
212 1 delete $self->[0]{iter};
213 1 $self->SUPER::close();
214 }
215  
216 =item C<< nodes >>
217  
218 Returns a list of the three node objects that comprise the triple pattern this plan will return.
219  
220 =cut
221  
222 2 ▶sub nodes {
Subroutine nodes: called CC=2 SCAR=7.9
Pod documented
223 2 my $self = shift;
224 2 ▶ if ($self->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) { } 0 2 2
225 0 return @{ $self->[1] }[0..3];
226 } else {
227 2 return @{ $self->[1] }[0..2];
228 }
229 }
230  
231 =item C<< triple >>
232  
233 Returns a RDF::Trine::Statement object representing the triple pattern this plan will return.
234  
235 =cut
236  
237 0 ▶sub triple {
Subroutine triple: not called CC=2 SCAR=17.9
Pod documented
238 0 my $self = shift;
239 0 my @nodes = $self->nodes;
240 0 ▶ if ($self->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[2]) { } 0 0 0
241 0 return RDF::Trine::Statement::Quad->new( @nodes );
242 } else {
243 0 return RDF::Trine::Statement->new( @nodes );
244 }
245 }
246  
247 =item C<< bf () >>
248  
249 Returns a string representing the state of the nodes of the triple (bound or free).
250  
251 =cut
252  
253 0 ▶sub bf {
Subroutine bf: not called CC=4 SCAR=30.0
Pod documented
254 0 my $self = shift;
255 0 my $context = shift;
256 0 my $bf = '';
257 0 my $bound = $context->bound;
258 0 foreach my $n (@{ $self->[1] }[0..3]) {
259 0 ▶ if ($n->isa('RDF::Trine::Node::Variable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa('RDF::Trine::Node::Variable')) { } 0 0 0
260 0 ▶ if (my $b = $bound->{ $n->name }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $b = $$bound{$n->name}) { } 0 0 0
261 0 $bf .= 'b';
262 } else {
263 0 $bf .= 'f';
264 }
265 } else {
266 0 $bf .= 'b';
267 }
268 }
269 0 return $bf;
270 }
271  
272 =item C<< distinct >>
273  
274 Returns true if the pattern is guaranteed to return distinct results.
275  
276 =cut
277  
278 0 ▶sub distinct {
Subroutine distinct: not called CC=1 SCAR=6.9
Pod documented
279 0 return 0;
280 }
281  
282 =item C<< ordered >>
283  
284 Returns true if the pattern is guaranteed to return ordered results.
285  
286 =cut
287  
288 0 ▶sub ordered {
Subroutine ordered: not called CC=1 SCAR=6.9
Pod documented
289 0 return [];
290 }
291  
292 =item C<< plan_node_name >>
293  
294 Returns the string name of this plan node, suitable for use in serialization.
295  
296 =cut
297  
298 1 ▶sub plan_node_name {
Subroutine plan_node_name: called CC=1 SCAR=0.0
Pod documented
299 1 return 'computedstatement';
300 }
301  
302 =item C<< plan_prototype >>
303  
304 Returns a list of scalar identifiers for the type of the content (children)
305 nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
306 identifiers.
307  
308 =cut
309  
310 1 ▶sub plan_prototype {
Subroutine plan_prototype: called CC=1 SCAR=0.0
Pod documented
311 1 my $self = shift;
312 1 return qw(N N N N i);
313 }
314  
315 =item C<< plan_node_data >>
316  
317 Returns the data for this plan node that corresponds to the values described by
318 the signature returned by C<< plan_prototype >>.
319  
320 =cut
321  
322 2 ▶sub plan_node_data {
Subroutine plan_node_data: called CC=1 SCAR=0.0
Pod documented
323 2 my $self = shift;
324 2 return ($self->nodes);
325 }
326  
327 =item C<< graph ( $g ) >>
328  
329 =cut
330  
331 0 ▶sub graph {
Subroutine graph: not called CC=2 SCAR=17.9
Pod documented
332 0 my $self = shift;
333 0 my $g = shift;
334 0 my $label = $self->graph_labels;
335 0 $g->add_node( "$self", label => "Computed Statement" . $self->graph_labels );
336 0 my @names = qw(subject predicate object graph);
337 0 foreach my $i (0 .. 3) {
338 0 my $n = $self->[ $i + 1 ];
339 0 my $rel = $names[ $i ];
340 0 my $str = $n->sse( {}, '' );
341 0 $g->add_node( "${self}$n", label => $str );
342 0 $g->add_edge( "$self" => "${self}$n", label => $names[ $i ] );
343 }
344 0 return "$self";
345 }
346  
347 35 1;
348  
349 __END__
« Query/Algebra/Table.pm ↑ Summary Query/Algebra/BasicGraphPattern.pm »