File Coverage

blib/lib/RDF/Query/Algebra/Filter.pm
Criterion Covered Total %
statement 69 84 82.1
branch 9 16 56.2
condition 9 17 52.9
subroutine 19 23 82.6
pod 13 13 100.0
total 119 153 77.7


line stmt bran cond sub pod time code
1             # RDF::Query::Algebra::Filter
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Query::Algebra::Filter - Algebra class for Filter expressions
7              
8             =head1 VERSION
9              
10             This document describes RDF::Query::Algebra::Filter version 2.918.
11              
12             =cut
13              
14             package RDF::Query::Algebra::Filter;
15              
16 36     36   137 use strict;
  36         48  
  36         889  
17 36     36   114 use warnings;
  36         40  
  36         743  
18 36     36   122 no warnings 'redefine';
  36         52  
  36         956  
19 36     36   137 use base qw(RDF::Query::Algebra);
  36         55  
  36         2206  
20              
21 36     36   167 use Data::Dumper;
  36         49  
  36         1476  
22 36     36   132 use Carp qw(carp croak confess);
  36         53  
  36         1585  
23 36     36   146 use Scalar::Util qw(blessed reftype);
  36         46  
  36         1486  
24              
25 36     36   149 use RDF::Query::Error qw(:try);
  36         53  
  36         240  
26 36     36   4118 use RDF::Trine::Iterator qw(sgrep smap swatch);
  36         63  
  36         2401  
