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.915_01.
11              
12             =cut
13              
14             package RDF::Query::Algebra::Filter;
15              
16 36     36   179 use strict;
  36         72  
  36         909  
17 36     36   175 use warnings;
  36         72  
  36         925  
18 36     36   173 no warnings 'redefine';
  36         73  
  36         1135  
19 36     36   173 use base qw(RDF::Query::Algebra);
  36         67  
  36         2570  
20              
21 36     36   189 use Data::Dumper;
  36         81  
  36         1708  
22 36     36   171 use Carp qw(carp croak confess);
  36         74  
  36         2072  
23 36     36   244 use Scalar::Util qw(blessed reftype);
  36         82  
  36         1868  
24              
25 36     36   192 use RDF::Query::Error qw(:try);
  36         73  
  36         275  
26 36     36   4749 use RDF::Trine::Iterator qw(sgrep smap swatch);
  36         80  
  36         3002  
27              
28             ######################################################################
29              
30             our ($VERSION);
31             BEGIN {
32 36     36   30452 $VERSION = '2.915_01';
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 46     46 1 90 my $class = shift;
60 46         81 my $expr = shift;
61 46         93 my $pattern = shift;
62 46 50       265 Carp::confess "Not an algebra pattern: " . Dumper($pattern) unless ($pattern->isa('RDF::Query::Algebra'));
63 46 100 100     284 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         15 $pattern = RDF::Query::Algebra::GroupGraphPattern->new( $pattern );
66             }
67 46         268 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 152     152 1 221 my $self = shift;
79 152         345 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 303     303 1 394 my $self = shift;
90 303 50       683 if (@_) {
91 0         0 $self->[1] = shift;
92             }
93 303         1085 return $self->[1];
94             }
95              
96             =item C<< pattern >>
97              
98             Returns the filter pattern.
99              
100             =cut
101              
102             sub pattern {
103 298     298 1 3192 my $self = shift;
104 298 50       665 if (@_) {
105 0         0 $self->[2] = shift;
106             }
107 298         1436 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 139 my $self = shift;
118 84         113 my $context = shift;
119 84   100     277 my $prefix = shift || '';
120 84   50     272 my $indent = $context->{indent} || ' ';
121            
122 84         256 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 6     6 1 11 my $self = shift;
137 6   50     19 my $context = shift || {};
138 6   50     27 my $indent = shift || '';
139            
140 6 100       738 if ($context->{ skip_filter }) {
141 1         4 $context->{ skip_filter }--;
142 1         4 return $self->pattern->as_sparql( $context, $indent );
143             }
144            
145 5         15 my $expr = $self->expr;
146 5         27 my $filter_sparql = $expr->as_sparql( $context, $indent );
147 5         51 my $pattern_sparql = $self->pattern->as_sparql( $context, $indent );
148 5 50       32 if ($pattern_sparql =~ m#}\s*$#) {
149 5         44 $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 5         23 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 12     12 1 19 my $self = shift;
203 12         27 my $expr = $self->expr;
204 12         28 my $pattern = $self->pattern;
205 12         43 my @vars = $pattern->referenced_variables;
206 12 50 33     104 if (blessed($expr) and $expr->isa('RDF::Query::Algebra')) {
    0 0        
207 12         35 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 6     6 1 10 my $self = shift;
224 6         15 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 182 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