File Coverage

blib/lib/RDF/Query/Plan/Filter.pm
Criterion Covered Total %
statement 82 99 82.8
branch 15 24 62.5
condition n/a
subroutine 16 19 84.2
pod 11 11 100.0
total 124 153 81.0


line stmt bran cond sub pod time code
1             # RDF::Query::Plan::Filter
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Query::Plan::Filter - Executable query plan for Filters.
7              
8             =head1 VERSION
9              
10             This document describes RDF::Query::Plan::Filter version 2.918.
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::Filter;
22              
23 35     35   132 use strict;
  35         48  
  35         806  
24 35     35   113 use warnings;
  35         44  
  35         800  
25 35     35   118 use base qw(RDF::Query::Plan);
  35         44  
  35         2058  
26 35     35   175 use RDF::Query::Error qw(:try);
  35         59  
  35         230  
27 35     35   4427 use Scalar::Util qw(blessed);
  35         49  
  35         1871  
28              
29             ######################################################################
30              
31             our ($VERSION);
32             BEGIN {
33 35     35   9422 $VERSION = '2.918';
34             }
35              
36             ######################################################################
37              
38             =item C<< new ( $plan, $expr, $active_graph ) >>
39              
40             =cut
41              
42             sub new {
43 37     37 1 60 my $class = shift;
44 37         51 my $expr = shift;
45 37         43 my $plan = shift;
46 37         46 my $graph = shift;
47 37         125 my $self = $class->SUPER::new( $expr, $plan, $graph );
48 37         152 $self->[0]{referenced_variables} = [ $plan->referenced_variables ];
49 37         109 return $self;
50             }
51              
52             =item C<< execute ( $execution_context ) >>
53              
54             =cut
55              
56             sub execute ($) {
57 37     37 1 57 my $self = shift;
58 37         46 my $context = shift;
59 37         91 $self->[0]{delegate} = $context->delegate;
60 37 50       111 if ($self->state == $self->OPEN) {
61 0         0 throw RDF::Query::Error::ExecutionError -text => "FILTER plan can't be executed while already open";
62             }
63 37         65 my $plan = $self->[2];
64 37         137 $plan->execute( $context );
65 37         165 my $l = Log::Log4perl->get_logger("rdf.query.plan.filter");
66            
67 37 50       2923 if ($plan->state == $self->OPEN) {
68 37         135 $self->state( $self->OPEN );
69 37         58 my $expr = $self->[1];
70 37         184 my $bool = RDF::Query::Node::Resource->new( "sparql:ebv" );
71 37         699 my $filter = RDF::Query::Expression::Function->new( $bool, $expr );
72 37 50       125 if ($l->is_trace) {
73 0         0 $l->trace("filter constructed for " . $expr->sse({}, ''));
74             }
75 37         320 my $query = $context->query;
76 37         107 my $bridge = $context->model;
77             $self->[0]{filter} = sub {
78 83     83   105 my $row = shift;
79 83         97 my $bool = 0;
80             try {
81 83         2436 my $qok = ref($query);
82 83         204 local($query->{_query_row_cache}) = {};
83 83 50       169 unless ($qok) {
84             # $query may not be defined, but the local() call will autovivify it into a HASH.
85             # later on, if it's a ref, somebody's going to try to call a method on it, so
86             # undef it if it wasn't defined before the local() call.
87 0         0 $query = undef;
88             }
89 83         201 my $value = $filter->evaluate( $query, $row, $context, $self->active_graph );
90 83 100       186 $bool = ($value->literal_value eq 'true') ? 1 : 0;
91             } catch RDF::Query::Error with {
92 0         0 my $e = shift;
93 35     35   166 no warnings 'uninitialized';
  35         59  
  35         15867  
94 0         0 $l->debug( 'exception thrown during filter evaluation: ' . $e->text );
95             } otherwise {
96 0         0 $l->debug( 'error during filter evaluation: ' . $@);
97 83         772 };
98 83         1514 return $bool;
99 37         254 };
100             } else {
101 0         0 warn "could not execute plan in filter";
102             }
103 37         88 $self;
104             }
105              
106             =item C<< next >>
107              
108             =cut
109              
110             sub next {
111 80     80 1 100 my $self = shift;
112 80 50       182 unless ($self->state == $self->OPEN) {
113 0         0 throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open FILTER";
114             }
115 80         113 my $plan = $self->[2];
116 80         114 my $filter = $self->[0]{filter};
117 80         177 my $l = Log::Log4perl->get_logger("rdf.query.plan.filter");
118 80         925 while (1) {
119 110         479 my $row = $plan->next;
120 110 100       262 unless (defined($row)) {
121 27         80 $l->debug("no remaining rows in filter");
122 27         152 return;
123             }
124 83 50       221 if ($l->is_trace) {
125 0         0 $l->trace("filter processing bindings $row");
126             }
127 83 100       470 if ($filter->( $row )) {
128 53         189 $l->trace( "- filter returned true on row" );
129 53 50       415 if (my $d = $self->delegate) {
130 0         0 $d->log_result( $self, $row );
131             }
132 53         172 return $row;
133             } else {
134 30         98 $l->trace( "- filter returned false on row" );
135             }
136             }
137             }
138              
139             =item C<< close >>
140              
141             =cut
142              
143             sub close {
144 33     33 1 66 my $self = shift;
145 33 50       79 unless ($self->state == $self->OPEN) {
146 0         0 throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open FILTER";
147             }
148 33         487 delete $self->[0]{filter};
149 33 50       278 if (blessed($self->pattern)) {
150 33         86 $self->pattern->close();
151             }
152 33         115 $self->SUPER::close();
153             }
154              
155             =item C<< pattern >>
156              
157             Returns the query plan that will be used to produce the data to be filtered.
158              
159             =cut
160              
161             sub pattern {
162 135     135 1 140 my $self = shift;
163 135         460 return $self->[2];
164             }
165              
166             =item C<< active_graph >>
167              
168             Returns the active graph.
169              
170             =cut
171              
172             sub active_graph {
173 83     83 1 88 my $self = shift;
174 83         336 return $self->[3];
175             }
176              
177             =item C<< distinct >>
178              
179             Returns true if the pattern is guaranteed to return distinct results.
180              
181             =cut
182              
183             sub distinct {
184 35     35 1 48 my $self = shift;
185 35         94 return $self->pattern->distinct;
186             }
187              
188             =item C<< ordered >>
189              
190             Returns true if the pattern is guaranteed to return ordered results.
191              
192             =cut
193              
194             sub ordered {
195 34     34 1 47 my $self = shift;
196 34         70 return $self->pattern->ordered;
197             }
198              
199             =item C<< plan_node_name >>
200              
201             Returns the string name of this plan node, suitable for use in serialization.
202              
203             =cut
204              
205             sub plan_node_name {
206 0     0 1   return 'filter';
207             }
208              
209             =item C<< plan_prototype >>
210              
211             Returns a list of scalar identifiers for the type of the content (children)
212             nodes of this plan node. See L<RDF::Query::Plan> for a list of the allowable
213             identifiers.
214              
215             =cut
216              
217             sub plan_prototype {
218 0     0 1   my $self = shift;
219 0           return qw(E P);
220             }
221              
222             =item C<< plan_node_data >>
223              
224             Returns the data for this plan node that corresponds to the values described by
225             the signature returned by C<< plan_prototype >>.
226              
227             =cut
228              
229             sub plan_node_data {
230 0     0 1   my $self = shift;
231 0           my $expr = $self->[1];
232 0           return ($expr, $self->pattern);
233             }
234              
235             1;
236              
237             __END__
238              
239             =back
240              
241             =head1 AUTHOR
242              
243             Gregory Todd Williams <gwilliams@cpan.org>
244              
245             =cut