Query/Plan/Construct.pm

filter: Statementstmt 70.8% 90 / 127 Branchbran 66.6% 24 / 36 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 14 / 21 Podpod 100% 14 / 14 totaltotal 70.4% 143 / 203 CC 24 SCAR 20.9
CC
24
Cov
69%
next
16.7
explain
12.0
close
4.6
CRAP 8.1
« Query/Expression.pm ↑ Summary Query/Algebra/Sequence.pm »
1 # RDF::Query::Plan::Construct
2 # -----------------------------------------------------------------------------
3  
4 =head1 NAME
5  
6 RDF::Query::Plan::Construct - Executable query plan for constructing a graph from a set of variable bindings.
7  
8 =head1 VERSION
9  
10 This document describes RDF::Query::Plan::Construct 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::Construct;
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 refaddr);
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, \@triples ) >>
40  
41 =cut
42  
43 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
44 4 my $class = shift;
45 4 my $plan = shift;
46 4 my $triples = shift;
47
48 4 ▶ unless (@$triples) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$triples) 0 4 4
49 0 throw RDF::Query::Error::MethodInvocationError -text => "No triples passed to ::Plan::Construct constructor";
50 }
51
52 4 my $self = $class->SUPER::new( $plan, $triples );
53 4 $self->[0]{referenced_variables} = [ $plan->referenced_variables ];
54 4 return $self;
55 }
56  
57 =item C<< execute ( $execution_context ) >>
58  
59 =cut
60  
61 4 ▶sub execute ($) {
Subroutine execute: called CC=3 SCAR=11.3
Pod documented
62 4 my $self = shift;
63 4 my $context = shift;
64 4 $self->[0]{delegate} = $context->delegate;
65 4 ▶ if ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->state == $self->OPEN) 0 4 4
66 0 throw RDF::Query::Error::ExecutionError -text => "CONSTRUCT plan can't be executed while already open";
67 }
68
69 4 my $l = Log::Log4perl->get_logger("rdf.query.plan.construct");
70 4 $l->trace( "executing RDF::Query::Plan::Construct" );
71
72 4 my $plan = $self->pattern;
73 4 $plan->execute( $context );
74  
75 4 ▶ if ($plan->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plan->state == $self->OPEN) { } 4 0 4
76 4 $self->[0]{triples} = [];
77 4 $self->[0]{blank_map} = {};
78 4 $self->state( $self->OPEN );
79 } else {
80 0 warn "could not execute plan in construct";
81 }
82 4 $self;
83 }
84  
85 =item C<< next >>
86  
87 =cut
88  
89 29 ▶sub next {
Subroutine next: called CC=16 SCAR=28.2
Pod documented
90 29 my $self = shift;
91 29 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 29 29
92 0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open CONSTRUCT";
93 }
94
95 29 my $l = Log::Log4perl->get_logger("rdf.query.plan.triple");
96 29 my $plan = $self->[1];
97 29 while (1) {
98 45 ▶ while (scalar(@{ $self->[0]{triples} })) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->[0]{"triples"};}) 25 20 45
99 25 my $t = shift(@{ $self->[0]{triples} });
100 25 ▶ if (my $d = $self->delegate) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $d = $self->delegate) 0 25 25
101 0 $d->log_result( $self, $t );
102 }
103 25 return $t;
104 }
105 20 my $row = $plan->next;
106 20 ▶ return undef unless ($row);
Branch: 1 branch
BranchTrueFalseTotal
unless $row 4 16 20
107 16 $self->[0]{blank_map} = {};
108
109 16 ▶ if ($l->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_debug) 0 16 16
110 0 $l->debug( "- got construct bindings from pattern: " . $row->as_string );
111 }
112 16 my $triples = $self->triples;
113
114 16 foreach my $t (@$triples) {
115 28 ▶ if ($l->is_debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l->is_debug) 0 28 28
116 0 $l->debug( "- filling-in construct triple pattern: " . $t->as_string );
117 }
118 28 my @triple = $t->nodes;
119 28 for my $i (0 .. 2) {
120 84 ▶ if ($triple[$i]->isa('RDF::Trine::Node::Variable')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($triple[$i]->isa('RDF::Trine::Node::Variable')) { } 36 48 84
elsif ($triple[$i]->isa('RDF::Trine::Node::Blank')) { } 16 32 48
121 36 my $name = $triple[$i]->name;
122 36 $triple[$i] = $row->{ $name };
123 } elsif ($triple[$i]->isa('RDF::Trine::Node::Blank')) {
124 16 my $id = $triple[$i]->blank_identifier;
125 16 ▶ unless (exists($self->[0]{blank_map}{ $id })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->[0]{"blank_map"}{$id}) 8 8 16
126 8 $self->[0]{blank_map}{ $id } = RDF::Trine::Node::Blank->new();
127 }
128 16 $triple[$i] = $self->[0]{blank_map}{ $id };
129 }
130 }
131 28 my $ok = 1;
132 28 foreach (@triple) {
133 84 ▶ if (not blessed($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($_)) 3 81 84
134 3 $ok = 0;
135 }
136 }
137 28 ▶ next unless ($ok);
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 3 25 28
138 25 my $st = RDF::Trine::Statement->new( @triple );
139 25 ▶ unless ($self->[0]{seen}{ $st->as_string }++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[0]{"seen"}{$st->as_string}++) 25 0 25
140 25 push(@{ $self->[0]{triples} }, $st);
141 }
142 }
143 }
144 }
145  
146 =item C<< close >>
147  
148 =cut
149  
150 3 ▶sub close {
Subroutine close: called CC=4 SCAR=15.2
Pod documented
151 3 my $self = shift;
152 3 ▶ unless ($self->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->state == $self->OPEN) 0 3 3
153 0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open CONSTRUCT";
154 }
155 3 delete $self->[0]{blank_map};
156 3 delete $self->[0]{triples};
157
158 3 ▶ if ($self->[1] and $self->[1]->state == $self->OPEN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1] and $self->[1]->state == $self->OPEN) 3 0 3
Condition33%1/3
$self->[1] and $self->[1]->state == $self->OPEN
!ll&&!rl&&r
003
MC/DC0%0/2
$self->[1] and $self->[1]->state == $self->OPEN
$self->[1] $self->[1]->state == $self->OPEN
Truth table33%1/3
A and B
A= $self->[1]B= $self->[1]->state == $self->OPEN
ABresult
0X0
100
111
159 3 $self->[1]->close();
160 }
161 3 $self->SUPER::close();
162 }
163  
164 =item C<< pattern >>
165  
166 Returns the query plan that will be used to produce the variable bindings for constructing the new graph.
167  
168 =cut
169  
170 4 ▶sub pattern {
Subroutine pattern: called CC=1 SCAR=0.0
Pod documented
171 4 my $self = shift;
172 4 return $self->[1];
173 }
174  
175 =item C<< triples >>
176  
177 Returns the triples that are to be used in constructing the new graph for each variable binding.
178  
179 =cut
180  
181 16 ▶sub triples {
Subroutine triples: called CC=1 SCAR=0.0
Pod documented
182 16 my $self = shift;
183 16 return $self->[2];
184 }
185  
186 =item C<< distinct >>
187  
188 Returns true if the pattern is guaranteed to return distinct results.
189  
190 =cut
191  
192 0 ▶sub distinct {
Subroutine distinct: not called CC=1 SCAR=6.9
Pod documented
193 0 return 0;
194 }
195  
196 =item C<< ordered >>
197  
198 Returns true if the pattern is guaranteed to return ordered results.
199  
200 =cut
201  
202 0 ▶sub ordered {
Subroutine ordered: not called CC=1 SCAR=6.9
Pod documented
203 0 my $self = shift;
204 0 return [];
205 }
206  
207 =item C<< plan_node_name >>
208  
209 Returns the string name of this plan node, suitable for use in serialization.
210  
211 =cut
212  
213 0 ▶sub plan_node_name {
Subroutine plan_node_name: not called CC=1 SCAR=6.9
Pod documented
214 0 return 'construct';
215 }
216  
217 =item C<< plan_prototype >>
218  
219 Returns a list of scalar identifiers for the type of the content (children)
220 nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
221 identifiers.
222  
223 =cut
224  
225 0 ▶sub plan_prototype {
Subroutine plan_prototype: not called CC=1 SCAR=6.9
Pod documented
226 0 my $self = shift;
227 0 return qw(P \T);
228 }
229  
230 =item C<< plan_node_data >>
231  
232 Returns the data for this plan node that corresponds to the values described by
233 the signature returned by C<< plan_prototype >>.
234  
235 =cut
236  
237 0 ▶sub plan_node_data {
Subroutine plan_node_data: not called CC=1 SCAR=6.9
Pod documented
238 0 my $self = shift;
239 0 return ($self->pattern, [ @{ $self->triples } ]);
240 }
241  
242 =item C<< explain >>
243  
244 Returns a string serialization of the query plan appropriate for display
245 on the command line.
246  
247 =cut
248  
249 0 ▶sub explain {
Subroutine explain: not called CC=3 SCAR=24.8
Pod documented
250 0 my $self = shift;
251 0 my ($s, $count) = (' ', 0);
252 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
253 0 $s = shift;
254 0 $count = shift;
255 }
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 .= $self->pattern->explain( $s, $count+1 );
260 0 $string .= "${indent}${s}pattern\n";
261 0 foreach my $t (@{ $self->triples }) {
262 0 $string .= "${indent}${s}${s}" . $t->as_sparql . "\n";
263 }
264 0 return $string;
265 }
266  
267 =item C<< graph ( $g ) >>
268  
269 =cut
270  
271 0 ▶sub graph {
Subroutine graph: not called CC=1 SCAR=6.9
Pod documented
272 0 my $self = shift;
273 0 my $g = shift;
274 0 my $c = $self->pattern->graph( $g );
275 0 $g->add_node( "$self", label => "Construct" . $self->graph_labels );
276 0 $g->add_edge( "$self", $c );
277 0 return "$self";
278 }
279  
280 =item C<< as_iterator ( $context ) >>
281  
282 Returns an RDF::Trine::Iterator object for the current (already executed) plan.
283  
284 =cut
285  
286 4 ▶sub as_iterator {
Subroutine as_iterator: called CC=1 SCAR=0.0
Pod documented
287 4 my $self = shift;
288 4 my $context = shift;
289 4 ▶ my $stream = RDF::Trine::Iterator::Graph->new( sub { $self->next } );
Subroutine __ANON__: called CC=1 SCAR=0.0
290 4 return $stream;
291 }
292  
293  
294 35 1;
295  
296 __END__
« Query/Expression.pm ↑ Summary Query/Algebra/Sequence.pm »