File Coverage

blib/lib/RDF/Query/Algebra/Table.pm
Criterion Covered Total %
statement 31 92 33.7
branch 0 6 0.0
condition 0 6 0.0
subroutine 11 22 50.0
pod 10 10 100.0
total 52 136 38.2


line stmt bran cond sub pod time code
1             # RDF::Query::Algebra::Table
2             # -----------------------------------------------------------------------------
3              
4             =head1 NAME
5              
6             RDF::Query::Algebra::Table - Algebra class for constant table data
7              
8             =head1 VERSION
9              
10             This document describes RDF::Query::Algebra::Table version 2.916.
11              
12             =cut
13              
14             package RDF::Query::Algebra::Table;
15              
16 36     36   187 use strict;
  36         67  
  36         962  
17 36     36   181 use warnings;
  36         68  
  36         962  
18 36     36   188 no warnings 'redefine';
  36         58  
  36         1189  
19 36     36   188 use base qw(RDF::Query::Algebra);
  36         73  
  36         2752  
20              
21 36     36   186 use Data::Dumper;
  36         74  
  36         1665  
22 36     36   186 use Log::Log4perl;
  36         73  
  36         246  
23 36     36   1628 use Scalar::Util qw(blessed refaddr reftype);
  36         72  
  36         2108  
24 36     36   203 use Carp qw(carp croak confess);
  36         72  
  36         2017  
25 36     36   174 use Time::HiRes qw(gettimeofday tv_interval);
  36         86  
  36         235  
26 36     36   3892 use RDF::Trine::Iterator qw(smap swatch);
  36         84  
  36         2770  
27              
28             ######################################################################
29              
30             our ($VERSION);
31             my %AS_SPARQL;
32             BEGIN {
33 36     36   30430 $VERSION = '2.916';
34             }
35              
36             ######################################################################
37              
38             =head1 METHODS
39              
40             Beyond the methods documented below, this class inherits methods from the
41             L<RDF::Query::Algebra> class.
42              
43             =over 4
44              
45             =cut
46              
47             =item C<< new ( \@variables, $row1, $row2, ... ) >>
48              
49             Returns a new Table structure.
50              
51             =cut
52              
53             sub new {
54 0     0 1   my $class = shift;
55 0           my $vars = shift;
56 0           my @rows = @_;
57 0           foreach my $t (@rows) {
58 0 0         unless ($t->isa('RDF::Trine::VariableBindings')) {
59 0           throw RDF::Query::Error::QueryPatternError -text => "Rows belonging to a table must be variable bindings";
60             }
61             }
62 0           return bless( [ $vars, \@rows ], $class );
63             }
64              
65             =item C<< construct_args >>
66              
67             Returns a list of arguments that, passed to this class' constructor,
68             will produce a clone of this algebra pattern.
69              
70             =cut
71              
72             sub construct_args {
73 0     0 1   my $self = shift;
74 0           return ([$self->variables], $self->rows);
75             }
76              
77             =item C<< variables >>
78              
79             Returns a list of variable names used in this data table.
80              
81             =cut
82              
83             sub variables {
84 0     0 1   my $self = shift;
85 0           return @{ $self->[0] };
  0            
86             }
87              
88             =item C<< rows >>
89              
90             Returns a list of variable bindings belonging to this data table.
91              
92             =cut
93              
94             sub rows {
95 0     0 1   my $self = shift;
96 0           return @{ $self->[1] };
  0            
97             }
98              
99             =item C<< sse >>
100              
101             Returns the SSE string for this algebra expression.
102              
103             =cut
104              
105             sub sse {
106 0     0 1   my $self = shift;
107 0           my $context = shift;
108 0   0       my $prefix = shift || '';
109 0   0       my $indent = $context->{indent} || ' ';
110 0           my @rows = sort map { $_->sse( $context ) } $self->rows;
  0            
111 0           return sprintf(
112             "(table\n${prefix}${indent}%s\n${prefix})",
113             join("\n${prefix}${indent}", @rows)
114             );
115             }
116              
117             =item C<< explain >>
118              
119             Returns a string serialization of the algebra appropriate for display on the
120             command line.
121              
122             =cut
123              
124             sub explain {
125 0     0 1   my $self = shift;
126 0           my $s = shift;
127 0           my $count = shift;
128 0           my $indent = $s x $count;
129 0           my $string = "${indent}table\n";
130            
131 0           foreach my $t ($self->rows) {
132 0           $string .= $t->explain($s, $indent+1);
133             }
134 0           return $string;
135             }
136              
137              
138             =item C<< as_sparql >>
139              
140             Returns the SPARQL string for this algebra expression.
141              
142             =cut
143              
144             sub as_sparql {
145 0     0 1   my $self = shift;
146 0 0         if (exists $AS_SPARQL{ refaddr( $self ) }) {
147 0           return $AS_SPARQL{ refaddr( $self ) };
148             } else {
149 0           my $context = shift;
150             # if (ref($context)) {
151             # $context = { %$context };
152             # }
153 0   0       my $indent = shift || '';
154 0           my @values;
155 0           my @vars = $self->variables;
156 0           foreach my $row ($self->rows) {
157 0           my @row_values;
158 0           foreach my $var (@vars) {
159 0           my $node = $row->{$var};
160 0 0         my $value = ($node) ? $node->as_sparql($context, $indent) : 'UNDEF';
161 0           push(@row_values, $value);
162             }
163 0           push(@values, '(' . join(' ', @row_values) . ')');
164             }
165 0           my $vars = join(' ', map { "?$_" } @vars);
  0            
166 0           my $string = "VALUES ($vars) {\n${indent}" . join("\n${indent}", @values) . "\n}\n";
167 0           $AS_SPARQL{ refaddr( $self ) } = $string;
168 0           return $string;
169             }
170             }
171              
172             =item C<< as_hash >>
173              
174             Returns the query as a nested set of plain data structures (no objects).
175              
176             =cut
177              
178             sub as_hash {
179 0     0 1   my $self = shift;
180 0           my $context = shift;
181             return {
182             type => lc($self->type),
183 0           patterns => [ map { $_->as_hash } $self->rows ],
  0            
184             };
185             }
186              
187             =item C<< type >>
188              
189             Returns the type of this algebra expression.
190              
191             =cut
192              
193             sub type {
194 0     0 1   return 'TABLE';
195             }
196              
197             =item C<< referenced_variables >>
198              
199             Returns a list of the variable names used in this algebra expression.
200              
201             =cut
202              
203             sub referenced_variables {
204 0     0 1   my $self = shift;
205 0           my %vars;
206 0           foreach my $r ($self->rows) {
207 0           $vars{ $_ }++ foreach (keys %$r);
208             }
209 0           return keys %vars;
210             }
211              
212             sub DESTROY {
213 0     0     my $self = shift;
214 0           delete $AS_SPARQL{ refaddr( $self ) };
215             }
216              
217             1;
218              
219             __END__
220              
221             =back
222              
223             =head1 AUTHOR
224              
225             Gregory Todd Williams <gwilliams@cpan.org>
226              
227             =cut