Query/Plan/Extend.pm

filter: Statementstmt 71.6% 96 / 134 Branchbran 53.1% 17 / 32 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 81.8% 18 / 22 Podpod 100% 12 / 12 totaltotal 71.2% 146 / 205 CC 21 SCAR 15.2
CC
21
Cov
68%
next
10.6
graph
6.0
explain
6.0
CRAP 4.6
« Query/Algebra/Service.pm ↑ Summary Query/Plan/Sort.pm »
1 # RDF::Query::Plan::Extend
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan::Extend - Executable query plan for Extends.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan::Extend 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::Extend;
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 35 ▶use RDF::Query::Error qw(:try);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 35 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
28 35 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 ######################################################################
31  
32 35 our ($VERSION);
33 35 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
34 35 $VERSION = '2.919';
35 }
36  
37 ######################################################################
38  
39 =item C<< new ( $plan, \@keys ) >>
40  
41 =cut
42  
43 24 ▶sub new {
Subroutine new: called CC=5 SCAR=16.4
Pod documented
44 24 my $class = shift;
45 24 my $plan = shift;
46 24 my $keys = shift;
47 24 my (@vars, @exprs);
48 24 foreach my $k (@$keys) {
49 28 ▶ push(@exprs, $k) if ($k->isa('RDF::Query::Expression'));
Branch: 1 branch
BranchTrueFalseTotal
if $k->isa("RDF::Query::Expression") 28 0 28
50 28 ▶ push(@vars, $k->name) if ($k->isa('RDF::Query::Node::Variable'));
Branch: 1 branch
BranchTrueFalseTotal
if $k->isa("RDF::Query::Node::Variable") 0 28 28
51 28 ▶ push(@vars, $k) if (not(ref($k)));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $k 0 28 28
52 }
53 24 my $self = $class->SUPER::new( $plan, \@vars, \@exprs );
54 24 $self->[0]{referenced_variables} = [ $plan->referenced_variables ];
55 24 return $self;
56 }
57  
58 =item C<< execute ( $execution_context ) >>
59  
60 =cut
61  
62 24 ▶sub execute ($) {
Subroutine execute: called CC=3 SCAR=11.3
Pod documented
63 24 my $self = shift;
64 24 my $context = shift;
65 24 $self->[0]{delegate} = $context->delegate;
66 24 my $l = Log::Log4perl->get_logger("rdf.query.plan.extend");
67 24 $l->trace( "executing extend plan: " . $self->sse );
68 24 ▶ if ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == $self->OPEN) 0 24 24
69 0 throw RDF::Query::Error::ExecutionError -text => "EXTEND plan can't be executed while already open";
70 }
71 24 my $plan = $self->[1];
72 24 $plan->execute( $context );
73
74 24 ▶ if ($plan->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->state == $self->OPEN) { } 24 0 24
75 24 $self->[0]{context} = $context;
76 24 $self->state( $self->OPEN );
77 } else {
78 0 warn "could not execute plan in PROJECT";
79 }
80 24 $self;
81 }
82  
83 =item C<< next >>
84  
85 =cut
86  
87 65 ▶sub next {
Subroutine next: called CC=9 SCAR=23.6
Pod documented
88 65 my $self = shift;
89 65 my $ctx = $self->[0]{context};
90 65 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 65 65
91 0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open PROJECT";
92 }
93
94 65 my $l = Log::Log4perl->get_logger("rdf.query.plan.extend");
95 65 my $plan = $self->[1];
96 65 while (1) {
97 65 my $result = $plan->next;
98 65 ▶ unless (defined($result)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 23 42 65
99 23 $l->trace("no remaining rows in extend");
100 23 ▶ if ($self->[1]->state == $self->[1]->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]->state == $self->[1]->OPEN) 23 0 23
101 23 $self->[1]->close();
102 }
103 23 return;
104 }
105 42 my $row = RDF::Query::VariableBindings->new( { %$result } );
106 42 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 42 42
107 0 $l->trace( "extend on row $row" );
108 }
109
110 42 my $keys = $self->[2];
111 42 my $exprs = $self->[3];
112 42 my $query = $self->[0]{context}->query;
113
114 42 local($query->{_query_row_cache}) = {};
115 # my $proj = $row->project( @{ $keys } );
116 42 my $ok = 1;
117 42 foreach my $e (@$exprs) {
118 46 my $name = $e->name;
119 46 my $var_or_expr = $e->expression;
120 46 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 46 46
121 0 $l->trace( "- extend alias " . $var_or_expr->sse . " -> $name" );
122 }
123 46 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=7.6
124 46 my $value = $query->var_or_expr_value( $row, $var_or_expr, $ctx );
125 46 ▶ if ($l->is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_trace) 0 46 46
126 0 $l->trace( "- extend value $name -> $value" );
127 }
128 46 $row->{ $name } = $value;
129 0 ▶ } catch RDF::Query::Error with {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 $l->trace( "- evaluating extend expression resulted in an error; dropping the variable binding" );
131 0 ▶ } otherwise {
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 my $e = shift;
133 0 warn 'exception caught in Extend(): ' . Dumper($e);
134 46 };
135 }
136 42 ▶ next unless ($ok);
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 42 42
137 42 $l->trace( "Extended result: $row" );
138 42 ▶ if (my $d = $self->delegate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delegate) 0 42 42
139 0 $d->log_result( $self, $row );
140 }
141 42 return $row;
142 }
143 }
144  
145 =item C<< close >>
146  
147 =cut
148  
149 24 ▶sub close {
Subroutine close: called CC=4 SCAR=14.2
Pod documented
150 24 my $self = shift;
151 24 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 24 24
152 0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open PROJECT";
153 }
154 24 delete $self->[0]{context};
155 24 ▶ if (blessed($self->[1]) and $self->[1]->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self->[1]) and $self->[1]->state == $self->OPEN) 1 23 24
Condition66%2/3
builtin::blessed($self->[1]) and $self->[1]->state == $self->OPEN
!ll&&!rl&&r
0231
MC/DC50%1/2
builtin::blessed($self->[1]) and $self->[1]->state == $self->OPEN
builtin::blessed($self->[1]) $self->[1]->state == $self->OPEN
Truth table66%2/3
A and B
A= builtin::blessed($self->[1])B= $self->[1]->state == $self->OPEN
ABresult
0X0
100
111
156 1 $self->[1]->close();
157 }
158 24 $self->SUPER::close();
159 }
160  
161 =item C<< pattern >>
162  
163 Returns the query plan that will be used to produce the data to be extended.
164  
165 =cut
166  
167 67 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
168 67 my $self = shift;
169 67 return $self->[1];
170 }
171  
172 =item C<< distinct >>
173  
174 Returns true if the pattern is guaranteed to return distinct results.
175  
176 =cut
177  
178 24 ▶sub distinct {
Subroutine distinct: called CC=1 SCAR=0.0
Pod documented
179 24 my $self = shift;
180 24 return $self->pattern->distinct;
181 }
182  
183 =item C<< ordered >>
184  
185 Returns true if the pattern is guaranteed to return ordered results.
186  
187 =cut
188  
189 19 ▶sub ordered {
Subroutine ordered: called CC=1 SCAR=0.0
Pod documented
190 19 my $self = shift;
191 19 return $self->pattern->ordered;
192 }
193  
194 =item C<< plan_node_name >>
195  
196 Returns the string name of this plan node, suitable for use in serialization.
197  
198 =cut
199  
200 24 ▶sub plan_node_name {
Subroutine plan_node_name: called CC=1 SCAR=0.0
Pod documented
201 24 return 'extend';
202 }
203  
204 =item C<< plan_prototype >>
205  
206 Returns a list of scalar identifiers for the type of the content (children)
207 nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
208 identifiers.
209  
210 =cut
211  
212 24 ▶sub plan_prototype {
Subroutine plan_prototype: called CC=1 SCAR=0.0
Pod documented
213 24 my $self = shift;
214 24 return qw(\J P);
215 }
216  
217 =item C<< plan_node_data >>
218  
219 Returns the data for this plan node that corresponds to the values described by
220 the signature returned by C<< plan_prototype >>.
221  
222 =cut
223  
224 24 ▶sub plan_node_data {
Subroutine plan_node_data: called CC=1 SCAR=0.0
Pod documented
225 24 my $self = shift;
226 0 my @vars = map { RDF::Query::Node::Variable->new( $_ ) } @{$self->[2]};
227 24 my @exprs = @{$self->[3]};
228 24 return ([ @vars, @exprs ], $self->pattern);
229 }
230  
231 =item C<< graph ( $g ) >>
232  
233 =cut
234  
235 0 ▶sub graph {
Subroutine graph: not called CC=2 SCAR=17.9
Pod documented
236 0 my $self = shift;
237 0 my $g = shift;
238 0 my $c = $self->pattern->graph( $g );
239 0 ▶ my $expr = join(' ', @{$self->[2]}, map { blessed($_) ? $_->sse( {}, "" ) : $_ } @{$self->[3]});
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 0 0 0
240 0 $g->add_node( "$self", label => "Extend ($expr)" . $self->graph_labels );
241 0 $g->add_edge( "$self", $c );
242 0 return "$self";
243 }
244  
245 =item C<< explain >>
246  
247 Returns a string serialization of the plan appropriate for display on the
248 command line.
249  
250 =cut
251  
252 0 ▶sub explain {
Subroutine explain: not called CC=2 SCAR=17.9
Pod documented
253 0 my $self = shift;
254 0 my $s = shift;
255 0 my $count = shift;
256 0 my $indent = $s x $count;
257 0 my $type = $self->plan_node_name;
258 0 my $string = sprintf("%s%s (0x%x)\n", $indent, $type, refaddr($self));
259 0 $string .= "${indent}${s}vars:\n";
260 0 my @vars = map { RDF::Query::Node::Variable->new( $_ ) } @{$self->[2]};
261 0 my @exprs = @{$self->[3]};
262 0 foreach my $e (@vars, @exprs) {
263 0 $string .= $e->explain($s, $count+2);
264 }
265 0 $string .= $self->pattern->explain( $s, $count+1 );
266 0 return $string;
267 }
268  
269  
270  
271 35 1;
272  
273 __END__
« Query/Algebra/Service.pm ↑ Summary Query/Plan/Sort.pm »