| 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.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::Filter; | ||||||||||
| 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); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | ###################################################################### | ||||||||||
| 30 | |||||||||||
| 31 | 35 | our ($VERSION); | |||||||||
| 32 | 35 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 33 | 35 | $VERSION = '2.919'; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | ###################################################################### | ||||||||||
| 37 | |||||||||||
| 38 | =item C<< new ( $plan, $expr, $active_graph ) >> | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | 37 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 37 | my $class = shift; | |||||||||
| 44 | 37 | my $expr = shift; | |||||||||
| 45 | 37 | my $plan = shift; | |||||||||
| 46 | 37 | my $graph = shift; | |||||||||
| 47 | 37 | my $self = $class->SUPER::new( $expr, $plan, $graph ); | |||||||||
| 48 | 37 | $self->[0]{referenced_variables} = [ $plan->referenced_variables ]; | |||||||||
| 49 | 37 | return $self; | |||||||||
| 50 | } | ||||||||||
| 51 | |||||||||||
| 52 | =item C<< execute ( $execution_context ) >> | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 37 | ▶ | sub execute ($) { | ||||||||
|
Subroutine
execute: called CC=4 SCAR=14.4
Pod
documented
| |||||||||||
| 57 | 37 | my $self = shift; | |||||||||
| 58 | 37 | my $context = shift; | |||||||||
| 59 | 37 | $self->[0]{delegate} = $context->delegate; | |||||||||
| 60 | 37 | ▶ | if ($self->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 0 | throw RDF::Query::Error::ExecutionError -text => "FILTER plan can't be executed while already open"; | |||||||||
| 62 | } | ||||||||||
| 63 | 37 | my $plan = $self->[2]; | |||||||||
| 64 | 37 | $plan->execute( $context ); | |||||||||
| 65 | 37 | my $l = Log::Log4perl->get_logger("rdf.query.plan.filter"); | |||||||||
| 66 | |||||||||||
| 67 | 37 | ▶ | if ($plan->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 37 | $self->state( $self->OPEN ); | |||||||||
| 69 | 37 | my $expr = $self->[1]; | |||||||||
| 70 | 37 | my $bool = RDF::Query::Node::Resource->new( "sparql:ebv" ); | |||||||||
| 71 | 37 | my $filter = RDF::Query::Expression::Function->new( $bool, $expr ); | |||||||||
| 72 | 37 | ▶ | if ($l->is_trace) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 0 | $l->trace("filter constructed for " . $expr->sse({}, '')); | |||||||||
| 74 | } | ||||||||||
| 75 | 37 | my $query = $context->query; | |||||||||
| 76 | 37 | my $bridge = $context->model; | |||||||||
| 77 | 82 | ▶ | $self->[0]{filter} = sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
| |||||||||||
| 78 | 82 | my $row = shift; | |||||||||
| 79 | 82 | my $bool = 0; | |||||||||
| 80 | 82 | ▶ | try { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||
| 81 | 82 | my $qok = ref($query); | |||||||||
| 82 | 82 | local($query->{_query_row_cache}) = {}; | |||||||||
| 83 | 82 | ▶ | unless ($qok) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | $query = undef; | |||||||||
| 88 | } | ||||||||||
| 89 | 82 | my $value = $filter->evaluate( $query, $row, $context, $self->active_graph ); | |||||||||
| 90 | 82 | ▶ | $bool = ($value->literal_value eq 'true') ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 91 | 0 | ▶ | } catch RDF::Query::Error with { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.6
| |||||||||||
| 92 | 0 | my $e = shift; | |||||||||
| 93 | 35 | ▶ | no warnings 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 94 | 0 | $l->debug( 'exception thrown during filter evaluation: ' . $e->text ); | |||||||||
| 95 | 0 | ▶ | } otherwise { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 96 | 0 | $l->debug( 'error during filter evaluation: ' . $@); | |||||||||
| 97 | 82 | }; | |||||||||
| 98 | 82 | return $bool; | |||||||||
| 99 | 37 | }; | |||||||||
| 100 | } else { | ||||||||||
| 101 | 0 | warn "could not execute plan in filter"; | |||||||||
| 102 | } | ||||||||||
| 103 | 37 | $self; | |||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | =item C<< next >> | ||||||||||
| 107 | |||||||||||
| 108 | =cut | ||||||||||
| 109 | |||||||||||
| 110 | 80 | ▶ | sub next { | ||||||||
|
Subroutine
next: called CC=6 SCAR=18.4
Pod
documented
| |||||||||||
| 111 | 80 | my $self = shift; | |||||||||
| 112 | 80 | ▶ | unless ($self->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | 0 | throw RDF::Query::Error::ExecutionError -text => "next() cannot be called on an un-open FILTER"; | |||||||||
| 114 | } | ||||||||||
| 115 | 80 | my $plan = $self->[2]; | |||||||||
| 116 | 80 | my $filter = $self->[0]{filter}; | |||||||||
| 117 | 80 | my $l = Log::Log4perl->get_logger("rdf.query.plan.filter"); | |||||||||
| 118 | 80 | while (1) { | |||||||||
| 119 | 109 | my $row = $plan->next; | |||||||||
| 120 | 109 | ▶ | unless (defined($row)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 27 | $l->debug("no remaining rows in filter"); | |||||||||
| 122 | 27 | return; | |||||||||
| 123 | } | ||||||||||
| 124 | 82 | ▶ | if ($l->is_trace) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 125 | 0 | $l->trace("filter processing bindings $row"); | |||||||||
| 126 | } | ||||||||||
| 127 | 82 | ▶ | if ($filter->( $row )) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | 53 | $l->trace( "- filter returned true on row" ); | |||||||||
| 129 | 53 | ▶ | if (my $d = $self->delegate) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 130 | 0 | $d->log_result( $self, $row ); | |||||||||
| 131 | } | ||||||||||
| 132 | 53 | return $row; | |||||||||
| 133 | } else { | ||||||||||
| 134 | 29 | $l->trace( "- filter returned false on row" ); | |||||||||
| 135 | } | ||||||||||
| 136 | } | ||||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | =item C<< close >> | ||||||||||
| 140 | |||||||||||
| 141 | =cut | ||||||||||
| 142 | |||||||||||
| 143 | 33 | ▶ | sub close { | ||||||||
|
Subroutine
close: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||
| 144 | 33 | my $self = shift; | |||||||||
| 145 | 33 | ▶ | unless ($self->state == $self->OPEN) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 146 | 0 | throw RDF::Query::Error::ExecutionError -text => "close() cannot be called on an un-open FILTER"; | |||||||||
| 147 | } | ||||||||||
| 148 | 33 | delete $self->[0]{filter}; | |||||||||
| 149 | 33 | ▶ | if (blessed($self->pattern)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | 33 | $self->pattern->close(); | |||||||||
| 151 | } | ||||||||||
| 152 | 33 | $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 | 135 | ▶ | sub pattern { | ||||||||
|
Subroutine
pattern: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 162 | 135 | my $self = shift; | |||||||||
| 163 | 135 | return $self->[2]; | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | =item C<< active_graph >> | ||||||||||
| 167 | |||||||||||
| 168 | Returns the active graph. | ||||||||||
| 169 | |||||||||||
| 170 | =cut | ||||||||||
| 171 | |||||||||||
| 172 | 82 | ▶ | sub active_graph { | ||||||||
|
Subroutine
active_graph: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 173 | 82 | my $self = shift; | |||||||||
| 174 | 82 | 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 | 35 | ▶ | sub distinct { | ||||||||
|
Subroutine
distinct: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 184 | 35 | my $self = shift; | |||||||||
| 185 | 35 | 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 | 34 | ▶ | sub ordered { | ||||||||
|
Subroutine
ordered: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 195 | 34 | my $self = shift; | |||||||||
| 196 | 34 | 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 | 0 | ▶ | sub plan_node_name { | ||||||||
|
Subroutine
plan_node_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 206 | 0 | 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 | 0 | ▶ | sub plan_prototype { | ||||||||
|
Subroutine
plan_prototype: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 218 | 0 | 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 | 0 | ▶ | sub plan_node_data { | ||||||||
|
Subroutine
plan_node_data: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 230 | 0 | my $self = shift; | |||||||||
| 231 | 0 | my $expr = $self->[1]; | |||||||||
| 232 | 0 | return ($expr, $self->pattern); | |||||||||
| 233 | } | ||||||||||
| 234 | |||||||||||
| 235 | 35 | 1; | |||||||||
| 236 | |||||||||||
| 237 | __END__ | ||||||||||