File Coverage

blib/lib/RDF/Query/Algebra/Sort.pm
Criterion Covered Total %
statement 79 93 84.9
branch 4 6 66.6
condition 4 6 66.6
subroutine 19 24 79.1
pod 12 12 100.0
total 118 141 83.6


line stmt bran cond sub pod time code
1             # RDF::Query::Algebra::Sort
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Query::Algebra::Sort - Algebra class for sorting
7              
8             =head1 VERSION
9              
10             This document describes RDF::Query::Algebra::Sort version 2.916.
11              
12             =cut
13              
14             package RDF::Query::Algebra::Sort;
15              
16 36     36   181 use strict;
  36         76  
  36         891  
17 36     36   178 use warnings;
  36         62  
  36         930  
18 36     36   175 no warnings 'redefine';
  36         82  
  36         1166  
19 36     36   201 use base qw(RDF::Query::Algebra);
  36         66  
  36         2672  
20              
21 36     36   189 use Data::Dumper;
  36         90  
  36         1652  
22 36     36   188 use Set::Scalar;
  36         69  
  36         1285  
23 36     36   178 use Log::Log4perl;
  36         78  
  36         292  
24 36     36   1738 use Scalar::Util qw(blessed);
  36         64  
  36         1781  
25 36     36   194 use Carp qw(carp croak confess);
  36         73  
  36         2153  
26 36     36   195 use Time::HiRes qw(gettimeofday tv_interval);
  36         78  
  36         376  