27              
28             ######################################################################
29              
30             our ($VERSION);
31             BEGIN {
32 36     36   21587 $VERSION = '2.918';
33             }
34              
35             ######################################################################
36              
37             # function
38             # operator
39             # unary
40             # binary
41              
42              
43             =head1 METHODS
44              
45             Beyond the methods documented below, this class inherits methods from the
46             L<RDF::Query::Algebra> class.
47              
48             =over 4
49              
50             =cut
51              
52             =item C<new ( $expression, $pattern )>
53              
54             Returns a new Filter structure.
55              
56             =cut
57              
58             sub new {
59 48     48 1 84 my $class = shift;
60 48         71 my $expr = shift;
61 48         67 my $pattern = shift;
62 48 50       278 Carp::confess "Not an algebra pattern: " . Dumper($pattern) unless ($pattern->isa('RDF::Query::Algebra'));
63 48 100 100     220 unless ($pattern->isa('RDF::Query::Algebra::GroupGraphPattern') or $pattern->isa('RDF::Query::Algebra::Filter')) {
64             # for proper serialization, the pattern needs to be a GGP or another filter
65 3         10 $pattern = RDF::Query::Algebra::GroupGraphPattern->new( $pattern );
66             }
67 48         222 return bless( [ 'FILTER', $expr, $pattern ] );
68             }
69              
70             =item C<< construct_args >>
71              
72             Returns a list of arguments that, passed to this class' constructor,
73             will produce a clone of this algebra pattern.
74              
75             =cut
76              
77             sub construct_args {
78 158     158 1 166 my $self = shift;
79 158         304 return ($self->expr, $self->pattern);
80             }
81              
82             =item C<< expr >>
83              
84             Returns the filter expression.
85              
86             =cut
87              
88             sub expr {
89 319     319 1 255 my $self = shift;
90 319 50       500 if (@_) {
91 0         0 $self->[1] = shift;
92             }
93 319         849 return $self->[1];
94             }
95              
96             =item C<< pattern >>
97              
98             Returns the filter pattern.
99              
100             =cut
101              
102             sub pattern {
103 312     312 1 2460 my $self = shift;
104 312 50       507 if (@_) {
105 0         0 $self->[2] = shift;
106             }
107 312         1097 return $self->[2];
108             }
109              
110             =item C<< sse >>
111              
112             Returns the SSE string for this algebra expression.
113              
114             =cut
115              
116             sub sse {
117 84     84 1 100 my $self = shift;
118 84         86 my $context = shift;
119 84   100     219 my $prefix = shift || '';
120 84   50     186 my $indent = $context->{indent} || ' ';
121            
122 84         231 return sprintf(
123             "(filter %s\n${prefix}${indent}%s)",
124             $self->expr->sse( $context, "${prefix}${indent}" ),
125             $self->pattern->sse( $context, "${prefix}${indent}" ),
126             );
127             }
128              
129             =item C<< as_sparql >>
130              
131             Returns the SPARQL string for this algebra expression.
132              
133             =cut
134              
135             sub as_sparql {
136 8     8 1 12 my $self = shift;
137 8   50     23 my $context = shift || {};
138 8   50     38 my $indent = shift || '';
139            
140 8 100       661 if ($context->{ skip_filter }) {
141 1         4 $context->{ skip_filter }--;
142 1         3 return $self->pattern->as_sparql( $context, $indent );
143             }
144            
145 7         18 my $expr = $self->expr;
146 7         35 my $filter_sparql = $expr->as_sparql( $context, $indent );
147 7         62 my $pattern_sparql = $self->pattern->as_sparql( $context, $indent );
148 7 50       41 if ($pattern_sparql =~ m#}\s*$#) {
149 7         60 $pattern_sparql =~ s#}\s*$#${indent}\tFILTER( ${filter_sparql} ) .\n${indent}}#;
150             } else {
151 0         0 $pattern_sparql = "${pattern_sparql}\n${indent}FILTER( ${filter_sparql} )";
152             }
153 7         25 return $pattern_sparql;
154             }
155              
156             =item C<< as_hash >>
157              
158             Returns the query as a nested set of plain data structures (no objects).
159              
160             =cut
161              
162             sub as_hash {
163 0     0 1 0 my $self = shift;
164 0         0 my $context = shift;
165             return {
166 0         0 type => lc($self->type),
167             pattern => $self->pattern->as_hash,
168             expression => $self->expr->as_hash,
169             };
170             }
171              
172             =item C<< as_spin ( $model ) >>
173              
174             Adds statements to the given model to represent this algebra object in the
175             SPARQL Inferencing Notation (L<http://www.spinrdf.org/>).
176              
177             =cut
178              
179             sub as_spin {
180 0     0 1 0 my $self = shift;
181 0         0 my $model = shift;
182 0         0 return $self->pattern->as_spin($model);
183             }
184              
185             =item C<< type >>
186              
187             Returns the type of this algebra expression.
188              
189             =cut
190              
191             sub type {
192 0     0 1 0 return 'FILTER';
193             }
194              
195             =item C<< referenced_variables >>
196              
197             Returns a list of the variable names used in this algebra expression.
198              
199             =cut
200              
201             sub referenced_variables {
202 16     16 1 19 my $self = shift;
203 16         31 my $expr = $self->expr;
204 16         25 my $pattern = $self->pattern;
205 16         51 my @vars = $pattern->referenced_variables;
206 16 50 33     116 if (blessed($expr) and $expr->isa('RDF::Query::Algebra')) {
    0 0        
207 16         32 return RDF::Query::_uniq(@vars, $self->expr->referenced_variables);
208             } elsif (blessed($expr) and $expr->isa('RDF::Query::Node::Variable')) {
209 0         0 return RDF::Query::_uniq(@vars, $expr->name);
210             } else {
211 0         0 return (@vars);
212             }
213             }
214              
215             =item C<< potentially_bound >>
216              
217             Returns a list of the variable names used in this algebra expression that will
218             bind values during execution.
219              
220             =cut
221              
222             sub potentially_bound {
223 8     8 1 12 my $self = shift;
224 8         19 return $self->pattern->potentially_bound;
225             }
226              
227             =item C<< definite_variables >>
228              
229             Returns a list of the variable names that will be bound after evaluating this algebra expression.
230              
231             =cut
232              
233             sub definite_variables {
234 0     0 1 0 my $self = shift;
235 0         0 my $pattern = $self->pattern;
236 0         0 return $pattern->definite_variables;
237             }
238              
239             =item C<< is_solution_modifier >>
240              
241             Returns true if this node is a solution modifier.
242              
243             =cut
244              
245             sub is_solution_modifier {
246 37     37 1 139 return 0;
247             }
248              
249              
250             1;
251              
252             __END__
253              
254             =back
255              
256             =head1 AUTHOR
257              
258             Gregory Todd Williams <gwilliams@cpan.org>
259              
260             =cut