27              
28             ######################################################################
29              
30             our ($VERSION);
31             BEGIN {
32 36     36   36312 $VERSION = '2.916';
33             }
34              
35             ######################################################################
36              
37             =head1 METHODS
38              
39             Beyond the methods documented below, this class inherits methods from the
40             L<RDF::Query::Algebra> class.
41              
42             =over 4
43              
44             =cut
45              
46             =item C<new ( $pattern, [ $dir => $expr ] )>
47              
48             Returns a new Sort structure.
49              
50             =cut
51              
52             sub new {
53 15     15 1 29 my $class = shift;
54 15         29 my $pattern = shift;
55 15         36 my @orderby = @_;
56 15         66 return bless( [ $pattern, @orderby ], $class );
57             }
58              
59             =item C<< construct_args >>
60              
61             Returns a list of arguments that, passed to this class' constructor,
62             will produce a clone of this algebra pattern.
63              
64             =cut
65              
66             sub construct_args {
67 28     28 1 49 my $self = shift;
68 28         86 my $pattern = $self->pattern;
69 28         79 my @orderby = $self->orderby;
70 28         104 return ($pattern, @orderby);
71             }
72              
73             =item C<< pattern >>
74              
75             Returns the pattern to be sorted.
76              
77             =cut
78              
79             sub pattern {
80 86     86 1 122 my $self = shift;
81 86 50       200 if (@_) {
82 0         0 $self->[0] = shift;
83             }
84 86         448 return $self->[0];
85             }
86              
87             =item C<< orderby >>
88              
89             Returns the array of ordering definitions.
90              
91             =cut
92              
93             sub orderby {
94 79     79 1 119 my $self = shift;
95 79         127 my @orderby = @{ $self }[ 1 .. $#{ $self } ];
  79         161  
  79         161  
96 79         208 return @orderby;
97             }
98              
99             =item C<< sse >>
100              
101             Returns the SSE string for this algebra expression.
102              
103             =cut
104              
105             sub sse {
106 36     36 1 53 my $self = shift;
107 36         52 my $context = shift;
108 36   100     129 my $prefix = shift || '';
109 36   50     100 my $indent = $context->{indent} || ' ';
110            
111 36         49 my @order_sse;
112 36         92 my @orderby = $self->orderby;
113 36         70 foreach my $o (@orderby) {
114 36         90 my ($dir, $val) = @$o;
115 36         191 push(@order_sse, sprintf("($dir %s)", $val->sse( $context, "${prefix}${indent}" )));
116             }
117            
118 36         710 return sprintf(
119             "(sort\n${prefix}${indent}%s\n${prefix}${indent}%s)",
120             $self->pattern->sse( $context, "${prefix}${indent}" ),
121             join(' ', @order_sse),
122             );
123             }
124              
125             =item C<< as_sparql >>
126              
127             Returns the SPARQL string for this algebra expression.
128              
129             =cut
130              
131             sub as_sparql {
132 3     3 1 5 my $self = shift;
133 3   50     10 my $context = shift || {};
134 3         5 my $indent = shift;
135            
136 3 100       28 if ($context->{ skip_sort }) {
137 1         2 $context->{ skip_sort }--;
138 1         4 return $self->pattern->as_sparql( $context, $indent );
139             }
140            
141 2         11 my $order_exprs = $self->_as_sparql_order_exprs($context, $indent);
142            
143 2         8 my $string = sprintf(
144             "%s\nORDER BY %s",
145             $self->pattern->as_sparql( $context, $indent ),
146             $order_exprs,
147             );
148 2         9 return $string;
149             }
150              
151             sub _as_sparql_order_exprs {
152 3     3   5 my $self = shift;
153 3         5 my $context = shift;
154 3         6 my $intent = shift;
155            
156 3         4 my @order_sparql;
157 3         7 my @orderby = $self->orderby;
158 3         6 foreach my $o (@orderby) {
159 3         9 my ($dir, $val) = @$o;
160 3         9 $dir = uc($dir);
161 3 50       17 my $str = ($dir eq 'ASC')
162             ? $val->as_sparql( $context )
163             : sprintf("%s(%s)", $dir, $val->as_sparql( $context ));
164 3         28 push(@order_sparql, $str);
165             }
166 3         11 return join(' ', @order_sparql);
167             }
168              
169             =item C<< as_hash >>
170              
171             Returns the query as a nested set of plain data structures (no objects).
172              
173             =cut
174              
175             sub as_hash {
176 0     0 1 0 my $self = shift;
177 0         0 my $context = shift;
178            
179 0         0 my @order = $self->orderby;
180 0         0 my @expressions;
181 0         0 foreach my $o (@order) {
182 0         0 push(@expressions, { type => 'comparator', direction => $o->[0], expression => $o->[1]->as_hash });
183             }
184            
185             return {
186 0         0 type => lc($self->type),
187             pattern => $self->pattern->as_hash,
188             order => \@expressions,
189             };
190             }
191              
192             =item C<< type >>
193              
194             Returns the type of this algebra expression.
195              
196             =cut
197              
198             sub type {
199 0     0 1 0 return 'SORT';
200             }
201              
202             =item C<< referenced_variables >>
203              
204             Returns a list of the variable names used in this algebra expression.
205              
206             =cut
207              
208             sub referenced_variables {
209 6     6 1 7 my $self = shift;
210 6         17 return RDF::Query::_uniq($self->pattern->referenced_variables);
211             }
212              
213             =item C<< potentially_bound >>
214              
215             Returns a list of the variable names used in this algebra expression that will
216             bind values during execution.
217              
218             =cut
219              
220             sub potentially_bound {
221 0     0 1   my $self = shift;
222 0           return RDF::Query::_uniq($self->pattern->potentially_bound);
223             }
224              
225             =item C<< definite_variables >>
226              
227             Returns a list of the variable names that will be bound after evaluating this algebra expression.
228              
229             =cut
230              
231             sub definite_variables {
232 0     0 1   my $self = shift;
233 0           return $self->pattern->definite_variables;
234             }
235              
236             =item C<< is_solution_modifier >>
237              
238             Returns true if this node is a solution modifier.
239              
240             =cut
241              
242             sub is_solution_modifier {
243 0     0 1   return 1;
244             }
245              
246              
247             1;
248              
249             __END__
250              
251             =back
252              
253             =head1 AUTHOR
254              
255             Gregory Todd Williams <gwilliams@cpan.org>
256              
257             =cut